Skip to content

Commit cc6d443

Browse files
authored
Merge pull request #926 from lightpanda-io/noscript_exclude_preload
When --noscript is specified, also exclude <link rel=preload as=script>
2 parents 84d07f3 + b3c81c9 commit cc6d443

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/browser/dump.zig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn writeNode(node: *parser.Node, opts: Opts, writer: anytype) anyerror!void
6363
.element => {
6464
// open the tag
6565
const tag_type = try parser.nodeHTMLGetTagType(node) orelse .undef;
66-
if (tag_type == .script and opts.exclude_scripts) {
66+
if (opts.exclude_scripts and try isScriptOrRelated(tag_type, node)) {
6767
return;
6868
}
6969

@@ -147,6 +147,25 @@ pub fn writeChildren(root: *parser.Node, opts: Opts, writer: anytype) !void {
147147
}
148148
}
149149

150+
// When `exclude_scripts` is passed to dump, we don't include <script> tags.
151+
// We also want to omit <link rel=preload as=ascript>
152+
fn isScriptOrRelated(tag_type: parser.Tag, node: *parser.Node) !bool {
153+
if (tag_type == .script) {
154+
return true;
155+
}
156+
if (tag_type == .link) {
157+
const el = parser.nodeToElement(node);
158+
const as = try parser.elementGetAttribute(el, "as") orelse return false;
159+
if (!std.ascii.eqlIgnoreCase(as, "script")) {
160+
return false;
161+
}
162+
163+
const rel = try parser.elementGetAttribute(el, "rel") orelse return false;
164+
return std.ascii.eqlIgnoreCase(rel, "preload");
165+
}
166+
return false;
167+
}
168+
150169
// area, base, br, col, embed, hr, img, input, link, meta, source, track, wbr
151170
// https://html.spec.whatwg.org/#void-elements
152171
fn isVoid(elem: *parser.Element) !bool {

0 commit comments

Comments
 (0)