Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: preserve stylesheet base URL in shadow roots (#10746)

Christoph Deil99187d886d83
diff --git a/frontend/src/plugins/core/__test__/registerReactComponent.test.ts b/frontend/src/plugins/core/__test__/registerReactComponent.test.ts
index a285a72294..fef961a90f 100644
--- a/frontend/src/plugins/core/__test__/registerReactComponent.test.ts
+++ b/frontend/src/plugins/core/__test__/registerReactComponent.test.ts
@@ -32,6 +32,61 @@ function makePlugin(tagName: string) {
};
}
+describe("stylesheet copying", () => {
+ afterEach(() => {
+ delete (Document.prototype as Partial<Document>).adoptedStyleSheets;
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ });
+
+ test.each([
+ {
+ name: "linked stylesheet",
+ href: "http://localhost/assets/index.css",
+ title: "marimo-linked-test",
+ expectedBaseURL: "http://localhost/assets/index.css",
+ },
+ {
+ name: "inline stylesheet",
+ href: null,
+ title: "marimo-inline-test",
+ expectedBaseURL: document.baseURI,
+ },
+ ])(
+ "preserves the base URL of a $name",
+ ({ href, title, expectedBaseURL }) => {
+ const constructedStyleSheet = {
+ replaceSync: vi.fn(),
+ } as unknown as CSSStyleSheet;
+ const constructor = vi.fn(function () {
+ return constructedStyleSheet;
+ });
+ constructor.prototype.replace = vi.fn();
+ vi.stubGlobal("CSSStyleSheet", constructor);
+ Object.defineProperty(Document.prototype, "adoptedStyleSheets", {
+ configurable: true,
+ value: [],
+ });
+
+ const source = {
+ cssRules: [],
+ href,
+ ownerNode: null,
+ title,
+ } as unknown as CSSStyleSheet;
+ vi.spyOn(document, "styleSheets", "get").mockReturnValue([
+ source,
+ ] as unknown as StyleSheetList);
+
+ const tag = uniqueTag("stylesheet-base-url");
+ registerReactComponent(makePlugin(tag));
+ document.createElement(tag);
+
+ expect(constructor).toHaveBeenCalledWith({ baseURL: expectedBaseURL });
+ },
+ );
+});
+
describe("isCustomMarimoElement", () => {
test("returns false for null", () => {
expect(isCustomMarimoElement(null)).toBe(false);
diff --git a/frontend/src/plugins/core/registerReactComponent.tsx b/frontend/src/plugins/core/registerReactComponent.tsx
index 3503b6f016..233631b134 100644
--- a/frontend/src/plugins/core/registerReactComponent.tsx
+++ b/frontend/src/plugins/core/registerReactComponent.tsx
@@ -519,7 +519,9 @@ export function registerReactComponent<T>(plugin: IPlugin<T, unknown>): void {
if (!styleSheetCache.has(sheetUniqueKey)) {
// We need to create a new stylesheet because we can't use the same
// stylesheet otherwise the browser will throw an error.
- const newSheet = new CSSStyleSheet();
+ const newSheet = new CSSStyleSheet({
+ baseURL: sheet.href ?? document.baseURI,
+ });
try {
newSheet.replaceSync(
Array.from(sheet.cssRules)