Skip to content

Commit 1173551

Browse files
fix: catch invalid Google Charts format patterns and show i18n error (#1272)
Wrap gv.NumberFormat/DateFormat construction in a try/catch to prevent uncaught 'Too many percent/permill' errors from invalid ICU patterns. Show a localised error message inline below the format input field and clear it automatically when the pattern becomes valid.
1 parent a6a9328 commit 1173551

2 files changed

Lines changed: 32 additions & 19 deletions

File tree

classes/Visualizer/Module/Chart.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -876,12 +876,13 @@ private function _handleDataAndSettingsPage() {
876876
'visualizer',
877877
array(
878878
'l10n' => array(
879-
'invalid_source' => esc_html__( 'You have entered an invalid URL. Please provide a valid URL.', 'visualizer' ),
880-
'loading' => esc_html__( 'Loading...', 'visualizer' ),
881-
'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
882-
'select_columns' => esc_html__( 'Please select a few columns to include in the chart.', 'visualizer' ),
883-
'save_settings' => __( 'You have modified the chart\'s settings. To modify the source/data again, you must save this chart and reopen it for editing. If you continue without saving the chart, you may lose your changes.', 'visualizer' ),
884-
'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
879+
'invalid_source' => esc_html__( 'You have entered an invalid URL. Please provide a valid URL.', 'visualizer' ),
880+
'loading' => esc_html__( 'Loading...', 'visualizer' ),
881+
'json_error' => esc_html__( 'An error occured in fetching data.', 'visualizer' ),
882+
'select_columns' => esc_html__( 'Please select a few columns to include in the chart.', 'visualizer' ),
883+
'save_settings' => __( 'You have modified the chart\'s settings. To modify the source/data again, you must save this chart and reopen it for editing. If you continue without saving the chart, you may lose your changes.', 'visualizer' ),
884+
'copied' => __( 'The data has been copied to your clipboard. Hit Ctrl-V/Cmd-V in your spreadsheet editor to paste the data.', 'visualizer' ),
885+
'invalid_format' => esc_html__( 'This format pattern is not supported in the series settings field. Use the Manual Configuration option instead.', 'visualizer' ),
885886
),
886887
'charts' => array(
887888
'canvas' => $data,
@@ -1405,7 +1406,8 @@ private function _handleDataPage() {
14051406
array(
14061407
'l10n' => array(
14071408
'invalid_source' => esc_html__( 'You have entered an invalid URL. Please provide a valid URL.', 'visualizer' ),
1408-
'loading' => esc_html__( 'Loading...', 'visualizer' ),
1409+
'loading' => esc_html__( 'Loading...', 'visualizer' ),
1410+
'invalid_format' => esc_html__( 'This format pattern is not supported in the series settings field. To display percentages, use the Manual Configuration option instead.', 'visualizer' ),
14091411
),
14101412
'charts' => array(
14111413
'canvas' => $data,

js/render-google.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,19 +449,30 @@ var isResizeRequest = false;
449449
}
450450

451451
var formatter = null;
452-
switch (type) {
453-
case 'number':
454-
formatter = new gv.NumberFormat({pattern: format});
455-
break;
456-
case 'date':
457-
case 'datetime':
458-
case 'timeofday':
459-
formatter = new gv.DateFormat({pattern: format});
460-
break;
461-
}
452+
var $formatInput = $('input.control-text[name*="[format]"]').filter(function() {
453+
return $(this).val() === format;
454+
});
455+
try {
456+
switch (type) {
457+
case 'number':
458+
formatter = new gv.NumberFormat({pattern: format});
459+
break;
460+
case 'date':
461+
case 'datetime':
462+
case 'timeofday':
463+
formatter = new gv.DateFormat({pattern: format});
464+
break;
465+
}
462466

463-
if (formatter) {
464-
formatter.format(table, index);
467+
if (formatter) {
468+
formatter.format(table, index);
469+
$formatInput.nextAll('.visualizer-format-error').remove();
470+
}
471+
} catch (e) {
472+
if ($formatInput.length) {
473+
$formatInput.nextAll('.visualizer-format-error').remove();
474+
$('<p class="visualizer-format-error" style="color:#cc0000;margin:4px 0 0"></p>').text(visualizer.l10n.invalid_format).insertAfter($formatInput);
475+
}
465476
}
466477

467478
var arr = id.split('-');

0 commit comments

Comments
 (0)