mask-composite.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. function _defaults(obj, defaults) { var keys = Object.getOwnPropertyNames(defaults); for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = Object.getOwnPropertyDescriptor(defaults, key); if (value && value.configurable && obj[key] === undefined) { Object.defineProperty(obj, key, value); } } return obj; }
  3. function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  4. function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  5. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  6. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  7. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  8. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _defaults(subClass, superClass); }
  9. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  10. var Declaration = require('../declaration');
  11. var MaskComposite = /*#__PURE__*/function (_Declaration) {
  12. _inheritsLoose(MaskComposite, _Declaration);
  13. var _super = _createSuper(MaskComposite);
  14. function MaskComposite() {
  15. return _Declaration.apply(this, arguments) || this;
  16. }
  17. var _proto = MaskComposite.prototype;
  18. /**
  19. * Prefix mask-composite for webkit
  20. */
  21. _proto.insert = function insert(decl, prefix, prefixes) {
  22. var isCompositeProp = decl.prop === 'mask-composite';
  23. var compositeValues;
  24. if (isCompositeProp) {
  25. compositeValues = decl.value.split(',');
  26. } else {
  27. compositeValues = decl.value.match(MaskComposite.regexp) || [];
  28. }
  29. compositeValues = compositeValues.map(function (el) {
  30. return el.trim();
  31. }).filter(function (el) {
  32. return el;
  33. });
  34. var hasCompositeValues = compositeValues.length;
  35. var compositeDecl;
  36. if (hasCompositeValues) {
  37. compositeDecl = this.clone(decl);
  38. compositeDecl.value = compositeValues.map(function (value) {
  39. return MaskComposite.oldValues[value] || value;
  40. }).join(', ');
  41. if (compositeValues.includes('intersect')) {
  42. compositeDecl.value += ', xor';
  43. }
  44. compositeDecl.prop = prefix + 'mask-composite';
  45. }
  46. if (isCompositeProp) {
  47. if (!hasCompositeValues) {
  48. return undefined;
  49. }
  50. if (this.needCascade(decl)) {
  51. compositeDecl.raws.before = this.calcBefore(prefixes, decl, prefix);
  52. }
  53. return decl.parent.insertBefore(decl, compositeDecl);
  54. }
  55. var cloned = this.clone(decl);
  56. cloned.prop = prefix + cloned.prop;
  57. if (hasCompositeValues) {
  58. cloned.value = cloned.value.replace(MaskComposite.regexp, '');
  59. }
  60. if (this.needCascade(decl)) {
  61. cloned.raws.before = this.calcBefore(prefixes, decl, prefix);
  62. }
  63. decl.parent.insertBefore(decl, cloned);
  64. if (!hasCompositeValues) {
  65. return decl;
  66. }
  67. if (this.needCascade(decl)) {
  68. compositeDecl.raws.before = this.calcBefore(prefixes, decl, prefix);
  69. }
  70. return decl.parent.insertBefore(decl, compositeDecl);
  71. };
  72. return MaskComposite;
  73. }(Declaration);
  74. _defineProperty(MaskComposite, "names", ['mask', 'mask-composite']);
  75. _defineProperty(MaskComposite, "oldValues", {
  76. add: 'source-over',
  77. substract: 'source-out',
  78. intersect: 'source-in',
  79. exclude: 'xor'
  80. });
  81. _defineProperty(MaskComposite, "regexp", new RegExp("\\s+(" + Object.keys(MaskComposite.oldValues).join('|') + ")\\b(?!\\))\\s*(?=[,])", 'ig'));
  82. module.exports = MaskComposite;