Skip to content

Commit

Permalink
Fix edge case hints mode crash in Firefox
Browse files Browse the repository at this point in the history
The user can input invalid CSS when customizing the look of hints.
Normally, that’s handled by the CSS engine automatically – silently
ignoring the invalid parts.

However, in Firefox on pages with very strict CSP rules – such as
crates.io – the CSS is applied manually. Previsouly, an invalid selector
caused `element.matches(selector)` to throw an error, preventing the
hints from being rendered at all. With this commit, if a rule has an
invalid selector it is ignored completely, just like in regular CSS.
  • Loading branch information
lydell committed Feb 22, 2020
1 parent 4799ae8 commit e7a8edf
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/renderer/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,18 @@ export function parseCSS(css: string): Array<Rule> {
ruleRegex.lastIndex = 0;

while ((match = ruleRegex.exec(normalized))) {
const [, selector, declarationsString] = match;
const [, rawSelector, declarationsString] = match;
const selector = rawSelector.trim();

try {
document.querySelector(selector);
} catch {
// Just like in CSS, ignore the entire rule if the selector is invalid.
continue;
}

rules.push({
selector: selector.trim(),
selector,
declarations: parseDeclarations(declarationsString),
});
}
Expand Down

0 comments on commit e7a8edf

Please sign in to comment.