Skip to content

Commit

Permalink
throw if no head to insert styles into
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoff committed Sep 28, 2022
1 parent e57f39d commit 3a9ab9c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
18 changes: 18 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFloat-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,24 @@ describe('ReactDOMFloat', () => {
return readText(text);
}

it('errors if the document does not contain a head when inserting a resource', async () => {
document.head.parentNode.removeChild(document.head);
const root = ReactDOMClient.createRoot(document);
root.render(
<html>
<body>
<link rel="stylesheet" href="foo" precedence="default" />
foo
</body>
</html>,
);
expect(() => {
expect(Scheduler).toFlushWithoutYielding();
}).toThrow(
'While attempting to insert a Resource, React expected the Document to contain a head element but it was not found.',
);
});

describe('HostResource', () => {
// @gate enableFloat
it('warns when you update props to an invalid type', async () => {
Expand Down
23 changes: 17 additions & 6 deletions packages/react-dom/src/client/ReactDOMFloatClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -736,13 +736,20 @@ function insertStyleInstance(
break;
}
}
if (prior && prior.parentNode) {
prior.parentNode.insertBefore(instance, prior.nextSibling);
if (prior) {
// We get the prior from the document so we know it is in the tree.
// We also know that links can't be the topmost Node so the parentNode
// must exist.
((prior.parentNode: any): Node).insertBefore(instance, prior.nextSibling);
} else {
const parent =
ownerDocument.head || ownerDocument.body || ownerDocument.documentElement;
const parent = ownerDocument.head;
if (parent) {
parent.insertBefore(instance, parent.firstChild);
} else {
throw new Error(
'While attempting to insert a Resource, React expected the Document to contain' +
' a head element but it was not found.',
);
}
}
}
Expand All @@ -766,10 +773,14 @@ function insertPreloadInstance(
ownerDocument: Document,
): void {
if (!ownerDocument.contains(instance)) {
const parent =
ownerDocument.head || ownerDocument.body || ownerDocument.documentElement;
const parent = ownerDocument.head;
if (parent) {
parent.appendChild(instance);
} else {
throw new Error(
'While attempting to insert a Resource, React expected the Document to contain' +
' a head element but it was not found.',
);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/error-codes/codes.json
Original file line number Diff line number Diff line change
Expand Up @@ -431,5 +431,6 @@
"443": "acquireResource encountered a resource type it did not expect: \"%s\". this is a bug in React.",
"444": "getResource encountered a resource type it did not expect: \"%s\". this is a bug in React.",
"445": "\"currentResources\" was expected to exist. This is a bug in React.",
"446": "\"currentDocument\" was expected to exist. This is a bug in React."
"446": "\"currentDocument\" was expected to exist. This is a bug in React.",
"447": "While attempting to insert a Resource, React expected the Document to contain a head element but it was not found."
}

0 comments on commit 3a9ab9c

Please sign in to comment.