parseBoxShadowValue.js 2.6 KB

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