@@ -34,16 +34,9 @@ exports.validate = function(xmlData, options){
3434
3535 i ++ ;
3636 if ( xmlData [ i ] === "?" ) {
37- if ( i !== 1 ) {
38- return { err : { code : "InvalidXml" , msg : "XML declaration allowed only at the start of the document." } } ;
39- } else {
40- //read until ?> is found
41- for ( ; i < xmlData . length ; i ++ ) {
42- if ( xmlData [ i ] == "?" && xmlData [ i + 1 ] == ">" ) {
43- i ++ ;
44- break ;
45- }
46- }
37+ i = readPI ( xmlData , ++ i ) ;
38+ if ( i . err ) {
39+ return i ;
4740 }
4841 } else if ( xmlData [ i ] === "!" ) {
4942 i = readCommentAndCDATA ( xmlData , i ) ;
@@ -138,7 +131,29 @@ exports.validate = function(xmlData, options){
138131
139132 return true ;
140133}
141-
134+ /**
135+ * Read Processing insstructions and skip
136+ * @param {* } xmlData
137+ * @param {* } i
138+ */
139+ function readPI ( xmlData , i ) {
140+ var start = i ;
141+ for ( ; i < xmlData . length ; i ++ ) {
142+ if ( xmlData [ i ] == "?" || xmlData [ i ] == " " ) { //tagname
143+ var tagname = xmlData . substr ( start , i - start ) ;
144+ if ( i > 5 && tagname === "xml" ) {
145+ return { err : { code : "InvalidXml" , msg : "XML declaration allowed only at the start of the document." } } ;
146+ } else if ( xmlData [ i ] == "?" && xmlData [ i + 1 ] == ">" ) {
147+ //check if valid attribut string
148+ i ++ ;
149+ break ;
150+ } else {
151+ continue ;
152+ }
153+ }
154+ }
155+ return i ;
156+ }
142157function readCommentAndCDATA ( xmlData , i ) {
143158 if ( xmlData . length > i + 5 && xmlData [ i + 1 ] === "-" && xmlData [ i + 2 ] === "-" ) { //comment
144159 for ( i += 3 ; i < xmlData . length ; i ++ ) {
0 commit comments