Skip to content

Commit b00ff74

Browse files
committed
fixed selector parser to parse attribute selectors with/without comma properly
1 parent aa7e232 commit b00ff74

File tree

3 files changed

+250
-1
lines changed

3 files changed

+250
-1
lines changed

index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,15 @@ module.exports = function(css, options){
191191
if (!m) return;
192192
/* @fix Remove all comments from selectors
193193
* http://ostermiller.org/findcomment.html */
194-
return trim(m[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '').split(/\s*,\s*/);
194+
return trim(m[0])
195+
.replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '')
196+
.replace(/(?:"[^"]*"|'[^']*')/g, function(m) {
197+
return m.replace(/,/g, '\u200C');
198+
})
199+
.split(/\s*,\s*/)
200+
.map(function(s) {
201+
return s.replace(/\u200C/g, ',');
202+
});
195203
}
196204

197205
/**

test/cases/comma-attribute.css

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.foo[bar="baz,quz"] {
2+
foobar: 123;
3+
}
4+
5+
.bar,
6+
#bar[baz="qux,foo"],
7+
#qux {
8+
foobar: 456;
9+
}
10+
11+
.baz[qux=",foo"],
12+
.baz[qux="foo,"],
13+
.baz[qux="foo,bar,baz"],
14+
.baz[qux=",foo,bar,baz,"],
15+
.baz[qux=" , foo , bar , baz , "] {
16+
foobar: 789;
17+
}
18+
19+
.qux[foo='bar,baz'],
20+
.qux[bar="baz,foo"],
21+
#qux[foo="foobar"],
22+
#qux[foo=',bar,baz, '] {
23+
foobar: 012;
24+
}
25+
26+
#foo[foo=""],
27+
#foo[bar=" "],
28+
#foo[bar=","],
29+
#foo[bar=", "],
30+
#foo[bar=" ,"],
31+
#foo[bar=" , "],
32+
#foo[baz=''],
33+
#foo[qux=' '],
34+
#foo[qux=','],
35+
#foo[qux=', '],
36+
#foo[qux=' ,'],
37+
#foo[qux=' , '] {
38+
foobar: 345;
39+
}

test/cases/comma-attribute.json

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
{
2+
"type": "stylesheet",
3+
"stylesheet": {
4+
"rules": [
5+
{
6+
"type": "rule",
7+
"selectors": [
8+
".foo[bar=\"baz,quz\"]"
9+
],
10+
"declarations": [
11+
{
12+
"type": "declaration",
13+
"property": "foobar",
14+
"value": "123",
15+
"position": {
16+
"start": {
17+
"line": 2,
18+
"column": 3
19+
},
20+
"end": {
21+
"line": 2,
22+
"column": 14
23+
},
24+
"source": "comma-attribute.css"
25+
}
26+
}
27+
],
28+
"position": {
29+
"start": {
30+
"line": 1,
31+
"column": 1
32+
},
33+
"end": {
34+
"line": 3,
35+
"column": 2
36+
},
37+
"source": "comma-attribute.css"
38+
}
39+
},
40+
{
41+
"type": "rule",
42+
"selectors": [
43+
".bar",
44+
"#bar[baz=\"qux,foo\"]",
45+
"#qux"
46+
],
47+
"declarations": [
48+
{
49+
"type": "declaration",
50+
"property": "foobar",
51+
"value": "456",
52+
"position": {
53+
"start": {
54+
"line": 8,
55+
"column": 3
56+
},
57+
"end": {
58+
"line": 8,
59+
"column": 14
60+
},
61+
"source": "comma-attribute.css"
62+
}
63+
}
64+
],
65+
"position": {
66+
"start": {
67+
"line": 5,
68+
"column": 1
69+
},
70+
"end": {
71+
"line": 9,
72+
"column": 2
73+
},
74+
"source": "comma-attribute.css"
75+
}
76+
},
77+
{
78+
"type": "rule",
79+
"selectors": [
80+
".baz[qux=\",foo\"]",
81+
".baz[qux=\"foo,\"]",
82+
".baz[qux=\"foo,bar,baz\"]",
83+
".baz[qux=\",foo,bar,baz,\"]",
84+
".baz[qux=\" , foo , bar , baz , \"]"
85+
],
86+
"declarations": [
87+
{
88+
"type": "declaration",
89+
"property": "foobar",
90+
"value": "789",
91+
"position": {
92+
"start": {
93+
"line": 16,
94+
"column": 3
95+
},
96+
"end": {
97+
"line": 16,
98+
"column": 14
99+
},
100+
"source": "comma-attribute.css"
101+
}
102+
}
103+
],
104+
"position": {
105+
"start": {
106+
"line": 11,
107+
"column": 1
108+
},
109+
"end": {
110+
"line": 17,
111+
"column": 2
112+
},
113+
"source": "comma-attribute.css"
114+
}
115+
},
116+
{
117+
"type": "rule",
118+
"selectors": [
119+
".qux[foo='bar,baz']",
120+
".qux[bar=\"baz,foo\"]",
121+
"#qux[foo=\"foobar\"]",
122+
"#qux[foo=',bar,baz, ']"
123+
],
124+
"declarations": [
125+
{
126+
"type": "declaration",
127+
"property": "foobar",
128+
"value": "012",
129+
"position": {
130+
"start": {
131+
"line": 23,
132+
"column": 3
133+
},
134+
"end": {
135+
"line": 23,
136+
"column": 14
137+
},
138+
"source": "comma-attribute.css"
139+
}
140+
}
141+
],
142+
"position": {
143+
"start": {
144+
"line": 19,
145+
"column": 1
146+
},
147+
"end": {
148+
"line": 24,
149+
"column": 2
150+
},
151+
"source": "comma-attribute.css"
152+
}
153+
},
154+
{
155+
"type": "rule",
156+
"selectors": [
157+
"#foo[foo=\"\"]",
158+
"#foo[bar=\" \"]",
159+
"#foo[bar=\",\"]",
160+
"#foo[bar=\", \"]",
161+
"#foo[bar=\" ,\"]",
162+
"#foo[bar=\" , \"]",
163+
"#foo[baz='']",
164+
"#foo[qux=' ']",
165+
"#foo[qux=',']",
166+
"#foo[qux=', ']",
167+
"#foo[qux=' ,']",
168+
"#foo[qux=' , ']"
169+
],
170+
"declarations": [
171+
{
172+
"type": "declaration",
173+
"property": "foobar",
174+
"value": "345",
175+
"position": {
176+
"start": {
177+
"line": 38,
178+
"column": 3
179+
},
180+
"end": {
181+
"line": 38,
182+
"column": 14
183+
},
184+
"source": "comma-attribute.css"
185+
}
186+
}
187+
],
188+
"position": {
189+
"start": {
190+
"line": 26,
191+
"column": 1
192+
},
193+
"end": {
194+
"line": 39,
195+
"column": 2
196+
},
197+
"source": "comma-attribute.css"
198+
}
199+
}
200+
]
201+
}
202+
}

0 commit comments

Comments
 (0)