Skip to content

Commit 963b574

Browse files
authored
fix: unknown functions hang. fixes #112 (#114)
Credit goes to @jonathantneal for identifying the offending code and cause.
1 parent 8c42edf commit 963b574

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

lib/nodes/Func.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const { registerWalker } = require('../walker');
1414
const Container = require('./Container');
1515
const Punctuation = require('./Punctuation');
1616

17-
const allFunctions = [
17+
const cssFunctions = [
1818
'annotation',
1919
'attr',
2020
'blur',
@@ -85,7 +85,7 @@ const allFunctions = [
8585
'var'
8686
];
8787
const vendorPrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
88-
const reFunctions = new RegExp(`^(${vendorPrefixes.join('|')})?(${allFunctions.join('|')})`, 'i');
88+
const reFunctions = new RegExp(`^(${vendorPrefixes.join('|')})?(${cssFunctions.join('|')})`, 'i');
8989
const rePunctuation = new RegExp(`^(\\${Punctuation.chars.join('|\\')})`);
9090
const reColorFunctions = /^(hsla?|hwb|lab|lch|rgba?)$/i;
9191
const reVar = /^var$/i;
@@ -122,7 +122,8 @@ class Func extends Container {
122122
let lastToken = brackets;
123123

124124
// fixes #92
125-
if (!reFunctions.test(node.name) && !/^[a-zA-Z]+$/.test(node.name)) {
125+
// eslint-disable-next-line no-useless-escape
126+
if (!reFunctions.test(node.name) && !/^[a-zA-Z\-]+$/.test(node.name)) {
126127
const nameTokens = getTokens(node.name);
127128
tokens.unshift(...nameTokens, brackets);
128129
parser.back(tokens);

test/fixtures/func.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ module.exports = {
4545
'rotate(0.5grad)',
4646
'rotate(0.5turn)',
4747
'1em/var(--line-height)',
48-
'local(foo),local(bar)'
48+
'local(foo),local(bar)',
49+
'bat-man(#000)',
50+
'conic-gradient()'
4951
],
5052

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

test/snapshots/func.test.js.md

+114
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,78 @@ Generated by [AVA](https://avajs.dev).
845845
},
846846
]
847847

848+
## bat-man(#000)
849+
850+
> Snapshot 1
851+
852+
'bat-man(#000)'
853+
854+
> Snapshot 2
855+
856+
'bat-man(#000)'
857+
858+
> Snapshot 3
859+
860+
[
861+
Func {
862+
isColor: false,
863+
isVar: false,
864+
name: 'bat-man',
865+
nodes: [
866+
Word {
867+
isColor: true,
868+
isHex: true,
869+
isUrl: false,
870+
isVariable: false,
871+
parent: [Circular],
872+
raws: {
873+
after: '',
874+
before: '',
875+
},
876+
source: {
877+
end: {
878+
column: 1,
879+
line: 1,
880+
},
881+
input: Input {
882+
css: '#000',
883+
hasBOM: false,
884+
id: '<input css 78>',
885+
},
886+
start: {
887+
column: 1,
888+
line: 1,
889+
},
890+
},
891+
type: 'word',
892+
value: '#000',
893+
},
894+
],
895+
params: '(#000)',
896+
raws: {
897+
after: '',
898+
before: '',
899+
semicolon: false,
900+
},
901+
source: {
902+
end: {
903+
column: 8,
904+
line: 1,
905+
},
906+
input: Input {
907+
css: 'bat-man(#000)',
908+
hasBOM: false,
909+
id: '<input css 77>',
910+
},
911+
start: {
912+
column: 1,
913+
line: 1,
914+
},
915+
},
916+
type: 'func',
917+
},
918+
]
919+
848920
## calc(((768px - 100vw) / 2) - 15px)
849921

850922
> Snapshot 1
@@ -1475,6 +1547,48 @@ Generated by [AVA](https://avajs.dev).
14751547
},
14761548
]
14771549

1550+
## conic-gradient()
1551+
1552+
> Snapshot 1
1553+
1554+
'conic-gradient()'
1555+
1556+
> Snapshot 2
1557+
1558+
'conic-gradient()'
1559+
1560+
> Snapshot 3
1561+
1562+
[
1563+
Func {
1564+
isColor: false,
1565+
isVar: false,
1566+
name: 'conic-gradient',
1567+
nodes: [],
1568+
params: '()',
1569+
raws: {
1570+
after: '',
1571+
before: '',
1572+
},
1573+
source: {
1574+
end: {
1575+
column: 15,
1576+
line: 1,
1577+
},
1578+
input: Input {
1579+
css: 'conic-gradient()',
1580+
hasBOM: false,
1581+
id: '<input css 79>',
1582+
},
1583+
start: {
1584+
column: 1,
1585+
line: 1,
1586+
},
1587+
},
1588+
type: 'func',
1589+
},
1590+
]
1591+
14781592
## hwb(90deg 0% 0% / 0.5)
14791593

14801594
> Snapshot 1

test/snapshots/func.test.js.snap

423 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)