dom: compare tagname in case insensitive way

This commit is contained in:
Pierre Tachoire
2023-10-25 15:54:56 +02:00
parent 062a1a4010
commit 3a188c4568
2 changed files with 11 additions and 5 deletions

View File

@@ -48,7 +48,6 @@ pub const Document = struct {
const root = parser.documentGetDocumentNode(self); const root = parser.documentGetDocumentNode(self);
return HTMLCollection{ return HTMLCollection{
.root = root, .root = root,
// TODO handle case insensitive comparison.
.match = tag_name, .match = tag_name,
}; };
} }
@@ -77,7 +76,7 @@ pub fn testExecFn(
try checkCases(js_env, &getElementById); try checkCases(js_env, &getElementById);
var getElementsByTagName = [_]Case{ var getElementsByTagName = [_]Case{
.{ .src = "let getElementsByTagName = document.getElementsByTagName('P')", .ex = "undefined" }, .{ .src = "let getElementsByTagName = document.getElementsByTagName('p')", .ex = "undefined" },
.{ .src = "getElementsByTagName.length", .ex = "2" }, .{ .src = "getElementsByTagName.length", .ex = "2" },
.{ .src = "getElementsByTagName.item(0).localName", .ex = "p" }, .{ .src = "getElementsByTagName.item(0).localName", .ex = "p" },
.{ .src = "getElementsByTagName.item(1).localName", .ex = "p" }, .{ .src = "getElementsByTagName.item(1).localName", .ex = "p" },

View File

@@ -4,6 +4,7 @@ const parser = @import("../netsurf.zig");
const jsruntime = @import("jsruntime"); const jsruntime = @import("jsruntime");
const utils = @import("utils.z");
const Element = @import("element.zig").Element; const Element = @import("element.zig").Element;
// WEB IDL https://dom.spec.whatwg.org/#htmlcollection // WEB IDL https://dom.spec.whatwg.org/#htmlcollection
@@ -15,7 +16,7 @@ pub const HTMLCollection = struct {
root: *parser.Node, root: *parser.Node,
// match is used to select node against their name. // match is used to select node against their name.
// match comparison is case sensitive. // match comparison is case insensitive.
match: []const u8, match: []const u8,
/// _get_length computes the collection's length dynamically according to /// _get_length computes the collection's length dynamically according to
@@ -26,12 +27,15 @@ pub const HTMLCollection = struct {
var node: ?*parser.Node = self.root; var node: ?*parser.Node = self.root;
var ntype: parser.NodeType = undefined; var ntype: parser.NodeType = undefined;
var buffer: [128]u8 = undefined;
const imatch = std.ascii.upperString(&buffer, self.match);
var is_wildcard = std.mem.eql(u8, self.match, "*"); var is_wildcard = std.mem.eql(u8, self.match, "*");
while (node != null) { while (node != null) {
ntype = parser.nodeType(node.?); ntype = parser.nodeType(node.?);
if (ntype == .element) { if (ntype == .element) {
if (is_wildcard or std.mem.eql(u8, self.match, parser.nodeName(node.?))) { if (is_wildcard or std.mem.eql(u8, imatch, parser.nodeName(node.?))) {
len += 1; len += 1;
} }
} }
@@ -75,10 +79,13 @@ pub const HTMLCollection = struct {
var is_wildcard = std.mem.eql(u8, self.match, "*"); var is_wildcard = std.mem.eql(u8, self.match, "*");
var buffer: [128]u8 = undefined;
const imatch = std.ascii.upperString(&buffer, self.match);
while (node != null) { while (node != null) {
ntype = parser.nodeType(node.?); ntype = parser.nodeType(node.?);
if (ntype == .element) { if (ntype == .element) {
if (is_wildcard or std.mem.eql(u8, self.match, parser.nodeName(node.?))) { if (is_wildcard or std.mem.eql(u8, imatch, parser.nodeName(node.?))) {
len += 1; len += 1;
// check if we found the searched element. // check if we found the searched element.