You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.5 KiB

2 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. function _export(target, all) {
  6. for(var name in all)Object.defineProperty(target, name, {
  7. enumerable: true,
  8. get: all[name]
  9. });
  10. }
  11. _export(exports, {
  12. parseBoxShadowValue: ()=>parseBoxShadowValue,
  13. formatBoxShadowValue: ()=>formatBoxShadowValue
  14. });
  15. const _splitAtTopLevelOnly = require("./splitAtTopLevelOnly");
  16. let KEYWORDS = new Set([
  17. "inset",
  18. "inherit",
  19. "initial",
  20. "revert",
  21. "unset"
  22. ]);
  23. let SPACE = /\ +(?![^(]*\))/g // Similar to the one above, but with spaces instead.
  24. ;
  25. let LENGTH = /^-?(\d+|\.\d+)(.*?)$/g;
  26. function parseBoxShadowValue(input) {
  27. let shadows = Array.from((0, _splitAtTopLevelOnly.splitAtTopLevelOnly)(input, ","));
  28. return shadows.map((shadow)=>{
  29. let value = shadow.trim();
  30. let result = {
  31. raw: value
  32. };
  33. let parts = value.split(SPACE);
  34. let seen = new Set();
  35. for (let part of parts){
  36. // Reset index, since the regex is stateful.
  37. LENGTH.lastIndex = 0;
  38. // Keyword
  39. if (!seen.has("KEYWORD") && KEYWORDS.has(part)) {
  40. result.keyword = part;
  41. seen.add("KEYWORD");
  42. } else if (LENGTH.test(part)) {
  43. if (!seen.has("X")) {
  44. result.x = part;
  45. seen.add("X");
  46. } else if (!seen.has("Y")) {
  47. result.y = part;
  48. seen.add("Y");
  49. } else if (!seen.has("BLUR")) {
  50. result.blur = part;
  51. seen.add("BLUR");
  52. } else if (!seen.has("SPREAD")) {
  53. result.spread = part;
  54. seen.add("SPREAD");
  55. }
  56. } else {
  57. if (!result.color) {
  58. result.color = part;
  59. } else {
  60. if (!result.unknown) result.unknown = [];
  61. result.unknown.push(part);
  62. }
  63. }
  64. }
  65. // Check if valid
  66. result.valid = result.x !== undefined && result.y !== undefined;
  67. return result;
  68. });
  69. }
  70. function formatBoxShadowValue(shadows) {
  71. return shadows.map((shadow)=>{
  72. if (!shadow.valid) {
  73. return shadow.raw;
  74. }
  75. return [
  76. shadow.keyword,
  77. shadow.x,
  78. shadow.y,
  79. shadow.blur,
  80. shadow.spread,
  81. shadow.color
  82. ].filter(Boolean).join(" ");
  83. }).join(", ");
  84. }