Skip to content

fix(axis, radar, geo): preserve literal replacement characters in string formatters - #21847

Open
farisbasha wants to merge 1 commit into
apache:releasefrom
farisbasha:fix-formatter-replacement-chars
Open

farisbasha wants to merge 1 commit into
apache:releasefrom
farisbasha:fix-formatter-replacement-chars

Conversation

@farisbasha

Copy link
Copy Markdown

Brief Information

This pull request is in the type of:

  • bug fixing
  • new feature
  • others

What does this PR do?

Shows $&, $$, $` and $' literally in category axis labels, radar indicator names and geo region labels that use a string formatter.

Fixed issues

None filed. Same bug class as #21751 (formatTpl) and #21787 (aria label).

Details

Before: What was the problem?

Three string formatters pass the user's text as the replacement string of String.prototype.replace, so JavaScript expands the special $ patterns in it:

  • src/coord/axisHelper.ts makeLabelFormatter: axisLabel.formatter such as 'Label: {value}'
  • src/coord/radar/RadarModel.ts: radar.axisName.formatter such as 'Name: {value}'
  • src/coord/geo/GeoModel.ts getFormattedLabel: geo.label.formatter such as 'Geo: {a}'

For example, with axisLabel.formatter: 'Label: {value}!':

category name shown before
Item $& Label: Item {value}!
Item $$ Label: Item $!
Item $` Label: Item Label: !
Item $' Label: Item !!

After: How does it behave after the fixing?

The value is passed through a replacer function (() => value), the same way #21751 and #21787 fixed formatTpl and the aria label, so the text is inserted as is. Nothing else changes.

New unit test test/ut/spec/component/stringFormatter.test.ts checks the three formatters with $&, $$, $`, $' and plain text.

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/component/stringFormatter.test.ts

Merging options

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

Other information

I used an AI assistant to find this and write the change. The new test runs in CI; I did not run the test suite locally.

…ing formatters

The string formatters of the category axis label (`{value}`), the radar
indicator name (`{value}`) and the geo region label (`{a}`) passed the
user's text as the replacement string of `String.prototype.replace`.
So `$&`, `$$`, `` $` `` and `$'` inside a category, indicator or region
name were expanded instead of being shown literally.

Use a replacer function, as apache#21751 did for formatTpl and apache#21787 did for
the aria label.
@echarts-bot

echarts-bot Bot commented Nov 18, 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.

@Raman-Katiyar

Copy link
Copy Markdown

Nice fix — using a function as the replacement avoids the $-pattern substitution, and the new tests cover the three sites well. I noticed the same String.replace('{...}', value) pattern with a plain-string replacement is still used in a few places where the substituted text is user-controlled, so the same mangling can still happen there. The clearest one is src/component/legend/LegendView.ts:461: content = formatter.replace('{name}', name ...) — name is the series name, so a series named e.g. Sales $& Q1 would still render with {name} substituted back in. The same pattern is also in src/util/format.ts (formatTplSimple), src/component/dataZoom/SliderZoomView.ts:1159 and src/component/axisPointer/viewHelper.ts:184, where the value can be a category/axis label. Would it make sense to apply the same () => ... form in those places too, or keep this PR scoped to axis/radar/geo?

@farisbasha

Copy link
Copy Markdown
Author

Thanks for looking and for the list! I'd prefer to keep this PR scoped to the axis/radar/geo label formatters, to match the earlier merged fixes (#21751, #21787) and keep it easy to review. If the maintainers want, I'm happy to send the legend / formatTplSimple / dataZoom / axisPointer sites as a separate follow-up PR.

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.

2 participants