fontStyle.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const property = "font-style";
  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 value = parsers.parsePropertyValue(property, v, {
  12. globalObject,
  13. inArray: true
  14. });
  15. if (Array.isArray(value) && value.length) {
  16. if (value.length === 1) {
  17. const [{ name, type }] = value;
  18. switch (type) {
  19. case AST_TYPES.GLOBAL_KEYWORD:
  20. case AST_TYPES.IDENTIFIER: {
  21. return name;
  22. }
  23. default:
  24. }
  25. } else if (value.length === 2) {
  26. const [part1, part2] = value;
  27. const val1 = part1.type === AST_TYPES.IDENTIFIER && part1.name;
  28. const val2 = parsers.resolveNumericValue([part2], {
  29. type: "angle"
  30. });
  31. if (val1 && val1 === "oblique" && val2) {
  32. return `${val1} ${val2}`;
  33. }
  34. }
  35. } else if (typeof value === "string") {
  36. return value;
  37. }
  38. };
  39. module.exports.definition = {
  40. set(v) {
  41. v = parsers.prepareValue(v);
  42. if (parsers.hasVarFunc(v)) {
  43. this._setProperty(shorthand, "");
  44. this._setProperty(property, v);
  45. } else {
  46. const val = module.exports.parse(v, {
  47. globalObject: this._global
  48. });
  49. if (typeof val === "string") {
  50. const priority =
  51. !this._priorities.get(shorthand) && this._priorities.has(property)
  52. ? this._priorities.get(property)
  53. : "";
  54. this._setProperty(property, val, priority);
  55. }
  56. }
  57. },
  58. get() {
  59. return this.getPropertyValue(property);
  60. },
  61. enumerable: true,
  62. configurable: true
  63. };
  64. module.exports.property = property;