mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
Leverage comptime fields to give generated Tuple a default value, allowing TupleT and Tuple to be merged. Only call generate.Tuple on the final output. This eliminates redundant deduplication, and results in a simpler API (nested types just need to expose a natural Zig tuple). generate.Union leverages the new Tuple and removes unused features.
72 lines
2.3 KiB
Zig
72 lines
2.3 KiB
Zig
// Copyright (C) 2023-2025 Lightpanda (Selecy SAS)
|
|
//
|
|
// Francis Bouvier <francis@lightpanda.io>
|
|
// Pierre Tachoire <pierre@lightpanda.io>
|
|
//
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
//
|
|
const std = @import("std");
|
|
|
|
const jsruntime = @import("jsruntime");
|
|
const Case = jsruntime.test_utils.Case;
|
|
const checkCases = jsruntime.test_utils.checkCases;
|
|
|
|
const DOMError = @import("netsurf").DOMError;
|
|
|
|
const parser = @import("netsurf");
|
|
const dump = @import("../browser/dump.zig");
|
|
|
|
pub const Interfaces = .{
|
|
XMLSerializer,
|
|
};
|
|
|
|
// https://w3c.github.io/DOM-Parsing/#dom-xmlserializer-constructor
|
|
pub const XMLSerializer = struct {
|
|
pub const mem_guarantied = true;
|
|
|
|
pub fn constructor() !XMLSerializer {
|
|
return .{};
|
|
}
|
|
|
|
pub fn deinit(_: *XMLSerializer, _: std.mem.Allocator) void {}
|
|
|
|
pub fn _serializeToString(_: XMLSerializer, alloc: std.mem.Allocator, root: *parser.Node) ![]const u8 {
|
|
var buf = std.ArrayList(u8).init(alloc);
|
|
defer buf.deinit();
|
|
|
|
if (try parser.nodeType(root) == .document) {
|
|
try dump.writeHTML(@as(*parser.Document, @ptrCast(root)), buf.writer());
|
|
} else {
|
|
try dump.writeNode(root, buf.writer());
|
|
}
|
|
// TODO express the caller owned the slice.
|
|
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
|
|
return try buf.toOwnedSlice();
|
|
}
|
|
};
|
|
|
|
// Tests
|
|
// -----
|
|
|
|
pub fn testExecFn(
|
|
_: std.mem.Allocator,
|
|
js_env: *jsruntime.Env,
|
|
) anyerror!void {
|
|
var serializer = [_]Case{
|
|
.{ .src = "const s = new XMLSerializer()", .ex = "undefined" },
|
|
.{ .src = "s.serializeToString(document.getElementById('para'))", .ex = "<p id=\"para\"> And</p>" },
|
|
};
|
|
try checkCases(js_env, &serializer);
|
|
}
|