Skip to content

Commit

Permalink
Move xml_escape_attr to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Einenlum committed Jun 16, 2022
1 parent 3e02f2e commit a9d0bd6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
26 changes: 5 additions & 21 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
memoized,
xml_oneliner_re_from_tab_width,
hr_tag_re_from_tab_width,
xml_escape_attr,
)

# ---- globals
Expand Down Expand Up @@ -1501,7 +1502,7 @@ def _do_links(self, text):
.replace('_', self._escape_table['_'])
if title:
title_str = ' title="%s"' % (
_xml_escape_attr(title)
xml_escape_attr(_AMPERSAND_RE, title)
.replace('*', self._escape_table['*'])
.replace('_', self._escape_table['_']))
else:
Expand All @@ -1510,7 +1511,7 @@ def _do_links(self, text):
img_class_str = self._html_class_str_from_tag("img")
result = '<img src="%s" alt="%s"%s%s%s' \
% (_html_escape_url(url, safe_mode=self.safe_mode),
_xml_escape_attr(link_text),
xml_escape_attr(_AMPERSAND_RE, link_text),
title_str,
img_class_str,
self.empty_element_suffix)
Expand Down Expand Up @@ -1556,7 +1557,7 @@ def _do_links(self, text):
.replace('_', self._escape_table['_'])
title = self.titles.get(link_id)
if title:
title = _xml_escape_attr(title) \
title = xml_escape_attr(_AMPERSAND_RE, title) \
.replace('*', self._escape_table['*']) \
.replace('_', self._escape_table['_'])
title_str = ' title="%s"' % title
Expand All @@ -1566,7 +1567,7 @@ def _do_links(self, text):
img_class_str = self._html_class_str_from_tag("img")
result = '<img src="%s" alt="%s"%s%s%s' \
% (_html_escape_url(url, safe_mode=self.safe_mode),
_xml_escape_attr(link_text),
xml_escape_attr(_AMPERSAND_RE, link_text),
title_str,
img_class_str,
self.empty_element_suffix)
Expand Down Expand Up @@ -2481,23 +2482,6 @@ class UnicodeWithAttrs(str):
toc_html = None


def _xml_escape_attr(attr, skip_single_quote=True):
"""Escape the given string for use in an HTML/XML tag attribute.
By default this doesn't bother with escaping `'` to `&#39;`, presuming that
the tag attribute is surrounded by double quotes.
"""
escaped = _AMPERSAND_RE.sub('&amp;', attr)

escaped = (attr
.replace('"', '&quot;')
.replace('<', '&lt;')
.replace('>', '&gt;'))
if not skip_single_quote:
escaped = escaped.replace("'", "&#39;")
return escaped


def _xml_encode_email_char_at_random(ch):
r = random()
# Roughly 10% raw, 45% hex, 45% dec.
Expand Down
17 changes: 17 additions & 0 deletions lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,20 @@ def hr_tag_re_from_tab_width(tab_width):


hr_tag_re_from_tab_width = memoized(hr_tag_re_from_tab_width)


def xml_escape_attr(ampersand_re, attr, skip_single_quote=True):
"""Escape the given string for use in an HTML/XML tag attribute.
By default this doesn't bother with escaping `'` to `&#39;`, presuming that
the tag attribute is surrounded by double quotes.
"""
escaped = ampersand_re.sub('&amp;', attr)

escaped = (attr
.replace('"', '&quot;')
.replace('<', '&lt;')
.replace('>', '&gt;'))
if not skip_single_quote:
escaped = escaped.replace("'", "&#39;")
return escaped

0 comments on commit a9d0bd6

Please sign in to comment.