Skip to content

Commit a6c9816

Browse files
committed
Keep symbol-led specs out of the trimmed-prefix path
camdecoster pointed out that folding $/# into the prefix group broke symbol-led specs: "$f" became "$~f", an invalid spec, and stripping the symbol before the trim check meant "$1.500000" would come out as "$1.5" once the tilde got added. Only sign flags (+, -, (, space) belong in the stripped prefix; symbol-led specs now fall straight through to the untrimmed return, matching the existing hex/octal/binary "#" cases. Added test coverage for $f/#f/$s, reworded the draftlog line to "improperly handling" since it's not strictly a silent drop anymore.
1 parent 95dc3ad commit a6c9816

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

‎draftlogs/7900_fix.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
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)]
1+
- 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)]

‎src/lib/index.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@ lib.adjustFormat = function adjustFormat(formatStr) {
1919
if (/^\d%/.test(formatStr)) return '~%';
2020
if (/^\ds/.test(formatStr)) return '~s';
2121

22-
// A d3-format spec may begin with a sign flag (+, -, (, space) and/or a
23-
// symbol ($, #). Look past that prefix before deciding whether to trim, and
24-
// reattach it: prepending the tilde to the whole string (e.g. "~+.2f") is an
25-
// invalid spec that d3Format rejects, so "+.2f" used to be silently dropped.
26-
var prefix = (formatStr.match(/^[+\-( ]?[$#]?/) || [''])[0];
22+
// A d3-format spec may begin with a sign flag (+, -, (, space). Look past
23+
// that prefix before deciding whether to trim, and reattach it: prepending
24+
// the tilde to the whole string (e.g. "~+.2f") is an invalid spec that
25+
// d3Format rejects, so "+.2f" used to be silently dropped.
26+
var prefix = (formatStr.match(/^[+\-( ]/) || [''])[0];
2727
var rest = formatStr.slice(prefix.length);
2828

29-
// try adding tilde to trim trailing zeros
30-
if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest;
29+
// try adding tilde to trim trailing zeros; leave symbol-led specs ($, #)
30+
// untrimmed, since the symbol isn't part of the prefix we stripped above
31+
if (!/^[~,.0$#]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest;
3132

3233
return formatStr;
3334
};

‎test/jasmine/tests/lib_number_format_test.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ describe('number format', function() {
5959
{ format: '+.0f', number: float, exp: '+12346'},
6060
{ format: '-.4f', number: float, exp: '12345.6789'},
6161

62+
// symbol-led specs ($, #) are left untrimmed
63+
{ format: '$f', number: 1.5, exp: '$1.500000'},
64+
{ format: '#f', number: 1.5, exp: '1.500000'},
65+
{ format: '$s', number: 1500, exp: '$1.50000k'},
66+
6267
// space-filled and default sign
6368
{ format: '-13', number: float, exp: '-12345.678901'},
6469
{ format: '-14', number: float, exp: ' -12345.678901'},

0 commit comments

Comments
 (0)