fix: adjust FxTwitter facet indices for surrogate-pair emoji#281
Open
Thinkscape wants to merge 1 commit into
Open
fix: adjust FxTwitter facet indices for surrogate-pair emoji#281Thinkscape wants to merge 1 commit into
Thinkscape wants to merge 1 commit into
Conversation
FxTwitter returns facet indices counting Unicode code points (emoji = 1), but JavaScript string operations (indexOf, slice, .length) use UTF-16 code units where surrogate-pair emoji (e.g. 🦞, 🩺, 🔌, 🌐) count as 2. When a tweet contains emoji before a URL or mention facet, the indices are off by the number of preceding surrogate pairs, causing links to be placed at incorrect positions in the text. This produces broken HTML like: <p>Small maintenance relea<a href="...">se:<br>...</a>oy7V</p> instead of: <p>Small maintenance release:<br><a href="...">https://t.co/...</a></p> Fix: add codePointToUtf16Index() and adjustFacetIndicesToUtf16() helpers to convert facet indices from code-point space to UTF-16 code units before applying markers. Fast-path returns early when no surrogate pairs exist.
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.
Problem
FxTwitter API returns facet indices counting Unicode code points (emoji = 1), but
renderTweet()andapplyFacets()treat them as UTF-16 code units (emoji in the supplementary multilingual plane are surrogate pairs that count as 2 in JavaScript strings).When a tweet contains emoji (🦞, 🩺, 🔌, 🌐, etc.) before a URL or mention facet, the facet markers are placed at incorrect positions. This produces broken HTML like:
After Turndown conversion, this becomes garbled markdown:
Real-world example
Tweet: https://x.com/openclaw/status/2052096219233587451
The tweet contains 4 emoji before the t.co URL, causing a 4-code-unit offset between FxTwitter facet indices and JavaScript string positions.
Fix
Added two helper methods to
XOembedExtractor:codePointToUtf16Index()— converts a single code-point index to its UTF-16 code-unit equivalent by iterating over characters and counting surrogate pairsadjustFacetIndicesToUtf16()— applies the conversion to all facet indices, with a fast-path returning early when the text contains no surrogate pairsThese are called in
renderTweet()before facets are passed toapplyFacets().Tests
Added
tests/x-oembed-surrogates.test.tswith 3 test cases:All 284 existing tests continue to pass.