From 9d0f04afa32815e4785170d28b580617ab0b89fc Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Thu, 7 Nov 2019 15:19:38 +0200 Subject: [PATCH] Fix validation of self-closing tag followed by text If there is no space before the closing tag, and the tag is followed by text, validation failed:
text
--- spec/validator_spec.js | 32 +++++++++++++++++++++++++++----- src/validator.js | 2 +- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/spec/validator_spec.js b/spec/validator_spec.js index 27a25482..a4a84fad 100644 --- a/spec/validator_spec.js +++ b/spec/validator_spec.js @@ -50,19 +50,41 @@ describe("XMLParser", function() { }); it("should validate self closing tags", function() { - const xmlData = ""; + const xmlData = "texttext"; const result = validator.validate(xmlData); expect(result).toBe(true); }); - it("should not validate self closing tags", function() { - const xmlData = ""; - const expected = {code: "InvalidAttr", msg: "boolean attribute tag is not allowed."}; + it("should not consider these as self closing tags", function() { + let xmlData = ""; + let expected = {code: "InvalidAttr", msg: "boolean attribute tag is not allowed."}; - const result = validator.validate(xmlData).err; + let result = validator.validate(xmlData).err; //console.log(JSON.stringify(result,null,4)); expect(result).toEqual(expected); + + xmlData = ""; + expected = {code: "InvalidAttr", msg: "attribute / has no space in starting."}; + + result = validator.validate(xmlData).err; + expect(result).toEqual(expected); + }); + + /* + it("should correctly identify self closing tags", function() { + let xmlData = ""; + let expected = {code: "InvalidTag", msg: "Tag in/valid is an invalid name."}; + + let result = validator.validate(xmlData).err; + expect(result).toEqual(expected); + + xmlData = ""; + expected = {code: "InvalidTag", msg: "Tag in#valid is an invalid name."}; + + result = validator.validate(xmlData).err; + expect(result).toEqual(expected); }); + */ it("should not validate xml string when closing tag is different", function() { const xmlData = ""; diff --git a/src/validator.js b/src/validator.js index c0cc83cf..0b8520f4 100644 --- a/src/validator.js +++ b/src/validator.js @@ -66,7 +66,7 @@ exports.validate = function(xmlData, options) { if (tagName[tagName.length - 1] === '/') { //self closing tag without attributes tagName = tagName.substring(0, tagName.length - 1); - continue; + i--; } if (!validateTagName(tagName, regxTagName)) { return {err: {code: 'InvalidTag', msg: 'Tag ' + tagName + ' is an invalid name.'}};