Repository navigation
Conversation
… a text node
With trimValues: false, the whitespace between the XML declaration (or the
DOCTYPE) and a comment or processing instruction was saved as a text node of
the document itself: `[{"#text": "\n"}, ...]` with preserveOrder, and a
`"#text": "\n"` property next to the root element without it. The same
whitespace before the root element was already dropped, because the
opening-tag branch skips text whose parent is the document node while the
comment, PI, CDATA and closing-tag branches save it unconditionally.
Whitespace outside the root element is never part of the document (XML 1.0
production [27] Misc), so saveTextToParentTag now drops whitespace-only text
whose parent is the document node. Non-whitespace text outside the root is
still saved as before, so the lenient handling of malformed input such as
`</a>text</b>` is unchanged.
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.
Purpose / Goal
With
trimValues: false, the whitespace between the XML declaration (or the DOCTYPE) and a comment or processing instruction is saved as a text node of the document itself, while the same whitespace before the root element is dropped.Input
Actual output
Expected output
Whitespace outside the root element is never part of the document (XML 1.0, production [27]
Misc), and the parser already drops it when the next thing it meets is the root element: the opening-tag branch skips text whose parent is the document node (currentNode.tagname !== '!xml'), but the comment, PI, CDATA and closing-tag branches callsaveTextToParentTagunconditionally. WithpreserveOrderthe stray{"#text": "\n"}shifts every following index, and without it the result object gets a"#text"property next to the root element. The same happens for whitespace after the root element when it is followed by a comment.Found by comparing the parser's output with xmldom on generated, libxml2-validated documents; this was the only divergence left once the documented ones (numeric character references, attribute value normalization) were set aside.
Fix
saveTextToParentTagnow returns without saving when the parent is the document node and the text is whitespace only. Non-whitespace text outside the root element is still saved as before, so the lenient handling of malformed input from #861 (</a>text</b>→{"#text": "text"}) is unchanged, andtrimValues: trueis unaffected because it already trimmed that text to nothing.The spec covers the declaration, a comment and a PI in the prolog and a comment after the root element, in both
preserveOrderand plain mode, and checks the whitespace inside the root element is still kept.Performance
Parsing
spec/assets/midsize.xml20 times withtrimValues: false(best of 5 rounds, three runs each, Node 26): 9184 / 9519 / 9458 ms before and 9763 / 8998 / 9271 ms after, so the extra check is within the run-to-run noise. (node benchmark/XmlParser.mjsneedsbenchmark,xml2jsandxmlbuilder2, which are not in the dev dependencies.)Type