Debian Codegraph @

debian/patches/html-escape-None.patch

raw
1 From: Styx0x6 <[email protected]>
2 Date: Mon, 30 Mar 2026 16:15:16 +0200
3 Subject: Handle None object before HTML escaping
4
5 Origin: upstream, https://github.com/pygments/pygments/pull/3078
6 Bug: https://github.com/pygments/pygments/issues/3076
7 Bug-Debian: https://bugs.debian.org/1141791
8 Last-Update: 2026-07-13
9 ---
10 pygments/formatters/html.py | 16 +++++++---------
11 pygments/util.py | 17 +++++++++++++++++
12 tests/test_html_formatter.py | 36 ++++++++++++++++++++++++++++++++++++
13 tests/test_util.py | 6 ++++++
14 4 files changed, 66 insertions(+), 9 deletions(-)
15
16 diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
17 index 17e6741..827cf3f 100644
18 --- a/pygments/formatters/html.py
19 +++ b/pygments/formatters/html.py
20 @@ -16,9 +16,7 @@ from io import StringIO
21
22 from pygments.formatter import Formatter
23 from pygments.token import Token, Text, STANDARD_TYPES
24 -from pygments.util import get_bool_opt, get_int_opt, get_list_opt
25 -
26 -import html
27 +from pygments.util import get_bool_opt, get_int_opt, get_list_opt, html_escape
28
29 try:
30 import ctags
31 @@ -424,14 +422,14 @@ class HtmlFormatter(Formatter):
32 self.nowrap = get_bool_opt(options, 'nowrap', False)
33 self.noclasses = get_bool_opt(options, 'noclasses', False)
34 self.classprefix = options.get('classprefix', '')
35 - self.cssclass = html.escape(self._decodeifneeded(options.get('cssclass', 'highlight')))
36 - self.cssstyles = html.escape(self._decodeifneeded(options.get('cssstyles', '')))
37 + self.cssclass = html_escape(self._decodeifneeded(options.get('cssclass', 'highlight')))
38 + self.cssstyles = html_escape(self._decodeifneeded(options.get('cssstyles', '')))
39 self.prestyles = self._decodeifneeded(options.get('prestyles', ''))
40 self.cssfile = self._decodeifneeded(options.get('cssfile', ''))
41 self.noclobber_cssfile = get_bool_opt(options, 'noclobber_cssfile', False)
42 self.tagsfile = self._decodeifneeded(options.get('tagsfile', ''))
43 self.tagurlformat = self._decodeifneeded(options.get('tagurlformat', ''))
44 - self.filename = html.escape(self._decodeifneeded(options.get('filename', '')))
45 + self.filename = html_escape(self._decodeifneeded(options.get('filename', '')))
46 self.wrapcode = get_bool_opt(options, 'wrapcode', False)
47 self.span_element_openers = {}
48 self.debug_token_types = get_bool_opt(options, 'debug_token_types', False)
49 @@ -454,9 +452,9 @@ class HtmlFormatter(Formatter):
50 self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
51 self.linenospecial = abs(get_int_opt(options, 'linenospecial', 0))
52 self.nobackground = get_bool_opt(options, 'nobackground', False)
53 - self.lineseparator = html.escape(options.get('lineseparator', '\n'))
54 - self.lineanchors = html.escape(options.get('lineanchors', ''))
55 - self.linespans = html.escape(options.get('linespans', ''))
56 + self.lineseparator = html_escape(options.get('lineseparator', '\n'))
57 + self.lineanchors = html_escape(options.get('lineanchors', ''))
58 + self.linespans = html_escape(options.get('linespans', ''))
59 self.anchorlinenos = get_bool_opt(options, 'anchorlinenos', False)
60 self.hl_lines = set()
61 for lineno in get_list_opt(options, 'hl_lines', []):
62 diff --git a/pygments/util.py b/pygments/util.py
63 index 548d9d7..fca5dd3 100644
64 --- a/pygments/util.py
65 +++ b/pygments/util.py
66 @@ -10,6 +10,7 @@
67
68 import re
69 from io import TextIOWrapper
70 +import html
71
72
73 split_path_re = re.compile(r'[/\\ ]')
74 @@ -322,3 +323,19 @@ class UnclosingTextIOWrapper(TextIOWrapper):
75 # Don't close underlying buffer on destruction.
76 def close(self):
77 self.flush()
78 +
79 +def html_escape(string, quote=True):
80 + """Return a safe version of the passed `string`,
81 + and an empty string if `None`.
82 +
83 + `NoneType` is not supported by `html.escape`, as `html.escape`
84 + uses the built-in `replace` function on `string`, so we need to
85 + check for it first.
86 +
87 + Optional flag quote is true by default, which also escapes
88 + double and single quotes.
89 + See https://docs.python.org/3/library/html.html#html.escape for more details.
90 + """
91 + if string is not None:
92 + return html.escape(string, quote=quote)
93 + return ''
94 diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py
95 index 9d2abac..1b58f67 100644
96 --- a/tests/test_html_formatter.py
97 +++ b/tests/test_html_formatter.py
98 @@ -269,3 +269,39 @@ def test_debug_token_types():
99 fmt_debug_token_types.format(tokensource, outfile_debug_token_types)
100 html_debug_token_types = outfile_debug_token_types.getvalue()
101 assert '<span class="n" title="Name">TESTDIR</span>' in html_debug_token_types
102 +
103 +
104 +def test_html_escape_attributes():
105 + """Test that HTML special characters in formatter option values are properly escaped."""
106 +
107 + fmt = HtmlFormatter(
108 + cssclass='bad<script>',
109 + cssstyles='color: "&"',
110 + filename='<file\'>.py',
111 + lineseparator='<br>',
112 + lineanchors='anchor"name',
113 + linespans='span&name',
114 + )
115 + assert fmt.cssclass == 'bad&lt;script&gt;'
116 + assert fmt.cssstyles == 'color: &quot;&amp;&quot;'
117 + assert fmt.filename == '&lt;file&#x27;&gt;.py'
118 + assert fmt.lineseparator == '&lt;br&gt;'
119 + assert fmt.lineanchors == 'anchor&quot;name'
120 + assert fmt.linespans == 'span&amp;name'
121 +
122 + """Test that None values for these options are handled gracefully."""
123 +
124 + fmt_none = HtmlFormatter(
125 + cssclass=None,
126 + cssstyles=None,
127 + filename=None,
128 + lineseparator=None,
129 + lineanchors=None,
130 + linespans=None,
131 + )
132 + assert fmt_none.cssclass == ''
133 + assert fmt_none.cssstyles == ''
134 + assert fmt_none.filename == ''
135 + assert fmt_none.lineseparator == ''
136 + assert fmt_none.lineanchors == ''
137 + assert fmt_none.linespans == ''
138 diff --git a/tests/test_util.py b/tests/test_util.py
139 index f02292d..89d345a 100644
140 --- a/tests/test_util.py
141 +++ b/tests/test_util.py
142 @@ -187,3 +187,9 @@ def test_console_functions():
143 assert console.reset_color() == console.codes['reset']
144 assert console.colorize('blue', 'text') == \
145 console.codes['blue'] + 'text' + console.codes['reset']
146 +
147 +def test_html_escape():
148 + assert util.html_escape('test') == 'test'
149 + assert util.html_escape('<>&"\'') == '&lt;&gt;&amp;&quot;&#x27;'
150 + assert util.html_escape('<>&"\'', quote=False) == '&lt;&gt;&amp;"\''
151 + assert util.html_escape(None) == ''