Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dom: declare document.createElementsbyNS and getElementsByTagNameNS #70

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/dom/document.zig
Original file line number Diff line number Diff line change
@@ -36,6 +36,11 @@ pub const Document = struct {
return Element.toInterface(e);
}

pub fn _createElementNS(self: *parser.Document, ns: []const u8, tag_name: []const u8) ElementUnion {
const e = parser.documentCreateElementNS(self, ns, tag_name);
return Element.toInterface(e);
}

// We can't simply use libdom dom_document_get_elements_by_tag_name here.
// Indeed, netsurf implemented a previous dom spec when
// getElementsByTagName returned a NodeList.
@@ -51,6 +56,16 @@ pub const Document = struct {
.match = tag_name,
};
}

// TODO implement the filter by namespace
pub fn _getElementsByTagNameNS(self: *parser.Document, namespace: []const u8, localname: []const u8) HTMLCollection {
_ = namespace;
const root = parser.documentGetDocumentNode(self);
return HTMLCollection{
.root = root,
.match = localname,
};
}
};

// Tests
@@ -88,6 +103,14 @@ pub fn testExecFn(
};
try checkCases(js_env, &getElementsByTagName);

var getElementsByTagNameNS = [_]Case{
.{ .src = "let a = document.getElementsByTagNameNS('', 'p')", .ex = "undefined" },
.{ .src = "a.length", .ex = "2" },
.{ .src = "let b = document.getElementsByTagNameNS(null, 'p')", .ex = "undefined" },
.{ .src = "b.length", .ex = "2" },
};
try checkCases(js_env, &getElementsByTagNameNS);

const tags = comptime parser.Tag.all();
comptime var createElements: [(tags.len) * 2]Case = undefined;
inline for (tags, 0..) |tag, i| {
6 changes: 6 additions & 0 deletions src/netsurf.zig
Original file line number Diff line number Diff line change
@@ -752,6 +752,12 @@ pub inline fn documentCreateElement(doc: *Document, tag_name: []const u8) *Eleme
return elem.?;
}

pub inline fn documentCreateElementNS(doc: *Document, ns: []const u8, tag_name: []const u8) *Element {
var elem: ?*Element = undefined;
_ = documentVtable(doc).dom_document_create_element_ns.?(doc, stringFromData(ns), stringFromData(tag_name), &elem);
return elem.?;
}

// DocumentHTML
pub const DocumentHTML = c.dom_html_document;