|
2 | 2 | from collections import OrderedDict |
3 | 3 | from pathlib import Path |
4 | 4 | from typing import Dict, Type, Optional, Union |
| 5 | +import html |
5 | 6 |
|
6 | 7 | from . import config |
7 | 8 | from .colors import Color |
@@ -122,6 +123,8 @@ def __init__( |
122 | 123 | value_format: Optional[str] = None, |
123 | 124 | text_color: Optional[str] = None, |
124 | 125 | semver: Optional[bool] = False, |
| 126 | + escape_label: Optional[bool] = True, |
| 127 | + escape_value: Optional[bool] = True, |
125 | 128 | ): |
126 | 129 | """Constructor for Badge class.""" |
127 | 130 | # Set defaults if values were not passed |
@@ -209,6 +212,9 @@ def __init__( |
209 | 212 | self.use_max_when_value_exceeds = use_max_when_value_exceeds |
210 | 213 | self.mask_str = self.__class__._get_next_mask_str() |
211 | 214 |
|
| 215 | + self.escape_label = escape_label |
| 216 | + self.escape_value = escape_value |
| 217 | + |
212 | 218 | def __repr__(self) -> str: |
213 | 219 | """Return a representation of the Badge object instance. |
214 | 220 |
|
@@ -333,6 +339,20 @@ def _get_svg_template(self) -> str: |
333 | 339 | else: |
334 | 340 | return self.template |
335 | 341 |
|
| 342 | + @property |
| 343 | + def encoded_label(self) -> str: |
| 344 | + if self.escape_label: |
| 345 | + return html.escape(self.label) |
| 346 | + else: |
| 347 | + return self.label |
| 348 | + |
| 349 | + @property |
| 350 | + def encoded_value(self) -> str: |
| 351 | + if self.escape_value: |
| 352 | + return html.escape(self.value_text) |
| 353 | + else: |
| 354 | + return self.value_text |
| 355 | + |
336 | 356 | @property |
337 | 357 | def semver_version(self) -> Version: |
338 | 358 | """The semantic version represented by the value string. |
@@ -638,8 +658,8 @@ def badge_svg_text(self) -> str: |
638 | 658 | badge_text.replace("{{ badge width }}", str(self.badge_width)) |
639 | 659 | .replace("{{ font name }}", self.font_name) |
640 | 660 | .replace("{{ font size }}", str(self.font_size)) |
641 | | - .replace("{{ label }}", self.label) |
642 | | - .replace("{{ value }}", self.value_text) |
| 661 | + .replace("{{ label }}", self.encoded_label) |
| 662 | + .replace("{{ value }}", self.encoded_value) |
643 | 663 | .replace("{{ label anchor }}", str(self.label_anchor)) |
644 | 664 | .replace("{{ label anchor shadow }}", str(self.label_anchor_shadow)) |
645 | 665 | .replace("{{ value anchor }}", str(self.value_anchor)) |
|
0 commit comments