Skip to content

Commit 5b7a39e

Browse files
committed
Fixed format string serialization logic, particularly when serializing to JSON.
1 parent 9281c9f commit 5b7a39e

File tree

6 files changed

+33
-0
lines changed

6 files changed

+33
-0
lines changed

highcharts_core/js_literal_functions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,12 @@ def get_js_literal(item, careful_validation = False) -> str:
373373
elif isinstance(item, str):
374374
if (item.startswith('[') or item.startswith('Date')) and item != 'Date':
375375
as_str += f"""{item}"""
376+
elif item.startswith('${'):
377+
if "'" in item:
378+
item = item.replace("'", "\\'")
379+
as_str += f'"{item[1:]}"'
380+
else:
381+
as_str += f"'{item[1:]}'"
376382
elif item.startswith('{') and item.endswith('}'):
377383
if is_js_object(item, careful_validation = careful_validation):
378384
as_str += f"""{item}"""

highcharts_core/metaclasses.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ def trim_iterable(untrimmed,
220220
if isinstance(untrimmed,
221221
(str, bytes, dict, UserDict)) or not hasattr(untrimmed,
222222
'__iter__'):
223+
if to_json and isinstance(untrimmed, str) and untrimmed.startswith('${'):
224+
return untrimmed[1:]
225+
223226
return untrimmed
224227

225228
trimmed = []

highcharts_core/options/annotations/label_options.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ def format(self) -> Optional[str]:
295295
296296
* :meth:`PlotOptions.series.data_labels`
297297
298+
.. warning::
299+
300+
If your format string begins with ``{`` and ends with ``}``, Highcharts
301+
for Python may interpret it as a JavaScript or JSON object. To prevent this, please
302+
add a ``$`` before the opening curly brace, like so: ``${value:.1f}``.
303+
298304
:returns: The format string to apply to the labels.
299305
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
300306
"""

highcharts_core/options/axes/labels.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,12 @@ def format(self) -> Optional[str]:
231231
232232
To add custom numeric or datetime formatting, use ``'{value}'`` with formatting,
233233
for example ``'{value:.1f}'`` or ``'{value:%Y-%m-%d}'``.
234+
235+
.. warning::
236+
237+
If your format string begins with ``{`` and ends with ``}``, Highcharts
238+
for Python may interpret it as a JavaScript or JSON object. To prevent this, please
239+
add a ``$`` before the opening curly brace, like so: ``${value:.1f}``.
234240
235241
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
236242
"""

highcharts_core/options/series/labels.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,12 @@ def format(self) -> Optional[str]:
207207
``options.xxx``, ``color``, and other members from the ``series`` object.
208208
Use this option also to set a static text for the label.
209209
210+
.. warning::
211+
212+
If your format string begins with ``{`` and ends with ``}``, Highcharts
213+
for Python may interpret it as a JavaScript or JSON object. To prevent this, please
214+
add a ``$`` before the opening curly brace, like so: ``${value:.1f}``.
215+
210216
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
211217
"""
212218
return self._format

highcharts_core/utility_classes/data_labels.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,12 @@ def format(self) -> Optional[str]:
432432
``'point.value'``.
433433
434434
435+
.. warning::
436+
437+
If your format string begins with ``{`` and ends with ``}``, Highcharts
438+
for Python may interpret it as a JavaScript or JSON object. To prevent this, please
439+
add a ``$`` before the opening curly brace, like so: ``${value}``.
440+
435441
:returns: The format string to apply to the labels.
436442
:rtype: :class:`str <python:str>` or :obj:`None <python:None>`
437443
"""

0 commit comments

Comments
 (0)