diff --git a/spec/validator_spec.js b/spec/validator_spec.js index d1cf43bc..b256a343 100644 --- a/spec/validator_spec.js +++ b/spec/validator_spec.js @@ -403,6 +403,30 @@ describe("should not validate XML documents with multiple root nodes", () => { InvalidXml: 'Multiple possible root nodes found.' }, 5); }); + + it('when a self closing root node is followed by another self closing node', () => { + validate(``, { + InvalidXml: 'Multiple possible root nodes found.' + }); + }); + + it('when a self closing root node is followed by a paired node', () => { + validate(``, { + InvalidXml: 'Multiple possible root nodes found.' + }); + }); + + it('when a paired root node is followed by a self closing node', () => { + validate(``, { + InvalidXml: 'Multiple possible root nodes found.' + }); + }); + + it('when a self closing root node is followed by trailing text', () => { + validate(`extra`, { + InvalidXml: 'Extra text at the end' + }); + }); }); describe("should report correct line numbers for unclosed tags", () => { diff --git a/src/validator.js b/src/validator.js index 277225c2..3e26a29a 100644 --- a/src/validator.js +++ b/src/validator.js @@ -91,6 +91,14 @@ export function validate(xmlData, options) { const isValid = validateAttributeString(attrStr, options); if (isValid === true) { tagFound = true; + //a self closing tag at the root level is a complete root element + if (tags.length === 0) { + //if the root level has already been reached, this is a second root + if (reachedRoot === true) { + return getErrorObject('InvalidXml', 'Multiple possible root nodes found.', getLineNumberForPosition(xmlData, tagStartPos)); + } + reachedRoot = true; + } //continue; //text may presents after self closing tag } else { //the result from the nested function returns the position of the error within the attribute