Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/validator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<xml/><xml2/>`, {
InvalidXml: 'Multiple possible root nodes found.'
});
});

it('when a self closing root node is followed by a paired node', () => {
validate(`<xml/><xml2></xml2>`, {
InvalidXml: 'Multiple possible root nodes found.'
});
});

it('when a paired root node is followed by a self closing node', () => {
validate(`<xml></xml><xml2/>`, {
InvalidXml: 'Multiple possible root nodes found.'
});
});

it('when a self closing root node is followed by trailing text', () => {
validate(`<xml/>extra`, {
InvalidXml: 'Extra text at the end'
});
});
});

describe("should report correct line numbers for unclosed tags", () => {
Expand Down
8 changes: 8 additions & 0 deletions src/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down