postcss-values-parser
Advanced tools
Comparing version 3.1.1 to 3.2.0
@@ -11,8 +11,84 @@ /* | ||
*/ | ||
const { getTokens } = require('../tokenize'); | ||
const { registerWalker } = require('../walker'); | ||
const Container = require('./Container'); | ||
const Punctuation = require('./Punctuation'); | ||
const colorFunctions = ['hsl', 'hsla', 'rgb', 'rgba']; | ||
const reVar = /^--[^\s]+$/; | ||
const allFunctions = [ | ||
'annotation', | ||
'attr', | ||
'blur', | ||
'brightness', | ||
'calc', | ||
'character-variant', | ||
'circle', | ||
'contrast', | ||
'cubic-bezier', | ||
'dir', | ||
'drop-shadow', | ||
'element', | ||
'ellipse', | ||
'grayscale', | ||
'hsl', | ||
'hsla', | ||
'hue-rotate', | ||
'image', | ||
'inset', | ||
'invert', | ||
'lang', | ||
'linear-gradient', | ||
'matrix', | ||
'matrix3d', | ||
'minmax', | ||
'not', | ||
'nth-child', | ||
'nth-last-child', | ||
'nth-last-of-type', | ||
'nth-of-type', | ||
'opacity', | ||
'ornaments', | ||
'perspective', | ||
'polygon', | ||
'radial-gradient', | ||
'rect', | ||
'repeat', | ||
'repeating-linear-gradient', | ||
'repeating-radial-gradient', | ||
'rgb', | ||
'rgba', | ||
'rotate', | ||
'rotatex', | ||
'rotatey', | ||
'rotatez', | ||
'rotate3d', | ||
'saturate', | ||
'scale', | ||
'scalex', | ||
'scaley', | ||
'scalez', | ||
'scale3d', | ||
'sepia', | ||
'skew', | ||
'skewx', | ||
'skewy', | ||
'steps', | ||
'styleset', | ||
'stylistic', | ||
'swash', | ||
'symbols', | ||
'translate', | ||
'translatex', | ||
'translatey', | ||
'translatez', | ||
'translate3d', | ||
'url', | ||
'var' | ||
]; | ||
const vendorPrefixes = ['-webkit-', '-moz-', '-ms-', '-o-']; | ||
const reFunctions = new RegExp(`^(${vendorPrefixes.join('|')})?(${allFunctions.join('|')})`, 'i'); | ||
const rePunctuation = new RegExp(`^(\\${Punctuation.chars.join('|\\')})`); | ||
const reColorFunctions = /^(hsla?|hwb|lab|lch|rgba?)$/i; | ||
const reVar = /^var$/i; | ||
const reVarPrefix = /^--[^\s]+$/; | ||
@@ -35,2 +111,4 @@ class Func extends Container { | ||
tokens[0][0] === 'word' && | ||
// fixes #91 | ||
!rePunctuation.test(tokens[0][1]) && | ||
(tokens[1][0] === 'brackets' || tokens[1][0] === '(') | ||
@@ -48,2 +126,10 @@ ); | ||
// fixes #92 | ||
if (!reFunctions.test(node.name) && !/^[a-zA-Z]+$/.test(node.name)) { | ||
const nameTokens = getTokens(node.name); | ||
tokens.unshift(...nameTokens, brackets); | ||
parser.back(tokens); | ||
return; | ||
} | ||
parser.init(node, startLine, startChar); | ||
@@ -91,3 +177,4 @@ parser.current = node; // eslint-disable-line no-param-reassign | ||
// we must require this here due to circular dependency resolution | ||
const { parse } = require('../'); // eslint-disable-line global-require | ||
// eslint-disable-next-line global-require | ||
const { parse } = require('../'); | ||
const root = parse(params, opts); | ||
@@ -110,6 +197,5 @@ const { nodes: children } = root; | ||
const { lastNode } = parser; | ||
const lowerName = lastNode.name.toLowerCase(); | ||
const { nodes } = node; | ||
lastNode.isColor = colorFunctions.includes(lowerName); | ||
lastNode.isVar = lowerName === 'var' && nodes.length && reVar.test(nodes[0].value); | ||
lastNode.isColor = reColorFunctions.test(lastNode.name); | ||
lastNode.isVar = reVar.test(lastNode.name) && nodes.length && reVarPrefix.test(nodes[0].value); | ||
} | ||
@@ -116,0 +202,0 @@ } |
@@ -11,3 +11,2 @@ /* | ||
*/ | ||
const isNumber = require('is-number'); | ||
@@ -18,4 +17,44 @@ const { registerWalker } = require('../walker'); | ||
const unitRegex = /%|ch|cm|em|ex|in|mm|ms|pc|pt|px|s|q|rem|vh|vmax|vmin|vw$/i; | ||
/** A Number is: | ||
* 1. None or one plus or minus symbol; then | ||
* 2. Either, | ||
* 2.1. One or more digits; and / or, | ||
* 2.2. One period symbol; followed by, | ||
* 2.2.1. One or more digits; | ||
* then, | ||
* 3. If one "e" letter, | ||
* 3.1. One "e" letter; followed by, | ||
* 3.1.1. None or one plus or minus symbol; followed by, | ||
* 3.1.1.1. One or more digits. | ||
* @see https://drafts.csswg.org/css-syntax/#consume-a-number | ||
*/ | ||
const numberRegex = /^([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?)$/; | ||
/** A Unit is: | ||
* 1. Either, | ||
* 1.1. One dash; followed by, | ||
* 1.1.1. One letter, non-ASCII, underscore, dash; or, | ||
* 1.1.2. One escape slash; followed by, | ||
* 1.1.2.1 One non-newline; | ||
* or, | ||
* 1.2. One letter, non-ASCII, underscore; or, | ||
* 1.3. One escape slash; followed by, | ||
* 1.3.1. One non-newline; | ||
* then, | ||
* 2. Zero or more of; | ||
* 2.1 One letter, non-ASCII, underscore, dash; then / or, | ||
* 2.2 One escape slash; followed by, | ||
* 2.2.1. One non-newline. | ||
* @see https://drafts.csswg.org/css-syntax/#consume-numeric-token | ||
*/ | ||
const unitRegex = /^(-?(?:[-A-Z_a-z]|[^\x00-\x7F]|\\[^\n\f\r])(?:[-\w]|[^\x00-\x7F]|\\[^\n\f\r])*|%)$/; // eslint-disable-line no-control-regex | ||
/** A Numeric is: | ||
* 1. One Number; followed by, | ||
* 1.1 Zero or one Unit. | ||
*/ | ||
const numericRegex = new RegExp( | ||
`^${numberRegex.source.slice(1, -1) + unitRegex.source.slice(1, -1)}?$` | ||
); | ||
class Numeric extends Node { | ||
@@ -30,30 +69,14 @@ constructor(options = {}) { | ||
parser.fromFirst(tokens, Numeric); | ||
let [[, value]] = tokens; | ||
const unit = Numeric.parseUnit(value); | ||
value = value.replace(unit, ''); | ||
const [[, rawValue]] = tokens; | ||
const [, value, unit = ''] = rawValue.match(numericRegex); | ||
const { lastNode } = parser; | ||
lastNode.unit = unit || ''; | ||
lastNode.unit = unit; | ||
lastNode.value = value; | ||
} | ||
static parseUnit(what) { | ||
const matches = what.match(unitRegex); | ||
const [result] = matches || []; | ||
return result; | ||
} | ||
static test(what) { | ||
return isNumber(what); | ||
return numericRegex.test(what); | ||
} | ||
static testUnit(what) { | ||
const unit = Numeric.parseUnit(what); | ||
if (unit) { | ||
const remaining = what.replace(unit, ''); | ||
return isNumber(remaining); | ||
} | ||
return false; | ||
} | ||
} | ||
@@ -60,0 +83,0 @@ |
@@ -27,3 +27,3 @@ /* | ||
static get chars() { | ||
return [':', '(', ')', '[', ']', '{', '}']; | ||
return [',', ':', '(', ')', '[', ']', '{', '}']; | ||
} | ||
@@ -30,0 +30,0 @@ |
@@ -182,3 +182,3 @@ /* | ||
Word.fromTokens(tokens, this); | ||
} else if (Numeric.test(value) || Numeric.testUnit(value)) { | ||
} else if (Numeric.test(value)) { | ||
Numeric.fromTokens(tokens, this); | ||
@@ -185,0 +185,0 @@ } else if (UnicodeRange.test(value)) { |
{ | ||
"name": "postcss-values-parser", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "A CSS property value parser for use with PostCSS", | ||
@@ -38,3 +38,2 @@ "license": "MPL-2.0", | ||
"color-name": "^1.1.4", | ||
"is-number": "^7.0.0", | ||
"is-url-superb": "^3.0.0", | ||
@@ -47,8 +46,8 @@ "postcss": "^7.0.5", | ||
"@commitlint/config-conventional": "^8.1.0", | ||
"ava": "^2.2.0", | ||
"chalk": "^2.4.2", | ||
"ava": "^3.5.1", | ||
"chalk": "^3.0.0", | ||
"eslint-config-shellscape": "^2.0.2", | ||
"globby": "^10.0.1", | ||
"lint-staged": "^9.2.0", | ||
"nyc": "^14.1.0", | ||
"globby": "^11.0.0", | ||
"lint-staged": "^10.0.8", | ||
"nyc": "^15.0.0", | ||
"perfy": "^1.1.5", | ||
@@ -58,5 +57,4 @@ "postcss-value-parser": "^4.0.0", | ||
"pre-commit": "^1.2.2", | ||
"prettier": "^1.15.2", | ||
"standard-version": "^6.0.1", | ||
"strip-ansi": "^5.0.0", | ||
"prettier": "^2.0.1", | ||
"strip-ansi": "^6.0.0", | ||
"text-table": "^0.2.0" | ||
@@ -83,4 +81,3 @@ }, | ||
"*.js": [ | ||
"eslint --fix", | ||
"git add" | ||
"eslint --fix" | ||
] | ||
@@ -87,0 +84,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62410
4
15
1255
- Removedis-number@^7.0.0
- Removedis-number@7.0.0(transitive)