index.cjs.js 3.1 KB

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