Skip to content

Commit 3552433

Browse files
committed
fix: wonky function names. fixes #92
1 parent a5d5540 commit 3552433

File tree

4 files changed

+204
-2
lines changed

4 files changed

+204
-2
lines changed

lib/nodes/Func.js

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,84 @@
88
The above copyright notice and this permission notice shall be
99
included in all copies or substantial portions of this Source Code Form.
1010
*/
11+
const { getTokens } = require('../tokenize');
1112
const { registerWalker } = require('../walker');
1213

1314
const Container = require('./Container');
1415

16+
const allFunctions = [
17+
'annotation',
18+
'attr',
19+
'blur',
20+
'brightness',
21+
'calc',
22+
'character-variant',
23+
'circle',
24+
'contrast',
25+
'cubic-bezier',
26+
'dir',
27+
'drop-shadow',
28+
'element',
29+
'ellipse',
30+
'grayscale',
31+
'hsl',
32+
'hsla',
33+
'hue-rotate',
34+
'image',
35+
'inset',
36+
'invert',
37+
'lang',
38+
'linear-gradient',
39+
'matrix',
40+
'matrix3d',
41+
'minmax',
42+
'not',
43+
'nth-child',
44+
'nth-last-child',
45+
'nth-last-of-type',
46+
'nth-of-type',
47+
'opacity',
48+
'ornaments',
49+
'perspective',
50+
'polygon',
51+
'radial-gradient',
52+
'rect',
53+
'repeat',
54+
'repeating-linear-gradient',
55+
'repeating-radial-gradient',
56+
'rgb',
57+
'rgba',
58+
'rotate',
59+
'rotatex',
60+
'rotatey',
61+
'rotatez',
62+
'rotate3d',
63+
'saturate',
64+
'scale',
65+
'scalex',
66+
'scaley',
67+
'scalez',
68+
'scale3d',
69+
'sepia',
70+
'skew',
71+
'skewx',
72+
'skewy',
73+
'steps',
74+
'styleset',
75+
'stylistic',
76+
'swash',
77+
'symbols',
78+
'translate',
79+
'translatex',
80+
'translatey',
81+
'translatez',
82+
'translate3d',
83+
'url',
84+
'var'
85+
];
1586
const colorFunctions = ['hsl', 'hsla', 'rgb', 'rgba'];
87+
const vendorPrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
88+
const reFunctions = new RegExp(`^(${vendorPrefixes.join('|')})?(${allFunctions.join('|')})`, 'i');
1689
const reVar = /^--[^\s]+$/;
1790

1891
class Func extends Container {
@@ -43,6 +116,14 @@ class Func extends Container {
43116
let expectedParens = 1;
44117
let lastToken = brackets;
45118

119+
// fixes #92
120+
if (!reFunctions.test(node.name) && !/^[a-zA-Z]+$/.test(node.name)) {
121+
const nameTokens = getTokens(node.name);
122+
tokens.unshift(...nameTokens, brackets);
123+
parser.back(tokens);
124+
return;
125+
}
126+
46127
parser.init(node, startLine, startChar);
47128
parser.current = node; // eslint-disable-line no-param-reassign
48129

@@ -85,7 +166,8 @@ class Func extends Container {
85166
opts.parentNode = node;
86167
// use a new parser to parse the params of the function. recursion here makes for easier maint
87168
// we must require this here due to circular dependency resolution
88-
const { parse } = require('../'); // eslint-disable-line global-require
169+
// eslint-disable-next-line global-require
170+
const { parse } = require('../');
89171
const root = parse(params, opts);
90172
const { nodes: children } = root;
91173

test/fixtures/func.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ module.exports = {
4040
'rotate(.5deg)',
4141
'rotate(0.5rad)',
4242
'rotate(0.5grad)',
43-
'rotate(0.5turn)'
43+
'rotate(0.5turn)',
44+
'1em/var(--line-height)'
4445
],
4546

4647
throws: ['url( /gfx/img/bg.jpg ']

test/snapshots/func.test.js.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3362,3 +3362,122 @@ Generated by [AVA](https://ava.li).
33623362
type: 'func',
33633363
},
33643364
]
3365+
3366+
## 1em/var(--line-height)
3367+
3368+
> Snapshot 1
3369+
3370+
'1em'
3371+
3372+
> Snapshot 2
3373+
3374+
'1em/var(--line-height)'
3375+
3376+
> Snapshot 3
3377+
3378+
[
3379+
Numeric {
3380+
raws: {
3381+
after: '',
3382+
before: '',
3383+
},
3384+
source: {
3385+
end: {
3386+
column: 1,
3387+
line: 1,
3388+
},
3389+
input: Input {
3390+
css: '1em/var(--line-height)',
3391+
hasBOM: false,
3392+
id: '<input css 65>',
3393+
},
3394+
start: {
3395+
column: 1,
3396+
line: 1,
3397+
},
3398+
},
3399+
type: 'numeric',
3400+
unit: 'em',
3401+
value: '1',
3402+
},
3403+
Operator {
3404+
raws: {
3405+
after: '',
3406+
before: '',
3407+
},
3408+
source: {
3409+
end: {
3410+
column: 4,
3411+
line: 1,
3412+
},
3413+
input: Input {
3414+
css: '1em/var(--line-height)',
3415+
hasBOM: false,
3416+
id: '<input css 65>',
3417+
},
3418+
start: {
3419+
column: 4,
3420+
line: 1,
3421+
},
3422+
},
3423+
type: 'operator',
3424+
value: '/',
3425+
},
3426+
Func {
3427+
isColor: false,
3428+
isVar: true,
3429+
name: 'var',
3430+
nodes: [
3431+
Word {
3432+
isColor: false,
3433+
isHex: false,
3434+
isUrl: false,
3435+
isVariable: true,
3436+
parent: [Circular],
3437+
raws: {
3438+
after: '',
3439+
before: '',
3440+
},
3441+
source: {
3442+
end: {
3443+
column: 1,
3444+
line: 1,
3445+
},
3446+
input: Input {
3447+
css: '--line-height',
3448+
hasBOM: false,
3449+
id: '<input css 67>',
3450+
},
3451+
start: {
3452+
column: 1,
3453+
line: 1,
3454+
},
3455+
},
3456+
type: 'word',
3457+
value: '--line-height',
3458+
},
3459+
],
3460+
params: '(--line-height)',
3461+
raws: {
3462+
after: '',
3463+
before: '',
3464+
semicolon: false,
3465+
},
3466+
source: {
3467+
end: {
3468+
column: 8,
3469+
line: 1,
3470+
},
3471+
input: Input {
3472+
css: '1em/var(--line-height)',
3473+
hasBOM: false,
3474+
id: '<input css 65>',
3475+
},
3476+
start: {
3477+
column: 5,
3478+
line: 1,
3479+
},
3480+
},
3481+
type: 'func',
3482+
},
3483+
]

test/snapshots/func.test.js.snap

586 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)