Skip to content

Commit aa8e2c3

Browse files
committed
Meta: Update scripts/insert_warning.mjs for HTML spec conformance
1 parent aeb4769 commit aa8e2c3

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

scripts/insert_warning.mjs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,18 @@ const main = async args => {
3939
// Parse the template into DOM nodes for appending to page <head>s (metadata
4040
// such as <style> elements) or prepending to page <body>s (everything else).
4141
// https://html.spec.whatwg.org/multipage/dom.html#metadata-content-2
42-
const metadataNames =
43-
'base, link, meta, noscript, script, style, template, title'
44-
.toUpperCase()
45-
.split(', ');
42+
// https://html.spec.whatwg.org/multipage/semantics.html#allowed-in-the-body
43+
// https://html.spec.whatwg.org/multipage/links.html#body-ok
44+
const bodyOkRelPatt =
45+
/^(?:dns-prefetch|modulepreload|pingback|preconnect|prefetch|preload|stylesheet)$/i;
46+
const forceHead = node =>
47+
node.matches?.('base, style, title, meta:not([itemprop])') ||
48+
(node.matches?.('link:not([itemprop])') &&
49+
[...node.relList].some(rel => !rel.match(bodyOkRelPatt)));
4650
const insertDom = JSDOM.fragment(resolved);
4751
const headInserts = [], bodyInserts = [];
4852
for (const node of insertDom.childNodes) {
49-
if (metadataNames.includes(node.nodeName)) headInserts.push(node);
53+
if (forceHead(node)) headInserts.push(node);
5054
else bodyInserts.push(node);
5155
}
5256

0 commit comments

Comments
 (0)