Repository navigation
fix(js): don't split surrogate pairs - #1403
Merged
Merged
Conversation
tushuhei
approved these changes
Sep 29, 2026
kwy404
added a commit
to kwy404/kwy404
that referenced
this pull request
Sep 29, 2026
tushuhei
pushed a commit
that referenced
this pull request
Oct 5, 2026
Parser.parse walks the sentence by UTF-16 code units, so it can put a boundary between the two halves of a surrogate pair and cut a character outside the BMP (emoji, or CJK Extension B characters like 𠮷) in two. This skips those positions, like the JavaScript parser does since #1403.
1 task
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.
Parser.parseBoundarieswalks the sentence by UTF-16 code units, so it can return a boundary between the two halves of a surrogate pair, which cuts a character outside the BMP (emoji, or CJK Extension B characters like𠮷) in two. With the default models,loadDefaultSimplifiedChineseParser().parse('我爱\u{1F363}')returns['我', '爱\ud83c', '\udf63'], andloadDefaultTraditionalChineseParser().translateHTMLString('吉野家の𠮷')puts a ZWSP between\ud842and\udfb7, so the browser renders two replacement characters instead of𠮷. The Python version does not have this problem because it iterates by code points. This is related to the UTF-16 discussion in #504.This skips the positions that fall inside a surrogate pair, detected with
codePointAt(i - 1) > 0xffff. Text without surrogate pairs gets exactly the same boundaries as before, andparseandHTMLProcessorare both fixed since they useparseBoundaries.Added a test to the
Parser.parsetests with a model that separates everywhere:parse('𠮷野家')returns['\ud842', '\udfb7', '野', '家']before the change and['𠮷', '野', '家']after.Tested with
npm test(Node and Chromium) andnpm run lint.