@@ -15,7 +15,9 @@ describe('single-byte encodings are supersets of ascii', () => {
1515 for ( const encoding of encodings ) {
1616 test ( encoding , ( t ) => {
1717 const decoder = createSinglebyteDecoder ( encoding )
18+ const decoderLoose = createSinglebyteDecoder ( encoding , true )
1819 const encoder = createSinglebyteEncoder ( encoding )
20+ const encoderLoose = createSinglebyteEncoder ( encoding , { mode : 'replacement' } )
1921 for ( let i = 0 ; i < 128 ; i ++ ) {
2022 let str
2123 try {
@@ -27,7 +29,9 @@ describe('single-byte encodings are supersets of ascii', () => {
2729 t . assert . strictEqual ( str . length , 1 , i )
2830 t . assert . strictEqual ( str . codePointAt ( 0 ) , i , i )
2931
32+ t . assert . strictEqual ( decoderLoose ( Uint8Array . of ( i ) ) , str , i )
3033 t . assert . deepStrictEqual ( encoder ( str ) , Uint8Array . of ( i ) )
34+ t . assert . deepStrictEqual ( encoderLoose ( str ) , Uint8Array . of ( i ) )
3135 }
3236 } )
3337 }
@@ -84,6 +88,7 @@ describe('single-byte encodings index: Unicode', () => {
8488 const decoder = createSinglebyteDecoder ( encoding )
8589 const decoderLoose = createSinglebyteDecoder ( encoding , true )
8690 const encoder = createSinglebyteEncoder ( encoding )
91+ const encoderLoose = createSinglebyteEncoder ( encoding , { mode : 'replacement' } )
8792 const text = readFileSync (
8893 join ( import . meta. dirname , 'encoding/fixtures/unicode/' , fileName ) ,
8994 'utf8'
@@ -145,6 +150,7 @@ describe('single-byte encodings index: Unicode', () => {
145150 t . assert . strictEqual ( str , decoderLoose ( Uint8Array . of ( byte ) ) )
146151
147152 t . assert . deepStrictEqual ( encoder ( str ) , Uint8Array . of ( byte ) )
153+ t . assert . deepStrictEqual ( encoderLoose ( str ) , Uint8Array . of ( byte ) )
148154 }
149155 }
150156 } )
@@ -158,6 +164,7 @@ describe('single-byte encodings index: WHATWG', () => {
158164 const decoder = createSinglebyteDecoder ( encoding )
159165 const decoderLoose = createSinglebyteDecoder ( encoding , true )
160166 const encoder = createSinglebyteEncoder ( encoding )
167+ const encoderLoose = createSinglebyteEncoder ( encoding , { mode : 'replacement' } )
161168 const text = readFileSync (
162169 join ( import . meta. dirname , 'encoding/fixtures/single-byte' , `index-${ encoding } .txt` ) ,
163170 'utf8'
@@ -199,6 +206,7 @@ describe('single-byte encodings index: WHATWG', () => {
199206 t . assert . strictEqual ( str , decoderLoose ( Uint8Array . of ( byte ) ) )
200207
201208 t . assert . deepStrictEqual ( encoder ( str ) , Uint8Array . of ( byte ) )
209+ t . assert . deepStrictEqual ( encoderLoose ( str ) , Uint8Array . of ( byte ) )
202210 } else {
203211 t . assert . throws ( ( ) => decoder ( Uint8Array . of ( byte ) ) )
204212 try {
@@ -230,6 +238,7 @@ describe('single-byte encodings index: WHATWG non-normative indexes.json', () =>
230238 const decoder = createSinglebyteDecoder ( encoding )
231239 const decoderLoose = createSinglebyteDecoder ( encoding , true )
232240 const encoder = createSinglebyteEncoder ( encoding )
241+ const encoderLoose = createSinglebyteEncoder ( encoding , { mode : 'replacement' } )
233242
234243 t . assert . strictEqual ( data . length , 128 )
235244 for ( let i = 0 ; i < data . length ; i ++ ) {
@@ -244,6 +253,7 @@ describe('single-byte encodings index: WHATWG non-normative indexes.json', () =>
244253 t . assert . strictEqual ( decoder ( Uint8Array . of ( byte ) ) , str )
245254 t . assert . strictEqual ( decoderLoose ( Uint8Array . of ( byte ) ) , str )
246255 t . assert . deepStrictEqual ( encoder ( str ) , Uint8Array . of ( byte ) )
256+ t . assert . deepStrictEqual ( encoderLoose ( str ) , Uint8Array . of ( byte ) )
247257 } else {
248258 t . assert . throws ( ( ) => decoder ( Uint8Array . of ( byte ) ) )
249259 t . assert . strictEqual ( decoderLoose ( Uint8Array . of ( byte ) ) , '\uFFFD' )
@@ -268,13 +278,16 @@ describe('x-user-defined', () => {
268278
269279 test ( 'encode' , ( t ) => {
270280 const encoder = createSinglebyteEncoder ( encoding )
281+ const encoderLoose = createSinglebyteEncoder ( encoding , { mode : 'replacement' } )
271282 for ( let byte = 0 ; byte < 256 ; byte ++ ) {
272283 const str = String . fromCodePoint ( byte >= 0x80 ? 0xf7_80 + byte - 0x80 : byte )
273284 t . assert . deepStrictEqual ( encoder ( str ) , Uint8Array . of ( byte ) , byte )
285+ t . assert . deepStrictEqual ( encoderLoose ( str ) , Uint8Array . of ( byte ) , byte )
274286 }
275287
276288 for ( let i = 128 ; i < 512 ; i ++ ) {
277289 t . assert . throws ( ( ) => encoder ( String . fromCodePoint ( i ) ) , / I n p u t i s n o t w e l l - f o r m e d / )
290+ t . assert . deepStrictEqual ( encoderLoose ( String . fromCodePoint ( i ) ) , Uint8Array . of ( 0x3f ) , i )
278291 }
279292 } )
280293} )
@@ -284,21 +297,31 @@ describe('codes above 0x7F are non-ASCII', () => {
284297 for ( const encoding of [ 'iso-8859-2' , 'iso-8859-16' ] ) {
285298 test ( encoding , ( t ) => {
286299 const encoder = createSinglebyteEncoder ( encoding )
300+ const encoderLoose = createSinglebyteEncoder ( encoding , { mode : 'replacement' } )
287301 t . assert . deepStrictEqual ( encoder ( '\x80' ) , new Uint8Array ( 1 ) . fill ( 0x80 ) )
288302 t . assert . deepStrictEqual ( encoder ( '\x80' . repeat ( 4 ) ) , new Uint8Array ( 4 ) . fill ( 0x80 ) )
289303 t . assert . deepStrictEqual ( encoder ( '\x80' . repeat ( 8 ) ) , new Uint8Array ( 8 ) . fill ( 0x80 ) )
290304 t . assert . deepStrictEqual ( encoder ( '\x80' . repeat ( 16 ) ) , new Uint8Array ( 16 ) . fill ( 0x80 ) )
305+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' ) , new Uint8Array ( 1 ) . fill ( 0x80 ) )
306+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' . repeat ( 4 ) ) , new Uint8Array ( 4 ) . fill ( 0x80 ) )
307+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' . repeat ( 8 ) ) , new Uint8Array ( 8 ) . fill ( 0x80 ) )
308+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' . repeat ( 16 ) ) , new Uint8Array ( 16 ) . fill ( 0x80 ) )
291309 } )
292310 }
293311
294312 // 0x80 maps to something else
295313 for ( const encoding of [ 'windows-1250' , 'windows-1252' , 'x-user-defined' ] ) {
296314 test ( encoding , ( t ) => {
297315 const encoder = createSinglebyteEncoder ( encoding )
316+ const encoderLoose = createSinglebyteEncoder ( encoding , { mode : 'replacement' } )
298317 t . assert . throws ( ( ) => encoder ( '\x80' ) )
299318 t . assert . throws ( ( ) => encoder ( '\x80' . repeat ( 4 ) ) )
300319 t . assert . throws ( ( ) => encoder ( '\x80' . repeat ( 8 ) ) )
301320 t . assert . throws ( ( ) => encoder ( '\x80' . repeat ( 16 ) ) )
321+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' ) , new Uint8Array ( 1 ) . fill ( 0x3f ) )
322+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' . repeat ( 4 ) ) , new Uint8Array ( 4 ) . fill ( 0x3f ) )
323+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' . repeat ( 8 ) ) , new Uint8Array ( 8 ) . fill ( 0x3f ) )
324+ t . assert . deepStrictEqual ( encoderLoose ( '\x80' . repeat ( 16 ) ) , new Uint8Array ( 16 ) . fill ( 0x3f ) )
302325 } )
303326 }
304327} )
0 commit comments