Skip to content

Commit 71f8583

Browse files
committed
implement TextEncoder
1 parent 7bb6506 commit 71f8583

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
21+
const Env = @import("../env.zig").Env;
22+
const SessionState = @import("../env.zig").SessionState;
23+
24+
pub const Interfaces = .{
25+
TextEncoder,
26+
};
27+
28+
// https://encoding.spec.whatwg.org/#interface-textencoder
29+
pub const TextEncoder = struct {
30+
pub fn constructor() !TextEncoder {
31+
return .{};
32+
}
33+
34+
pub fn get_encoding(_: *const TextEncoder) []const u8 {
35+
return "utf-8";
36+
}
37+
38+
pub fn _encode(_: *const TextEncoder, v: []const u8) !Env.TypedArray(u8) {
39+
// Ensure the input is a valid utf-8
40+
// It seems chrome accepts invalid utf-8 sequence. We should maybe
41+
// ignore this check.
42+
if (!std.unicode.utf8ValidateSlice(v)) {
43+
return error.InvalidUtf8Input;
44+
}
45+
46+
return .{ .values = v };
47+
}
48+
};
49+
50+
const testing = @import("../../testing.zig");
51+
test "Browser.Encoding.TextEncoder" {
52+
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
53+
defer runner.deinit();
54+
55+
try runner.testCases(&.{
56+
.{ "var encoder = new TextEncoder();", "undefined" },
57+
.{ "encoder.encoding;", "utf-8" },
58+
.{ "encoder.encode('€');", "226,130,172" },
59+
// Invalid utf-8 sequence.
60+
.{ "try {encoder.encode(new Uint8Array([0xE2,0x28,0xA1])) } catch (e) { e };", "Error: InvalidUtf8Input" },
61+
}, .{});
62+
}

src/browser/env.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const WebApis = struct {
2828
@import("crypto/crypto.zig").Crypto,
2929
@import("console/console.zig").Console,
3030
@import("dom/dom.zig").Interfaces,
31+
@import("encoding/text_encoder.zig").Interfaces,
3132
@import("events/event.zig").Interfaces,
3233
@import("html/html.zig").Interfaces,
3334
@import("iterator/iterator.zig").Interfaces,

0 commit comments

Comments
 (0)