fix: parse closing tags without matching opening tags (#149)#244
Open
maruthang wants to merge 2 commits into
Open
fix: parse closing tags without matching opening tags (#149)#244maruthang wants to merge 2 commits into
maruthang wants to merge 2 commits into
Conversation
Collaborator
|
I worry that this change will break existing clients. Maybe best to make this conditional. |
Guard the creation of nodes for closing tags without a matching opening tag behind a new createNodesForOrphanEndTags option so the default parse output is unchanged (orphan end tags remain discarded). Thread the option through HTMLParser.parse/parseDocument and expose it on the public parseHTMLDocument API. Revert MissingTags expectations to the original behavior and assert the issue microsoft#149 output only on the opt-in path.
Contributor
Author
|
Good point, thanks. I've reworked this so the new behavior is fully opt-in and the default parse output is unchanged — orphan closing tags (those with no matching opening tag) are still discarded by default, so existing clients see no difference. Callers who want the orphan nodes can now pass an option: |
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.
Summary
Fixes #149
Bug: The HTML parser silently discarded closing tags that had no matching opening tag (e.g.,
</div>appearing without a prior<div>), making it impossible for consumers to detect orphan closing tags.Root Cause: In
htmlParser.ts, theEndTagClosebranch only handled the case where a matching ancestor was found. When no match existed, the closing tag was simply ignored with no node created.Fix: Added an
elsebranch that creates an "orphan" node for unmatched closing tags. The node hasendTagStartset but nostartTagEnd, allowing consumers to detect orphan closing tags by checkingnode.endTagStart !== undefined && node.startTagEnd === undefined.Changes
src/parser/htmlParser.ts: When no matching opening tag is found for a closing tag, create an orphan node withendTagStartset and add it as a child of the current node.src/test/parser.test.ts: Updated existingMissingTagstest expectations and added new regression tests for standalone closing tags, mismatched closing tags, and multiple orphan closing tags.Testing
src/test/parser.test.ts(Closing tag without opening tag (issue #149)) that verify orphan closing tags are properly represented in the parse tree.MissingTagstest expectations to match the new behavior.