1
- 'use strict'
1
+ import sax from 'sax'
2
+ import Message from 'vfile-message'
2
3
3
- var Parser = require ( 'sax' ) . SAXParser
4
- var Message = require ( 'vfile-message' )
5
-
6
- module . exports = fromXml
4
+ var Parser = sax . SAXParser
7
5
8
6
var fromCharCode = String . fromCharCode
9
7
10
8
var search = / \r ? \n | \r / g
11
9
12
- function fromXml ( doc ) {
10
+ export function fromXml ( doc ) {
13
11
var parser = new Parser ( true , { position : true , strictEntities : true } )
14
12
var stack = [ { type : 'root' , children : [ ] } ]
15
13
var position = now ( )
@@ -32,16 +30,17 @@ function fromXml(doc) {
32
30
33
31
function onerror ( error ) {
34
32
var index = error . message . indexOf ( '\nLine' )
35
- /* istanbul ignore next
36
- * - The substring should always be included, but this guards against
37
- * changes in newer sax versions */
33
+ // The substring should always be included, but this guards against
34
+ // changes in newer sax versions.
35
+ /* c8 ignore next */
38
36
fail ( index === - 1 ? error . message : error . message . slice ( 0 , index ) , 'sax' )
39
37
}
40
38
41
39
function onsgmldeclaration ( ) {
42
40
fail ( 'Unexpected SGML declaration' , 'unexpected-sgml' )
43
41
}
44
42
43
+ // eslint-disable-next-line complexity
45
44
function ondoctype ( value ) {
46
45
var node = { type : 'doctype' , name : '' , public : null , system : null }
47
46
var index = - 1
@@ -97,24 +96,39 @@ function fromXml(doc) {
97
96
// Done.
98
97
} else if ( isSpace ( code ) ) {
99
98
// As expected.
100
- } else if ( code === 80 /* `P` */ ) {
101
- state = 'IN_EID'
102
- returnState = 'AFTER_PUBLIC'
103
- buffer = 'PUBLIC'
104
- bufferIndex = 0
105
- } else if ( code === 83 /* `S` */ ) {
106
- state = 'IN_EID'
107
- returnState = 'AFTER_SYSTEM'
108
- buffer = 'SYSTEM'
109
- bufferIndex = 0
110
- } else if ( code === 91 /* `[` */ ) {
111
- fail ( 'Unexpected internal subset' , 'doctype-internal-subset' )
112
- } else {
113
- fail (
114
- 'Expected external identifier (`PUBLIC` or `SYSTEM`), whitespace, or doctype end' ,
115
- 'doctype-external-identifier'
116
- )
117
- }
99
+ } else
100
+ switch ( code ) {
101
+ case 80 : {
102
+ state = 'IN_EID'
103
+ returnState = 'AFTER_PUBLIC'
104
+ buffer = 'PUBLIC'
105
+ bufferIndex = 0
106
+
107
+ break
108
+ }
109
+
110
+ case 83 : {
111
+ state = 'IN_EID'
112
+ returnState = 'AFTER_SYSTEM'
113
+ buffer = 'SYSTEM'
114
+ bufferIndex = 0
115
+
116
+ break
117
+ }
118
+
119
+ case 91 : {
120
+ fail ( 'Unexpected internal subset' , 'doctype-internal-subset' )
121
+
122
+ break
123
+ }
124
+
125
+ default : {
126
+ fail (
127
+ 'Expected external identifier (`PUBLIC` or `SYSTEM`), whitespace, or doctype end' ,
128
+ 'doctype-external-identifier'
129
+ )
130
+ }
131
+ }
118
132
119
133
break
120
134
case 'IN_EID' :
@@ -203,9 +217,9 @@ function fromXml(doc) {
203
217
204
218
break
205
219
case 'IN_SYSTEM_LITERAL' :
206
- /* istanbul ignore next
207
- * - Handled by SAX, but keep it to guard against changes in newer sax
208
- * versions. */
220
+ // Handled by SAX, but keep it to guard against changes in newer sax
221
+ // versions.
222
+ /* c8 ignore next 5 */
209
223
if ( code === null /* EOF */ ) {
210
224
fail (
211
225
'Expected quote or apostrophe to end system literal' ,
@@ -232,7 +246,8 @@ function fromXml(doc) {
232
246
}
233
247
234
248
break
235
- /* istanbul ignore next - Guard against new states */
249
+ // Guard against new states.
250
+ /* c8 ignore next 2 */
236
251
default :
237
252
throw new Error ( 'Unhandled state `' + state + '`' )
238
253
}
@@ -252,7 +267,7 @@ function fromXml(doc) {
252
267
}
253
268
254
269
function oncomment ( value ) {
255
- var node = { type : 'comment' , value : value }
270
+ var node = { type : 'comment' , value}
256
271
257
272
// Comment has a positional bug… 😢
258
273
// They end right before the last character (`>`), so let’s add that:
@@ -276,7 +291,7 @@ function fromXml(doc) {
276
291
}
277
292
278
293
function ontext ( value ) {
279
- var node = { type : 'text' , value : value }
294
+ var node = { type : 'text' , value}
280
295
// Text has a positional bug… 😢
281
296
// When they are added, the position is already at the next token.
282
297
// So let’s reverse that.
@@ -343,7 +358,7 @@ function fromXml(doc) {
343
358
344
359
// See: <https://www.w3.org/TR/xml/#NT-NameStartChar>
345
360
function isNameStartChar ( code ) {
346
- return / [: A -Z _ a - z \xc0 - \xd6 \xd8 - \xf6 \xf8 - \u02ff \u0370 - \u037d \u037f - \u1fff \u200c \u200d \u2070 - \u218f \u2c00 - \u2fef \u3001 - \ud7ff \uf900 - \ufdcf \ufdf0 - \ufffd ] / . test (
361
+ 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 (
347
362
fromCharCode ( code )
348
363
)
349
364
}
@@ -352,7 +367,7 @@ function isNameStartChar(code) {
352
367
function isNameChar ( code ) {
353
368
return (
354
369
isNameStartChar ( code ) ||
355
- / [ - . \d \xb7 \u0300 - \u036f \u203f \u2040 ] / . test ( fromCharCode ( code ) )
370
+ / [ - . \d \u00B7 \u0300 - \u036F \u203F \u2040 ] / . test ( fromCharCode ( code ) )
356
371
)
357
372
}
358
373
0 commit comments