Skip to content

Commit dfed9b0

Browse files
committed
fix: handle missing style container
1 parent c241125 commit dfed9b0

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/Dom/dynamicCSS.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ function getOrder(prepend?: Prepend): AppendType {
5151
/**
5252
* Find style which inject by rc-util
5353
*/
54-
function findStyles(container: ContainerType) {
54+
function findStyles(container: ContainerType | null) {
55+
if (!container) {
56+
return [];
57+
}
58+
5559
return Array.from(
5660
(containerCache.get(container) || container).children,
5761
).filter(node => node.tagName === 'STYLE') as HTMLStyleElement[];
@@ -79,6 +83,10 @@ export function injectCSS(css: string, option: Options = {}) {
7983
styleNode.innerHTML = css;
8084

8185
const container = getContainer(option);
86+
if (!container) {
87+
return null;
88+
}
89+
8290
const { firstChild } = container;
8391

8492
if (prepend) {
@@ -132,7 +140,7 @@ export function removeCSS(key: string, option: Options = {}) {
132140
const existNode = findExistNode(key, option);
133141
if (existNode) {
134142
const container = getContainer(option);
135-
container.removeChild(existNode);
143+
container?.removeChild(existNode);
136144
}
137145
}
138146

@@ -145,6 +153,10 @@ function syncRealContainer(container: ContainerType, option: Options) {
145153
// Find real container when not cached or cached container removed
146154
if (!cachedRealContainer || !contains(document, cachedRealContainer)) {
147155
const placeholderStyle = injectCSS('', option);
156+
if (!placeholderStyle) {
157+
return;
158+
}
159+
148160
const { parentNode } = placeholderStyle;
149161
containerCache.set(container, parentNode);
150162
container.removeChild(placeholderStyle);
@@ -164,6 +176,10 @@ export function updateCSS(
164176
originOption: Options = {},
165177
) {
166178
const container = getContainer(originOption);
179+
if (!container) {
180+
return null;
181+
}
182+
167183
const styles = findStyles(container);
168184
const option = { ...originOption, styles };
169185

@@ -185,6 +201,10 @@ export function updateCSS(
185201
}
186202

187203
const newNode = injectCSS(css, option);
204+
if (!newNode) {
205+
return null;
206+
}
207+
188208
newNode.setAttribute(getMark(option), key);
189209
return newNode;
190210
}

‎tests/dynamicCSS.test.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ describe('dynamicCSS', () => {
3434
expect(document.querySelector('style').nonce).toEqual('light');
3535
});
3636

37+
it('does not throw when the document has no style container', () => {
38+
const { head, body } = document;
39+
head.remove();
40+
body.remove();
41+
42+
try {
43+
expect(injectCSS(TEST_STYLE)).toBeNull();
44+
} finally {
45+
document.documentElement.append(head, body);
46+
}
47+
});
48+
3749
describe('prepend', () => {
3850
function testPrepend() {
3951
const head = document.querySelector('head');
@@ -138,6 +150,18 @@ describe('dynamicCSS', () => {
138150
expect(document.querySelector('style').innerHTML).toEqual(REPLACE_STYLE);
139151
});
140152

153+
it('does not throw when the document has no style container', () => {
154+
const { head, body } = document;
155+
head.remove();
156+
body.remove();
157+
158+
try {
159+
expect(updateCSS(TEST_STYLE, 'missing-container')).toBeNull();
160+
} finally {
161+
document.documentElement.append(head, body);
162+
}
163+
});
164+
141165
it('replace with CSP', () => {
142166
const REPLACE_STYLE = '.bamboo { context: "little" }';
143167
updateCSS(REPLACE_STYLE, 'unique', { csp: { nonce: 'only' } });

0 commit comments

Comments
 (0)