margin.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const marginTop = require("./marginTop");
  4. const marginRight = require("./marginRight");
  5. const marginBottom = require("./marginBottom");
  6. const marginLeft = require("./marginLeft");
  7. const property = "margin";
  8. module.exports.position = "edges";
  9. module.exports.shorthandFor = new Map([
  10. [marginTop.property, marginTop],
  11. [marginRight.property, marginRight],
  12. [marginBottom.property, marginBottom],
  13. [marginLeft.property, marginLeft]
  14. ]);
  15. module.exports.parse = (v, opt = {}) => {
  16. const { globalObject } = opt;
  17. if (v === "") {
  18. return v;
  19. }
  20. const values = parsers.parsePropertyValue(property, v, {
  21. globalObject,
  22. inArray: true
  23. });
  24. const parsedValues = [];
  25. if (Array.isArray(values) && values.length) {
  26. if (values.length > 4) {
  27. return;
  28. }
  29. for (const value of values) {
  30. const parsedValue = parsers.resolveNumericValue([value], {
  31. length: values.length,
  32. type: "length"
  33. });
  34. if (!parsedValue) {
  35. return;
  36. }
  37. parsedValues.push(parsedValue);
  38. }
  39. } else if (typeof values === "string") {
  40. parsedValues.push(values);
  41. }
  42. if (parsedValues.length) {
  43. return parsedValues;
  44. }
  45. };
  46. module.exports.definition = {
  47. set(v) {
  48. v = parsers.prepareValue(v);
  49. if (parsers.hasVarFunc(v)) {
  50. for (const [longhand] of module.exports.shorthandFor) {
  51. this._setProperty(longhand, "");
  52. }
  53. this._setProperty(property, v);
  54. } else {
  55. const val = module.exports.parse(v, {
  56. globalObject: this._global
  57. });
  58. if (Array.isArray(val) || typeof val === "string") {
  59. const priority = this._priorities.get(property) ?? "";
  60. this._positionShorthandSetter(property, val, priority);
  61. }
  62. }
  63. },
  64. get() {
  65. return this.getPropertyValue(property);
  66. },
  67. enumerable: true,
  68. configurable: true
  69. };
  70. module.exports.property = property;