Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

postcss-color-functional-notation

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

postcss-color-functional-notation - npm Package Compare versions

Comparing version 1.0.2 to 2.0.0

index.cjs.js.map

5

CHANGELOG.md
# Changes to PostCSS Color Functional Notation
### 2.0.0 (September 17, 2018)
- Updated: Support for PostCSS v7+
- Updated: Support for Node 6+
### 1.0.2 (July 13, 2018)

@@ -4,0 +9,0 @@

200

index.cjs.js

@@ -8,130 +8,108 @@ 'use strict';

var index = postcss.plugin('postcss-color-functional-notation', function (opts) {
var preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
var index = postcss.plugin('postcss-color-functional-notation', opts => {
const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false;
return root => {
root.walkDecls(decl => {
const value = decl.value;
return function (root) {
root.walkDecls(function (decl) {
var value = decl.value;
if (colorAnyRegExp.test(value)) {
const ast = parser(value).parse();
ast.walkType('func', node => {
if (colorRegExp.test(node.value)) {
const children = node.nodes.slice(1, -1);
const isFunctionalHSL = matchFunctionalHSL(node, children);
const isFunctionalRGB1 = matchFunctionalRGB1(node, children);
const isFunctionalRGB2 = matchFunctionalRGB2(node, children);
if (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {
const slashNode = children[3];
const alphaNode = children[4];
if (colorAnyRegExp.test(value)) {
var ast = parser(value).parse();
if (alphaNode) {
if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
alphaNode.unit = '';
alphaNode.value = String(alphaNode.value / 100);
}
ast.walkType('func', function (node) {
if (colorRegExp.test(node.value)) {
var children = node.nodes.slice(1, -1);
var isFunctionalHSL = matchFunctionalHSL(node, children);
var isFunctionalRGB1 = matchFunctionalRGB1(node, children);
var isFunctionalRGB2 = matchFunctionalRGB2(node, children);
if (isHslRgb(node)) {
node.value += 'a';
}
} else if (isHslaRgba(node)) {
node.value = node.value.slice(0, -1);
}
if (isFunctionalHSL || isFunctionalRGB1 || isFunctionalRGB2) {
var slashNode = children[3];
var alphaNode = children[4];
if (isSlash(slashNode)) {
slashNode.replaceWith(newComma());
}
if (alphaNode) {
if (isPercentage(alphaNode) && !isCalc(alphaNode)) {
alphaNode.unit = '';
alphaNode.value = String(alphaNode.value / 100);
}
if (isFunctionalRGB2) {
children[0].unit = children[1].unit = children[2].unit = '';
children[0].value = String(Math.floor(children[0].value * 255 / 100));
children[1].value = String(Math.floor(children[1].value * 255 / 100));
children[2].value = String(Math.floor(children[2].value * 255 / 100));
}
if (isHslRgb(node)) {
node.value += 'a';
}
} else if (isHslaRgba(node)) {
node.value = node.value.slice(0, -1);
}
node.nodes.splice(3, 0, [newComma()]);
node.nodes.splice(2, 0, [newComma()]);
}
}
});
const newValue = String(ast);
if (isSlash(slashNode)) {
slashNode.replaceWith(newComma());
}
if (preserve) {
decl.cloneBefore({
value: newValue
});
} else {
decl.value = newValue;
}
}
});
};
});
const alphaUnitMatch = /^%?$/i;
const calcFuncMatch = /^calc$/i;
const colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
const colorRegExp = /^(hsla?|rgba?)$/i;
const hslishRegExp = /^hsla?$/i;
const hslRgbFuncMatch = /^(hsl|rgb)$/i;
const hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
const hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
const rgbishRegExp = /^rgba?$/i;
if (isFunctionalRGB2) {
children[0].unit = children[1].unit = children[2].unit = '';
const isAlphaValue = node => isCalc(node) || Object(node).type === 'number' && alphaUnitMatch.test(node.unit);
children[0].value = String(Math.floor(children[0].value * 255 / 100));
children[1].value = String(Math.floor(children[1].value * 255 / 100));
children[2].value = String(Math.floor(children[2].value * 255 / 100));
}
const isCalc = node => Object(node).type === 'func' && calcFuncMatch.test(node.value);
node.nodes.splice(3, 0, [newComma()]);
node.nodes.splice(2, 0, [newComma()]);
}
}
});
const isHue = node => isCalc(node) || Object(node).type === 'number' && hueUnitMatch.test(node.unit);
var newValue = String(ast);
const isNumber = node => isCalc(node) || Object(node).type === 'number' && node.unit === '';
if (preserve) {
decl.cloneBefore({ value: newValue });
} else {
decl.value = newValue;
}
}
});
};
});
const isPercentage = node => isCalc(node) || Object(node).type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');
var alphaUnitMatch = /^%?$/i;
var calcFuncMatch = /^calc$/i;
var colorAnyRegExp = /(^|[^\w-])(hsla?|rgba?)\(/i;
var colorRegExp = /^(hsla?|rgba?)$/i;
var hslishRegExp = /^hsla?$/i;
var hslRgbFuncMatch = /^(hsl|rgb)$/i;
var hslaRgbaFuncMatch = /^(hsla|rgba)$/i;
var hueUnitMatch = /^(deg|grad|rad|turn)?$/i;
var rgbishRegExp = /^rgba?$/i;
var isAlphaValue = function isAlphaValue(node) {
return isCalc(node) || Object(node).type === 'number' && alphaUnitMatch.test(node.unit);
};
var isCalc = function isCalc(node) {
return Object(node).type === 'func' && calcFuncMatch.test(node.value);
};
var isHue = function isHue(node) {
return isCalc(node) || Object(node).type === 'number' && hueUnitMatch.test(node.unit);
};
var isNumber = function isNumber(node) {
return isCalc(node) || Object(node).type === 'number' && node.unit === '';
};
var isPercentage = function isPercentage(node) {
return isCalc(node) || Object(node).type === 'number' && (node.unit === '%' || node.unit === '' && node.value === '0');
};
var isHslish = function isHslish(node) {
return Object(node).type === 'func' && hslishRegExp.test(node.value);
};
var isHslRgb = function isHslRgb(node) {
return Object(node).type === 'func' && hslRgbFuncMatch.test(node.value);
};
var isHslaRgba = function isHslaRgba(node) {
return Object(node).type === 'func' && hslaRgbaFuncMatch.test(node.value);
};
var isRgbish = function isRgbish(node) {
return Object(node).type === 'func' && rgbishRegExp.test(node.value);
};
var isSlash = function isSlash(node) {
return Object(node).type === 'operator' && node.value === '/';
};
var functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
var functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
var functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
const isHslish = node => Object(node).type === 'func' && hslishRegExp.test(node.value);
var matchFunctionalHSL = function matchFunctionalHSL(node, children) {
return isHslish(node) && children.every(function (child, index) {
return typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child);
});
};
var matchFunctionalRGB1 = function matchFunctionalRGB1(node, children) {
return isRgbish(node) && children.every(function (child, index) {
return typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child);
});
};
var matchFunctionalRGB2 = function matchFunctionalRGB2(node, children) {
return isRgbish(node) && children.every(function (child, index) {
return typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child);
});
};
const isHslRgb = node => Object(node).type === 'func' && hslRgbFuncMatch.test(node.value);
var newComma = function newComma() {
return parser.comma({ value: ',' });
};
const isHslaRgba = node => Object(node).type === 'func' && hslaRgbaFuncMatch.test(node.value);
const isRgbish = node => Object(node).type === 'func' && rgbishRegExp.test(node.value);
const isSlash = node => Object(node).type === 'operator' && node.value === '/';
const functionalHSLMatch = [isHue, isPercentage, isPercentage, isSlash, isAlphaValue];
const functionalRGB1Match = [isNumber, isNumber, isNumber, isSlash, isAlphaValue];
const functionalRGB2Match = [isPercentage, isPercentage, isPercentage, isSlash, isAlphaValue];
const matchFunctionalHSL = (node, children) => isHslish(node) && children.every((child, index) => typeof functionalHSLMatch[index] === 'function' && functionalHSLMatch[index](child));
const matchFunctionalRGB1 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB1Match[index] === 'function' && functionalRGB1Match[index](child));
const matchFunctionalRGB2 = (node, children) => isRgbish(node) && children.every((child, index) => typeof functionalRGB2Match[index] === 'function' && functionalRGB2Match[index](child));
const newComma = () => parser.comma({
value: ','
});
module.exports = index;
//# sourceMappingURL=index.cjs.js.map
{
"name": "postcss-color-functional-notation",
"version": "1.0.2",
"version": "2.0.0",
"description": "Use space and slash separated color notation in CSS",

@@ -11,6 +11,8 @@ "author": "Jonathan Neal <jonathantneal@hotmail.com>",

"main": "index.cjs.js",
"module": "index.es.js",
"module": "index.es.mjs",
"files": [
"index.cjs.js",
"index.es.js"
"index.cjs.js.map",
"index.es.mjs",
"index.es.mjs.map"
],

@@ -26,18 +28,18 @@ "scripts": {

"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"dependencies": {
"postcss": "^6.0.23",
"postcss": "^7.0.2",
"postcss-values-parser": "^1.5.0"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.6",
"babel-preset-env": "^1.7.0",
"eslint": "^5.1.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"babel-eslint": "^9.0.0",
"eslint": "^5.6.0",
"eslint-config-dev": "^2.0.0",
"postcss-tape": "^2.2.0",
"pre-commit": "^1.2.2",
"rollup": "^0.62.0",
"rollup-plugin-babel": "^3.0.7"
"rollup": "^0.66.0",
"rollup-plugin-babel": "^4.0.1"
},

@@ -44,0 +46,0 @@ "eslintConfig": {

@@ -6,3 +6,2 @@ # PostCSS Color Functional Notation [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]

[![Build Status][cli-img]][cli-url]
[![Windows Build Status][win-img]][win-url]
[![Support Chat][git-img]][git-url]

@@ -33,3 +32,3 @@

Add [PostCSS Color Functional Notation] to your build tool:
Add [PostCSS Color Functional Notation] to your project:

@@ -40,117 +39,26 @@ ```bash

#### Node
Use [PostCSS Color Functional Notation] to process your CSS:
```js
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
const postcssColorFunctionalNotation = require('postcss-color-functional-notation');
postcssColorFunctionalNotation.process(YOUR_CSS, /* processOptions */, /* pluginOptions */);
postcssColorFunctionalNotation.process(YOUR_CSS /*, processOptions, pluginOptions */);
```
#### PostCSS
Or use it as a [PostCSS] plugin:
Add [PostCSS] to your build tool:
```bash
npm install postcss --save-dev
```
Use [PostCSS Color Functional Notation] as a plugin:
```js
import postcss from 'gulp-postcss';
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
const postcss = require('postcss');
const postcssColorFunctionalNotation = require('postcss-color-functional-notation');
postcss([
postcssColorFunctionalNotation(/* pluginOptions */)
]).process(YOUR_CSS);
]).process(YOUR_CSS /*, processOptions */);
```
#### Webpack
[PostCSS Color Functional Notation] runs in all Node environments, with special instructions for:
Add [PostCSS Loader] to your build tool:
| [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
| --- | --- | --- | --- | --- | --- |
```bash
npm install postcss-loader --save-dev
```
Use [PostCSS Color Functional Notation] in your Webpack configuration:
```js
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
{ loader: 'css-loader', options: { importLoaders: 1 } },
{ loader: 'postcss-loader', options: {
ident: 'postcss',
plugins: () => [
postcssColorFunctionalNotation(/* pluginOptions */)
]
} }
]
}
]
}
}
```
#### Gulp
Add [Gulp PostCSS] to your build tool:
```bash
npm install gulp-postcss --save-dev
```
Use [PostCSS Color Functional Notation] in your Gulpfile:
```js
import postcss from 'gulp-postcss';
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
gulp.task('css', () => gulp.src('./src/*.css').pipe(
postcss([
postcssColorFunctionalNotation(/* pluginOptions */)
])
).pipe(
gulp.dest('.')
));
```
#### Grunt
Add [Grunt PostCSS] to your build tool:
```bash
npm install grunt-postcss --save-dev
```
Use [PostCSS Color Functional Notation] in your Gruntfile:
```js
import postcssColorFunctionalNotation from 'postcss-color-functional-notation';
grunt.loadNpmTasks('grunt-postcss');
grunt.initConfig({
postcss: {
options: {
use: [
postcssColorFunctionalNotation(/* pluginOptions */)
]
},
dist: {
src: '*.css'
}
}
});
```
## Options

@@ -197,4 +105,2 @@

[npm-url]: https://www.npmjs.com/package/postcss-color-functional-notation
[win-img]: https://img.shields.io/appveyor/ci/jonathantneal/postcss-color-functional-notation.svg
[win-url]: https://ci.appveyor.com/project/jonathantneal/postcss-color-functional-notation

@@ -201,0 +107,0 @@ [CSS Color]: https://drafts.csswg.org/css-color/#ref-for-funcdef-rgb%E2%91%A1%E2%91%A0

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc