grid-end.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. let Declaration = require('../declaration')
  2. let { isPureNumber } = require('../utils')
  3. class GridEnd extends Declaration {
  4. /**
  5. * Change repeating syntax for IE
  6. */
  7. insert(decl, prefix, prefixes, result) {
  8. if (prefix !== '-ms-') return super.insert(decl, prefix, prefixes)
  9. let clonedDecl = this.clone(decl)
  10. let startProp = decl.prop.replace(/end$/, 'start')
  11. let spanProp = prefix + decl.prop.replace(/end$/, 'span')
  12. if (decl.parent.some(i => i.prop === spanProp)) {
  13. return undefined
  14. }
  15. clonedDecl.prop = spanProp
  16. if (decl.value.includes('span')) {
  17. clonedDecl.value = decl.value.replace(/span\s/i, '')
  18. } else {
  19. let startDecl
  20. decl.parent.walkDecls(startProp, d => {
  21. startDecl = d
  22. })
  23. if (startDecl) {
  24. if (isPureNumber(startDecl.value)) {
  25. let value = Number(decl.value) - Number(startDecl.value) + ''
  26. clonedDecl.value = value
  27. } else {
  28. return undefined
  29. }
  30. } else {
  31. decl.warn(
  32. result,
  33. `Can not prefix ${decl.prop} (${startProp} is not found)`
  34. )
  35. }
  36. }
  37. decl.cloneBefore(clonedDecl)
  38. return undefined
  39. }
  40. }
  41. GridEnd.names = ['grid-row-end', 'grid-column-end']
  42. module.exports = GridEnd