background.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. "use strict";
  2. const parsers = require("../parsers");
  3. const backgroundImage = require("./backgroundImage");
  4. const backgroundPosition = require("./backgroundPosition");
  5. const backgroundSize = require("./backgroundSize");
  6. const backgroundRepeat = require("./backgroundRepeat");
  7. const backgroundOrigin = require("./backgroundOrigin");
  8. const backgroundClip = require("./backgroundClip");
  9. const backgroundAttachment = require("./backgroundAttachment");
  10. const backgroundColor = require("./backgroundColor");
  11. const property = "background";
  12. module.exports.initialValues = new Map([
  13. [backgroundImage.property, "none"],
  14. [backgroundPosition.property, "0% 0%"],
  15. [backgroundSize.property, "auto"],
  16. [backgroundRepeat.property, "repeat"],
  17. [backgroundOrigin.property, "padding-box"],
  18. [backgroundClip.property, "border-box"],
  19. [backgroundAttachment.property, "scroll"],
  20. [backgroundColor.property, "transparent"]
  21. ]);
  22. module.exports.shorthandFor = new Map([
  23. [backgroundImage.property, backgroundImage],
  24. [backgroundPosition.property, backgroundPosition],
  25. [backgroundSize.property, backgroundSize],
  26. [backgroundRepeat.property, backgroundRepeat],
  27. [backgroundOrigin.property, backgroundOrigin],
  28. [backgroundClip.property, backgroundClip],
  29. [backgroundAttachment.property, backgroundAttachment],
  30. [backgroundColor.property, backgroundColor]
  31. ]);
  32. module.exports.parse = (v, opt = {}) => {
  33. const { globalObject } = opt;
  34. if (v === "") {
  35. return v;
  36. } else if (parsers.hasCalcFunc(v)) {
  37. v = parsers.resolveCalc(v);
  38. }
  39. if (!parsers.isValidPropertyValue(property, v)) {
  40. return;
  41. }
  42. const values = parsers.splitValue(v, {
  43. delimiter: ","
  44. });
  45. const bgValues = [];
  46. const l = values.length;
  47. for (let i = 0; i < l; i++) {
  48. let bg = {
  49. [backgroundImage.property]: module.exports.initialValues.get(backgroundImage.property),
  50. [backgroundPosition.property]: module.exports.initialValues.get(backgroundPosition.property),
  51. [backgroundSize.property]: module.exports.initialValues.get(backgroundSize.property),
  52. [backgroundRepeat.property]: module.exports.initialValues.get(backgroundRepeat.property),
  53. [backgroundOrigin.property]: module.exports.initialValues.get(backgroundOrigin.property),
  54. [backgroundClip.property]: module.exports.initialValues.get(backgroundClip.property),
  55. [backgroundAttachment.property]: module.exports.initialValues.get(
  56. backgroundAttachment.property
  57. ),
  58. [backgroundColor.property]: module.exports.initialValues.get(backgroundColor.property)
  59. };
  60. if (l > 1 && i !== l - 1) {
  61. bg = {
  62. [backgroundImage.property]: module.exports.initialValues.get(backgroundImage.property),
  63. [backgroundPosition.property]: module.exports.initialValues.get(
  64. backgroundPosition.property
  65. ),
  66. [backgroundSize.property]: module.exports.initialValues.get(backgroundSize.property),
  67. [backgroundRepeat.property]: module.exports.initialValues.get(backgroundRepeat.property),
  68. [backgroundOrigin.property]: module.exports.initialValues.get(backgroundOrigin.property),
  69. [backgroundClip.property]: module.exports.initialValues.get(backgroundClip.property),
  70. [backgroundAttachment.property]: module.exports.initialValues.get(
  71. backgroundAttachment.property
  72. )
  73. };
  74. }
  75. const bgPosition = [];
  76. const bgSize = [];
  77. const bgRepeat = [];
  78. const bgBox = [];
  79. const bgParts = parsers.splitValue(values[i], {
  80. delimiter: "/"
  81. });
  82. if (!bgParts.length || bgParts.length > 2) {
  83. return;
  84. }
  85. const [bgPart1, bgPart2 = ""] = bgParts;
  86. const parts1 = parsers.splitValue(bgPart1);
  87. for (const part of parts1) {
  88. let partValid = false;
  89. for (const [longhand, value] of module.exports.shorthandFor) {
  90. if (parsers.isValidPropertyValue(longhand, part)) {
  91. partValid = true;
  92. switch (longhand) {
  93. case backgroundClip.property:
  94. case backgroundOrigin.property: {
  95. const parsedValue = value.parse(part, { globalObject });
  96. if (parsedValue) {
  97. bgBox.push(parsedValue);
  98. }
  99. break;
  100. }
  101. case backgroundColor.property: {
  102. if (i !== values.length - 1) {
  103. return;
  104. }
  105. const parsedValue = value.parse(part, { globalObject });
  106. if (parsedValue) {
  107. bg[longhand] = parsedValue;
  108. }
  109. break;
  110. }
  111. case backgroundPosition.property: {
  112. const parsedValue = value.parse(part, { globalObject });
  113. if (parsedValue) {
  114. bgPosition.push(parsedValue);
  115. }
  116. break;
  117. }
  118. case backgroundRepeat.property: {
  119. const parsedValue = value.parse(part, { globalObject });
  120. if (parsedValue) {
  121. bgRepeat.push(parsedValue);
  122. }
  123. break;
  124. }
  125. case backgroundSize.property: {
  126. break;
  127. }
  128. default: {
  129. const parsedValue = value.parse(part, { globalObject });
  130. if (parsedValue) {
  131. bg[longhand] = parsedValue;
  132. }
  133. }
  134. }
  135. }
  136. }
  137. if (!partValid) {
  138. return;
  139. }
  140. }
  141. if (bgPart2) {
  142. const parts2 = parsers.splitValue(bgPart2);
  143. for (const part of parts2) {
  144. let partValid = false;
  145. for (const [longhand, value] of module.exports.shorthandFor) {
  146. if (parsers.isValidPropertyValue(longhand, part)) {
  147. partValid = true;
  148. switch (longhand) {
  149. case backgroundClip.property:
  150. case backgroundOrigin.property: {
  151. const parsedValue = value.parse(part, { globalObject });
  152. if (parsedValue) {
  153. bgBox.push(parsedValue);
  154. }
  155. break;
  156. }
  157. case backgroundColor.property: {
  158. if (i !== l - 1) {
  159. return;
  160. }
  161. const parsedValue = value.parse(part, { globalObject });
  162. if (parsedValue) {
  163. bg[longhand] = parsedValue;
  164. }
  165. break;
  166. }
  167. case backgroundPosition.property: {
  168. break;
  169. }
  170. case backgroundRepeat.property: {
  171. const parsedValue = value.parse(part, { globalObject });
  172. if (parsedValue) {
  173. bgRepeat.push(parsedValue);
  174. }
  175. break;
  176. }
  177. case backgroundSize.property: {
  178. const parsedValue = value.parse(part, { globalObject });
  179. if (parsedValue) {
  180. bgSize.push(parsedValue);
  181. }
  182. break;
  183. }
  184. default: {
  185. const parsedValue = value.parse(part, { globalObject });
  186. if (parsedValue) {
  187. bg[longhand] = parsedValue;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. if (!partValid) {
  194. return;
  195. }
  196. }
  197. }
  198. if (bgPosition.length) {
  199. const { parse: parser } = module.exports.shorthandFor.get(backgroundPosition.property);
  200. const value = parser(bgPosition.join(" "), { globalObject });
  201. if (value) {
  202. bg[backgroundPosition.property] = value;
  203. }
  204. }
  205. if (bgSize.length) {
  206. const { parse: parser } = module.exports.shorthandFor.get(backgroundSize.property);
  207. const value = parser(bgSize.join(" "), { globalObject });
  208. if (value) {
  209. bg[backgroundSize.property] = value;
  210. }
  211. }
  212. if (bgRepeat.length) {
  213. const { parse: parser } = module.exports.shorthandFor.get(backgroundRepeat.property);
  214. const value = parser(bgRepeat.join(" "), { globalObject });
  215. if (value) {
  216. bg[backgroundRepeat.property] = value;
  217. }
  218. }
  219. if (bgBox.length) {
  220. switch (bgBox.length) {
  221. case 1: {
  222. const [value] = bgBox;
  223. bg[backgroundOrigin.property] = value;
  224. bg[backgroundClip.property] = value;
  225. break;
  226. }
  227. case 2: {
  228. const [value1, value2] = bgBox;
  229. bg[backgroundOrigin.property] = value1;
  230. bg[backgroundClip.property] = value2;
  231. break;
  232. }
  233. default: {
  234. return;
  235. }
  236. }
  237. }
  238. bgValues.push(bg);
  239. }
  240. return bgValues;
  241. };
  242. module.exports.definition = {
  243. set(v) {
  244. v = parsers.prepareValue(v);
  245. if (v === "" || parsers.hasVarFunc(v)) {
  246. for (const [key] of module.exports.shorthandFor) {
  247. this._setProperty(key, "");
  248. }
  249. this._setProperty(property, v);
  250. } else {
  251. const bgValues = module.exports.parse(v, {
  252. globalObject: this._global
  253. });
  254. if (!Array.isArray(bgValues)) {
  255. return;
  256. }
  257. const bgMap = new Map([
  258. [backgroundImage.property, []],
  259. [backgroundPosition.property, []],
  260. [backgroundSize.property, []],
  261. [backgroundRepeat.property, []],
  262. [backgroundOrigin.property, []],
  263. [backgroundClip.property, []],
  264. [backgroundAttachment.property, []],
  265. [backgroundColor.property, []]
  266. ]);
  267. const backgrounds = [];
  268. for (const bgValue of bgValues) {
  269. const bg = [];
  270. for (const [longhand, value] of Object.entries(bgValue)) {
  271. if (value) {
  272. const arr = bgMap.get(longhand);
  273. arr.push(value);
  274. bgMap.set(longhand, arr);
  275. if (value !== module.exports.initialValues.get(longhand)) {
  276. if (longhand === backgroundSize.property) {
  277. bg.push(`/ ${value}`);
  278. } else {
  279. bg.push(value);
  280. }
  281. } else if (longhand === backgroundImage.property) {
  282. if (v === "none") {
  283. bg.push(value);
  284. }
  285. } else if (longhand === backgroundColor.property) {
  286. if (v === "transparent") {
  287. bg.push(value);
  288. }
  289. }
  290. }
  291. }
  292. backgrounds.push(bg.join(" "));
  293. }
  294. const priority = this._priorities.get(property) ?? "";
  295. for (const [longhand, value] of bgMap) {
  296. this._setProperty(longhand, value.join(", "), priority);
  297. }
  298. this._setProperty(property, backgrounds.join(", "), priority);
  299. }
  300. },
  301. get() {
  302. const v = this.getPropertyValue(property);
  303. if (parsers.hasVarFunc(v)) {
  304. return v;
  305. }
  306. const bgMap = new Map();
  307. let l = 0;
  308. for (const [longhand] of module.exports.shorthandFor) {
  309. const val = this.getPropertyValue(longhand);
  310. if (longhand === backgroundImage.property) {
  311. if (
  312. val === "none" &&
  313. v === "none" &&
  314. this.getPropertyValue(backgroundColor.property) === "transparent"
  315. ) {
  316. return val;
  317. }
  318. if (val !== module.exports.initialValues.get(longhand)) {
  319. const imgValues = parsers.splitValue(val, {
  320. delimiter: ","
  321. });
  322. l = imgValues.length;
  323. bgMap.set(longhand, imgValues);
  324. }
  325. } else if (longhand === backgroundColor.property) {
  326. if (val !== module.exports.initialValues.get(longhand) || v.includes(val)) {
  327. bgMap.set(longhand, [val]);
  328. }
  329. } else if (val !== module.exports.initialValues.get(longhand)) {
  330. bgMap.set(
  331. longhand,
  332. parsers.splitValue(val, {
  333. delimiter: ","
  334. })
  335. );
  336. }
  337. }
  338. if (l === 0) {
  339. const bgColArr = bgMap.get(backgroundColor.property);
  340. const background = bgColArr ? bgColArr[0] : null;
  341. if (background) {
  342. return background;
  343. }
  344. return "";
  345. }
  346. const bgValues = [];
  347. for (let i = 0; i < l; i++) {
  348. bgValues[i] = [];
  349. }
  350. for (const [longhand, values] of bgMap) {
  351. for (let i = 0; i < l; i++) {
  352. switch (longhand) {
  353. case backgroundColor.property: {
  354. if (i === l - 1) {
  355. const value = values[0];
  356. if (parsers.hasVarFunc(value)) {
  357. return "";
  358. }
  359. if (value && value !== module.exports.initialValues.get(longhand)) {
  360. const bgValue = bgValues[i];
  361. bgValue.push(value);
  362. }
  363. }
  364. break;
  365. }
  366. case backgroundSize.property: {
  367. const value = values[i];
  368. if (parsers.hasVarFunc(value)) {
  369. return "";
  370. }
  371. if (value && value !== module.exports.initialValues.get(longhand)) {
  372. const bgValue = bgValues[i];
  373. bgValue.push(`/ ${value}`);
  374. }
  375. break;
  376. }
  377. default: {
  378. const value = values[i];
  379. if (parsers.hasVarFunc(value)) {
  380. return "";
  381. }
  382. if (value && value !== module.exports.initialValues.get(longhand)) {
  383. const bgValue = bgValues[i];
  384. bgValue.push(value);
  385. }
  386. }
  387. }
  388. }
  389. }
  390. const backgrounds = [];
  391. for (const bgValue of bgValues) {
  392. backgrounds.push(bgValue.join(" "));
  393. }
  394. return backgrounds.join(", ");
  395. },
  396. enumerable: true,
  397. configurable: true
  398. };
  399. module.exports.property = property;