mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-12-16 16:28:58 +00:00
dom: add NamedNodeMap implementation
and create Attr type
This commit is contained in:
15
src/dom/attribute.zig
Normal file
15
src/dom/attribute.zig
Normal file
@@ -0,0 +1,15 @@
|
||||
const std = @import("std");
|
||||
|
||||
const parser = @import("../netsurf.zig");
|
||||
|
||||
const Node = @import("node.zig").Node;
|
||||
const DOMException = @import("exceptions.zig").DOMException;
|
||||
|
||||
// WEB IDL https://dom.spec.whatwg.org/#attr
|
||||
pub const Attr = struct {
|
||||
pub const Self = parser.Attribute;
|
||||
pub const prototype = *Node;
|
||||
pub const mem_guarantied = true;
|
||||
|
||||
pub const Exception = DOMException;
|
||||
};
|
||||
@@ -3,12 +3,14 @@ const generate = @import("../generate.zig");
|
||||
const DOMException = @import("exceptions.zig").DOMException;
|
||||
const EventTarget = @import("event_target.zig").EventTarget;
|
||||
const DOMImplementation = @import("implementation.zig").DOMImplementation;
|
||||
const NamedNodeMap = @import("namednodemap.zig").NamedNodeMap;
|
||||
const Nod = @import("node.zig");
|
||||
|
||||
pub const Interfaces = generate.Tuple(.{
|
||||
DOMException,
|
||||
EventTarget,
|
||||
DOMImplementation,
|
||||
NamedNodeMap,
|
||||
Nod.Node,
|
||||
Nod.Interfaces,
|
||||
});
|
||||
|
||||
@@ -10,12 +10,16 @@ const Node = @import("node.zig").Node;
|
||||
const HTMLElem = @import("../html/elements.zig");
|
||||
pub const Union = @import("../html/elements.zig").Union;
|
||||
|
||||
const DOMException = @import("exceptions.zig").DOMException;
|
||||
|
||||
// WEB IDL https://dom.spec.whatwg.org/#element
|
||||
pub const Element = struct {
|
||||
pub const Self = parser.Element;
|
||||
pub const prototype = *Node;
|
||||
pub const mem_guarantied = true;
|
||||
|
||||
pub const Exception = DOMException;
|
||||
|
||||
pub fn toInterface(e: *parser.Element) !Union {
|
||||
return try HTMLElem.toInterface(Union, e);
|
||||
}
|
||||
@@ -27,6 +31,10 @@ pub const Element = struct {
|
||||
return try parser.elementLocalName(self);
|
||||
}
|
||||
|
||||
pub fn get_attributes(self: *parser.Element) !*parser.NamedNodeMap {
|
||||
return try parser.nodeGetAttributes(parser.elementToNode(self));
|
||||
}
|
||||
|
||||
pub fn _hasAttributes(self: *parser.Element) !bool {
|
||||
return try parser.nodeHasAttributes(parser.elementToNode(self));
|
||||
}
|
||||
@@ -89,6 +97,8 @@ pub fn testExecFn(
|
||||
var attribute = [_]Case{
|
||||
.{ .src = "let a = document.getElementById('content')", .ex = "undefined" },
|
||||
.{ .src = "a.hasAttributes()", .ex = "true" },
|
||||
.{ .src = "a.attributes.length", .ex = "1" },
|
||||
|
||||
.{ .src = "a.getAttribute('id')", .ex = "content" },
|
||||
|
||||
.{ .src = "a.hasAttribute('foo')", .ex = "false" },
|
||||
|
||||
80
src/dom/namednodemap.zig
Normal file
80
src/dom/namednodemap.zig
Normal file
@@ -0,0 +1,80 @@
|
||||
const std = @import("std");
|
||||
|
||||
const parser = @import("../netsurf.zig");
|
||||
|
||||
const jsruntime = @import("jsruntime");
|
||||
const Case = jsruntime.test_utils.Case;
|
||||
const checkCases = jsruntime.test_utils.checkCases;
|
||||
|
||||
const DOMException = @import("exceptions.zig").DOMException;
|
||||
|
||||
// WEB IDL https://dom.spec.whatwg.org/#namednodemap
|
||||
pub const NamedNodeMap = struct {
|
||||
pub const Self = parser.NamedNodeMap;
|
||||
pub const mem_guarantied = true;
|
||||
|
||||
pub const Exception = DOMException;
|
||||
|
||||
// TODO implement LegacyUnenumerableNamedProperties.
|
||||
// https://webidl.spec.whatwg.org/#LegacyUnenumerableNamedProperties
|
||||
|
||||
pub fn get_length(self: *parser.NamedNodeMap) !u32 {
|
||||
return try parser.namedNodeMapGetLength(self);
|
||||
}
|
||||
|
||||
pub fn _item(self: *parser.NamedNodeMap, index: u32) !?*parser.Attribute {
|
||||
return try parser.namedNodeMapItem(self, index);
|
||||
}
|
||||
|
||||
pub fn _getNamedItem(self: *parser.NamedNodeMap, qname: []const u8) !?*parser.Attribute {
|
||||
return try parser.namedNodeMapGetNamedItem(self, qname);
|
||||
}
|
||||
|
||||
pub fn _getNamedItemNS(
|
||||
self: *parser.NamedNodeMap,
|
||||
namespace: []const u8,
|
||||
localname: []const u8,
|
||||
) !?*parser.Attribute {
|
||||
return try parser.namedNodeMapGetNamedItemNS(self, namespace, localname);
|
||||
}
|
||||
|
||||
pub fn _setNamedItem(self: *parser.NamedNodeMap, attr: *parser.Attribute) !?*parser.Attribute {
|
||||
return try parser.namedNodeMapSetNamedItem(self, attr);
|
||||
}
|
||||
|
||||
pub fn _setNamedItemNS(self: *parser.NamedNodeMap, attr: *parser.Attribute) !?*parser.Attribute {
|
||||
return try parser.namedNodeMapSetNamedItemNS(self, attr);
|
||||
}
|
||||
|
||||
pub fn _removeNamedItem(self: *parser.NamedNodeMap, qname: []const u8) !*parser.Attribute {
|
||||
return try parser.namedNodeMapRemoveNamedItem(self, qname);
|
||||
}
|
||||
|
||||
pub fn _removeNamedItemNS(
|
||||
self: *parser.NamedNodeMap,
|
||||
namespace: []const u8,
|
||||
localname: []const u8,
|
||||
) !*parser.Attribute {
|
||||
return try parser.namedNodeMapRemoveNamedItemNS(self, namespace, localname);
|
||||
}
|
||||
};
|
||||
|
||||
// Tests
|
||||
// -----
|
||||
|
||||
pub fn testExecFn(
|
||||
_: std.mem.Allocator,
|
||||
js_env: *jsruntime.Env,
|
||||
comptime _: []jsruntime.API,
|
||||
) !void {
|
||||
var setItem = [_]Case{
|
||||
.{ .src = "let a = document.getElementById('content').attributes", .ex = "undefined" },
|
||||
.{ .src = "a.length", .ex = "1" },
|
||||
.{ .src = "a.item(0)", .ex = "[object Attr]" },
|
||||
.{ .src = "a.item(1)", .ex = "null" },
|
||||
.{ .src = "a.getNamedItem('id')", .ex = "[object Attr]" },
|
||||
.{ .src = "a.getNamedItem('foo')", .ex = "null" },
|
||||
.{ .src = "a.setNamedItem(a.getNamedItem('id'))", .ex = "[object Attr]" },
|
||||
};
|
||||
try checkCases(js_env, &setItem);
|
||||
}
|
||||
@@ -11,6 +11,7 @@ const parser = @import("../netsurf.zig");
|
||||
const EventTarget = @import("event_target.zig").EventTarget;
|
||||
|
||||
// DOM
|
||||
const Attr = @import("attribute.zig").Attr;
|
||||
const CData = @import("character_data.zig");
|
||||
const Element = @import("element.zig").Element;
|
||||
const Document = @import("document.zig").Document;
|
||||
@@ -23,6 +24,7 @@ const HTMLElem = @import("../html/elements.zig");
|
||||
|
||||
// Node interfaces
|
||||
pub const Interfaces = generate.Tuple(.{
|
||||
Attr,
|
||||
CData.CharacterData,
|
||||
CData.Interfaces,
|
||||
Element,
|
||||
@@ -52,6 +54,7 @@ pub const Node = struct {
|
||||
.text => .{ .Text = @as(*parser.Text, @ptrCast(node)) },
|
||||
.document => .{ .HTMLDocument = @as(*parser.DocumentHTML, @ptrCast(node)) },
|
||||
.document_type => .{ .DocumentType = @as(*parser.DocumentType, @ptrCast(node)) },
|
||||
.attribute => .{ .Attr = @as(*parser.Attribute, @ptrCast(node)) },
|
||||
else => @panic("node type not handled"), // TODO
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user