Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/common/src/lib/create-styles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, it, expect, afterEach } from 'vitest';
import { createStyles } from './create-styles.js';

describe('createStyles', () => {
const originalWindow = (globalThis as any).window;
const originalDocument = (globalThis as any).Document;

afterEach(() => {
if (originalWindow === undefined) {
delete (globalThis as any).window;
} else {
(globalThis as any).window = originalWindow;
}

if (originalDocument === undefined) {
delete (globalThis as any).Document;
} else {
(globalThis as any).Document = originalDocument;
}
});

it('returns a CSSStyleSheet when constructable stylesheets are supported', () => {
class FakeSheet {
replaceSync(_css: string) {}
}
(FakeSheet as any).prototype.replaceSync = function () {};
const FakeDoc = function () {} as any;
FakeDoc.prototype = { adoptedStyleSheets: [] };

(globalThis as any).window = { CSSStyleSheet: FakeSheet };
(globalThis as any).Document = FakeDoc;

const result = createStyles('body{}');
expect(result).toBeInstanceOf(FakeSheet);
});
});

12 changes: 3 additions & 9 deletions packages/common/src/lib/create-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,15 @@ const hasConstructableStylesheetsSupport = () =>

export const createStyles = (cssText: string) => {
if (!hasConstructableStylesheetsSupport()) {
return css`
${unsafeCSS(cssText)}
`;
return css`${unsafeCSS(cssText)}`;
}

try {
const sheet = new CSSStyleSheet();
sheet.replaceSync(cssText);
return css`
${unsafeCSS(cssText)}
`;
return sheet;
} catch (error) {
warn("Failed to create CSSStyleSheet:", error);
return css`
${unsafeCSS(cssText)}
`;
return css`${unsafeCSS(cssText)}`;
}
};
Loading