Skip to content

Commit 2461f0a

Browse files
committed
strip quotes, parse pseudos when appropriate
1 parent d54c285 commit 2461f0a

File tree

2 files changed

+1210
-109
lines changed

2 files changed

+1210
-109
lines changed

index.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ var attribSelectors = {
3434
".": ["class", "element"]
3535
};
3636

37+
//pseudos, whose data-property is parsed as well
38+
var unpackPseudos = {
39+
__proto__: null,
40+
"has": true,
41+
"not": true,
42+
"matches": true
43+
};
44+
45+
var stripQuotesFromPseudos = {
46+
__proto__: unpackPseudos,
47+
"contains": true
48+
};
49+
3750
//unescape function taken from https://github.com/jquery/sizzle/blob/master/src/sizzle.js#L139
3851
function funescape( _, escaped, escapedWhitespace ) {
3952
var high = "0x" + escaped - 0x10000;
@@ -71,7 +84,7 @@ function parse(selector, options){
7184
tokens = [],
7285
sawWS = false,
7386
data, firstChar, name;
74-
87+
7588
function getName(){
7689
var sub = selector.match(re_name)[0];
7790
selector = selector.substr(sub.length);
@@ -154,29 +167,41 @@ function parse(selector, options){
154167
value: unescapeCSS(data[4] || data[5] || ""),
155168
ignoreCase: !!data[6]
156169
});
157-
170+
158171
} else if(firstChar === ":"){
159172
//if(selector.charAt(0) === ":"){} //TODO pseudo-element
160173
name = getName().toLowerCase();
161174
data = null;
162-
175+
163176
if(selector.charAt(0) === "("){
164177
var pos = getClosingPos(selector);
165178
data = selector.substr(1, pos - 2);
166179
selector = selector.substr(pos);
180+
181+
if(name in stripQuotesFromPseudos){
182+
var quot = data.charAt(0);
183+
184+
if(quot === data.slice(-1) && (quot === "'" || quot === "\"")){
185+
data = data.slice(1, -1);
186+
}
187+
188+
if(name in unpackPseudos){
189+
data = parse(data, options);
190+
}
191+
}
167192
}
168-
193+
169194
tokens.push({type: "pseudo", name: name, data: data});
170195
} else {
171196
//otherwise, the parser needs to throw or it would enter an infinite loop
172197
throw new SyntaxError("Unmatched selector: " + firstChar + selector);
173198
}
174199
}
175200
}
176-
201+
177202
if(subselects.length > 0 && tokens.length === 0){
178203
throw new SyntaxError("empty sub-selector");
179204
}
180205
subselects.push(tokens);
181206
return subselects;
182-
}
207+
}

0 commit comments

Comments
 (0)