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