11'use strict' ;
22
3- import { getAllMatches , isName } from './util.js' ;
3+ import { getAllMatches , isName } from './util.js' ;
44
55const defaultOptions = {
66 allowBooleanAttributes : false , //A tag can have attributes without any value
@@ -24,19 +24,19 @@ export function validate(xmlData, options) {
2424 // check for byte order mark (BOM)
2525 xmlData = xmlData . substr ( 1 ) ;
2626 }
27-
27+
2828 for ( let i = 0 ; i < xmlData . length ; i ++ ) {
2929
30- if ( xmlData [ i ] === '<' && xmlData [ i + 1 ] === '?' ) {
31- i += 2 ;
32- i = readPI ( xmlData , i ) ;
30+ if ( xmlData [ i ] === '<' && xmlData [ i + 1 ] === '?' ) {
31+ i += 2 ;
32+ i = readPI ( xmlData , i ) ;
3333 if ( i . err ) return i ;
34- } else if ( xmlData [ i ] === '<' ) {
34+ } else if ( xmlData [ i ] === '<' ) {
3535 //starting of tag
3636 //read until you reach to '>' avoiding any '>' in attribute value
3737 let tagStartPos = i ;
3838 i ++ ;
39-
39+
4040 if ( xmlData [ i ] === '!' ) {
4141 i = readCommentAndCDATA ( xmlData , i ) ;
4242 continue ;
@@ -72,14 +72,14 @@ export function validate(xmlData, options) {
7272 if ( tagName . trim ( ) . length === 0 ) {
7373 msg = "Invalid space after '<'." ;
7474 } else {
75- msg = "Tag '" + tagName + "' is an invalid name." ;
75+ msg = "Tag '" + tagName + "' is an invalid name." ;
7676 }
7777 return getErrorObject ( 'InvalidTag' , msg , getLineNumberForPosition ( xmlData , i ) ) ;
7878 }
7979
8080 const result = readAttributeStr ( xmlData , i ) ;
8181 if ( result === false ) {
82- return getErrorObject ( 'InvalidAttr' , "Attributes for '" + tagName + "' have open quote." , getLineNumberForPosition ( xmlData , i ) ) ;
82+ return getErrorObject ( 'InvalidAttr' , "Attributes for '" + tagName + "' have open quote." , getLineNumberForPosition ( xmlData , i ) ) ;
8383 }
8484 let attrStr = result . value ;
8585 i = result . index ;
@@ -100,17 +100,17 @@ export function validate(xmlData, options) {
100100 }
101101 } else if ( closingTag ) {
102102 if ( ! result . tagClosed ) {
103- return getErrorObject ( 'InvalidTag' , "Closing tag '" + tagName + "' doesn't have proper closing." , getLineNumberForPosition ( xmlData , i ) ) ;
103+ return getErrorObject ( 'InvalidTag' , "Closing tag '" + tagName + "' doesn't have proper closing." , getLineNumberForPosition ( xmlData , i ) ) ;
104104 } else if ( attrStr . trim ( ) . length > 0 ) {
105- return getErrorObject ( 'InvalidTag' , "Closing tag '" + tagName + "' can't have attributes or invalid starting." , getLineNumberForPosition ( xmlData , tagStartPos ) ) ;
105+ return getErrorObject ( 'InvalidTag' , "Closing tag '" + tagName + "' can't have attributes or invalid starting." , getLineNumberForPosition ( xmlData , tagStartPos ) ) ;
106106 } else if ( tags . length === 0 ) {
107- return getErrorObject ( 'InvalidTag' , "Closing tag '" + tagName + "' has not been opened." , getLineNumberForPosition ( xmlData , tagStartPos ) ) ;
107+ return getErrorObject ( 'InvalidTag' , "Closing tag '" + tagName + "' has not been opened." , getLineNumberForPosition ( xmlData , tagStartPos ) ) ;
108108 } else {
109109 const otg = tags . pop ( ) ;
110110 if ( tagName !== otg . tagName ) {
111111 let openPos = getLineNumberForPosition ( xmlData , otg . tagStartPos ) ;
112112 return getErrorObject ( 'InvalidTag' ,
113- "Expected closing tag '" + otg . tagName + "' (opened in line " + openPos . line + ", col " + openPos . col + ") instead of closing tag '" + tagName + "'." ,
113+ "Expected closing tag '" + otg . tagName + "' (opened in line " + openPos . line + ", col " + openPos . col + ") instead of closing tag '" + tagName + "'." ,
114114 getLineNumberForPosition ( xmlData , tagStartPos ) ) ;
115115 }
116116
@@ -131,10 +131,10 @@ export function validate(xmlData, options) {
131131 //if the root level has been reached before ...
132132 if ( reachedRoot === true ) {
133133 return getErrorObject ( 'InvalidXml' , 'Multiple possible root nodes found.' , getLineNumberForPosition ( xmlData , i ) ) ;
134- } else if ( options . unpairedTags . indexOf ( tagName ) !== - 1 ) {
134+ } else if ( options . unpairedTags . indexOf ( tagName ) !== - 1 ) {
135135 //don't push into stack
136136 } else {
137- tags . push ( { tagName, tagStartPos} ) ;
137+ tags . push ( { tagName, tagStartPos } ) ;
138138 }
139139 tagFound = true ;
140140 }
@@ -148,18 +148,18 @@ export function validate(xmlData, options) {
148148 i ++ ;
149149 i = readCommentAndCDATA ( xmlData , i ) ;
150150 continue ;
151- } else if ( xmlData [ i + 1 ] === '?' ) {
151+ } else if ( xmlData [ i + 1 ] === '?' ) {
152152 i = readPI ( xmlData , ++ i ) ;
153153 if ( i . err ) return i ;
154- } else {
154+ } else {
155155 break ;
156156 }
157157 } else if ( xmlData [ i ] === '&' ) {
158158 const afterAmp = validateAmpersand ( xmlData , i ) ;
159159 if ( afterAmp == - 1 )
160160 return getErrorObject ( 'InvalidChar' , "char '&' is not expected." , getLineNumberForPosition ( xmlData , i ) ) ;
161161 i = afterAmp ;
162- } else {
162+ } else {
163163 if ( reachedRoot === true && ! isWhiteSpace ( xmlData [ i ] ) ) {
164164 return getErrorObject ( 'InvalidXml' , "Extra text at the end" , getLineNumberForPosition ( xmlData , i ) ) ;
165165 }
@@ -170,28 +170,28 @@ export function validate(xmlData, options) {
170170 }
171171 }
172172 } else {
173- if ( isWhiteSpace ( xmlData [ i ] ) ) {
173+ if ( isWhiteSpace ( xmlData [ i ] ) ) {
174174 continue ;
175175 }
176- return getErrorObject ( 'InvalidChar' , "char '" + xmlData [ i ] + "' is not expected." , getLineNumberForPosition ( xmlData , i ) ) ;
176+ return getErrorObject ( 'InvalidChar' , "char '" + xmlData [ i ] + "' is not expected." , getLineNumberForPosition ( xmlData , i ) ) ;
177177 }
178178 }
179179
180180 if ( ! tagFound ) {
181181 return getErrorObject ( 'InvalidXml' , 'Start tag expected.' , 1 ) ;
182- } else if ( tags . length == 1 ) {
183- return getErrorObject ( 'InvalidTag' , "Unclosed tag '" + tags [ 0 ] . tagName + "'." , getLineNumberForPosition ( xmlData , tags [ 0 ] . tagStartPos ) ) ;
184- } else if ( tags . length > 0 ) {
185- return getErrorObject ( 'InvalidXml' , "Invalid '" +
186- JSON . stringify ( tags . map ( t => t . tagName ) , null , 4 ) . replace ( / \r ? \n / g, '' ) +
187- "' found." , { line : 1 , col : 1 } ) ;
182+ } else if ( tags . length == 1 ) {
183+ return getErrorObject ( 'InvalidTag' , "Unclosed tag '" + tags [ 0 ] . tagName + "'." , getLineNumberForPosition ( xmlData , tags [ 0 ] . tagStartPos ) ) ;
184+ } else if ( tags . length > 0 ) {
185+ return getErrorObject ( 'InvalidXml' , "Invalid '" +
186+ JSON . stringify ( tags . map ( t => t . tagName ) , null , 4 ) . replace ( / \r ? \n / g, '' ) +
187+ "' found." , { line : 1 , col : 1 } ) ;
188188 }
189189
190190 return true ;
191191} ;
192192
193- function isWhiteSpace ( char ) {
194- return char === ' ' || char === '\t' || char === '\n' || char === '\r' ;
193+ function isWhiteSpace ( char ) {
194+ return char === ' ' || char === '\t' || char === '\n' || char === '\r' ;
195195}
196196/**
197197 * Read Processing insstructions and skip
@@ -327,25 +327,25 @@ function validateAttributeString(attrStr, options) {
327327 for ( let i = 0 ; i < matches . length ; i ++ ) {
328328 if ( matches [ i ] [ 1 ] . length === 0 ) {
329329 //nospace before attribute name: a="sd"b="saf"
330- return getErrorObject ( 'InvalidAttr' , "Attribute '" + matches [ i ] [ 2 ] + "' has no space in starting." , getPositionFromMatch ( matches [ i ] ) )
330+ return getErrorObject ( 'InvalidAttr' , "Attribute '" + matches [ i ] [ 2 ] + "' has no space in starting." , getPositionFromMatch ( matches [ i ] ) )
331331 } else if ( matches [ i ] [ 3 ] !== undefined && matches [ i ] [ 4 ] === undefined ) {
332- return getErrorObject ( 'InvalidAttr' , "Attribute '" + matches [ i ] [ 2 ] + "' is without value." , getPositionFromMatch ( matches [ i ] ) ) ;
332+ return getErrorObject ( 'InvalidAttr' , "Attribute '" + matches [ i ] [ 2 ] + "' is without value." , getPositionFromMatch ( matches [ i ] ) ) ;
333333 } else if ( matches [ i ] [ 3 ] === undefined && ! options . allowBooleanAttributes ) {
334334 //independent attribute: ab
335- return getErrorObject ( 'InvalidAttr' , "boolean attribute '" + matches [ i ] [ 2 ] + "' is not allowed." , getPositionFromMatch ( matches [ i ] ) ) ;
335+ return getErrorObject ( 'InvalidAttr' , "boolean attribute '" + matches [ i ] [ 2 ] + "' is not allowed." , getPositionFromMatch ( matches [ i ] ) ) ;
336336 }
337337 /* else if(matches[i][6] === undefined){//attribute without value: ab=
338338 return { err: { code:"InvalidAttr",msg:"attribute " + matches[i][2] + " has no value assigned."}};
339339 } */
340340 const attrName = matches [ i ] [ 2 ] ;
341341 if ( ! validateAttrName ( attrName ) ) {
342- return getErrorObject ( 'InvalidAttr' , "Attribute '" + attrName + "' is an invalid name." , getPositionFromMatch ( matches [ i ] ) ) ;
342+ return getErrorObject ( 'InvalidAttr' , "Attribute '" + attrName + "' is an invalid name." , getPositionFromMatch ( matches [ i ] ) ) ;
343343 }
344- if ( ! attrNames . hasOwnProperty ( attrName ) ) {
344+ if ( ! Object . prototype . hasOwnProperty . call ( attrNames , attrName ) ) {
345345 //check for duplicate attribute.
346346 attrNames [ attrName ] = 1 ;
347347 } else {
348- return getErrorObject ( 'InvalidAttr' , "Attribute '" + attrName + "' is repeated." , getPositionFromMatch ( matches [ i ] ) ) ;
348+ return getErrorObject ( 'InvalidAttr' , "Attribute '" + attrName + "' is repeated." , getPositionFromMatch ( matches [ i ] ) ) ;
349349 }
350350 }
351351
0 commit comments