Skip to content

Commit 883b225

Browse files
committed
Fixes in HTMLSerializer and additional serializer tests.
--HG-- extra : convert_revision : svn%3Aacbfec75-9323-0410-a652-858a13e371e0/trunk%40623
1 parent 6b514c0 commit 883b225

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

src/serializer.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ class HTMLSerializer(object):
2525
quote_char = '"'
2626
minimize_boolean_attributes = True
2727

28+
use_trailing_solidus = False
2829
trailing_solidus = " /"
2930

3031
omit_optional_tags = True
3132

3233
def __init__(self, **kwargs):
33-
for attr in ("quote_attr_values", "quote_char",
34-
"minimize_boolean_attributes", "trailing_solidus",
35-
"omit_optional_tags"):
34+
for attr in ("quote_attr_values", "quote_char", "minimize_boolean_attributes",
35+
"trailing_solidus", "use_trailing_solidus", "omit_optional_tags"):
3636
if attr in kwargs:
3737
setattr(self, attr, kwargs[attr])
3838
self.errors = []
@@ -77,7 +77,7 @@ def serialize(self, treewalker):
7777
and k not in booleanAttributes.get("", tuple())):
7878
attributes.append("=")
7979
v = v.replace("&", "&")
80-
if self.quote_attr_values:
80+
if self.quote_attr_values or not v:
8181
quote_attr = True
8282
else:
8383
quote_attr = reduce(lambda x,y: x or y in v,
@@ -92,8 +92,8 @@ def serialize(self, treewalker):
9292
attributes.append(self.quote_char)
9393
else:
9494
attributes.append(v)
95-
if name in voidElements and self.include_trailing_slashes:
96-
attributes.append(" /")
95+
if name in voidElements and self.use_trailing_solidus:
96+
attributes.append(self.trailing_solidus)
9797
yield u"<%s%s>" % (name, u"".join(attributes))
9898

9999
elif type == "EndTag":
@@ -186,8 +186,8 @@ def is_optional_start(self, tagname, next):
186186
# TODO
187187
return False
188188

189-
def _is_optional_end(self, tagname, next_event):
190-
type, data = next_event
189+
def is_optional_end(self, tagname, next):
190+
type = next and next["type"] or None
191191
if tagname in ('html', 'head', 'body'):
192192
# An html element's end tag may be omitted if the html element
193193
# is not immediately followed by a space character or a comment.

tests/test_serializer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ def normalizeTokens(self, tokens):
6565
elif type == "EndTag":
6666
yield self.endTag(token[1])
6767
elif type == "EmptyTag":
68-
yield self.emptyTag(token[1])
68+
for token in self.emptyTag(token[1], token[2]):
69+
yield token
6970
elif type == "Comment":
7071
yield self.comment(token[1])
71-
elif type == "Characters":
72-
for textToken in self.text(token[1]):
73-
yield textToken
72+
elif type in ("Characters", "SpaceCharacters"):
73+
for token in self.text(token[1]):
74+
yield token
7475
elif type == "Doctype":
7576
yield self.doctype(token[1])
7677
else:
@@ -88,7 +89,7 @@ def buildTestSuite():
8889
tests += 1
8990
testName = 'test%d' % tests
9091
TestCase.addTest(testName, test["expected"], test["input"], \
91-
test["description"], test["options"])
92+
test["description"], test.get("options", {}))
9293
return unittest.TestLoader().loadTestsFromTestCase(TestCase)
9394

9495
def main():

0 commit comments

Comments
 (0)