Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7c80db2

Browse files
committedAug 29, 2019
rework attribute selector matching to not use regexes (sveltejs#1710) [WIP]
1 parent 5b4758f commit 7c80db2

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed
 

‎src/compiler/compile/css/Selector.ts

+19-19
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function apply_selector(stylesheet: Stylesheet, blocks: Block[], node: Node, sta
150150
}
151151

152152
if (selector.type === 'ClassSelector') {
153-
if (!attribute_matches(node, 'class', selector.name, '~=', false) && !class_matches(node, selector.name)) return false;
153+
if (!attribute_matches(node, 'class', selector.name, '~=', false) && !node.classes.some(c => c.name === selector.name)) return false;
154154
}
155155

156156
else if (selector.type === 'IdSelector') {
@@ -206,14 +206,21 @@ function apply_selector(stylesheet: Stylesheet, blocks: Block[], node: Node, sta
206206
return true;
207207
}
208208

209-
const operators = {
210-
'=' : (value: string, flags: string) => new RegExp(`^${value}$`, flags),
211-
'~=': (value: string, flags: string) => new RegExp(`\\b${value}\\b`, flags),
212-
'|=': (value: string, flags: string) => new RegExp(`^${value}(-.+)?$`, flags),
213-
'^=': (value: string, flags: string) => new RegExp(`^${value}`, flags),
214-
'$=': (value: string, flags: string) => new RegExp(`${value}$`, flags),
215-
'*=': (value: string, flags: string) => new RegExp(value, flags)
216-
};
209+
function test_attribute(operator, expected_value, case_insensitive, value) {
210+
if (case_insensitive) {
211+
expected_value = expected_value.toLowerCase();
212+
value = value.toLowerCase();
213+
}
214+
switch (operator) {
215+
case '=': return value === expected_value;
216+
case '~=': return ` ${value} `.includes(` ${expected_value} `);
217+
case '|=': return `${value}-`.startsWith(`${expected_value}-`);
218+
case '^=': return value.startsWith(expected_value);
219+
case '$=': return value.endsWith(expected_value);
220+
case '*=': return value.includes(expected_value);
221+
default: throw new Error(`this shouldn't happen`);
222+
}
223+
}
217224

218225
function attribute_matches(node: Node, name: string, expected_value: string, operator: string, case_insensitive: boolean) {
219226
const spread = node.attributes.find(attr => attr.type === 'Spread');
@@ -227,29 +234,22 @@ function attribute_matches(node: Node, name: string, expected_value: string, ope
227234
if (attr.chunks.length > 1) return true;
228235
if (!expected_value) return true;
229236

230-
const pattern = operators[operator](expected_value, case_insensitive ? 'i' : '');
231237
const value = attr.chunks[0];
232238

233239
if (!value) return false;
234-
if (value.type === 'Text') return pattern.test(value.data);
240+
if (value.type === 'Text') return test_attribute(operator, expected_value, case_insensitive, value.data);
235241

236242
const possible_values = new Set();
237243
gather_possible_values(value.node, possible_values);
238244
if (possible_values.has(UNKNOWN)) return true;
239245

240-
for (const x of Array.from(possible_values)) { // TypeScript for-of is slightly unlike JS
241-
if (pattern.test(x)) return true;
246+
for (const value of possible_values) {
247+
if (test_attribute(operator, expected_value, case_insensitive, value)) return true;
242248
}
243249

244250
return false;
245251
}
246252

247-
function class_matches(node, name: string) {
248-
return node.classes.some((class_directive) => {
249-
return new RegExp(`\\b${name}\\b`).test(class_directive.name);
250-
});
251-
}
252-
253253
function unquote(value: Node) {
254254
if (value.type === 'Identifier') return value.name;
255255
const str = value.value;

0 commit comments

Comments
 (0)
Please sign in to comment.