borderWidth.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const borderTopWidth = require("./borderTopWidth");
  4. const borderRightWidth = require("./borderRightWidth");
  5. const borderBottomWidth = require("./borderBottomWidth");
  6. const borderLeftWidth = require("./borderLeftWidth");
  7. const property = "border-width";
  8. const shorthand = "border";
  9. module.exports.shorthandFor = new Map([
  10. [borderTopWidth.property, borderTopWidth],
  11. [borderRightWidth.property, borderRightWidth],
  12. [borderBottomWidth.property, borderBottomWidth],
  13. [borderLeftWidth.property, borderLeftWidth]
  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. switch (parsedValues.length) {
  44. case 1: {
  45. return parsedValues;
  46. }
  47. case 2: {
  48. const [val1, val2] = parsedValues;
  49. if (val1 === val2) {
  50. return [val1];
  51. }
  52. return parsedValues;
  53. }
  54. case 3: {
  55. const [val1, val2, val3] = parsedValues;
  56. if (val1 === val3) {
  57. if (val1 === val2) {
  58. return [val1];
  59. }
  60. return [val1, val2];
  61. }
  62. return parsedValues;
  63. }
  64. case 4: {
  65. const [val1, val2, val3, val4] = parsedValues;
  66. if (val2 === val4) {
  67. if (val1 === val3) {
  68. if (val1 === val2) {
  69. return [val1];
  70. }
  71. return [val1, val2];
  72. }
  73. return [val1, val2, val3];
  74. }
  75. return parsedValues;
  76. }
  77. default:
  78. }
  79. }
  80. };
  81. module.exports.definition = {
  82. set(v) {
  83. v = parsers.prepareValue(v);
  84. if (parsers.hasVarFunc(v)) {
  85. this._borderSetter(property, v, "");
  86. } else {
  87. const val = module.exports.parse(v, {
  88. globalObject: this._global
  89. });
  90. if (Array.isArray(val) || typeof val === "string") {
  91. const priority =
  92. !this._priorities.get(shorthand) && this._priorities.has(property)
  93. ? this._priorities.get(property)
  94. : "";
  95. this._borderSetter(property, val, priority);
  96. }
  97. }
  98. },
  99. get() {
  100. return this.getPropertyValue(property);
  101. },
  102. enumerable: true,
  103. configurable: true
  104. };
  105. module.exports.property = property;