Skip to content

Commit 1e61166

Browse files
committed
validate xml processing instruction tags #62
1 parent 113f978 commit 1e61166

2 files changed

Lines changed: 36 additions & 11 deletions

File tree

spec/validator_spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,16 @@ describe("XMLParser", function () {
370370
var result = validator.validate(xmlData).err;
371371
expect(result).toEqual(expected);
372372
});
373+
374+
it("should validate XML PIs", function () {
375+
var xmlData = '<?xml version="1.0"?>'
376+
+'<?mso-contentType?>'
377+
+'<h1></h1>'
378+
+'<?mso-contentType something="val"?>';
379+
380+
var result = validator.validate(xmlData);
381+
expect(result).toBe(true);
382+
});
373383

374384
});
375385

src/validator.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
142157
function 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

Comments
 (0)