Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/7900_fix.md
Original file line number Diff line number Diff line change
@@ -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)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)]

11 changes: 9 additions & 2 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ lib.adjustFormat = function adjustFormat(formatStr) {
if (/^\d%/.test(formatStr)) return '~%';
if (/^\ds/.test(formatStr)) return '~s';

// try adding tilde to the start of format in order to trim
if (!/^[~,.0$]/.test(formatStr) && /[&fps]/.test(formatStr)) return '~' + formatStr;
// A d3-format spec may begin with a sign flag (+, -, (, space) and/or a
// 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];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!/^[~,.0$]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest;
if (!/^[~,.0$#]/.test(rest) && /[&fps]/.test(rest)) return prefix + '~' + rest;


return formatStr;
};
Expand Down
6 changes: 6 additions & 0 deletions test/jasmine/tests/lib_number_format_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ describe('number format', function() {
{ format: '0f', number: float, exp: '12345.678901'},
{ format: '1f', number: float, exp: '12345.678901'},

// 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'},
Comment on lines +56 to +60

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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'},


// space-filled and default sign
{ format: '-13', number: float, exp: '-12345.678901'},
{ format: '-14', number: float, exp: ' -12345.678901'},
Expand Down
Loading