Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merged: add escape_tags option to enable/disable escaping tag names and attribut... #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions htmlmin/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def minify(input,
reduce_empty_attributes=True,
reduce_boolean_attributes=False,
remove_optional_attribute_quotes=True,
escape_tags=True,
keep_pre=False,
pre_tags=parser.PRE_TAGS,
pre_attr='pre'):
Expand Down Expand Up @@ -93,6 +94,7 @@ def minify(input,
reduce_empty_attributes=reduce_empty_attributes,
reduce_boolean_attributes=reduce_boolean_attributes,
remove_optional_attribute_quotes=remove_optional_attribute_quotes,
escape_tags=escape_tags,
keep_pre=keep_pre,
pre_tags=pre_tags,
pre_attr=pre_attr)
Expand All @@ -118,6 +120,7 @@ def __init__(self,
reduce_empty_attributes=True,
reduce_boolean_attributes=False,
remove_optional_attribute_quotes=True,
escape_tags=True,
keep_pre=False,
pre_tags=parser.PRE_TAGS,
pre_attr='pre'):
Expand All @@ -132,6 +135,7 @@ def __init__(self,
reduce_empty_attributes=reduce_empty_attributes,
reduce_boolean_attributes=reduce_boolean_attributes,
remove_optional_attribute_quotes=remove_optional_attribute_quotes,
escape_tags=escape_tags,
keep_pre=keep_pre,
pre_tags=pre_tags,
pre_attr=pre_attr)
Expand Down
24 changes: 17 additions & 7 deletions htmlmin/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
# Tag omission rules:
# http://www.w3.org/TR/html51/syntax.html#optional-tags

def escape_noop(v, **kwargs):
return v

class HTMLMinError(Exception): pass
class ParseError(HTMLMinError): pass
class OpenTagNotFoundError(ParseError): pass
Expand All @@ -92,6 +95,7 @@ def __init__(self,
reduce_empty_attributes=True,
reduce_boolean_attributes=False,
remove_optional_attribute_quotes=True,
escape_tags=True,
keep_pre=False,
pre_tags=PRE_TAGS,
pre_attr='pre'):
Expand All @@ -113,6 +117,10 @@ def __init__(self,
self._tag_stack = []
self._title_newly_opened = False
self.__title_trailing_whitespace = False
if escape_tags:
self.escape = escape
else:
self.escape = escape_noop

def _has_pre(self, attrs):
for k,v in attrs:
Expand All @@ -123,24 +131,26 @@ def _has_pre(self, attrs):
def build_tag(self, tag, attrs, close_tag):
result = StringIO()
result.write('<')
result.write(escape(tag))
result.write(self.escape(tag))
needs_closing_space = False
for k,v in attrs:
result.write(' ')
result.write(escape(k))
result.write(self.escape(k))
if v:
if self.reduce_boolean_attributes and (
k in BOOLEAN_ATTRIBUTES.get(tag,[]) or
k in BOOLEAN_ATTRIBUTES['*']):
pass
elif self.remove_optional_attribute_quotes and not any((c in v for c in ('"', "'", ' ', '<', '>', '='))):
result.write('=')
result.write(escape(v, quote=True))
result.write(self.escape(v, quote=True))
needs_closing_space = v.endswith('/')
else:
result.write('="')
result.write(escape(v, quote=True).replace('&#x27;', "'"))
result.write('"')
attr_val = self.escape(v, quote=True).replace("'", '&#x27;')
if '"' in attr_val:
result.write("='{}'".format(attr_val))
else:
result.write('="{}"'.format(attr_val))
elif not self.reduce_empty_attributes:
result.write('=""')
if needs_closing_space:
Expand Down Expand Up @@ -256,7 +266,7 @@ def handle_endtag(self, tag):
# results in a '<p></p>' in Chrome.
pass
if tag not in NO_CLOSE_TAGS:
self._data_buffer.extend(['</', escape(tag), '>'])
self._data_buffer.extend(['</', self.escape(tag), '>'])

def handle_startendtag(self, tag, attrs):
self._after_doctype = False
Expand Down
16 changes: 16 additions & 0 deletions htmlmin/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
}

FEATURES_TEXTS = {
'escape_tags_true': (
'<body > <a href="http://example.com/?hello=world&foo=bar" >hello</a></ body> ',
'<body> <a href="http://example.com/?hello=world&amp;foo=bar">hello</a></body> ',
),
'escape_tags_false': (
'<body > <a href="http://example.com/?hello=world&foo=bar" >hello</a></ body> ',
'<body> <a href="http://example.com/?hello=world&foo=bar">hello</a></body> ',
),
'remove_quotes': (
'<body > <div id="x" style=" abc " data-a=b></div></ body> ',
'<body> <div id=x style=" abc " data-a=b></div></body> ',
Expand Down Expand Up @@ -342,6 +350,14 @@ def test_buffered_input(self):
class TestMinifyFeatures(HTMLMinTestCase):
__reference_texts__ = FEATURES_TEXTS

def test_escape_tags_true(self):
text = self.__reference_texts__['escape_tags_true']
self.assertEqual(htmlmin.minify(text[0], escape_tags=True), text[1])

def test_escape_tags_false(self):
text = self.__reference_texts__['escape_tags_false']
self.assertEqual(htmlmin.minify(text[0], escape_tags=False), text[1])

def test_remove_comments(self):
text = self.__reference_texts__['remove_comments']
self.assertEqual(htmlmin.minify(text[0], remove_comments=True), text[1])
Expand Down