Skip to content

Commit

Permalink
fix: support parsing */ expressions without surrounding spaces (#144)
Browse files Browse the repository at this point in the history
fixes #138
  • Loading branch information
cyjake committed Jan 7, 2022
1 parent a59fac9 commit b143c3a
Show file tree
Hide file tree
Showing 5 changed files with 1,381 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/ValuesParser.js
Expand Up @@ -148,6 +148,8 @@ module.exports = class ValuesParser extends Parser {

if (Punctuation.chars.includes(type)) {
Punctuation.fromTokens(tokens, this);
} else if (type === 'word' && Operator.test(tokens, this)) {
Operator.fromTokens(tokens, this);
} else if (Func.test(tokens)) {
Func.fromTokens(tokens, this);
} else if (this.options.interpolation && Interpolation.test(tokens, this)) {
Expand Down
8 changes: 8 additions & 0 deletions lib/nodes/Operator.js
Expand Up @@ -14,6 +14,7 @@ const Node = require('./Node');

const operators = ['+', '-', '/', '*', '%', '=', '<=', '>=', '<', '>'];
const operRegex = new RegExp(`([/|*}])`);
const compactRegex = /^[*/]\b/;

class Operator extends Node {
constructor(options) {
Expand All @@ -33,6 +34,13 @@ class Operator extends Node {
return operRegex;
}

static test(tokens, parser) {
const [first] = tokens;
const [, value] = first;
const { lastNode } = parser;
return lastNode && lastNode.type === 'func' && compactRegex.test(value);
}

static tokenize(tokens, parser) {
const [first, ...rest] = tokens;
const [, value, startLine, , endLine, endChar] = first;
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/func.js
Expand Up @@ -29,8 +29,10 @@ module.exports = {
'lCH(40% 68.8 34.5 / 50%)',
'hwb(90deg 0% 0% / 0.5)',
'calc(-0.5 * var(foo))',
'calc(var(--foo)*var(--bar))',
'calc(1px + -2vw - 4px)',
'calc(((768px - 100vw) / 2) - 15px)',
'calc(((768px - 100vw)/2) - 15px)',
'bar(baz(black, 10%), 10%)',
'-webkit-linear-gradient(0)',
'var(--foo)',
Expand Down

0 comments on commit b143c3a

Please sign in to comment.