Repository navigation
Trim the same whitespace as JavaScript in parse() - #25
Open
emincan2000 wants to merge 1 commit into
Open
emincan2000 wants to merge 1 commit into
emincan2000 wants to merge 1 commit into
Conversation
str.strip() and \s also remove U+001C to U+001F and U+0085, which
String.prototype.trim() and the JS \s keep, and they miss U+FEFF, which
JS removes. So parse("/u\x85") lost the trailing character and
markdown-it-py rendered such links differently from markdown-it.
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.
parse() trims its input with str.strip(), and the simple path regex uses \s. Python treats U+001C to U+001F and U+0085 as whitespace in both cases, while JavaScript's String.prototype.trim() and \s don't. Python also misses U+FEFF, which JavaScript does strip. This means parse("/u\x85").pathname is "/u" here but "/u\x85" in mdurl 2.1.0. It also means markdown-it-py renders x with href="/u", where markdown-it gives "/u%C2%85".
The fix adds a JS_WHITESPACE constant with the JavaScript set and uses it in both places. I checked it against node: it contains exactly the BMP characters that /\s/ matches, and trim() removes each of them.
I compared parse, encode and decode against the JS package on 50k random strings. The parse mismatches dropped from 6236 to 37. encode and decode already matched. The new tests fail on main, and the full suite passes (193). markdown-it-py's test suite also passes with this change.
All the remaining parse mismatches contain U+2028. JS regex "." stops at line terminators, which is a separate difference I left alone.