Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 2 additions & 73 deletions lib/nodes/Func.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,82 +10,11 @@
*/
const { getTokens } = require('../tokenize');
const { registerWalker } = require('../walker');
const { identifierRegExp } = require('../tokensRegExp');

const Container = require('./Container');
const Punctuation = require('./Punctuation');

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;
Expand Down Expand Up @@ -122,7 +51,7 @@ class Func extends Container {
let lastToken = brackets;

// fixes #92
if (!reFunctions.test(node.name) && !/^[a-zA-Z]+$/.test(node.name)) {
if (!identifierRegExp.test(node.name)) {
const nameTokens = getTokens(node.name);
tokens.unshift(...nameTokens, brackets);
parser.back(tokens);
Expand Down
43 changes: 1 addition & 42 deletions lib/nodes/Numeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,10 @@
*/

const { registerWalker } = require('../walker');
const { numericRegex } = require('../tokensRegExp');

const Node = require('./Node');

/** 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 {
constructor(options = {}) {
super(options);
Expand Down
41 changes: 41 additions & 0 deletions lib/tokensRegExp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** An Identifier 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-ident-like-token
*/
exports.identifierRegExp = /^(-?(?:[-A-Z_a-z]|[^\x00-\x7F]|\\[^\n\f\r])(?:[-\w]|[^\x00-\x7F]|\\[^\n\f\r])*|%)$/; // eslint-disable-line no-control-regex

/** 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
*/
exports.numberRegex = /^([+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?)$/;

/** A Numeric is:
* 1. One Number; followed by,
* 1.1 Zero or one Unit.
*/
exports.numericRegex = new RegExp(
`^${exports.numberRegex.source.slice(1, -1) + exports.identifierRegExp.source.slice(1, -1)}?$`
);
71 changes: 70 additions & 1 deletion test/fixtures/func.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,76 @@ module.exports = {
'rotate(0.5grad)',
'rotate(0.5turn)',
'1em/var(--line-height)',
'local(foo),local(bar)'
'local(foo),local(bar)',
'derp-face(#0366d6)',
'conic-gradient()',
'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()',
'var()'
],

throws: ['url( /gfx/img/bg.jpg ']
Expand Down
Loading