clip.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. "use strict";
  2. // deprecated
  3. // @see https://drafts.csswg.org/css-masking-1/#clip-property
  4. const parsers = require("../parsers");
  5. const property = "clip";
  6. module.exports.parse = (v, opt = {}) => {
  7. const { globalObject } = opt;
  8. if (v === "") {
  9. return v;
  10. }
  11. const { AST_TYPES } = parsers;
  12. const value = parsers.parsePropertyValue(property, v, {
  13. globalObject,
  14. inArray: true
  15. });
  16. if (Array.isArray(value) && value.length === 1) {
  17. const [{ name, type, value: itemValue }] = value;
  18. switch (type) {
  19. case AST_TYPES.FUNCTION: {
  20. const values = parsers.splitValue(itemValue, {
  21. delimiter: ","
  22. });
  23. const parsedValues = [];
  24. for (const item of values) {
  25. const parsedValue = parsers.parseCSS(item, { context: "value" }, true);
  26. const val = parsers.resolveNumericValue(parsedValue.children, {
  27. type: "length"
  28. });
  29. if (val) {
  30. parsedValues.push(val);
  31. } else {
  32. return;
  33. }
  34. }
  35. return `${name}(${parsedValues.join(", ")})`;
  36. }
  37. case AST_TYPES.GLOBAL_KEYWORD:
  38. case AST_TYPES.IDENTIFIER: {
  39. return name;
  40. }
  41. default:
  42. }
  43. } else if (typeof value === "string") {
  44. return value;
  45. }
  46. };
  47. module.exports.definition = {
  48. set(v) {
  49. v = parsers.prepareValue(v);
  50. if (parsers.hasVarFunc(v)) {
  51. this._setProperty(property, v);
  52. } else {
  53. const val = module.exports.parse(v, {
  54. globalObject: this._global
  55. });
  56. if (typeof val === "string") {
  57. const priority = this._priorities.get(property) ?? "";
  58. this._setProperty(property, val, priority);
  59. }
  60. }
  61. },
  62. get() {
  63. return this.getPropertyValue(property);
  64. },
  65. enumerable: true,
  66. configurable: true
  67. };
  68. module.exports.property = property;