mask-composite.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. let Declaration = require('../declaration')
  2. class MaskComposite extends Declaration {
  3. /**
  4. * Prefix mask-composite for webkit
  5. */
  6. insert(decl, prefix, prefixes) {
  7. let isCompositeProp = decl.prop === 'mask-composite'
  8. let compositeValues
  9. if (isCompositeProp) {
  10. compositeValues = decl.value.split(',')
  11. } else {
  12. compositeValues = decl.value.match(MaskComposite.regexp) || []
  13. }
  14. compositeValues = compositeValues.map(el => el.trim()).filter(el => el)
  15. let hasCompositeValues = compositeValues.length
  16. let compositeDecl
  17. if (hasCompositeValues) {
  18. compositeDecl = this.clone(decl)
  19. compositeDecl.value = compositeValues
  20. .map(value => MaskComposite.oldValues[value] || value)
  21. .join(', ')
  22. if (compositeValues.includes('intersect')) {
  23. compositeDecl.value += ', xor'
  24. }
  25. compositeDecl.prop = prefix + 'mask-composite'
  26. }
  27. if (isCompositeProp) {
  28. if (!hasCompositeValues) {
  29. return undefined
  30. }
  31. if (this.needCascade(decl)) {
  32. compositeDecl.raws.before = this.calcBefore(prefixes, decl, prefix)
  33. }
  34. return decl.parent.insertBefore(decl, compositeDecl)
  35. }
  36. let cloned = this.clone(decl)
  37. cloned.prop = prefix + cloned.prop
  38. if (hasCompositeValues) {
  39. cloned.value = cloned.value.replace(MaskComposite.regexp, '')
  40. }
  41. if (this.needCascade(decl)) {
  42. cloned.raws.before = this.calcBefore(prefixes, decl, prefix)
  43. }
  44. decl.parent.insertBefore(decl, cloned)
  45. if (!hasCompositeValues) {
  46. return decl
  47. }
  48. if (this.needCascade(decl)) {
  49. compositeDecl.raws.before = this.calcBefore(prefixes, decl, prefix)
  50. }
  51. return decl.parent.insertBefore(decl, compositeDecl)
  52. }
  53. }
  54. MaskComposite.names = ['mask', 'mask-composite']
  55. MaskComposite.oldValues = {
  56. add: 'source-over',
  57. subtract: 'source-out',
  58. intersect: 'source-in',
  59. exclude: 'xor'
  60. }
  61. MaskComposite.regexp = new RegExp(
  62. `\\s+(${Object.keys(MaskComposite.oldValues).join(
  63. '|'
  64. )})\\b(?!\\))\\s*(?=[,])`,
  65. 'ig'
  66. )
  67. module.exports = MaskComposite