source: trip-planner-front/node_modules/postcss-color-hex-alpha/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: 3.1 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 valueParser = _interopDefault(require('postcss-values-parser'));
7
8var index = postcss.plugin('postcss-color-hex-alpha', opts => {
9 // whether to preserve the original hexa
10 const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
11 return root => {
12 // for each declaration with a hexa
13 root.walkDecls(decl => {
14 if (hasAlphaHex(decl)) {
15 // replace instances of hexa with rgba()
16 const ast = valueParser(decl.value).parse();
17 walk(ast, node => {
18 if (isAlphaHex(node)) {
19 node.replaceWith(hexa2rgba(node));
20 }
21 }); // conditionally update the declaration
22
23 const modifiedValue = String(ast);
24
25 if (decl.value !== modifiedValue) {
26 if (preserve) {
27 decl.cloneBefore({
28 value: modifiedValue
29 });
30 } else {
31 decl.value = modifiedValue;
32 }
33 }
34 }
35 });
36 };
37}); // match any hexa
38
39const alphaHexRegExp = /#([0-9A-Fa-f]{4}(?:[0-9A-Fa-f]{4})?)\b/; // whether a node has a hexa
40
41const hasAlphaHex = node => alphaHexRegExp.test(node.value); // match an exact hexa
42
43
44const alphaHexValueRegExp = /^#([0-9A-Fa-f]{4}(?:[0-9A-Fa-f]{4})?)$/; // walk all nodes in a value
45
46const walk = (node, fn) => {
47 if (Object(node.nodes).length) {
48 node.nodes.slice().forEach(child => {
49 fn(child);
50 walk(child, fn);
51 });
52 }
53}; // decimal precision
54
55
56const alphaDecimalPrecision = 100000; // match a hexa node
57
58const isAlphaHex = node => node.type === 'word' && alphaHexValueRegExp.test(node.value);
59
60const hexa2rgba = node => {
61 // hex is the node value
62 const hex = node.value; // conditionally expand a hex
63
64 const hex8 = `0x${hex.length === 5 ? hex.slice(1).replace(/[0-9A-f]/g, '$&$&') : hex.slice(1)}`; // extract the red, blue, green, and alpha values from the hex
65
66 const _ref = [parseInt(hex8.slice(2, 4), 16), parseInt(hex8.slice(4, 6), 16), parseInt(hex8.slice(6, 8), 16), Math.round(parseInt(hex8.slice(8, 10), 16) / 255 * alphaDecimalPrecision) / alphaDecimalPrecision],
67 r = _ref[0],
68 g = _ref[1],
69 b = _ref[2],
70 a = _ref[3]; // return a new rgba function, preserving the whitespace of the original node
71
72 const rgbaFunc = valueParser.func({
73 value: 'rgba',
74 raws: Object.assign({}, node.raws)
75 });
76 rgbaFunc.append(valueParser.paren({
77 value: '('
78 }));
79 rgbaFunc.append(valueParser.number({
80 value: r
81 }));
82 rgbaFunc.append(valueParser.comma({
83 value: ','
84 }));
85 rgbaFunc.append(valueParser.number({
86 value: g
87 }));
88 rgbaFunc.append(valueParser.comma({
89 value: ','
90 }));
91 rgbaFunc.append(valueParser.number({
92 value: b
93 }));
94 rgbaFunc.append(valueParser.comma({
95 value: ','
96 }));
97 rgbaFunc.append(valueParser.number({
98 value: a
99 }));
100 rgbaFunc.append(valueParser.paren({
101 value: ')'
102 }));
103 return rgbaFunc;
104};
105
106module.exports = index;
107//# sourceMappingURL=index.cjs.js.map
Note: See TracBrowser for help on using the repository browser.