Skip to content

fix(format): replace every occurrence of a variable in string formatter templates. close #21843 - #21844

Open
0z5a wants to merge 1 commit into
apache:releasefrom
0z5a:fix/formattpl-replace-all
Open

0z5a wants to merge 1 commit into
apache:releasefrom
0z5a:fix/formattpl-replace-all

Conversation

@0z5a

@0z5a 0z5a commented Nov 15, 2026

Copy link
Copy Markdown

Brief Information

This pull request is in the type of:

  • bug fixing
  • new feature
  • others

What does this PR do?

Makes formatTpl and formatTplSimple replace every occurrence of a template variable, not only the first one.

Fixed issues

Details

Before: What was the problem?

formatTpl used tpl.replace(string, value), which only replaces the first match. A label formatter such as '{b}: {c} | {b}: {c}' rendered Mon: 120 | {b}: {c}, and multi-series templates that repeat {a0}/{c1} kept the later copies as literal text. formatTplSimple (calendar and matrix label formatters) had the same problem. {@xxx} dimension variables and time axis tokens were already replaced at every occurrence.

After: How does it behave after the fixing?

Both functions use a small replaceAll helper (str.split(search).join(value + '')), so '{b}: {c} | {b}: {c}' renders Mon: 120 | Mon: 120. The inserted value stays literal, so $ sequences in names are still kept as they are (same result as #21751). Unknown variables and out-of-range series indexes are left unchanged, as before.

Document Info

One of the following should be checked.

  • This PR doesn't relate to document changes
  • The document should be updated later
  • The document changes have been made in apache/echarts-doc#xxx

Misc

Security Checking

  • This PR uses security-sensitive Web APIs.

ZRender Changes

  • This PR depends on ZRender changes (ecomfe/zrender#xxx).

Related test cases or examples to use the new APIs

test/ut/spec/util/format.test.ts covers repeated {b}/{c} and {a}/{a0}, repeated indexed variables across two series, HTML encoding, unknown variables, formatTplSimple, and a bar series label formatter. The new cases fail without the fix and pass with it. The full unit test suite, npx tsc --noEmit and ESLint on the changed files pass.

Merging options

  • Please squash the commits into a single one when merging.

Other information

Based on release, which already has the replacers from #21751. The helper takes the value as a string, and the inserted text stays literal, so the $ tests from #21751 still pass. On release, the full unit test suite (231 tests), npx tsc --noEmit and ESLint on the changed files pass.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

…er templates

formatTpl and formatTplSimple used String.prototype.replace with a string
pattern, which only replaces the first match, so a template that uses
the same variable twice (e.g. '{b}: {c} | {b}: {c}') kept the second
copy as literal text. Replace all occurrences with a split/join helper
that inserts the value literally.
@echarts-bot

echarts-bot Bot commented Nov 15, 2026

Copy link
Copy Markdown

Thanks for your contribution!
The community will review it ASAP. In the meanwhile, please checkout the coding standard and Wiki about How to make a pull request.

Please DO NOT commit the files in dist, i18n, and ssr/client/dist folders in a non-release pull request. These folders are for release use only.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant