Skip to content

Commit

Permalink
fix producing invalid html with reduce_attributes
Browse files Browse the repository at this point in the history
Attributes with an equal sign but no actual value, either quotes or non quotes, are invalid.
Therefor when reduce_attribute is set to False we must add empty quotes to an empty attribute.
  • Loading branch information
trbs committed Jan 13, 2014
1 parent de5d347 commit 7a456c1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion htmlmin/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def build_tag(self, tag, attrs, close_tag):
result = '<{}'.format(escape(tag))
for k,v in attrs:
result += ' ' + escape(k)
if v or self.reduce_attributes is False:
if v:
if self.reduce_boolean_attributes and (
k in BOOLEAN_ATTRIBUTES.get(tag,[]) or
k in BOOLEAN_ATTRIBUTES['*']):
Expand All @@ -125,6 +125,8 @@ def build_tag(self, tag, attrs, close_tag):
result += '={}'.format(escape(v, quote=True))
else:
result += '="{}"'.format(escape(v, quote=True).replace('&#x27;', "'"))
elif not self.reduce_attributes:
result += '=""'
if close_tag:
return result + '/>'
return result + '>'
Expand Down
2 changes: 1 addition & 1 deletion htmlmin/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
),
'no_reduce_attributes': (
'<body><img src="/x.png" alt="" /></body>',
'<body><img src=/x.png alt=></body>',
'<body><img src=/x.png alt=""></body>',
),
'no_reduce_attributes_keep_quotes': (
'<body><img src="/x.png" alt="" /></body>',
Expand Down

0 comments on commit 7a456c1

Please sign in to comment.