fix(parser): incorrect parsing of hexadecimal NCRs - #474
Open
grnch wants to merge 2 commits into
Open
Conversation
When you have a decimal NCR (Numeric Character Reference) in your text:
before Ӓ after
it gets parsed like this:
TEXT => "before"
SEA_WS => " "
CharRef => "Ӓ"
SEA_WS => " "
TEXT => "after"
However, if you have a hexadecimal NCR:
before � after
it gets parsed like this instead:
TEXT => "before"
SEA_WS => " "
CharRef => ""
TEXT => "23ABC;"
SEA_WS => " "
TEXT => "after"
Note the extra `TEXT => "23ABC;"` token after the CharRef, it shouldn't
be there. The NCR got split into two tokens, instead of emitted as one.
The root cause was an apparent typo in the regex for CharRef tokens. It
was missing a plus sign and a semicolon on the hexadecimal side of the
alternation, which caused it to match only the first hexadecimal digit,
instead of the entire NCR all the way to the semicolon.
I added a hex NCR to the `valid/entity-char-ref` test case and verified
that the full CharRef token appeared correctly in the output before
updating the snapshot. I also checked that all subsequent offsets were
shifted by 15, which corresponds to the 14 chars added plus the newline.
Finally, I added a new `invalild/entity-char-ref` test case. The old
regex would produce some CharRef tokens on this text, even though none
of the NCRs in it were valid. With this fix, the text output is now
"chardata" only, no CharRef tokens.
When editing test fixtures and looking at the resulting test output, the diffs might get large due to a bunch of irrelevant offset changes. By the time you get to the interesting part of the diff, it gets cut off due to the 8k limit.
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.
When you have a decimal NCR (Numeric Character Reference) in your text:
it gets parsed like this:
However, if you have a hexadecimal NCR:
it gets parsed like this instead:
Note the extra
TEXT => "23ABC;"token after the CharRef, it shouldn't be there. The NCR got split into two tokens, instead of emitted as one.The root cause was an apparent typo in the regex for CharRef tokens. It was missing a plus sign and a semicolon on the hexadecimal side of the alternation, which caused it to match only the first hexadecimal digit, instead of the entire NCR all the way to the semicolon.
I added a hex NCR to the
valid/entity-char-reftest case and verified that the full CharRef token appeared correctly in the output before updating the snapshot. I also checked that all subsequent offsets were shifted by 15, which corresponds to the 14 chars added plus the newline.Finally, I added a new
invalild/entity-char-reftest case. The old regex would produce some CharRef tokens on this text, even though none of the NCRs in it were valid. With this fix, the text output is now "chardata" only, no CharRef tokens.