Skip to content
Merged
Show file tree
Hide file tree
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
12 changes: 9 additions & 3 deletions src/browser/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,19 @@ pub const Document = struct {
tag_name: []const u8,
page: *Page,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(page.arena, parser.documentToNode(self), tag_name, true);
return try collection.HTMLCollectionByTagName(page.arena, parser.documentToNode(self), tag_name, .{
.include_root = true,
});
}

pub fn _getElementsByClassName(
self: *parser.Document,
classNames: []const u8,
page: *Page,
) !collection.HTMLCollection {
return try collection.HTMLCollectionByClassName(page.arena, parser.documentToNode(self), classNames, true);
return try collection.HTMLCollectionByClassName(page.arena, parser.documentToNode(self), classNames, .{
.include_root = true,
});
}

pub fn _createDocumentFragment(self: *parser.Document) !*parser.DocumentFragment {
Expand Down Expand Up @@ -201,7 +205,9 @@ pub const Document = struct {
// ParentNode
// https://dom.spec.whatwg.org/#parentnode
pub fn get_children(self: *parser.Document) !collection.HTMLCollection {
return try collection.HTMLCollectionChildren(parser.documentToNode(self), false);
return collection.HTMLCollectionChildren(parser.documentToNode(self), .{
.include_root = false,
});
}

pub fn get_firstElementChild(self: *parser.Document) !?ElementUnion {
Expand Down
4 changes: 3 additions & 1 deletion src/browser/dom/document_fragment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ pub const DocumentFragment = struct {
}

pub fn get_children(self: *parser.DocumentFragment) !collection.HTMLCollection {
return collection.HTMLCollectionChildren(parser.documentFragmentToNode(self), false);
return collection.HTMLCollectionChildren(parser.documentFragmentToNode(self), .{
.include_root = false,
});
}
};

Expand Down
8 changes: 5 additions & 3 deletions src/browser/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub const Element = struct {
page.arena,
parser.elementToNode(self),
tag_name,
false,
.{ .include_root = false },
);
}

Expand All @@ -307,14 +307,16 @@ pub const Element = struct {
page.arena,
parser.elementToNode(self),
classNames,
false,
.{ .include_root = false },
);
}

// ParentNode
// https://dom.spec.whatwg.org/#parentnode
pub fn get_children(self: *parser.Element) !collection.HTMLCollection {
return try collection.HTMLCollectionChildren(parser.elementToNode(self), false);
return collection.HTMLCollectionChildren(parser.elementToNode(self), .{
.include_root = false,
});
}

pub fn get_firstElementChild(self: *parser.Element) !?Union {
Expand Down
41 changes: 27 additions & 14 deletions src/browser/dom/html_collection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,14 @@ pub fn HTMLCollectionByTagName(
arena: Allocator,
root: ?*parser.Node,
tag_name: []const u8,
include_root: bool,
opts: Opts,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = .{ .walkerDepthFirst = .{} },
.matcher = .{ .matchByTagName = try MatchByTagName.init(arena, tag_name) },
.include_root = include_root,
.mutable = opts.mutable,
.include_root = opts.include_root,
};
}

Expand Down Expand Up @@ -109,13 +110,14 @@ pub fn HTMLCollectionByClassName(
arena: Allocator,
root: ?*parser.Node,
classNames: []const u8,
include_root: bool,
opts: Opts,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = .{ .walkerDepthFirst = .{} },
.matcher = .{ .matchByClassName = try MatchByClassName.init(arena, classNames) },
.include_root = include_root,
.mutable = opts.mutable,
.include_root = opts.include_root,
};
}

Expand All @@ -139,13 +141,14 @@ pub fn HTMLCollectionByName(
arena: Allocator,
root: ?*parser.Node,
name: []const u8,
include_root: bool,
opts: Opts,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = .{ .walkerDepthFirst = .{} },
.matcher = .{ .matchByName = try MatchByName.init(arena, name) },
.include_root = include_root,
.mutable = opts.mutable,
.include_root = opts.include_root,
};
}

Expand Down Expand Up @@ -189,13 +192,14 @@ pub const HTMLAllCollection = struct {

pub fn HTMLCollectionChildren(
root: ?*parser.Node,
include_root: bool,
) !HTMLCollection {
opts: Opts,
) HTMLCollection {
return HTMLCollection{
.root = root,
.walker = .{ .walkerChildren = .{} },
.matcher = .{ .matchTrue = .{} },
.include_root = include_root,
.mutable = opts.mutable,
.include_root = opts.include_root,
};
}

Expand Down Expand Up @@ -224,13 +228,14 @@ pub const MatchByLinks = struct {

pub fn HTMLCollectionByLinks(
root: ?*parser.Node,
include_root: bool,
opts: Opts,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = .{ .walkerDepthFirst = .{} },
.matcher = .{ .matchByLinks = MatchByLinks{} },
.include_root = include_root,
.mutable = opts.mutable,
.include_root = opts.include_root,
};
}

Expand All @@ -249,13 +254,14 @@ pub const MatchByAnchors = struct {

pub fn HTMLCollectionByAnchors(
root: ?*parser.Node,
include_root: bool,
opts: Opts,
) !HTMLCollection {
return HTMLCollection{
.root = root,
.walker = .{ .walkerDepthFirst = .{} },
.matcher = .{ .matchByAnchors = MatchByAnchors{} },
.include_root = include_root,
.mutable = opts.mutable,
.include_root = opts.include_root,
};
}

Expand Down Expand Up @@ -285,6 +291,11 @@ pub const HTMLCollectionIterator = struct {
}
};

const Opts = struct {
include_root: bool,
mutable: bool = false,
};

// WEB IDL https://dom.spec.whatwg.org/#htmlcollection
// HTMLCollection is re implemented in zig here because libdom
// dom_html_collection expects a comparison function callback as arguement.
Expand All @@ -300,6 +311,8 @@ pub const HTMLCollection = struct {
// itself.
include_root: bool = false,

mutable: bool = false,

// save a state for the collection to improve the _item speed.
cur_idx: ?u32 = null,
cur_node: ?*parser.Node = null,
Expand Down Expand Up @@ -350,7 +363,7 @@ pub const HTMLCollection = struct {
var node: *parser.Node = undefined;

// Use the current state to improve speed if possible.
if (self.cur_idx != null and index >= self.cur_idx.?) {
if (self.mutable == false and self.cur_idx != null and index >= self.cur_idx.?) {
i = self.cur_idx.?;
node = self.cur_node.?;
} else {
Expand Down
28 changes: 21 additions & 7 deletions src/browser/html/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ pub const HTMLDocument = struct {
if (name.len == 0) return list;

const root = parser.documentHTMLToNode(self);
var c = try collection.HTMLCollectionByName(arena, root, name, false);
var c = try collection.HTMLCollectionByName(arena, root, name, .{
.include_root = false,
});

const ln = try c.get_length();
var i: u32 = 0;
Expand All @@ -132,35 +134,47 @@ pub const HTMLDocument = struct {
}

pub fn get_images(self: *parser.DocumentHTML, page: *Page) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "img", false);
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "img", .{
.include_root = false,
});
}

pub fn get_embeds(self: *parser.DocumentHTML, page: *Page) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "embed", false);
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "embed", .{
.include_root = false,
});
}

pub fn get_plugins(self: *parser.DocumentHTML, page: *Page) !collection.HTMLCollection {
return get_embeds(self, page);
}

pub fn get_forms(self: *parser.DocumentHTML, page: *Page) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "form", false);
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "form", .{
.include_root = false,
});
}

pub fn get_scripts(self: *parser.DocumentHTML, page: *Page) !collection.HTMLCollection {
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "script", false);
return try collection.HTMLCollectionByTagName(page.arena, parser.documentHTMLToNode(self), "script", .{
.include_root = false,
});
}

pub fn get_applets(_: *parser.DocumentHTML) !collection.HTMLCollection {
return try collection.HTMLCollectionEmpty();
}

pub fn get_links(self: *parser.DocumentHTML) !collection.HTMLCollection {
return try collection.HTMLCollectionByLinks(parser.documentHTMLToNode(self), false);
return try collection.HTMLCollectionByLinks(parser.documentHTMLToNode(self), .{
.include_root = false,
});
}

pub fn get_anchors(self: *parser.DocumentHTML) !collection.HTMLCollection {
return try collection.HTMLCollectionByAnchors(parser.documentHTMLToNode(self), false);
return try collection.HTMLCollectionByAnchors(parser.documentHTMLToNode(self), .{
.include_root = false,
});
}

pub fn get_all(self: *parser.DocumentHTML) collection.HTMLAllCollection {
Expand Down
9 changes: 1 addition & 8 deletions src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ pub const Interfaces = .{
HTMLOListElement,
HTMLObjectElement,
HTMLOptGroupElement,
HTMLOptionElement,
HTMLOutputElement,
HTMLParagraphElement,
HTMLParamElement,
Expand All @@ -102,7 +101,7 @@ pub const Interfaces = .{
HTMLVideoElement,

@import("form.zig").HTMLFormElement,
@import("select.zig").HTMLSelectElement,
@import("select.zig").Interfaces,
};

pub const Union = generate.Union(Interfaces);
Expand Down Expand Up @@ -813,12 +812,6 @@ pub const HTMLOptGroupElement = struct {
pub const subtype = .node;
};

pub const HTMLOptionElement = struct {
pub const Self = parser.Option;
pub const prototype = *HTMLElement;
pub const subtype = .node;
};

pub const HTMLOutputElement = struct {
pub const Self = parser.Output;
pub const prototype = *HTMLElement;
Expand Down
Loading