borderTop.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const borderTopWidth = require("./borderTopWidth");
  4. const borderTopStyle = require("./borderTopStyle");
  5. const borderTopColor = require("./borderTopColor");
  6. const property = "border-top";
  7. const shorthand = "border";
  8. const subProps = {
  9. width: borderTopWidth.property,
  10. style: borderTopStyle.property,
  11. color: borderTopColor.property
  12. };
  13. module.exports.initialValues = new Map([
  14. [borderTopWidth.property, "medium"],
  15. [borderTopStyle.property, "none"],
  16. [borderTopColor.property, "currentcolor"]
  17. ]);
  18. module.exports.shorthandFor = new Map([
  19. [borderTopWidth.property, borderTopWidth],
  20. [borderTopStyle.property, borderTopStyle],
  21. [borderTopColor.property, borderTopColor]
  22. ]);
  23. module.exports.parse = (v, opt = {}) => {
  24. const { globalObject } = opt;
  25. if (v === "") {
  26. return v;
  27. }
  28. const values = parsers.splitValue(v);
  29. const parsedValues = new Map();
  30. for (const val of values) {
  31. const value = parsers.parsePropertyValue(property, val, {
  32. globalObject,
  33. inArray: true
  34. });
  35. if (Array.isArray(value) && value.length === 1) {
  36. const parsedValue = parsers.resolveBorderShorthandValue(value, subProps, parsedValues);
  37. if (typeof parsedValue === "string") {
  38. return parsedValue;
  39. } else if (Array.isArray(parsedValue)) {
  40. const [key, resolvedVal] = parsedValue;
  41. parsedValues.set(key, resolvedVal);
  42. } else {
  43. return;
  44. }
  45. } else {
  46. return;
  47. }
  48. }
  49. if (parsedValues.size) {
  50. const keys = module.exports.shorthandFor.keys();
  51. const obj = {
  52. [borderTopWidth.property]: "medium"
  53. };
  54. for (const key of keys) {
  55. if (parsedValues.has(key)) {
  56. const parsedValue = parsedValues.get(key);
  57. if (parsedValue !== module.exports.initialValues.get(key)) {
  58. obj[key] = parsedValues.get(key);
  59. if (obj[borderTopWidth.property] && obj[borderTopWidth.property] === "medium") {
  60. delete obj[borderTopWidth.property];
  61. }
  62. }
  63. }
  64. }
  65. return obj;
  66. }
  67. };
  68. module.exports.definition = {
  69. set(v) {
  70. v = parsers.prepareValue(v);
  71. if (parsers.hasVarFunc(v)) {
  72. this._borderSetter(property, v, "");
  73. } else {
  74. const val = module.exports.parse(v, {
  75. globalObject: this._global
  76. });
  77. if (val || typeof val === "string") {
  78. const priority =
  79. !this._priorities.get(shorthand) && this._priorities.has(property)
  80. ? this._priorities.get(property)
  81. : "";
  82. this._borderSetter(property, val, priority);
  83. }
  84. }
  85. },
  86. get() {
  87. return this.getPropertyValue(property);
  88. },
  89. enumerable: true,
  90. configurable: true
  91. };
  92. module.exports.property = property;