Skip to content

Commit 6de72c0

Browse files
committed
Fix parsing of selectors with commas in functions
For example: .foo:matches(.bar, .baz) Thanks to @phosphoer
1 parent 72f4293 commit 6de72c0

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

History.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
HEAD
22
==================
33

4+
* allow commas inside selector functions
45
* allow empty property values
56
* changed default `options.position` value to `true`
67
* remove comments from properties and values

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ module.exports = function(css, options){
206206
.replace(/(?:"[^"]*"|'[^']*')/g, function(m) {
207207
return m.replace(/,/g, '\u200C');
208208
})
209-
.split(/\s*,\s*/)
209+
.split(/\s*(?![^(]*\)),\s*/)
210210
.map(function(s) {
211211
return s.replace(/\u200C/g, ',');
212212
});
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
.foo:matches(.bar,.baz),
2+
.foo:matches(.bar, .baz),
3+
.foo:matches(.bar , .baz),
4+
.foo:matches(.bar ,.baz) {
5+
prop: value;
6+
}
7+
8+
.foo:matches(.bar,.baz,.foobar),
9+
.foo:matches(.bar, .baz,),
10+
.foo:matches(,.bar , .baz) {
11+
anotherprop: anothervalue;
12+
}
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
{
2+
"type": "stylesheet",
3+
"stylesheet": {
4+
"rules": [
5+
{
6+
"type": "rule",
7+
"selectors": [
8+
".foo:matches(.bar,.baz)",
9+
".foo:matches(.bar, .baz)",
10+
".foo:matches(.bar , .baz)",
11+
".foo:matches(.bar ,.baz)"
12+
],
13+
"declarations": [
14+
{
15+
"type": "declaration",
16+
"property": "prop",
17+
"value": "value",
18+
"position": {
19+
"start": {
20+
"line": 5,
21+
"column": 3
22+
},
23+
"end": {
24+
"line": 5,
25+
"column": 14
26+
},
27+
"source": "comma-selector-function.css"
28+
}
29+
}
30+
],
31+
"position": {
32+
"start": {
33+
"line": 1,
34+
"column": 1
35+
},
36+
"end": {
37+
"line": 6,
38+
"column": 2
39+
},
40+
"source": "comma-selector-function.css"
41+
}
42+
},
43+
{
44+
"type": "rule",
45+
"selectors": [
46+
".foo:matches(.bar,.baz,.foobar)",
47+
".foo:matches(.bar, .baz,)",
48+
".foo:matches(,.bar , .baz)"
49+
],
50+
"declarations": [
51+
{
52+
"type": "declaration",
53+
"property": "anotherprop",
54+
"value": "anothervalue",
55+
"position": {
56+
"start": {
57+
"line": 11,
58+
"column": 3
59+
},
60+
"end": {
61+
"line": 11,
62+
"column": 28
63+
},
64+
"source": "comma-selector-function.css"
65+
}
66+
}
67+
],
68+
"position": {
69+
"start": {
70+
"line": 8,
71+
"column": 1
72+
},
73+
"end": {
74+
"line": 12,
75+
"column": 2
76+
},
77+
"source": "comma-selector-function.css"
78+
}
79+
}
80+
]
81+
}
82+
}
83+

test/css-parse.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ describe('parse(str)', function(){
1818
var css = read(path.join('test', 'cases', file + '.css'), 'utf8');
1919
var json = read(path.join('test', 'cases', file + '.json'), 'utf8');
2020
var ret = parse(css, { source: file + '.css' });
21+
// normalize line endings from input file
22+
json = JSON.parse(json);
23+
json = JSON.stringify(json, null, 2);
2124
ret = JSON.stringify(ret, null, 2);
2225
ret.should.equal(json);
2326
});

0 commit comments

Comments
 (0)