fontFamily.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const property = "font-family";
  4. const shorthand = "font";
  5. module.exports.parse = (v, opt = {}) => {
  6. const { globalObject } = opt;
  7. if (v === "") {
  8. return v;
  9. }
  10. const { AST_TYPES } = parsers;
  11. const values = parsers.splitValue(v, {
  12. delimiter: ","
  13. });
  14. const parsedValues = [];
  15. for (const val of values) {
  16. const value = parsers.parsePropertyValue(property, val, {
  17. globalObject,
  18. caseSensitive: true,
  19. inArray: true
  20. });
  21. if (Array.isArray(value) && value.length) {
  22. if (value.length === 1) {
  23. const [{ name, type, value: itemValue }] = value;
  24. switch (type) {
  25. case AST_TYPES.FUNCTION: {
  26. parsedValues.push(`${name}(${itemValue})`);
  27. break;
  28. }
  29. case AST_TYPES.GLOBAL_KEYWORD:
  30. case AST_TYPES.IDENTIFIER: {
  31. if (name === "undefined") {
  32. return;
  33. }
  34. parsedValues.push(name);
  35. break;
  36. }
  37. case "String": {
  38. const parsedValue = itemValue.replaceAll("\\", "").replaceAll('"', '\\"');
  39. parsedValues.push(`"${parsedValue}"`);
  40. break;
  41. }
  42. default: {
  43. return;
  44. }
  45. }
  46. } else {
  47. const parts = [];
  48. for (const item of value) {
  49. const { name, type } = item;
  50. if (type !== AST_TYPES.IDENTIFIER) {
  51. return;
  52. }
  53. parts.push(name);
  54. }
  55. const parsedValue = parts.join(" ").replaceAll("\\", "").replaceAll('"', '\\"');
  56. parsedValues.push(`"${parsedValue}"`);
  57. }
  58. } else if (typeof value === "string") {
  59. parsedValues.push(value);
  60. } else {
  61. return;
  62. }
  63. }
  64. if (parsedValues.length) {
  65. return parsedValues.join(", ");
  66. }
  67. };
  68. module.exports.definition = {
  69. set(v) {
  70. v = parsers.prepareValue(v);
  71. if (parsers.hasVarFunc(v)) {
  72. this._setProperty(shorthand, "");
  73. this._setProperty(property, v);
  74. } else {
  75. const val = module.exports.parse(v, {
  76. globalObject: this._global
  77. });
  78. if (typeof val === "string") {
  79. const priority =
  80. !this._priorities.get(shorthand) && this._priorities.has(property)
  81. ? this._priorities.get(property)
  82. : "";
  83. this._setProperty(property, val, priority);
  84. }
  85. }
  86. },
  87. get() {
  88. return this.getPropertyValue(property);
  89. },
  90. enumerable: true,
  91. configurable: true
  92. };
  93. module.exports.property = property;