summaryrefslogtreecommitdiff
path: root/node_modules/postcss-hexrgba/index.js
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2023-10-14 15:58:02 +0800
committerlittleblack111 <littleblack11111@gmail.com>2023-10-14 15:58:02 +0800
commitfee47f6414cc3ae9a88f3ee0ed20f1d6f2d9f556 (patch)
tree2b99dd57fc676a3819ef3cc46ac43df0043c2bb3 /node_modules/postcss-hexrgba/index.js
parent0379526e20e1ff212df23e6326741aa9c06df204 (diff)
added plugin from postcss: postcss-fixes rucksack-css
Diffstat (limited to 'node_modules/postcss-hexrgba/index.js')
-rw-r--r--node_modules/postcss-hexrgba/index.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/node_modules/postcss-hexrgba/index.js b/node_modules/postcss-hexrgba/index.js
new file mode 100644
index 00000000..19967d62
--- /dev/null
+++ b/node_modules/postcss-hexrgba/index.js
@@ -0,0 +1,66 @@
+'use strict';
+
+const postcss = require('postcss');
+
+module.exports = postcss.plugin('postcss-hexrgba', () => {
+
+ /**
+ * Hex to RGB converter
+ * @param {string} hex hexidecimal string without #
+ * @return {array} RGB values
+ */
+ function hexRgb(hex){
+ let shorthandCheck = /^([a-f\d])([a-f\d])([a-f\d])$/i,
+ rgbRegex = /^([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i,
+ rgb;
+
+ hex = hex.replace(shorthandCheck, function(m, r, g, b) {
+ return r + r + g + g + b + b;
+ });
+
+ rgb = hex.replace(/^\s+|\s+$/g, '').match(rgbRegex);
+
+ // Convert it
+ return rgb ? [
+ parseInt(rgb[1], 16),
+ parseInt(rgb[2], 16),
+ parseInt(rgb[3], 16)
+ ] : false;
+ }
+
+ /**
+ * CSS rule handler
+ * @param {string} decl CSS delcaration
+ */
+ function ruleHandler(decl, result) {
+ let input = decl.value;
+
+ // Get the raw hex values and replace them
+ let output = input.replace(/rgba\(#(.*?),/g, (match, hex) => {
+ let rgb = hexRgb(hex),
+ matchHex = new RegExp('#' + hex);
+
+ // If conversion fails, emit a warning
+ if (!rgb) {
+ result.warn('not a valid hex', { node: decl });
+ return match;
+ }
+
+ rgb = rgb.toString();
+
+ return match.replace(matchHex, rgb);
+ });
+
+ decl.value = output;
+ }
+
+ return function(css, result) {
+ css.walkDecls(decl => {
+ if (typeof decl.value === 'undefined' || decl.value.indexOf('rgba') === -1) {
+ return;
+ }
+
+ ruleHandler(decl, result);
+ });
+ };
+});