[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 valueParser = _interopDefault(require('postcss-values-parser'));
|
---|
| 7 |
|
---|
| 8 | var 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 |
|
---|
| 39 | const alphaHexRegExp = /#([0-9A-Fa-f]{4}(?:[0-9A-Fa-f]{4})?)\b/; // whether a node has a hexa
|
---|
| 40 |
|
---|
| 41 | const hasAlphaHex = node => alphaHexRegExp.test(node.value); // match an exact hexa
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | const alphaHexValueRegExp = /^#([0-9A-Fa-f]{4}(?:[0-9A-Fa-f]{4})?)$/; // walk all nodes in a value
|
---|
| 45 |
|
---|
| 46 | const 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 |
|
---|
| 56 | const alphaDecimalPrecision = 100000; // match a hexa node
|
---|
| 57 |
|
---|
| 58 | const isAlphaHex = node => node.type === 'word' && alphaHexValueRegExp.test(node.value);
|
---|
| 59 |
|
---|
| 60 | const 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 |
|
---|
| 106 | module.exports = index;
|
---|
| 107 | //# sourceMappingURL=index.cjs.js.map
|
---|