source: trip-planner-front/node_modules/postcss-color-functional-notation/index.cjs.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.6 KB
Line 
1'use strict';
2
3function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
4
5var postcss = _interopDefault(require('postcss'));
6var valuesParser = _interopDefault(require('postcss-values-parser'));
7
8var 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});
71const alphaUnitMatch = /^%?$/i;
72const calcFuncMatch = /^calc$/i;
73const colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
74const colorRegExp = /^(hsla?|rgba?)$/i;
75const hslishRegExp = /^hsla?$/i;
76const hslRgbFuncMatch = /^(hsl|rgb)$/i;
77const hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
78const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
79const rgbishRegExp = /^rgba?$/i;
80
81const isAlphaValue = node => isCalc(node) || node.type === 'number' && alphaUnitMatch.test(node.unit);
82
83const isCalc = node => node.type === 'func' && calcFuncMatch.test(node.value);
84
85const isHue = node => isCalc(node) || node.type === 'number' && hueUnitMatch.test(node.unit);
86
87const isNumber = node => isCalc(node) || node.type === 'number' && node.unit === '';
88
89const isPercentage = node => isCalc(node) || node.type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');
90
91const isHslish = node => node.type === 'func' && hslishRegExp.test(node.value);
92
93const isHslRgb = node => node.type === 'func' && hslRgbFuncMatch.test(node.value);
94
95const isHslaRgba = node => node.type === 'func' && hslaRgbaFuncMatch.test(node.value);
96
97const isRgbish = node => node.type === 'func' && rgbishRegExp.test(node.value);
98
99const isSlash = node => node.type === 'operator' && node.value === '/';
100
101const functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
102const functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
103const functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
104
105const matchFunctionalHSL = (node, children) => isHslish(node) && children.every((child, index) => typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child));
106
107const matchFunctionalRGB1 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child));
108
109const matchFunctionalRGB2 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child));
110
111const newComma = () => valuesParser.comma({
112 value: ','
113});
114
115module.exports = index;
116//# sourceMappingURL=index.cjs.js.map
Note: See TracBrowser for help on using the repository browser.