From b48ab66b13355062233ced2e2e893ab910f76c8b Mon Sep 17 00:00:00 2001 From: RyosukeFukatani Date: Tue, 19 Mar 2024 11:25:32 +0900 Subject: [PATCH 1/2] fix validator bag if tag is not opened. --- spec/validator_spec.js | 6 ++++++ src/validator.js | 2 ++ 2 files changed, 8 insertions(+) diff --git a/spec/validator_spec.js b/spec/validator_spec.js index ea07bc14..47fa1c31 100644 --- a/spec/validator_spec.js +++ b/spec/validator_spec.js @@ -129,6 +129,12 @@ describe("XML Validator", function () { }); }); + it("should not validate xml with unexpected closing tag", function () { + validate("", { + InvalidTag: "Closing tag 'tag1' has not been opened." + }); + }); + it("should validate xml with comment", function () { validate("1val"); }); diff --git a/src/validator.js b/src/validator.js index 11b051b1..3b1b2efb 100644 --- a/src/validator.js +++ b/src/validator.js @@ -103,6 +103,8 @@ exports.validate = function (xmlData, options) { return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' doesn't have proper closing.", getLineNumberForPosition(xmlData, i)); } else if (attrStr.trim().length > 0) { return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' can't have attributes or invalid starting.", getLineNumberForPosition(xmlData, tagStartPos)); + } else if (tags.length === 0) { + return getErrorObject('InvalidTag', "Closing tag '"+tagName+"' has not been opened.", getLineNumberForPosition(xmlData, tagStartPos)); } else { const otg = tags.pop(); if (tagName !== otg.tagName) { From b03c9681232aaa5ee7e8280c87ca4916045fec74 Mon Sep 17 00:00:00 2001 From: RyosukeFukatani Date: Sat, 23 Mar 2024 10:13:54 +0900 Subject: [PATCH 2/2] fix test --- spec/validator_spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/validator_spec.js b/spec/validator_spec.js index 47fa1c31..c282415f 100644 --- a/spec/validator_spec.js +++ b/spec/validator_spec.js @@ -130,8 +130,8 @@ describe("XML Validator", function () { }); it("should not validate xml with unexpected closing tag", function () { - validate("", { - InvalidTag: "Closing tag 'tag1' has not been opened." + validate("", { + InvalidTag: "Closing tag 'rootNode' has not been opened." }); });