1- 'use strict'
1+ import sax from 'sax'
2+ import Message from 'vfile-message'
23
3- var Parser = require ( 'sax' ) . SAXParser
4- var Message = require ( 'vfile-message' )
5-
6- module . exports = fromXml
4+ var Parser = sax . SAXParser
75
86var fromCharCode = String . fromCharCode
97
108var search = / \r ? \n | \r / g
119
12- function fromXml ( doc ) {
10+ export function fromXml ( doc ) {
1311 var parser = new Parser ( true , { position : true , strictEntities : true } )
1412 var stack = [ { type : 'root' , children : [ ] } ]
1513 var position = now ( )
@@ -32,16 +30,17 @@ function fromXml(doc) {
3230
3331 function onerror ( error ) {
3432 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 */
3836 fail ( index === - 1 ? error . message : error . message . slice ( 0 , index ) , 'sax' )
3937 }
4038
4139 function onsgmldeclaration ( ) {
4240 fail ( 'Unexpected SGML declaration' , 'unexpected-sgml' )
4341 }
4442
43+ // eslint-disable-next-line complexity
4544 function ondoctype ( value ) {
4645 var node = { type : 'doctype' , name : '' , public : null , system : null }
4746 var index = - 1
@@ -97,24 +96,39 @@ function fromXml(doc) {
9796 // Done.
9897 } else if ( isSpace ( code ) ) {
9998 // 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+ }
118132
119133 break
120134 case 'IN_EID' :
@@ -203,9 +217,9 @@ function fromXml(doc) {
203217
204218 break
205219 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 */
209223 if ( code === null /* EOF */ ) {
210224 fail (
211225 'Expected quote or apostrophe to end system literal' ,
@@ -232,7 +246,8 @@ function fromXml(doc) {
232246 }
233247
234248 break
235- /* istanbul ignore next - Guard against new states */
249+ // Guard against new states.
250+ /* c8 ignore next 2 */
236251 default :
237252 throw new Error ( 'Unhandled state `' + state + '`' )
238253 }
@@ -252,7 +267,7 @@ function fromXml(doc) {
252267 }
253268
254269 function oncomment ( value ) {
255- var node = { type : 'comment' , value : value }
270+ var node = { type : 'comment' , value}
256271
257272 // Comment has a positional bug… 😢
258273 // They end right before the last character (`>`), so let’s add that:
@@ -276,7 +291,7 @@ function fromXml(doc) {
276291 }
277292
278293 function ontext ( value ) {
279- var node = { type : 'text' , value : value }
294+ var node = { type : 'text' , value}
280295 // Text has a positional bug… 😢
281296 // When they are added, the position is already at the next token.
282297 // So let’s reverse that.
@@ -343,7 +358,7 @@ function fromXml(doc) {
343358
344359// See: <https://www.w3.org/TR/xml/#NT-NameStartChar>
345360function 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 (
347362 fromCharCode ( code )
348363 )
349364}
@@ -352,7 +367,7 @@ function isNameStartChar(code) {
352367function isNameChar ( code ) {
353368 return (
354369 isNameStartChar ( code ) ||
355- / [ - . \d \xb7 \u0300 - \u036f \u203f \u2040 ] / . test ( fromCharCode ( code ) )
370+ / [ - . \d \u00B7 \u0300 - \u036F \u203F \u2040 ] / . test ( fromCharCode ( code ) )
356371 )
357372}
358373
0 commit comments