Skip to content

Commit 8b4b179

Browse files
committed
Fix #265: CSS loading problems, if 3rd party CSS is loaded from external sources
1 parent 2e9ccc3 commit 8b4b179

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
submitted to the server. This caused invalid data in rare occasions.
2727
* Fix: In the `Selectize` widget, the placeholder in the internal lookup field now is only
2828
rendered if nothing has been selected.
29+
* Fix: CSS loading problems, if 3rd party CSS is loaded from external sources.
2930

3031
- 2.2.4
3132
* Fix #266: Timing issue in widgets `DateField` and `DateTimeField`. Input focus switched before

client/django-formset/helpers.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,20 @@ export namespace StyleHelpers {
136136
export function stylesAreInstalled(baseSelector: string) : CSSStyleSheet|null {
137137
// check if styles have been loaded for this widget and return the CSSStyleSheet
138138
for (let k = document.styleSheets.length - 1; k >= 0; --k) {
139-
const cssRule = document?.styleSheets?.item(k)?.cssRules?.item(0);
140-
if (cssRule instanceof CSSStyleRule && cssRule.selectorText.trim() === baseSelector) {
141-
return document.styleSheets.item(k);
139+
try {
140+
const cssRule = document?.styleSheets?.item(k)?.cssRules?.item(0);
141+
if (cssRule instanceof CSSStyleRule && cssRule.selectorText.trim() === baseSelector) {
142+
return document.styleSheets.item(k);
143+
}
144+
} catch (e: any) {
145+
if (e.name === 'SecurityError') {
146+
// Trying (and failing) to load a stylesheet from another domain.
147+
// It won't be the one we're looking for, so just swallow the error.
148+
// Please also read
149+
// https://medium.com/better-programming/how-to-fix-the-failed-to-read-the-cssrules-property-from-cssstylesheet-error-431d84e4a139
150+
} else {
151+
throw e;
152+
}
142153
}
143154
}
144155
return null;

0 commit comments

Comments
 (0)