borderStyle.js 2.7 KB

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