Repository navigation
fix(text): treat authored replace targets as literal text - #394
Open
rahul05ranjan wants to merge 1 commit into
Open
rahul05ranjan wants to merge 1 commit into
rahul05ranjan wants to merge 1 commit into
Conversation
String.prototype.replace/replaceAll interpret $&, $$, $` and $' in a plain replacement string as substitution patterns. The replace expression passed the authored `to` value straight through, so a target like "$& USD" silently rendered as the matched substring instead of the authored text. Pass a replacer function so `to` is always inserted literally, and pin the behavior with a regression test. Co-authored-by: rahul05ranjan <11793455+rahul05ranjan@users.noreply.github.com>
This branch has not been deployed
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.
What happens
The$&, $ $,
replaceexpression passes the authoredtostring as the replacement argument ofString.prototype.replace/replaceAll(packages/text/src/program.ts). JavaScript treats$`` and $' in a plain replacement string as substitution patterns, so a target like"$& USD"` silently renders as the matched substring:renders
price: X USDinstead of the authoredprice: $& USD. $$ collapses to a single$;$and$'splice in the text before/after the match. The corruption is silent becauseverifyTextTemplate` accepts the expression.Fix
Pass a replacer function so the authored
tois always inserted literally — no other change to the expression's semantics.fromwas already treated literally, sotonow matches it.Test
Adds a regression test to$&, $ $, $& $ $ USD` is authored.
packages/text/test/text.test.tspinning authored targets containing$and $' across bothallandfirstmodes. Focused run of that file: 8 tests pass, including the new one; the new test fails on current main withprice: X $ USDwhereprice:Fixes #393