Fix adjustFormat dropping d3-format specs that start with a sign flag - #7900
Open
TemRevil wants to merge 4 commits into
Open
Fix adjustFormat dropping d3-format specs that start with a sign flag#7900TemRevil wants to merge 4 commits into
TemRevil wants to merge 4 commits into
Conversation
A d3-format specifier beginning with a sign flag (+, -, (, space) was
silently ignored: adjustFormat prepended a trim tilde to the whole
string, producing an invalid spec like ~+.2f. d3.format then threw,
numberFormat fell back to noFormat, and the raw unformatted number was
shown (e.g. %{y:+.2f} rendered 12.3456789 instead of +12.35).
Skip past an optional leading sign flag and symbol ($, #) before the
trim heuristic and reattach it, so the tilde is only added in a valid
position. Formats without such a prefix are unaffected.
Fixes plotly#7897
Contributor
|
Thanks for the PR! I'll take a look and follow up. |
camdecoster
requested changes
Jul 27, 2026
camdecoster
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the fix! I'd like to make some changes to avoid some unintended consequences of this update.
| // symbol ($, #). Look past that prefix before deciding whether to trim, and | ||
| // reattach it: prepending the tilde to the whole string (e.g. "~+.2f") is an | ||
| // invalid spec that d3Format rejects, so "+.2f" used to be silently dropped. | ||
| var prefix = (formatStr.match(/^[+\-( ]?[$#]?/) || [''])[0]; |
Contributor
There was a problem hiding this comment.
Let's remove these from the prefix group. Without removing these, "$f" will get changed to "$~f". This means that "1.50" would get changed to "$1.5", which isn't what we want.
Suggested change
| var prefix = (formatStr.match(/^[+\-( ]?[$#]?/) || [''])[0]; | |
| var prefix = (formatStr.match(/^[+\-( ]/) || [''])[0]; |
| var rest = formatStr.slice(prefix.length); | ||
|
|
||
| // try adding tilde to trim trailing zeros | ||
| if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest; |
Contributor
There was a problem hiding this comment.
Suggested change
| if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest; | |
| if (!/^[~,.0$#]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest; |
Comment on lines
+56
to
+60
| // sign flag with an explicit precision must not be dropped (was silently | ||
| // ignored because adjustFormat produced the invalid spec "~+.2f") | ||
| { format: '+.2f', number: float, exp: '+12345.68'}, | ||
| { format: '+.0f', number: float, exp: '+12346'}, | ||
| { format: '-.4f', number: float, exp: '12345.6789'}, |
Contributor
There was a problem hiding this comment.
Suggested change
| // sign flag with an explicit precision must not be dropped (was silently | |
| // ignored because adjustFormat produced the invalid spec "~+.2f") | |
| { format: '+.2f', number: float, exp: '+12345.68'}, | |
| { format: '+.0f', number: float, exp: '+12346'}, | |
| { format: '-.4f', number: float, exp: '12345.6789'}, | |
| // sign flag with an explicit precision must not be dropped | |
| { format: '+.2f', number: float, exp: '+12345.68'}, | |
| { format: '+.0f', number: float, exp: '+12346'}, | |
| { format: '-.4f', number: float, exp: '12345. | |
| // symbol-led specs ($, #) are left untrimmed | |
| { format: '$f', number: 1.5, exp: '$1.500000'}, | |
| { format: '#f', number: 1.5, exp: '1.500000'}, | |
| { format: '$s', number: 1500, exp: '$1.50000k'}, |
| @@ -0,0 +1 @@ | |||
| - Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` silently ignoring d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)] | |||
Contributor
There was a problem hiding this comment.
Suggested change
| - Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` silently ignoring d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)] | |
| - Fix `hovertemplate`/`texttemplate`/`tickformat`/`hoverformat` improperly handling d3-format specs that start with a sign flag such as `+.2f` [[#7900](https://github.com/plotly/plotly.js/pull/7900)] |
Author
|
i'm getting some mind melting currently, so i'll review it and send you back. |
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.
Fixes #7897
A d3-format specifier that begins with a sign flag (
+,-,(, or a space) was silently ignored inhovertemplate,texttemplate,tickformat, andhoverformat, showing the raw unformatted number instead. For example%{y:+.2f}rendered12.3456789rather than+12.35.Cause:
Lib.adjustFormatprepends a trim tilde to the start of the whole string, so+.2fbecame~+.2f— an invalid spec (the~flag must sit just before the type).d3.formatthrew,Lib.numberFormatcaught it and fell back toLib.noFormat.Fix: skip past an optional leading sign flag and symbol (
$,#) before the trim heuristic, then reattach that prefix, so the tilde is only inserted in a valid position. Formats without such a prefix are unchanged.Verified against
d3-formatv1 directly:+.2f→+12345.68,+.0f→+12346,-.4f→12345.6789, while.2f,s,0.3s,+13,-13,($15,#0X,.2%produce identical output to before. Added matching cases intest/jasmine/tests/lib_number_format_test.js.