From a96b96807207bdde0c4ce6cc31841fda936b4000 Mon Sep 17 00:00:00 2001 From: RyosukeFukatani <88311926+RyosukeFukatani@users.noreply.github.com> Date: Sun, 24 Mar 2024 18:09:54 +0900 Subject: [PATCH] fix: validator bag when closing tag is not opened. (#647) * fix validator bag if tag is not opened. * fix test --- 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..c282415f 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 'rootNode' 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) {