Register HTMLImageElement name.

Handle DOMParser with empty string

This gets DDG results working.
This commit is contained in:
Karl Seguin
2025-12-23 14:33:47 +08:00
parent e387e005d8
commit 7c755483b1
3 changed files with 29 additions and 7 deletions

View File

@@ -196,12 +196,22 @@ pub fn create(allocator: Allocator) !Snapshot {
// Attach to global if it has a name // Attach to global if it has a name
if (@hasDecl(JsApi.Meta, "name")) { if (@hasDecl(JsApi.Meta, "name")) {
const class_name = if (@hasDecl(JsApi.Meta, "constructor_alias")) if (@hasDecl(JsApi.Meta, "constructor_alias")) {
JsApi.Meta.constructor_alias const v8_class_name = v8.String.initUtf8(isolate, JsApi.Meta.constructor_alias);
else _ = global_obj.setValue(context, v8_class_name, func);
JsApi.Meta.name;
const v8_class_name = v8.String.initUtf8(isolate, class_name); // @TODO: This is wrong. This name should be registered with the
_ = global_obj.setValue(context, v8_class_name, func); // illegalConstructorCallback. I.e. new Image() is OK, but
// new HTMLImageElement() isn't.
// But we _have_ to register the name, i.e. HTMLImageElement
// has to be registered so, for now, instead of creating another
// template, we just hook it into the constructor.
const illegal_class_name = v8.String.initUtf8(isolate, JsApi.Meta.name);
_ = global_obj.setValue(context, illegal_class_name, func);
} else {
const v8_class_name = v8.String.initUtf8(isolate, JsApi.Meta.name);
_ = global_obj.setValue(context, v8_class_name, func);
}
} }
} }

View File

@@ -237,3 +237,10 @@
testing.expectEqual(null, doc2.getElementById('doc1-el')); testing.expectEqual(null, doc2.getElementById('doc1-el'));
} }
</script> </script>
<script id=documentElement>
testing.expectEqual('', new DOMParser().parseFromString('', "text/html").documentElement.textContent);
testing.expectEqual('spice', new DOMParser().parseFromString('spice', "text/html").documentElement.textContent);
testing.expectEqual('<html><head></head><body>spice</body></html>', new DOMParser().parseFromString('spice', "text/html").documentElement.outerHTML);
testing.expectEqual('<html><head></head><body></body></html>', new DOMParser().parseFromString('<html></html>', "text/html").documentElement.outerHTML);
</script>

View File

@@ -42,10 +42,15 @@ pub fn parseFromString(self: *const DOMParser, html: []const u8, mime_type: []co
._proto = undefined, ._proto = undefined,
}); });
var normalized = std.mem.trim(u8, html, &std.ascii.whitespace);
if (normalized.len == 0) {
normalized = "<html></html>";
}
// Parse HTML into the document // Parse HTML into the document
const Parser = @import("../parser/Parser.zig"); const Parser = @import("../parser/Parser.zig");
var parser = Parser.init(page.arena, doc.asNode(), page); var parser = Parser.init(page.arena, doc.asNode(), page);
parser.parse(html); parser.parse(normalized);
if (parser.err) |pe| { if (parser.err) |pe| {
return pe.err; return pe.err;