Skip to content

Commit a9d0bd6

Browse files
committed
Move xml_escape_attr to utils
1 parent 3e02f2e commit a9d0bd6

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

lib/markdown2.py

+5-21
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
memoized,
123123
xml_oneliner_re_from_tab_width,
124124
hr_tag_re_from_tab_width,
125+
xml_escape_attr,
125126
)
126127

127128
# ---- globals
@@ -1501,7 +1502,7 @@ def _do_links(self, text):
15011502
.replace('_', self._escape_table['_'])
15021503
if title:
15031504
title_str = ' title="%s"' % (
1504-
_xml_escape_attr(title)
1505+
xml_escape_attr(_AMPERSAND_RE, title)
15051506
.replace('*', self._escape_table['*'])
15061507
.replace('_', self._escape_table['_']))
15071508
else:
@@ -1510,7 +1511,7 @@ def _do_links(self, text):
15101511
img_class_str = self._html_class_str_from_tag("img")
15111512
result = '<img src="%s" alt="%s"%s%s%s' \
15121513
% (_html_escape_url(url, safe_mode=self.safe_mode),
1513-
_xml_escape_attr(link_text),
1514+
xml_escape_attr(_AMPERSAND_RE, link_text),
15141515
title_str,
15151516
img_class_str,
15161517
self.empty_element_suffix)
@@ -1556,7 +1557,7 @@ def _do_links(self, text):
15561557
.replace('_', self._escape_table['_'])
15571558
title = self.titles.get(link_id)
15581559
if title:
1559-
title = _xml_escape_attr(title) \
1560+
title = xml_escape_attr(_AMPERSAND_RE, title) \
15601561
.replace('*', self._escape_table['*']) \
15611562
.replace('_', self._escape_table['_'])
15621563
title_str = ' title="%s"' % title
@@ -1566,7 +1567,7 @@ def _do_links(self, text):
15661567
img_class_str = self._html_class_str_from_tag("img")
15671568
result = '<img src="%s" alt="%s"%s%s%s' \
15681569
% (_html_escape_url(url, safe_mode=self.safe_mode),
1569-
_xml_escape_attr(link_text),
1570+
xml_escape_attr(_AMPERSAND_RE, link_text),
15701571
title_str,
15711572
img_class_str,
15721573
self.empty_element_suffix)
@@ -2481,23 +2482,6 @@ class UnicodeWithAttrs(str):
24812482
toc_html = None
24822483

24832484

2484-
def _xml_escape_attr(attr, skip_single_quote=True):
2485-
"""Escape the given string for use in an HTML/XML tag attribute.
2486-
2487-
By default this doesn't bother with escaping `'` to `&#39;`, presuming that
2488-
the tag attribute is surrounded by double quotes.
2489-
"""
2490-
escaped = _AMPERSAND_RE.sub('&amp;', attr)
2491-
2492-
escaped = (attr
2493-
.replace('"', '&quot;')
2494-
.replace('<', '&lt;')
2495-
.replace('>', '&gt;'))
2496-
if not skip_single_quote:
2497-
escaped = escaped.replace("'", "&#39;")
2498-
return escaped
2499-
2500-
25012485
def _xml_encode_email_char_at_random(ch):
25022486
r = random()
25032487
# Roughly 10% raw, 45% hex, 45% dec.

lib/utils.py

+17
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,20 @@ def hr_tag_re_from_tab_width(tab_width):
278278

279279

280280
hr_tag_re_from_tab_width = memoized(hr_tag_re_from_tab_width)
281+
282+
283+
def xml_escape_attr(ampersand_re, attr, skip_single_quote=True):
284+
"""Escape the given string for use in an HTML/XML tag attribute.
285+
286+
By default this doesn't bother with escaping `'` to `&#39;`, presuming that
287+
the tag attribute is surrounded by double quotes.
288+
"""
289+
escaped = ampersand_re.sub('&amp;', attr)
290+
291+
escaped = (attr
292+
.replace('"', '&quot;')
293+
.replace('<', '&lt;')
294+
.replace('>', '&gt;'))
295+
if not skip_single_quote:
296+
escaped = escaped.replace("'", "&#39;")
297+
return escaped

0 commit comments

Comments
 (0)