Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

test(Dialog, Popover): the second-document stories skip only cross-origin style sheets (#9063)

Christopher Chedeau4413083f8d97
diff --git a/apps/storybook/stories/DialogSecondDocumentA11y.stories.tsx b/apps/storybook/stories/DialogSecondDocumentA11y.stories.tsx
index 1f726639b8..2a87e15073 100644
--- a/apps/storybook/stories/DialogSecondDocumentA11y.stories.tsx
+++ b/apps/storybook/stories/DialogSecondDocumentA11y.stories.tsx
@@ -70,15 +70,19 @@ interface DocumentPictureInPicture {
/** Copy the page's style sheets into a second document, as an app does. */
function adoptPageStyles(target: Document) {
for (const sheet of Array.from(document.styleSheets)) {
- try {
- const style = target.createElement('style');
- style.textContent = Array.from(sheet.cssRules, rule => rule.cssText).join(
- '\n',
- );
- target.head.append(style);
- } catch {
- // A cross-origin sheet cannot be read; the page's own sheets can.
+ // A sheet from another origin cannot be read, so only that one is skipped.
+ // Any other error in the copy throws.
+ if (
+ sheet.href != null &&
+ new URL(sheet.href).origin !== window.location.origin
+ ) {
+ continue;
}
+ const style = target.createElement('style');
+ style.textContent = Array.from(sheet.cssRules, rule => rule.cssText).join(
+ '\n',
+ );
+ target.head.append(style);
}
}
diff --git a/apps/storybook/stories/PopoverSecondDocumentA11y.stories.tsx b/apps/storybook/stories/PopoverSecondDocumentA11y.stories.tsx
index 71b6416453..26882b281c 100644
--- a/apps/storybook/stories/PopoverSecondDocumentA11y.stories.tsx
+++ b/apps/storybook/stories/PopoverSecondDocumentA11y.stories.tsx
@@ -70,15 +70,19 @@ interface DocumentPictureInPicture {
/** Copy the page's style sheets into a second document, as an app does. */
function adoptPageStyles(target: Document) {
for (const sheet of Array.from(document.styleSheets)) {
- try {
- const style = target.createElement('style');
- style.textContent = Array.from(sheet.cssRules, rule => rule.cssText).join(
- '\n',
- );
- target.head.append(style);
- } catch {
- // A cross-origin sheet cannot be read; the page's own sheets can.
+ // A sheet from another origin cannot be read, so only that one is skipped.
+ // Any other error in the copy throws.
+ if (
+ sheet.href != null &&
+ new URL(sheet.href).origin !== window.location.origin
+ ) {
+ continue;
}
+ const style = target.createElement('style');
+ style.textContent = Array.from(sheet.cssRules, rule => rule.cssText).join(
+ '\n',
+ );
+ target.head.append(style);
}
}