index.cjs.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  3. var postcss = _interopDefault(require('postcss'));
  4. var valuesParser = _interopDefault(require('postcss-values-parser'));
  5. var index = postcss.plugin('postcss-color-functional-notation', opts => {
  6. const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
  7. return root => {
  8. root.walkDecls(decl => {
  9. const originalValue = decl.value;
  10. if (colorAnyRegExp.test(originalValue)) {
  11. const valueAST = valuesParser(originalValue).parse();
  12. valueAST.walkType('func', node => {
  13. if (colorRegExp.test(node.value)) {
  14. const children = node.nodes.slice(1, -1);
  15. const isFunctionalHSL = matchFunctionalHSL(node, children);
  16. const isFunctionalRGB1 = matchFunctionalRGB1(node, children);
  17. const isFunctionalRGB2 = matchFunctionalRGB2(node, children);
  18. if (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {
  19. const slashNode = children[3];
  20. const alphaNode = children[4];
  21. if (alphaNode) {
  22. if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
  23. alphaNode.unit = '';
  24. alphaNode.value = String(alphaNode.value / 100);
  25. }
  26. if (isHslRgb(node)) {
  27. node.value += 'a';
  28. }
  29. } else if (isHslaRgba(node)) {
  30. node.value = node.value.slice(0, -1);
  31. }
  32. if (slashNode && isSlash(slashNode)) {
  33. slashNode.replaceWith(newComma());
  34. }
  35. if (isFunctionalRGB2) {
  36. children[0].unit = children[1].unit = children[2].unit = '';
  37. children[0].value = String(Math.floor(children[0].value * 255 / 100));
  38. children[1].value = String(Math.floor(children[1].value * 255 / 100));
  39. children[2].value = String(Math.floor(children[2].value * 255 / 100));
  40. }
  41. node.nodes.splice(3, 0, [newComma()]);
  42. node.nodes.splice(2, 0, [newComma()]);
  43. }
  44. }
  45. });
  46. const modifiedValue = String(valueAST);
  47. if (modifiedValue !== originalValue) {
  48. if (preserve) {
  49. decl.cloneBefore({
  50. value: modifiedValue
  51. });
  52. } else {
  53. decl.value = modifiedValue;
  54. }
  55. }
  56. }
  57. });
  58. };
  59. });
  60. const alphaUnitMatch = /^%?$/i;
  61. const calcFuncMatch = /^calc$/i;
  62. const colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
  63. const colorRegExp = /^(hsla?|rgba?)$/i;
  64. const hslishRegExp = /^hsla?$/i;
  65. const hslRgbFuncMatch = /^(hsl|rgb)$/i;
  66. const hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
  67. const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
  68. const rgbishRegExp = /^rgba?$/i;
  69. const isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);
  70. const isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);
  71. const isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);
  72. const isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';
  73. const isPercentage = node => isCalc(node) || node.type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');
  74. const isHslish = node => node.type === 'func' && hslishRegExp.test(node.value);
  75. const isHslRgb = node => node.type === 'func' && hslRgbFuncMatch.test(node.value);
  76. const isHslaRgba = node => node.type === 'func' && hslaRgbaFuncMatch.test(node.value);
  77. const isRgbish = node => node.type === 'func' && rgbishRegExp.test(node.value);
  78. const isSlash = node => node.type === 'operator' && node.value === '/';
  79. const functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
  80. const functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
  81. const functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
  82. const matchFunctionalHSL = (node, children) => isHslish(node) && children.every((child, index) => typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child));
  83. const matchFunctionalRGB1 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child));
  84. const matchFunctionalRGB2 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child));
  85. const newComma = () => valuesParser.comma({
  86. value: ','
  87. });
  88. module.exports = index;
  89. //# sourceMappingURL=index.cjs.js.map