When --noscript is specified, also exclude <link rel=preload as=script>

This commit is contained in:
Karl Seguin
2025-08-06 15:43:12 +08:00
parent 84d07f3f18
commit b3c81c9e55

View File

@@ -63,7 +63,7 @@ pub fn writeNode(node: *parser.Node, opts: Opts, writer: anytype) anyerror!void
.element => {
// open the tag
const tag_type = try parser.nodeHTMLGetTagType(node) orelse .undef;
if (tag_type == .script and opts.exclude_scripts) {
if (opts.exclude_scripts and try isScriptOrRelated(tag_type, node)) {
return;
}
@@ -147,6 +147,25 @@ pub fn writeChildren(root: *parser.Node, opts: Opts, writer: anytype) !void {
}
}
// When `exclude_scripts` is passed to dump, we don't include <script> tags.
// We also want to omit <link rel=preload as=ascript>
fn isScriptOrRelated(tag_type: parser.Tag, node: *parser.Node) !bool {
if (tag_type == .script) {
return true;
}
if (tag_type == .link) {
const el = parser.nodeToElement(node);
const as = try parser.elementGetAttribute(el, "as") orelse return false;
if (!std.ascii.eqlIgnoreCase(as, "script")) {
return false;
}
const rel = try parser.elementGetAttribute(el, "rel") orelse return false;
return std.ascii.eqlIgnoreCase(rel, "preload");
}
return false;
}
// area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr
// https://html.spec.whatwg.org/#void-elements
fn isVoid(elem: *parser.Element) !bool {