fix: reject self-closing tags as multiple root nodes in validator#846
Open
chatman-media wants to merge 1 commit into
Open
Conversation
XMLValidator only detected multiple root elements when the roots had child content. A self-closing root element (e.g. <a/><b/>) bypassed the check because the self-closing branch never marked the document root as reached, so neither the duplicate-root nor the trailing-text guard fired. Per the XML 1.0 spec a well-formed document has exactly one root element, so a self-closing tag at depth 0 completes the root. Mark reachedRoot in the self-closing branch and reject a second root element there.
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.
Problem
XMLValidator.validateonly detects multiple root elements when the roots contain child content. A self-closing root element silently passes validation, even though the XML 1.0 spec requires exactly one root element:This is the self-closing counterpart of #207 / #210, which only fixed the paired-tag case.
Root cause
In
src/validator.js, thereachedRootflag is set totrueonly in the closing-tag branch when the tag stack empties. The duplicate-root guard (Multiple possible root nodes found.) and the trailing-text guard (Extra text at the end) both depend on that flag.A self-closing tag is handled in its own branch and never sets
reachedRoot, so a self-closing root never marks the document as complete. Any node that follows it therefore escapes both guards.Fix
In the self-closing branch, when the tag is at depth 0 (
tags.length === 0) it is a complete root element. MarkreachedRoot = true, and if the root level had already been reached, reject the second root with the existingMultiple possible root nodes found.error. This reuses the existing error codes and lets the trailing-text guard fire too.Tests
Added to
spec/validator_spec.jsunder the existingshould not validate XML documents with multiple root nodesblock. The new cases fail without the fix and pass with it; the full suite (319 specs) stays green, and single self-closing roots (<a/>,<a/>followed by whitespace/comments/PIs) remain valid.