Skip to content

Commit

Permalink
Merge branch 'mina86-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mankyd committed Aug 25, 2017
2 parents d3b90f6 + e8959df commit 7bcbb99
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 146 deletions.
28 changes: 18 additions & 10 deletions htmlmin/escape.py
Expand Up @@ -25,6 +25,8 @@
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""

import re

try:
from html import escape
except ImportError:
Expand All @@ -45,7 +47,9 @@
ZERO = ord('0')
NINE = ord('9')

chars_to_escape_re = re.compile(r'[=><`]')

# https://www.w3.org/TR/html5/syntax.html#attributes-0
CHARS_TO_QUOTE_RE = re.compile(u'[\x20\x09\x0a\x0c\x0d=><`]')

def escape_tag(val):
return escape(val)
Expand All @@ -58,16 +62,20 @@ def escape_attr_value(val, double_quote=False):
has_html_tag = '<' in val or '>' in val
if double_quote:
return (val.replace('"', '&#34;'), DOUBLE_QUOTE)
if '"' in val or has_html_tag:
if "'" in val or has_html_tag:
return (val.replace('"', '&#34;'), DOUBLE_QUOTE)
else:
return (val, SINGLE_QUOTE)
elif "'" in val:
return (val, DOUBLE_QUOTE)

if (not val or any((c.isspace() for c in val)) or
chars_to_escape_re.search(val)):
double_quote_count = 0
single_quote_count = 0
for ch in val:
if ch == '"':
double_quote_count += 1
elif ch == "'":
single_quote_count += 1
if double_quote_count > single_quote_count:
return (val.replace("'", '&#39;'), SINGLE_QUOTE)
elif single_quote_count:
return (val.replace('"', '&#34;'), DOUBLE_QUOTE)

if not val or CHARS_TO_QUOTE_RE.search(val):
return (val, DOUBLE_QUOTE)
return (val, NO_QUOTES)

Expand Down
11 changes: 6 additions & 5 deletions htmlmin/main.py
Expand Up @@ -38,7 +38,8 @@ def minify(input,
remove_optional_attribute_quotes=True,
keep_pre=False,
pre_tags=parser.PRE_TAGS,
pre_attr='pre'):
pre_attr='pre',
cls=parser.HTMLMinParser):
"""Minifies HTML in one shot.
:param input: A string containing the HTML to be minified.
Expand Down Expand Up @@ -84,7 +85,7 @@ def minify(input,
If you are going to be minifying multiple HTML documents, each with the same
settings, consider using :class:`.Minifier`.
"""
minifier = parser.HTMLMinParser(
minifier = cls(
remove_comments=remove_comments,
remove_empty_space=remove_empty_space,
remove_all_empty_space=remove_all_empty_space,
Expand Down Expand Up @@ -118,12 +119,13 @@ def __init__(self,
remove_optional_attribute_quotes=True,
keep_pre=False,
pre_tags=parser.PRE_TAGS,
pre_attr='pre'):
pre_attr='pre',
cls=parser.HTMLMinParser):
"""Initialize the Minifier.
See :class:`htmlmin.minify` for an explanation of options.
"""
self._parser = parser.HTMLMinParser(
self._parser = cls(
remove_comments=remove_comments,
remove_empty_space=remove_empty_space,
remove_all_empty_space=remove_all_empty_space,
Expand Down Expand Up @@ -180,4 +182,3 @@ def finalize(self):
result = self._parser.result
self._parser.reset()
return result

0 comments on commit 7bcbb99

Please sign in to comment.