backgroundImage.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const property = "background-image";
  4. const shorthand = "background";
  5. module.exports.parse = (v, opt = {}) => {
  6. const { globalObject } = opt;
  7. if (v === "") {
  8. return v;
  9. }
  10. const values = parsers.splitValue(v, { delimiter: "," });
  11. const parsedValues = [];
  12. for (const val of values) {
  13. const value = parsers.parsePropertyValue(property, val, {
  14. globalObject,
  15. inArray: true
  16. });
  17. if (Array.isArray(value) && value.length === 1) {
  18. const parsedValue = parsers.resolveGradientUrlValue(value);
  19. if (!parsedValue) {
  20. return;
  21. }
  22. parsedValues.push(parsedValue);
  23. } else if (typeof value === "string") {
  24. parsedValues.push(value);
  25. } else {
  26. return;
  27. }
  28. }
  29. if (parsedValues.length) {
  30. return parsedValues.join(", ");
  31. }
  32. };
  33. module.exports.definition = {
  34. set(v) {
  35. v = parsers.prepareValue(v);
  36. if (parsers.hasVarFunc(v)) {
  37. this._setProperty(shorthand, "");
  38. this._setProperty(property, v);
  39. } else {
  40. const val = module.exports.parse(v, {
  41. globalObject: this._global
  42. });
  43. if (typeof val === "string") {
  44. const priority =
  45. !this._priorities.get(shorthand) && this._priorities.has(property)
  46. ? this._priorities.get(property)
  47. : "";
  48. this._setProperty(property, val, priority);
  49. }
  50. }
  51. },
  52. get() {
  53. return this.getPropertyValue(property);
  54. },
  55. enumerable: true,
  56. configurable: true
  57. };
  58. module.exports.property = property;