webapi: implement validate-and-extract; real namespace URIs

Fixes 158 failing tests in WPT
/dom/nodes/DOMImplementation-createDocument.html (276/434 -> 434/434)
and improves Document-createElementNS.html (82/596 -> 197/596),
Document-createElement.html (29/147 -> 34/147),
Document-contentType/createDocument.html (0/1 -> 1/1) and
name-validation.html (0/5 -> 2/5). The remaining failures in those
files need XML/XHTML document loading, which Lightpanda doesn't have.

- Document now implements the DOM spec's §1.4 name validation
  productions (valid element/attribute local name, valid namespace
  prefix) and the "validate and extract" algorithm, replacing the
  older ad-hoc element name check: prefixes are validated, and the
  prefix/namespace consistency rules throw NamespaceError (prefix
  without namespace, xml/xmlns mismatches).
- createElementNS and DOMImplementation.createDocument route through
  it. createDocument also now: treats its two first arguments as
  required (namespace via the js.Nullable wrapper, qualifiedName as a
  raw js.Value so [LegacyNullToEmptyString] null maps to "" while
  undefined stringifies), sets the spec's namespace-dependent
  contentType, and registers custom namespace URIs for the root
  element.
- element.namespaceURI now returns the actual URI for namespaces
  outside the built-in set (it returned a lightpanda.io placeholder),
  and Element.clone copies the registration. The unit tests asserting
  the placeholder are updated to the correct values.

Coverage: DOMImplementation-createDocument.html 276/434 -> 434/434 and
the improvements above. No regressions across /dom/nodes; 1002/1002
unit tests pass.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Francis Bouvier
2026-07-10 22:12:54 +02:00
committed by Karl Seguin
parent 4c306d50b7
commit aba796eb36
6 changed files with 172 additions and 34 deletions

View File

@@ -32,8 +32,7 @@
const unknownNsElement = document.createElementNS('http://example.com/unknown', 'custom');
testing.expectEqual('custom', unknownNsElement.tagName);
// Should be http://example.com/unknown
testing.expectEqual('http://lightpanda.io/unsupported/namespace', unknownNsElement.namespaceURI);
testing.expectEqual('http://example.com/unknown', unknownNsElement.namespaceURI);
const regularDiv = document.createElement('div');
testing.expectEqual('DIV', regularDiv.tagName);
@@ -45,5 +44,5 @@
testing.expectEqual('te:ST', custom.tagName);
testing.expectEqual('te', custom.prefix);
testing.expectEqual('ST', custom.localName);
testing.expectEqual('http://lightpanda.io/unsupported/namespace', custom.namespaceURI); // Should be test
testing.expectEqual('test', custom.namespaceURI);
</script>

View File

@@ -221,8 +221,7 @@
const root = doc.documentElement;
testing.expectEqual('prefix:localName', root.tagName);
// TODO: Custom namespaces are being replaced with an empty value
testing.expectEqual('http://lightpanda.io/unsupported/namespace', root.namespaceURI);
testing.expectEqual('http://example.com', root.namespaceURI);
}
</script>

View File

@@ -16,6 +16,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const std = @import("std");
const js = @import("../js/js.zig");
const Frame = @import("../Frame.zig");
const Node = @import("Node.zig");
@@ -79,10 +80,34 @@ pub fn createHTMLDocument(_: *const DOMImplementation, title: ?js.NullableString
return document;
}
pub fn createDocument(_: *const DOMImplementation, namespace_: ?[]const u8, qualified_name: ?[]const u8, doctype: ?*DocumentType, frame: *Frame) !*Document {
pub fn createDocument(_: *const DOMImplementation, namespace_nullable: js.Nullable([]const u8), qualified_name_: js.Value, doctype: ?*DocumentType, frame: *Frame) !*Document {
// Both namespace (nullable) and qualifiedName are required arguments.
const namespace_ = namespace_nullable.value;
// Per Web IDL, qualifiedName is [LegacyNullToEmptyString]: null becomes
// the empty string, while undefined stringifies to "undefined". The raw
// js.Value keeps that distinction.
const qname: []const u8 = blk: {
if (qualified_name_.isNull()) {
break :blk "";
}
break :blk try qualified_name_.toStringSlice();
};
if (qname.len > 0) {
_ = try Document.validateAndExtract(namespace_, qname, .element);
}
// Create XML Document
const document = (try frame._factory.document(Node.Document.XMLDocument{ ._proto = undefined })).asDocument();
document._url = "about:blank";
// Per spec the content type depends on the requested namespace.
document._content_type = blk: {
const ns = namespace_ orelse break :blk "application/xml";
if (std.mem.eql(u8, ns, "http://www.w3.org/1999/xhtml")) break :blk "application/xhtml+xml";
if (std.mem.eql(u8, ns, "http://www.w3.org/2000/svg")) break :blk "image/svg+xml";
break :blk "application/xml";
};
// Append doctype if provided
if (doctype) |dt| {
@@ -90,12 +115,20 @@ pub fn createDocument(_: *const DOMImplementation, namespace_: ?[]const u8, qual
}
// Create and append root element if qualified_name provided
if (qualified_name) |qname| {
if (qname.len > 0) {
const namespace = Node.Element.Namespace.parse(namespace_);
const root = try Frame.node_factory.createElementNS(frame, namespace, qname, null);
_ = try document.asNode().appendChild(root, frame);
if (qname.len > 0) {
const namespace = Node.Element.Namespace.parse(namespace_);
const root = try Frame.node_factory.createElementNS(frame, namespace, qname, null);
// Store the original URI for unknown namespaces so namespaceURI and
// lookupNamespaceURI can return it (mirrors Document.createElementNS).
if (namespace == .unknown) {
if (namespace_) |uri| {
const duped = try frame.dupeString(uri);
try frame._element_namespace_uris.put(frame.arena, root.as(Node.Element), duped);
}
}
_ = try document.asNode().appendChild(root, frame);
}
return document;

View File

@@ -54,6 +54,8 @@ _type: Type,
_proto: *Node,
_frame: ?*Frame = null,
_url: ?[:0]const u8 = null, // URL for documents created via DOMImplementation (about:blank)
// content type override for documents created via DOMImplementation.createDocument
_content_type: ?[]const u8 = null,
_ready_state: ReadyState = .loading,
_current_script: ?*Element.Html.Script = null,
_elements_by_id: std.StringHashMapUnmanaged(*Element) = .empty,
@@ -158,6 +160,9 @@ pub fn setLocation(self: *Document, url: [:0]const u8) !void {
}
pub fn getContentType(self: *const Document) []const u8 {
if (self._content_type) |content_type| {
return content_type;
}
return switch (self._type) {
.html => "text/html",
.xml => "application/xml",
@@ -300,7 +305,7 @@ pub fn createElement(self: *Document, name: []const u8, options_: ?CreateElement
}
pub fn createElementNS(self: *Document, namespace: ?[]const u8, name: []const u8, frame: *Frame) !*Element {
try validateElementName(name);
_ = try validateAndExtract(namespace, name, .element);
const ns = Element.Namespace.parse(namespace);
// Per spec, createElementNS does NOT lowercase (unlike createElement).
const node = try Frame.node_factory.createElementNS(frame, ns, name, null);
@@ -1239,26 +1244,118 @@ fn validateDocumentNodes(self: *Document, nodes: []const Node.NodeOrText, compti
}
}
fn validateElementName(name: []const u8) !void {
// DOM §1.4 "Name validation" productions.
pub fn isValidElementLocalName(name: []const u8) bool {
if (name.len == 0) {
return error.InvalidCharacterError;
return false;
}
const first = name[0];
// Element names cannot start with: digits, period, hyphen
if ((first >= '0' and first <= '9') or first == '.' or first == '-') {
return error.InvalidCharacterError;
if (std.ascii.isAlphabetic(name[0])) {
// Names the HTML parser can construct: anything except ASCII
// whitespace, NUL, '/' or '>'.
for (name[1..]) |c| {
switch (c) {
'\t', '\n', 0x0C, '\r', ' ', 0, '/', '>' => return false,
else => {},
}
}
return true;
}
// Otherwise the first code point must be ':', '_' or beyond ASCII, and
// the rest restricted to alphanumerics, '-', '.', ':', '_' or non-ASCII.
if (name[0] != ':' and name[0] != '_' and name[0] < 0x80) {
return false;
}
for (name[1..]) |c| {
const is_valid = std.ascii.isAlphanumeric(c) or
c == '_' or c == '-' or c == '.' or c == ':' or
c >= 128; // Allow non-ASCII UTF-8
const valid = std.ascii.isAlphanumeric(c) or
c == '-' or c == '.' or c == ':' or c == '_' or c >= 0x80;
if (!valid) {
return false;
}
}
return true;
}
if (!is_valid) {
pub fn isValidNamespacePrefix(prefix: []const u8) bool {
if (prefix.len == 0) {
return false;
}
for (prefix) |c| {
switch (c) {
'\t', '\n', 0x0C, '\r', ' ', 0, '/', '>' => return false,
else => {},
}
}
return true;
}
pub fn isValidAttributeLocalName(name: []const u8) bool {
if (name.len == 0) {
return false;
}
for (name) |c| {
switch (c) {
'\t', '\n', 0x0C, '\r', ' ', 0, '/', '=', '>' => return false,
else => {},
}
}
return true;
}
fn validateElementName(name: []const u8) !void {
if (!isValidElementLocalName(name)) {
return error.InvalidCharacterError;
}
}
pub const ValidatedName = struct {
prefix: ?[]const u8,
local_name: []const u8,
namespace: ?[]const u8,
};
// The DOM spec's "validate and extract a namespace and qualifiedName".
pub fn validateAndExtract(namespace_: ?[]const u8, qualified_name: []const u8, comptime context: enum { element, attribute }) !ValidatedName {
var namespace: ?[]const u8 = namespace_;
if (namespace) |ns| {
if (ns.len == 0) {
namespace = null;
}
}
var prefix: ?[]const u8 = null;
var local_name = qualified_name;
if (std.mem.indexOfScalar(u8, qualified_name, ':')) |colon| {
prefix = qualified_name[0..colon];
local_name = qualified_name[colon + 1 ..];
if (!isValidNamespacePrefix(prefix.?)) {
return error.InvalidCharacterError;
}
}
const local_valid = switch (context) {
.element => isValidElementLocalName(local_name),
.attribute => isValidAttributeLocalName(local_name),
};
if (!local_valid) {
return error.InvalidCharacterError;
}
if (prefix != null and namespace == null) {
return error.NamespaceError;
}
if (prefix) |p| {
if (std.mem.eql(u8, p, "xml") and (namespace == null or !std.mem.eql(u8, namespace.?, "http://www.w3.org/XML/1998/namespace"))) {
return error.NamespaceError;
}
}
const is_xmlns = std.mem.eql(u8, qualified_name, "xmlns") or (prefix != null and std.mem.eql(u8, prefix.?, "xmlns"));
const ns_is_xmlns = namespace != null and std.mem.eql(u8, namespace.?, "http://www.w3.org/2000/xmlns/");
if (is_xmlns != ns_is_xmlns) {
return error.NamespaceError;
}
return .{ .prefix = prefix, .local_name = local_name, .namespace = namespace };
}
// When a frame's URL is about:blank, or as soon as a frame is

View File

@@ -1505,6 +1505,14 @@ pub fn clone(self: *Element, deep: bool, frame: *Frame) !*Node {
const tag_name = self.getTagNameDump();
const node = try Frame.node_factory.createElementNS(frame, self._namespace, tag_name, &self._attributes);
// A namespace outside the built-in set lives in a side table; the clone
// must report the same namespaceURI.
if (self._namespace == .unknown) {
if (frame._element_namespace_uris.get(self)) |uri| {
try frame._element_namespace_uris.put(frame.arena, node.as(Element), uri);
}
}
// Allow element-specific types to copy their runtime state
_ = Element.Build.call(node.as(Element), "cloned", .{ self, node.as(Element), deep, frame }) catch |err| {
log.err(.dom, "element.clone.failed", .{ .err = err });
@@ -1916,7 +1924,9 @@ pub const JsApi = struct {
fn _tagName(self: *Element, frame: *Frame) []const u8 {
return self.getTagNameSpec(&frame.buf);
}
pub const namespaceURI = bridge.accessor(Element.getNamespaceURI, null, .{});
// the frame-aware variant returns the original URI for namespaces
// outside the built-in set instead of the placeholder
pub const namespaceURI = bridge.accessor(Element.getNamespaceUri, null, .{});
pub const innerText = bridge.accessor(_innerText, Element.setInnerText, .{ .ce_reactions = true });
fn _innerText(self: *Element, frame: *Frame) ![]const u8 {

View File

@@ -288,17 +288,17 @@ pub const JsApi = struct {
pub const download = bridge.accessor(Area.getDownload, Area.setDownload, .{ .ce_reactions = true });
pub const rel = bridge.accessor(Area.getRel, Area.setRel, .{ .ce_reactions = true });
pub const referrerPolicy = bridge.accessor(Area.getReferrerPolicy, Area.setReferrerPolicy, .{ .ce_reactions = true });
pub const relList = bridge.accessor(_getRelList, null, .{ .null_as_undefined = true });
pub const toString = bridge.function(Area.getHref, .{});
pub const relList = bridge.accessor(_getRelList, null, .{ .null_as_undefined = true });
fn _getRelList(self: *Area, frame: *Frame) !?*@import("../../collections.zig").DOMTokenList {
const element = self.asElement();
// relList is only valid for HTML <area> elements
if (element._namespace != .html) {
return null;
pub const relList = bridge.accessor(struct{
fn wrap(self: *Area, frame: *Frame) !?*@import("../../collections.zig").DOMTokenList {
const element = self.asElement();
// relList is only valid for HTML <area> elements
if (element._namespace != .html) {
return null;
}
return element.getRelList(frame);
}
return element.getRelList(frame);
}
}.wrap, null, .{ .null_as_undefined = true });
};
const testing = @import("../../../../testing.zig");