diff --git a/src/dom/html_collection.zig b/src/dom/html_collection.zig
index 1f04ae84..41e88299 100644
--- a/src/dom/html_collection.zig
+++ b/src/dom/html_collection.zig
@@ -16,6 +16,7 @@ const Matcher = union(enum) {
matchByTagName: MatchByTagName,
matchByClassName: MatchByClassName,
matchByLinks: MatchByLinks,
+ matchByAnchors: MatchByAnchors,
matchTrue: struct {},
matchFalse: struct {},
@@ -27,6 +28,7 @@ const Matcher = union(enum) {
inline .matchByClassName => |case| return case.match(node),
inline .matchByName => |case| return case.match(node),
inline .matchByLinks => return MatchByLinks.match(node),
+ inline .matchByAnchors => return MatchByAnchors.match(node),
}
}
@@ -38,6 +40,7 @@ const Matcher = union(enum) {
inline .matchByClassName => |case| return case.deinit(alloc),
inline .matchByName => |case| return case.deinit(alloc),
inline .matchByLinks => return,
+ inline .matchByAnchors => return,
}
}
};
@@ -213,6 +216,33 @@ pub fn HTMLCollectionByLinks(
};
}
+// MatchByAnchors matches the a elements in the Document that have name
+// attributes.
+// https://html.spec.whatwg.org/#dom-document-anchors
+pub const MatchByAnchors = struct {
+ pub fn match(node: *parser.Node) !bool {
+ const tag = try parser.nodeName(node);
+ if (!std.ascii.eqlIgnoreCase(tag, "a")) return false;
+
+ const elem = @as(*parser.Element, @ptrCast(node));
+ return parser.elementHasAttribute(elem, "name");
+ }
+};
+
+pub fn HTMLCollectionByAnchors(
+ root: ?*parser.Node,
+ include_root: bool,
+) !HTMLCollection {
+ return HTMLCollection{
+ .root = root,
+ .walker = Walker{ .walkerDepthFirst = .{} },
+ .matcher = Matcher{
+ .matchByAnchors = MatchByAnchors{},
+ },
+ .include_root = include_root,
+ };
+}
+
const Walker = union(enum) {
walkerDepthFirst: WalkerDepthFirst,
walkerChildren: WalkerChildren,
diff --git a/src/html/document.zig b/src/html/document.zig
index bc182af4..4fb05692 100644
--- a/src/html/document.zig
+++ b/src/html/document.zig
@@ -134,6 +134,10 @@ pub const HTMLDocument = struct {
return try collection.HTMLCollectionByLinks(try rootNode(self), false);
}
+ pub fn get_anchors(self: *parser.DocumentHTML) !collection.HTMLCollection {
+ return try collection.HTMLCollectionByAnchors(try rootNode(self), false);
+ }
+
pub fn get_currentScript(_: *parser.DocumentHTML) !?*parser.Element {
return null;
}
@@ -177,6 +181,7 @@ pub fn testExecFn(
.{ .src = "document.forms.length", .ex = "0" },
.{ .src = "document.links.length", .ex = "1" },
.{ .src = "document.applets.length", .ex = "0" },
+ .{ .src = "document.anchors.length", .ex = "0" },
.{ .src = "document.currentScript", .ex = "null" },
};
try checkCases(js_env, &getters);