fix: keep the em/strong mask the same length as the source - #4044
Open
Jaybhade wants to merge 1 commit into
Open
fix: keep the em/strong mask the same length as the source#4044Jaybhade wants to merge 1 commit into
Jaybhade wants to merge 1 commit into
Conversation
emStrong and del align maskedSrc with src by slicing the mask from the
end, so every mask must keep the length of the text it replaces. The
escape mask substituted a fixed two-character '++', but anyPunctuation is
/\\([\p{P}\p{S}])/gu and matches astral code points, so an escaped
character above U+FFFF is three code units masked as two.
The mask then ended up shorter than the source, the clip reached one code
unit too far back, and the resulting match.index was applied to src: the
closing delimiter was consumed a code unit late, leaving a stray
delimiter inside the emphasis and, for some inputs, dropping a character
of the document.
Substitute one '+' per code unit instead. '+' is punctuation, so the
delimiter flanking of the masked region is unchanged, and the ASCII case
still produces '++'.
|
@jayesh-keychain is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize 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.
Marked version:
18.0.9(currentmaster, 61a9442)Markdown flavor: CommonMark|GitHub Flavored Markdown
Description
No existing issue that I could find, so here it is in the template's format.
Expectation
A backslash followed by a non-ASCII-punctuation character is literal text, and it should
not affect the emphasis around it:
Both reference implementations agree —
commonmark0.31.2 andmarkdown-it15 (bothalready dev-dependencies here) return exactly that.
Result
The closing delimiter is consumed one code unit late, so a stray
*lands inside theemphasis, the backslash is swallowed, and in the last case a character of the document is
silently deleted.
Any of the 4887 code points in
\p{P}/\p{S}above U+FFFF triggers it — everyemoji is
\p{So}, so\🙂,\💩,\🚀all do, as do astral punctuation likeU+10100
𐄀.What was attempted
Lexer.inlineTokensmasks the source once and hands the mask toemStrong/del, whichalign the two strings by slicing the mask from the end:
That is only correct while
maskedSrc.length === src.length, and every mask is written tohold that invariant —
reflinkSearchandblockSkipboth pad with'a'.repeat(...)computed from the match length, and the
emStrongMaskhook example indocs/USING_PRO.mdteaches extension authors to do the same:The escape mask is the one that doesn't. It substitutes a fixed two-character
'++':anyPunctuationis/\\([\p{P}\p{S}])/gu, and with theuflag that class matches anastral code point — three UTF-16 code units, replaced by two. The mask then ends up
shorter than the source,
slice(-1 * src.length + lLength)reaches one character too farback, and the
match.indexit produces is applied straight tosrc:Fixed by making the substitution as long as what it replaces.
+is punctuation, so thedelimiter-flanking classification of the masked region is unchanged, and for the ASCII
case
'+'.repeat(2) === '++'— byte-identical to today.This is not a regression from #4017; the fixed-width
'++'dates back to the emphasisrework in #1864, and
anyPunctuationbecame Unicode-aware in #2841.Verification
Built
masterand the patch side by side and diffed their output.strings over an alphabet of
* _ ~ \`[ ] ( ) a, space and two astral symbols):303 outputs differ, and in 303/303 the patched build matches the
commonmark + markdown-it consensus. 0 cases where
masterwas right and the patch broke it.\p{P}/\p{S}code point (4887 of them) in four templates × bothgfmmodes = 39,096 cases:
masterdeviates from the reference in 34,209, the patch in 0.\p{P}/\p{S}code point below U+FFFFin the same templates produces byte-identical output before and after — the change only
reaches the astral case.
test/specs/new/em_escaped_astral_punctuation.mdcovers em, strong, del, and theinside-the-span case. It fails on unpatched
lib/and passes with the patch.test:specs(1775),test:unit(190),test:umd,test:cjs,test:typesandtest:lintare all green locally, with one caveat I should be upfront about: three of thequadratic_*timing specs (quadratic_emstrong_delim[0],quadratic_inline_masking[1]and
[2]) exceed their budget on my machine — but they do so on a clean unpatchedmastercheckout too, so it is this laptop and not the patch.quadratic_inline_masking[0],which is the 100k-escape input that exercises exactly the line I changed, passes in both.
I also benchmarked the two builds against all four
quadratic_*masking inputs plus a50k-astral-escape input, and the patched build is within noise of
masteron every one(the callback is invoked once per escape, but
String.prototype.replacewas alreadybuilding a new string either way).
One thing I deliberately left alone:
anyPunctuationmasks\+ any Unicodepunctuation, while the
escapetokenizer only consumes\+ ASCII punctuation, sosequences that are not escapes at all still get masked. That is a pre-existing behavioural
question, length-preserving today, and changing it would alter output for BMP punctuation —
happy to look at it separately if you think it's worth it.
Contributor
Ticket type: L1 - broken by the table in CONTRIBUTING, I think — the output is wrong
against both supported specs and there is no workaround from the caller's side, since the
trigger is ordinary document text. Happy to be corrected to L2.
Committer
In most cases, this should be a different person than the contributor.