Skip to content

Trim the same whitespace as JavaScript in parse() - #25

Open
emincan2000 wants to merge 1 commit into
executablebooks:masterfrom
emincan2000:js-whitespace
Open

emincan2000 wants to merge 1 commit into
executablebooks:masterfrom
emincan2000:js-whitespace

Conversation

@emincan2000

Copy link
Copy Markdown

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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant