@@ -24,14 +24,14 @@ const search = /\r?\n|\r/g
24
24
* @param {string|Uint8Array } doc
25
25
*/
26
26
export function fromXml ( doc ) {
27
- // @ts -ignore `strictEntities` is most definitely fine.
27
+ // @ts -expect-error `strictEntities` is most definitely fine.
28
28
const parser = new Parser ( true , { position : true , strictEntities : true } )
29
29
/** @type {Array.<Node> } */
30
30
const stack = [ { type : 'root' , children : [ ] } ]
31
31
let position = now ( )
32
32
33
33
parser . ondoctype = ondoctype
34
- // @ts -ignore `onsgmldeclaration` is most definitely fine.
34
+ // @ts -expect-error `onsgmldeclaration` is most definitely fine.
35
35
parser . onsgmldeclaration = onsgmldeclaration
36
36
parser . onprocessinginstruction = onprocessinginstruction
37
37
parser . ontext = ontext
@@ -43,7 +43,7 @@ export function fromXml(doc) {
43
43
parser . onclosetag = exit
44
44
parser . onerror = onerror
45
45
46
- // @ts -ignore Buffers are most definitely fine.
46
+ // @ts -expect-error Buffers are most definitely fine.
47
47
parser . write ( doc ) . close ( )
48
48
49
49
return stack [ 0 ]
@@ -74,18 +74,19 @@ export function fromXml(doc) {
74
74
// eslint-disable-next-line complexity
75
75
function ondoctype ( value ) {
76
76
/** @type {Doctype } */
77
+ // @ts -expect-error: `null`s are fine.
77
78
const node = { type : 'doctype' , name : '' , public : null , system : null }
78
79
let index = - 1
79
80
let state = 'BEGIN'
80
- /** @type {string } */
81
+ /** @type {string|undefined } */
81
82
let returnState
82
- /** @type {string } */
83
+ /** @type {string|undefined } */
83
84
let buffer
84
- /** @type {number } */
85
+ /** @type {number|undefined } */
85
86
let bufferIndex
86
- /** @type {number } */
87
+ /** @type {number|undefined } */
87
88
let start
88
- /** @type {number } */
89
+ /** @type {number|undefined } */
89
90
let marker
90
91
91
92
while ( ++ index <= value . length ) {
@@ -169,7 +170,12 @@ export function fromXml(doc) {
169
170
170
171
break
171
172
case 'IN_EID' :
172
- if ( code === buffer . charCodeAt ( ++ bufferIndex ) ) {
173
+ if (
174
+ returnState &&
175
+ buffer &&
176
+ bufferIndex !== undefined &&
177
+ code === buffer . charCodeAt ( ++ bufferIndex )
178
+ ) {
173
179
if ( bufferIndex === buffer . length - 1 ) {
174
180
state = returnState
175
181
}
@@ -314,12 +320,20 @@ export function fromXml(doc) {
314
320
// Comment has a positional bug… 😢
315
321
// They end right before the last character (`>`), so let’s add that:
316
322
const actualEnd = now ( )
323
+
324
+ /* c8 ignore next 3 */
325
+ if ( typeof actualEnd . offset !== 'number' ) {
326
+ throw new TypeError ( 'Expected offset' )
327
+ }
328
+
317
329
actualEnd . column ++
318
330
actualEnd . offset ++
319
331
320
332
enter ( node )
321
333
exit ( )
322
334
335
+ /* c8 ignore next */
336
+ if ( ! node . position ) throw new Error ( 'Expected position' )
323
337
node . position . end = Object . assign ( { } , actualEnd )
324
338
position = actualEnd
325
339
}
@@ -367,11 +381,16 @@ export function fromXml(doc) {
367
381
}
368
382
}
369
383
384
+ /* c8 ignore next */
385
+ if ( typeof actualEnd . offset !== 'number' ) throw new Error ( 'Expected offset' )
386
+
370
387
actualEnd . offset += value . length
371
388
372
389
enter ( node )
373
390
exit ( )
374
391
392
+ /* c8 ignore next */
393
+ if ( ! node . position ) throw new Error ( 'Expected position' )
375
394
node . position . end = Object . assign ( { } , actualEnd )
376
395
position = actualEnd
377
396
}
@@ -394,8 +413,9 @@ export function fromXml(doc) {
394
413
* @returns {void }
395
414
*/
396
415
function enter ( node ) {
416
+ // @ts -expect-error Set later.
397
417
node . position = { start : Object . assign ( { } , position ) , end : undefined }
398
- // @ts -ignore Assume valid child.
418
+ // @ts -expect-error Assume valid child.
399
419
stack [ stack . length - 1 ] . children . push ( node )
400
420
stack . push ( node )
401
421
position = now ( )
@@ -405,8 +425,11 @@ export function fromXml(doc) {
405
425
* @returns {void }
406
426
*/
407
427
function exit ( ) {
428
+ const tail = stack . pop ( )
429
+ /* c8 ignore next */
430
+ if ( ! tail || ! tail . position ) throw new Error ( 'Expected tail' )
408
431
position = now ( )
409
- stack . pop ( ) . position . end = Object . assign ( { } , position )
432
+ tail . position . end = Object . assign ( { } , position )
410
433
}
411
434
412
435
/**
@@ -433,42 +456,46 @@ export function fromXml(doc) {
433
456
/**
434
457
* See: <https://www.w3.org/TR/xml/#NT-NameStartChar>
435
458
*
436
- * @param {number } code
459
+ * @param {number|null } code
437
460
* @returns {boolean }
438
461
*/
439
462
function isNameStartChar ( code ) {
440
- // eslint-disable-next-line no-misleading-character-class
441
- return / [: A -Z _ a - z \u00C0 - \u00D6 \u00D8 - \u00F6 \u00F8 - \u02FF \u0370 - \u037D \u037F - \u1FFF \u200C \u200D \u2070 - \u218F \u2C00 - \u2FEF \u3001 - \uD7FF \uF900 - \uFDCF \uFDF0 - \uFFFD ] / . test (
442
- fromCharCode ( code )
463
+ return (
464
+ code !== null &&
465
+ // eslint-disable-next-line no-misleading-character-class
466
+ / [: A -Z _ a - z \u00C0 - \u00D6 \u00D8 - \u00F6 \u00F8 - \u02FF \u0370 - \u037D \u037F - \u1FFF \u200C \u200D \u2070 - \u218F \u2C00 - \u2FEF \u3001 - \uD7FF \uF900 - \uFDCF \uFDF0 - \uFFFD ] / . test (
467
+ fromCharCode ( code )
468
+ )
443
469
)
444
470
}
445
471
446
472
/**
447
473
* See: <https://www.w3.org/TR/xml/#NT-NameChar>
448
474
*
449
- * @param {number } code
475
+ * @param {number|null } code
450
476
* @returns {boolean }
451
477
*/
452
478
function isNameChar ( code ) {
453
479
return (
454
- isNameStartChar ( code ) ||
455
- // eslint-disable-next-line no-misleading-character-class
456
- / [ - . \d \u00B7 \u0300 - \u036F \u203F \u2040 ] / . test ( fromCharCode ( code ) )
480
+ code !== null &&
481
+ ( isNameStartChar ( code ) ||
482
+ // eslint-disable-next-line no-misleading-character-class
483
+ / [ - . \d \u00B7 \u0300 - \u036F \u203F \u2040 ] / . test ( fromCharCode ( code ) ) )
457
484
)
458
485
}
459
486
460
487
/**
461
- * @param {number } code
488
+ * @param {number|null } code
462
489
* @returns {boolean }
463
490
*/
464
491
function isSpace ( code ) {
465
- return / [ \t \n \r ] / . test ( fromCharCode ( code ) )
492
+ return code !== null && / [ \t \n \r ] / . test ( fromCharCode ( code ) )
466
493
}
467
494
468
495
/**
469
- * @param {number } code
496
+ * @param {number|null } code
470
497
* @returns {boolean }
471
498
*/
472
499
function isPubidChar ( code ) {
473
- return / [ \n \r ! # $ % ' - ; = ? - Z _ a - z ] / . test ( fromCharCode ( code ) )
500
+ return code !== null && / [ \n \r ! # $ % ' - ; = ? - Z _ a - z ] / . test ( fromCharCode ( code ) )
474
501
}
0 commit comments