@@ -20,6 +20,7 @@ const std = @import("std");
2020const log = @import ("../../log.zig" );
2121
2222const Env = @import ("../env.zig" ).Env ;
23+ const Page = @import ("../page.zig" ).Page ;
2324
2425// https://encoding.spec.whatwg.org/#interface-textdecoder
2526const TextDecoder = @This ();
@@ -37,6 +38,7 @@ const Options = struct {
3738
3839fatal : bool ,
3940ignore_bom : bool ,
41+ stream : std .ArrayList (u8 ),
4042
4143pub fn constructor (label_ : ? []const u8 , opts_ : ? Options ) ! TextDecoder {
4244 if (label_ ) | l | {
@@ -47,6 +49,7 @@ pub fn constructor(label_: ?[]const u8, opts_: ?Options) !TextDecoder {
4749 }
4850 const opts = opts_ orelse Options {};
4951 return .{
52+ .stream = .empty ,
5053 .fatal = opts .fatal ,
5154 .ignore_bom = opts .ignoreBOM ,
5255 };
@@ -64,18 +67,34 @@ pub fn get_fatal(self: *const TextDecoder) bool {
6467 return self .fatal ;
6568}
6669
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 )) {
70+ const DecodeOptions = struct {
71+ stream : bool = false ,
72+ };
73+ pub fn _decode (self : * TextDecoder , input_ : ? []const u8 , opts_ : ? DecodeOptions , page : * Page ) ! []const u8 {
74+ var str = input_ orelse return "" ;
75+ const opts : DecodeOptions = opts_ orelse .{};
76+
77+ if (self .stream .items .len > 0 ) {
78+ try self .stream .appendSlice (page .arena , str );
79+ str = self .stream .items ;
80+ }
81+
82+ if (self .fatal and ! std .unicode .utf8ValidateSlice (str )) {
83+ if (opts .stream ) {
84+ if (self .stream .items .len == 0 ) {
85+ try self .stream .appendSlice (page .arena , str );
86+ }
87+ return "" ;
88+ }
7189 return error .InvalidUtf8 ;
7290 }
7391
74- if (self .ignore_bom == false and std .mem .startsWith (u8 , v , &.{ 0xEF , 0xBB , 0xBF })) {
75- return v [3.. ];
92+ self .stream .clearRetainingCapacity ();
93+ if (self .ignore_bom == false and std .mem .startsWith (u8 , str , &.{ 0xEF , 0xBB , 0xBF })) {
94+ return str [3.. ];
7695 }
7796
78- return v ;
97+ return str ;
7998}
8099
81100const testing = @import ("../../testing.zig" );
0 commit comments