HTMLDocument: implements anchors

This commit is contained in:
Pierre Tachoire
2024-01-04 15:16:48 +01:00
parent 552bf4224c
commit 91d1f539d4
2 changed files with 35 additions and 0 deletions

View File

@@ -16,6 +16,7 @@ const Matcher = union(enum) {
matchByTagName: MatchByTagName, matchByTagName: MatchByTagName,
matchByClassName: MatchByClassName, matchByClassName: MatchByClassName,
matchByLinks: MatchByLinks, matchByLinks: MatchByLinks,
matchByAnchors: MatchByAnchors,
matchTrue: struct {}, matchTrue: struct {},
matchFalse: struct {}, matchFalse: struct {},
@@ -27,6 +28,7 @@ const Matcher = union(enum) {
inline .matchByClassName => |case| return case.match(node), inline .matchByClassName => |case| return case.match(node),
inline .matchByName => |case| return case.match(node), inline .matchByName => |case| return case.match(node),
inline .matchByLinks => return MatchByLinks.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 .matchByClassName => |case| return case.deinit(alloc),
inline .matchByName => |case| return case.deinit(alloc), inline .matchByName => |case| return case.deinit(alloc),
inline .matchByLinks => return, 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) { const Walker = union(enum) {
walkerDepthFirst: WalkerDepthFirst, walkerDepthFirst: WalkerDepthFirst,
walkerChildren: WalkerChildren, walkerChildren: WalkerChildren,

View File

@@ -134,6 +134,10 @@ pub const HTMLDocument = struct {
return try collection.HTMLCollectionByLinks(try rootNode(self), false); 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 { pub fn get_currentScript(_: *parser.DocumentHTML) !?*parser.Element {
return null; return null;
} }
@@ -177,6 +181,7 @@ pub fn testExecFn(
.{ .src = "document.forms.length", .ex = "0" }, .{ .src = "document.forms.length", .ex = "0" },
.{ .src = "document.links.length", .ex = "1" }, .{ .src = "document.links.length", .ex = "1" },
.{ .src = "document.applets.length", .ex = "0" }, .{ .src = "document.applets.length", .ex = "0" },
.{ .src = "document.anchors.length", .ex = "0" },
.{ .src = "document.currentScript", .ex = "null" }, .{ .src = "document.currentScript", .ex = "null" },
}; };
try checkCases(js_env, &getters); try checkCases(js_env, &getters);