From: Styx0x6 Date: Mon, 30 Mar 2026 16:15:16 +0200 Subject: Handle None object before HTML escaping Origin: upstream, https://github.com/pygments/pygments/pull/3078 Bug: https://github.com/pygments/pygments/issues/3076 Bug-Debian: https://bugs.debian.org/1141791 Last-Update: 2026-07-13 --- pygments/formatters/html.py | 16 +++++++--------- pygments/util.py | 17 +++++++++++++++++ tests/test_html_formatter.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_util.py | 6 ++++++ 4 files changed, 66 insertions(+), 9 deletions(-) diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py index 17e6741..827cf3f 100644 --- a/pygments/formatters/html.py +++ b/pygments/formatters/html.py @@ -16,9 +16,7 @@ from io import StringIO from pygments.formatter import Formatter from pygments.token import Token, Text, STANDARD_TYPES -from pygments.util import get_bool_opt, get_int_opt, get_list_opt - -import html +from pygments.util import get_bool_opt, get_int_opt, get_list_opt, html_escape try: import ctags @@ -424,14 +422,14 @@ class HtmlFormatter(Formatter): self.nowrap = get_bool_opt(options, 'nowrap', False) self.noclasses = get_bool_opt(options, 'noclasses', False) self.classprefix = options.get('classprefix', '') - self.cssclass = html.escape(self._decodeifneeded(options.get('cssclass', 'highlight'))) - self.cssstyles = html.escape(self._decodeifneeded(options.get('cssstyles', ''))) + self.cssclass = html_escape(self._decodeifneeded(options.get('cssclass', 'highlight'))) + self.cssstyles = html_escape(self._decodeifneeded(options.get('cssstyles', ''))) self.prestyles = self._decodeifneeded(options.get('prestyles', '')) self.cssfile = self._decodeifneeded(options.get('cssfile', '')) self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False) self.tagsfile = self._decodeifneeded(options.get('tagsfile', '')) self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', '')) - self.filename = html.escape(self._decodeifneeded(options.get('filename', ''))) + self.filename = html_escape(self._decodeifneeded(options.get('filename', ''))) self.wrapcode = get_bool_opt(options, 'wrapcode', False) self.span_element_openers = {} self.debug_token_types = get_bool_opt(options, 'debug_token_types', False) @@ -454,9 +452,9 @@ class HtmlFormatter(Formatter): self.linenostep = abs(get_int_opt(options, 'linenostep', 1)) self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0)) self.nobackground = get_bool_opt(options, 'nobackground', False) - self.lineseparator = html.escape(options.get('lineseparator', '\n')) - self.lineanchors = html.escape(options.get('lineanchors', '')) - self.linespans = html.escape(options.get('linespans', '')) + self.lineseparator = html_escape(options.get('lineseparator', '\n')) + self.lineanchors = html_escape(options.get('lineanchors', '')) + self.linespans = html_escape(options.get('linespans', '')) self.anchorlinenos = get_bool_opt(options, 'anchorlinenos', False) self.hl_lines = set() for lineno in get_list_opt(options, 'hl_lines', []): diff --git a/pygments/util.py b/pygments/util.py index 548d9d7..fca5dd3 100644 --- a/pygments/util.py +++ b/pygments/util.py @@ -10,6 +10,7 @@ import re from io import TextIOWrapper +import html split_path_re = re.compile(r'[/\\ ]') @@ -322,3 +323,19 @@ class UnclosingTextIOWrapper(TextIOWrapper): # Don't close underlying buffer on destruction. def close(self): self.flush() + +def html_escape(string, quote=True): + """Return a safe version of the passed `string`, + and an empty string if `None`. + + `NoneType` is not supported by `html.escape`, as `html.escape` + uses the built-in `replace` function on `string`, so we need to + check for it first. + + Optional flag quote is true by default, which also escapes + double and single quotes. + See https://docs.python.org/3/library/html.html#html.escape for more details. + """ + if string is not None: + return html.escape(string, quote=quote) + return '' diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py index 9d2abac..1b58f67 100644 --- a/tests/test_html_formatter.py +++ b/tests/test_html_formatter.py @@ -269,3 +269,39 @@ def test_debug_token_types(): fmt_debug_token_types.format(tokensource, outfile_debug_token_types) html_debug_token_types = outfile_debug_token_types.getvalue() assert 'TESTDIR' in html_debug_token_types + + +def test_html_escape_attributes(): + """Test that HTML special characters in formatter option values are properly escaped.""" + + fmt = HtmlFormatter( + cssclass='bad