From bff3d275185ea42eaf0c1e2ccab0e2c7702fea19 Mon Sep 17 00:00:00 2001 From: Muki Kiboigo Date: Fri, 16 May 2025 08:51:51 -0700 Subject: [PATCH] add DOMParser --- src/browser/dom/dom.zig | 2 ++ src/browser/dom/dom_parser.zig | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/browser/dom/dom_parser.zig diff --git a/src/browser/dom/dom.zig b/src/browser/dom/dom.zig index f2f3aef7..2cfb521d 100644 --- a/src/browser/dom/dom.zig +++ b/src/browser/dom/dom.zig @@ -25,6 +25,7 @@ const NodeList = @import("nodelist.zig"); const Node = @import("node.zig"); const MutationObserver = @import("mutation_observer.zig"); const IntersectionObserver = @import("intersection_observer.zig"); +const DOMParser = @import("dom_parser.zig").DOMParser; pub const Interfaces = .{ DOMException, @@ -37,4 +38,5 @@ pub const Interfaces = .{ Node.Interfaces, MutationObserver.Interfaces, IntersectionObserver.Interfaces, + DOMParser, }; diff --git a/src/browser/dom/dom_parser.zig b/src/browser/dom/dom_parser.zig new file mode 100644 index 00000000..505d2b10 --- /dev/null +++ b/src/browser/dom/dom_parser.zig @@ -0,0 +1,47 @@ +// Copyright (C) 2023-2024 Lightpanda (Selecy SAS) +// +// Francis Bouvier +// Pierre Tachoire +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +const std = @import("std"); +const parser = @import("../netsurf.zig"); + +// https://developer.mozilla.org/en-US/docs/Web/API/DOMParser +pub const DOMParser = struct { + pub fn constructor() !DOMParser { + return .{}; + } + + pub fn _parseFromString(_: *DOMParser, string: []const u8, mime_type: []const u8) !*parser.DocumentHTML { + if (!std.mem.eql(u8, mime_type, "text/html")) { + // TODO: Support XML + return error.TypeError; + } + + return try parser.documentHTMLParseFromStr(string); + } +}; + +const testing = @import("../../testing.zig"); +test "Browser.DOM.DOMParser" { + var runner = try testing.jsRunner(testing.tracking_allocator, .{}); + defer runner.deinit(); + + try runner.testCases(&.{ + .{ "const dp = new DOMParser()", "undefined" }, + .{ "dp.parseFromString('
abc
', 'text/html')", "[object HTMLDocument]" }, + }, .{}); +}