Skip to content

Commit

Permalink
validate xml processing instruction tags #62
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Mar 5, 2018
1 parent 113f978 commit 1e61166
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
10 changes: 10 additions & 0 deletions spec/validator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,16 @@ describe("XMLParser", function () {
var result = validator.validate(xmlData).err;
expect(result).toEqual(expected);
});

it("should validate XML PIs", function () {
var xmlData = '<?xml version="1.0"?>'
+'<?mso-contentType?>'
+'<h1></h1>'
+'<?mso-contentType something="val"?>';

var result = validator.validate(xmlData);
expect(result).toBe(true);
});

});

Expand Down
37 changes: 26 additions & 11 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,9 @@ exports.validate = function(xmlData, options){

i++;
if(xmlData[i] === "?"){
if(i !== 1){
return {err : { code : "InvalidXml", msg : "XML declaration allowed only at the start of the document."}};
}else{
//read until ?> is found
for(;i<xmlData.length;i++){
if(xmlData[i] == "?" && xmlData[i+1] == ">"){
i++;
break;
}
}
i = readPI(xmlData,++i);
if(i.err){
return i;
}
}else if(xmlData[i] === "!"){
i = readCommentAndCDATA(xmlData,i);
Expand Down Expand Up @@ -138,7 +131,29 @@ exports.validate = function(xmlData, options){

return true;
}

/**
* Read Processing insstructions and skip
* @param {*} xmlData
* @param {*} i
*/
function readPI(xmlData,i){
var start = i;
for(;i<xmlData.length;i++){
if(xmlData[i] == "?" || xmlData[i] == " "){//tagname
var tagname = xmlData.substr(start,i-start);
if(i > 5 && tagname === "xml"){
return {err : { code : "InvalidXml", msg : "XML declaration allowed only at the start of the document."}};
}else if(xmlData[i] == "?" && xmlData[i+1] == ">"){
//check if valid attribut string
i++;
break;
}else{
continue;
}
}
}
return i;
}
function readCommentAndCDATA(xmlData,i){
if(xmlData.length > i+5 && xmlData[i+1] === "-" && xmlData[i+2] === "-"){//comment
for(i+=3;i<xmlData.length;i++){
Expand Down

0 comments on commit 1e61166

Please sign in to comment.