Repository navigation
fix: validator accepts multiple root nodes when a root is self closing - #879
Open
yu2971512385-ui wants to merge 1 commit into
Open
yu2971512385-ui wants to merge 1 commit into
yu2971512385-ui wants to merge 1 commit into
Conversation
reachedRoot was only set when a closing tag emptied the tag stack, and
the 'Multiple possible root nodes found.' check only ran on the branch
for paired opening tags. A self closing tag at the top level therefore
neither ended the root level nor was rejected after one, so documents
with two roots validated as long as either root was self closing:
XMLValidator.validate('<xml/><xml2/>') // true
XMLValidator.validate('<xml></xml><xml2/>') // true
XMLValidator.validate('<xml/>extra') // true
while the paired forms of the same documents are already rejected.
Treat a self closing tag with an empty tag stack as a root node: report
the multiple-root error if one was already seen, otherwise mark the root
level as reached.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
XMLValidatoralready rejects documents with more than one root node, but the check is skipped whenever one of the roots is self closing:All of these are not well-formed XML — a document has exactly one root element — and the validator's own error messages (
Multiple possible root nodes found.,Extra text at the end) exist for precisely these cases.Root cause
In
src/validator.js:reachedRoot = trueis only assigned in the closing-tag branch, when popping a tag empties the stack. A self closing tag never reaches that branch, so it never ends the root level — hence'<xml/>extra'and'<xml/><xml2/>'pass.Multiple possible root nodes found.check lives only in the branch for a paired opening tag, so a self closing tag after a completed root is never checked — hence'<xml></xml><xml2/>'passes.Fix
In the self closing branch, once the attributes validate, treat a tag with an empty stack as a root node: report the multiple-root error if a root was already seen, otherwise mark the root level as reached. Nested self closing tags (
<test><nested/></test>) are unaffected because the stack is not empty there.Tests
Added to
spec/validator_spec.js:4 of the new specs fail without the source change. The full suite passes with it (
npx jasmine spec/*spec.js→ 338 specs, 0 failures, 2 pending).