background.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. // FIXME:
  3. // * support multiple backgrounds
  4. // * also fix longhands
  5. const parsers = require("../parsers");
  6. const strings = require("../utils/strings");
  7. const backgroundImage = require("./backgroundImage");
  8. const backgroundPosition = require("./backgroundPosition");
  9. const backgroundRepeat = require("./backgroundRepeat");
  10. const backgroundAttachment = require("./backgroundAttachment");
  11. const backgroundColor = require("./backgroundColor");
  12. const shorthandFor = new Map([
  13. ["background-image", backgroundImage],
  14. ["background-position", backgroundPosition],
  15. ["background-repeat", backgroundRepeat],
  16. ["background-attachment", backgroundAttachment],
  17. ["background-color", backgroundColor]
  18. ]);
  19. module.exports.definition = {
  20. set(v) {
  21. v = parsers.prepareValue(v, this._global);
  22. if (/^none$/i.test(v)) {
  23. for (const [key] of shorthandFor) {
  24. this._setProperty(key, "");
  25. }
  26. this._setProperty("background", strings.asciiLowercase(v));
  27. } else if (parsers.hasVarFunc(v)) {
  28. for (const [key] of shorthandFor) {
  29. this._setProperty(key, "");
  30. }
  31. this._setProperty("background", v);
  32. } else {
  33. this._shorthandSetter("background", v, shorthandFor);
  34. }
  35. },
  36. get() {
  37. let val = this.getPropertyValue("background");
  38. if (parsers.hasVarFunc(val)) {
  39. return val;
  40. }
  41. val = this._shorthandGetter("background", shorthandFor);
  42. if (parsers.hasVarFunc(val)) {
  43. return "";
  44. }
  45. return val;
  46. },
  47. enumerable: true,
  48. configurable: true
  49. };