Skip to content

Commit bff3d27

Browse files
committed
add DOMParser
1 parent afd29fe commit bff3d27

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

src/browser/dom/dom.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const NodeList = @import("nodelist.zig");
2525
const Node = @import("node.zig");
2626
const MutationObserver = @import("mutation_observer.zig");
2727
const IntersectionObserver = @import("intersection_observer.zig");
28+
const DOMParser = @import("dom_parser.zig").DOMParser;
2829

2930
pub const Interfaces = .{
3031
DOMException,
@@ -37,4 +38,5 @@ pub const Interfaces = .{
3738
Node.Interfaces,
3839
MutationObserver.Interfaces,
3940
IntersectionObserver.Interfaces,
41+
DOMParser,
4042
};

src/browser/dom/dom_parser.zig

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (C) 2023-2024 Lightpanda (Selecy SAS)
2+
//
3+
// Francis Bouvier <[email protected]>
4+
// Pierre Tachoire <[email protected]>
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU Affero General Public License as
8+
// published by the Free Software Foundation, either version 3 of the
9+
// License, or (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU Affero General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU Affero General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
const std = @import("std");
20+
const parser = @import("../netsurf.zig");
21+
22+
// https://developer.mozilla.org/en-US/docs/Web/API/DOMParser
23+
pub const DOMParser = struct {
24+
pub fn constructor() !DOMParser {
25+
return .{};
26+
}
27+
28+
pub fn _parseFromString(_: *DOMParser, string: []const u8, mime_type: []const u8) !*parser.DocumentHTML {
29+
if (!std.mem.eql(u8, mime_type, "text/html")) {
30+
// TODO: Support XML
31+
return error.TypeError;
32+
}
33+
34+
return try parser.documentHTMLParseFromStr(string);
35+
}
36+
};
37+
38+
const testing = @import("../../testing.zig");
39+
test "Browser.DOM.DOMParser" {
40+
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
41+
defer runner.deinit();
42+
43+
try runner.testCases(&.{
44+
.{ "const dp = new DOMParser()", "undefined" },
45+
.{ "dp.parseFromString('<div>abc</div>', 'text/html')", "[object HTMLDocument]" },
46+
}, .{});
47+
}

0 commit comments

Comments
 (0)