From 1e611660f546c355bfe50c89e01bd2bb50dfa82c Mon Sep 17 00:00:00 2001 From: Amit Gupta Date: Sat, 3 Mar 2018 13:16:23 +0530 Subject: [PATCH] validate xml processing instruction tags #62 --- spec/validator_spec.js | 10 ++++++++++ src/validator.js | 37 ++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/spec/validator_spec.js b/spec/validator_spec.js index 38ba77a3..5d4faa39 100644 --- a/spec/validator_spec.js +++ b/spec/validator_spec.js @@ -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 = '' + +'' + +'

' + +''; + + var result = validator.validate(xmlData); + expect(result).toBe(true); + }); }); diff --git a/src/validator.js b/src/validator.js index 911c20e6..d3634ad2 100644 --- a/src/validator.js +++ b/src/validator.js @@ -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"){ - i++; - break; - } - } + i = readPI(xmlData,++i); + if(i.err){ + return i; } }else if(xmlData[i] === "!"){ i = readCommentAndCDATA(xmlData,i); @@ -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 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