@@ -291,13 +291,15 @@ export default class Tokenizer {
291
291
private readonly entityTrie : Uint16Array ;
292
292
293
293
constructor (
294
- options : { xmlMode ?: boolean ; decodeEntities ?: boolean } | null ,
294
+ {
295
+ xmlMode = false ,
296
+ decodeEntities = true ,
297
+ } : { xmlMode ?: boolean ; decodeEntities ?: boolean } ,
295
298
private readonly cbs : Callbacks
296
299
) {
297
- this . cbs = cbs ;
298
- this . xmlMode = ! ! options ?. xmlMode ;
299
- this . decodeEntities = options ?. decodeEntities ?? true ;
300
- this . entityTrie = this . xmlMode ? xmlDecodeTree : htmlDecodeTree ;
300
+ this . xmlMode = xmlMode ;
301
+ this . decodeEntities = decodeEntities ;
302
+ this . entityTrie = xmlMode ? xmlDecodeTree : htmlDecodeTree ;
301
303
}
302
304
303
305
public reset ( ) : void {
@@ -314,7 +316,8 @@ export default class Tokenizer {
314
316
315
317
public write ( chunk : string ) : void {
316
318
if ( this . ended ) this . cbs . onerror ( Error ( ".write() after done!" ) ) ;
317
- this . buffer += chunk ;
319
+ if ( this . buffer . length ) this . buffer += chunk ;
320
+ else this . buffer = chunk ;
318
321
this . parse ( ) ;
319
322
}
320
323
@@ -777,7 +780,7 @@ export default class Tokenizer {
777
780
const entity = this . buffer . substring ( sectionStart , this . _index ) ;
778
781
const parsed = parseInt ( entity , base ) ;
779
782
this . emitPartial ( decodeCodePoint ( parsed ) ) ;
780
- this . sectionStart = strict ? this . _index + 1 : this . _index ;
783
+ this . sectionStart = this . _index + Number ( strict ) ;
781
784
}
782
785
this . _state = this . baseState ;
783
786
}
@@ -980,11 +983,9 @@ export default class Tokenizer {
980
983
this . stateInHexEntity ( c ) ;
981
984
} else if ( this . _state === State . InNumericEntity ) {
982
985
this . stateInNumericEntity ( c ) ;
983
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
984
- } else if ( this . _state === State . BeforeNumericEntity ) {
985
- stateBeforeNumericEntity ( this , c ) ;
986
986
} else {
987
- this . cbs . onerror ( Error ( "unknown _state" ) , this . _state ) ;
987
+ // `this._state === State.BeforeNumericEntity`
988
+ stateBeforeNumericEntity ( this , c ) ;
988
989
}
989
990
this . _index ++ ;
990
991
}
@@ -1023,16 +1024,10 @@ export default class Tokenizer {
1023
1024
}
1024
1025
} else if ( this . _state === State . InNumericEntity && ! this . xmlMode ) {
1025
1026
this . decodeNumericEntity ( 10 , false ) ;
1026
- if ( this . sectionStart < this . _index ) {
1027
- this . _state = this . baseState ;
1028
- this . handleTrailingData ( ) ;
1029
- }
1027
+ // All trailing data will have been consumed
1030
1028
} else if ( this . _state === State . InHexEntity && ! this . xmlMode ) {
1031
1029
this . decodeNumericEntity ( 16 , false ) ;
1032
- if ( this . sectionStart < this . _index ) {
1033
- this . _state = this . baseState ;
1034
- this . handleTrailingData ( ) ;
1035
- }
1030
+ // All trailing data will have been consumed
1036
1031
} else if (
1037
1032
this . _state !== State . InTagName &&
1038
1033
this . _state !== State . BeforeAttributeName &&
@@ -1047,7 +1042,6 @@ export default class Tokenizer {
1047
1042
this . cbs . ontext ( data ) ;
1048
1043
}
1049
1044
/*
1050
- * Else, ignore remaining data
1051
1045
* TODO add a way to remove current tag
1052
1046
*/
1053
1047
}
@@ -1057,7 +1051,7 @@ export default class Tokenizer {
1057
1051
}
1058
1052
private emitPartial ( value : string ) {
1059
1053
if ( this . baseState !== State . Text ) {
1060
- this . cbs . onattribdata ( value ) ; // TODO implement the new event
1054
+ this . cbs . onattribdata ( value ) ;
1061
1055
} else {
1062
1056
this . cbs . ontext ( value ) ;
1063
1057
}
0 commit comments