Skip to content

Commit 6b52946

Browse files
committed
fix: detect all insertRule errors caused by unrecognized pseudo-selectors
1 parent 14a61e4 commit 6b52946

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

packages/sheet/src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,26 @@ export class StyleSheet {
144144
// the big drawback is that the css won't be editable in devtools
145145
sheet.insertRule(rule, sheet.cssRules.length)
146146
} catch (e) {
147+
/*
148+
Unrecognized vendor-specific pseudo-selectors like -moz-*
149+
or -webkit-* will throw a SyntaxError when parsed during
150+
the insertRule() call. Since the failing selector(s) target
151+
a different browser, these errors are safe to ignore.
152+
153+
All browsers seem to have slightly different error messages around
154+
this case, making the detection just a little bit more difficult.
155+
SyntaxError errors aren't surfacing frequently.
156+
As long as the rule includes a left curly bracket marking
157+
the beginning of a declaration block AND a vendor-specific
158+
pseudo-selector, we can be almost certain that it is the reason
159+
for the thrown SyntaxError.
160+
*/
147161
if (
148162
isDevelopment &&
149-
!/:(-moz-placeholder|-moz-focus-inner|-moz-focusring|-ms-input-placeholder|-moz-read-write|-moz-read-only|-ms-clear|-ms-expand|-ms-reveal){/.test(
150-
rule
163+
!(
164+
e instanceof SyntaxError &&
165+
/:-(webkit|moz|ms|o)-{/.test(rule) &&
166+
rule.indexOf('{') !== -1
151167
)
152168
) {
153169
console.error(

0 commit comments

Comments
 (0)