|
| 1 | +// Copyright (C) 2023-2025 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 log = @import("../../log.zig"); |
| 21 | + |
| 22 | +const Env = @import("../env.zig").Env; |
| 23 | + |
| 24 | +// https://encoding.spec.whatwg.org/#interface-textdecoder |
| 25 | +const TextDecoder = @This(); |
| 26 | + |
| 27 | +const SupportedLabels = enum { |
| 28 | + utf8, |
| 29 | + @"utf-8", |
| 30 | + @"unicode-1-1-utf-8", |
| 31 | +}; |
| 32 | + |
| 33 | +const Options = struct { |
| 34 | + fatal: bool = false, |
| 35 | + ignoreBOM: bool = false, |
| 36 | +}; |
| 37 | + |
| 38 | +fatal: bool, |
| 39 | +ignore_bom: bool, |
| 40 | + |
| 41 | +pub fn constructor(label_: ?[]const u8, opts_: ?Options) !TextDecoder { |
| 42 | + if (label_) |l| { |
| 43 | + _ = std.meta.stringToEnum(SupportedLabels, l) orelse { |
| 44 | + log.warn(.web_api, "not implemented", .{ .feature = "TextDecoder label", .label = l }); |
| 45 | + return error.NotImplemented; |
| 46 | + }; |
| 47 | + } |
| 48 | + const opts = opts_ orelse Options{}; |
| 49 | + return .{ |
| 50 | + .fatal = opts.fatal, |
| 51 | + .ignore_bom = opts.ignoreBOM, |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +pub fn get_encoding(_: *const TextDecoder) []const u8 { |
| 56 | + return "utf-8"; |
| 57 | +} |
| 58 | + |
| 59 | +pub fn get_ignoreBOM(self: *const TextDecoder) bool { |
| 60 | + return self.ignore_bom; |
| 61 | +} |
| 62 | + |
| 63 | +pub fn get_fatal(self: *const TextDecoder) bool { |
| 64 | + return self.fatal; |
| 65 | +} |
| 66 | + |
| 67 | +// TODO: Should accept an ArrayBuffer, TypedArray or DataView |
| 68 | +// js.zig will currently only map a TypedArray to our []const u8. |
| 69 | +pub fn _decode(self: *const TextDecoder, v: []const u8) ![]const u8 { |
| 70 | + if (self.fatal and !std.unicode.utf8ValidateSlice(v)) { |
| 71 | + return error.InvalidUtf8; |
| 72 | + } |
| 73 | + |
| 74 | + if (self.ignore_bom == false and std.mem.startsWith(u8, v, &.{ 0xEF, 0xBB, 0xBF })) { |
| 75 | + return v[3..]; |
| 76 | + } |
| 77 | + |
| 78 | + return v; |
| 79 | +} |
| 80 | + |
| 81 | +const testing = @import("../../testing.zig"); |
| 82 | +test "Browser.Encoding.TextDecoder" { |
| 83 | + var runner = try testing.jsRunner(testing.tracking_allocator, .{ |
| 84 | + .html = "", |
| 85 | + }); |
| 86 | + defer runner.deinit(); |
| 87 | + |
| 88 | + try runner.testCases(&.{ |
| 89 | + .{ "let d1 = new TextDecoder();", null }, |
| 90 | + .{ "d1.encoding;", "utf-8" }, |
| 91 | + .{ "d1.fatal", "false" }, |
| 92 | + .{ "d1.ignoreBOM", "false" }, |
| 93 | + .{ "d1.decode(new Uint8Array([240, 160, 174, 183]))", "𠮷" }, |
| 94 | + .{ "d1.decode(new Uint8Array([0xEF, 0xBB, 0xBF, 240, 160, 174, 183]))", "𠮷" }, |
| 95 | + |
| 96 | + .{ "let d2 = new TextDecoder('utf8', {fatal: true})", null }, |
| 97 | + .{ |
| 98 | + \\ try { |
| 99 | + \\ let data = new Uint8Array([240, 240, 160, 174, 183]); |
| 100 | + \\ d2.decode(data); |
| 101 | + \\ } catch (e) {e} |
| 102 | + , |
| 103 | + "Error: InvalidUtf8", |
| 104 | + }, |
| 105 | + }, .{}); |
| 106 | +} |
0 commit comments