Skip to content

Commit 4834f64

Browse files
committed
Remove the "XHTML" Serializer.
This never did what it claimed, and never actually guaranteed its output was well-formed XML, and hence provided little of use. (The only thing it was good at was producing stylized HTML, and this can be done by manually setting the relevant options without having a misleadingly named module.) If you want XHTML, use one of the tree-builders that supports it natively. These are DOM (with most DOM implementations, inc. the standard library minidom with its Node.toxml function) and ElementTree (ElementTree.tostring gives XML). Neither simpletree nor the deprecated BeautifulSoup tree-builders support this. If you want polyglot, there is no solution. I (personally) would be happy to add a polyglot serializer to html5lib if one is written.
1 parent 3e50aad commit 4834f64

File tree

3 files changed

+8
-41
lines changed

3 files changed

+8
-41
lines changed

html5lib/serializer/__init__.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
from __future__ import absolute_import, division, unicode_literals
22

3-
43
from html5lib import treewalkers
54

65
from .htmlserializer import HTMLSerializer
7-
from .xhtmlserializer import XHTMLSerializer
6+
87

98
def serialize(input, tree="simpletree", format="html", encoding=None,
109
**serializer_opts):
1110
# XXX: Should we cache this?
1211
walker = treewalkers.getTreeWalker(tree)
1312
if format == "html":
1413
s = HTMLSerializer(**serializer_opts)
15-
elif format == "xhtml":
16-
s = XHTMLSerializer(**serializer_opts)
1714
else:
18-
raise ValueError("type must be either html or xhtml")
15+
raise ValueError("type must be html")
1916
return s.render(walker(input), encoding)

html5lib/serializer/xhtmlserializer.py

Lines changed: 0 additions & 11 deletions
This file was deleted.

html5lib/tests/test_serializer.py

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,35 +86,19 @@ def serialize_html(input, options):
8686
options = dict([(str(k),v) for k,v in options.items()])
8787
return serializer.HTMLSerializer(**options).render(JsonWalker(input),options.get("encoding",None))
8888

89-
def serialize_xhtml(input, options):
90-
options = dict([(str(k),v) for k,v in options.items()])
91-
return serializer.XHTMLSerializer(**options).render(JsonWalker(input),options.get("encoding",None))
92-
93-
def runSerializerTest(input, expected, xhtml, options):
89+
def runSerializerTest(input, expected, options):
9490
encoding = options.get("encoding", None)
9591

9692
if encoding:
9793
encode = lambda x: x.encode(encoding)
9894
expected = list(map(encode, expected))
99-
if xhtml:
100-
xhtml = list(map(encode, xhtml))
101-
10295

10396
result = serialize_html(input, options)
10497
if len(expected) == 1:
105-
assert expected[0] == result, "Expected:\n%s\nActual:\n%s\nOptions\nxhtml:False\n%s"%(expected[0], result, str(options))
98+
assert expected[0] == result, "Expected:\n%s\nActual:\n%s\nOptions:\n%s"%(expected[0], result, str(options))
10699
elif result not in expected:
107100
assert False, "Expected: %s, Received: %s" % (expected, result)
108101

109-
if not xhtml:
110-
return
111-
112-
result = serialize_xhtml(input, options)
113-
if len(xhtml) == 1:
114-
assert xhtml[0] == result, "Expected:\n%s\nActual:\n%s\nOptions\nxhtml:True\n%s"%(xhtml[0], result, str(options))
115-
elif result not in xhtml:
116-
assert False, "Expected: %s, Received: %s" % (xhtml, result)
117-
118102

119103
class EncodingTestCase(unittest.TestCase):
120104
def throwsWithLatin1(self, input):
@@ -131,11 +115,11 @@ def testDoctypeSystemId(self):
131115

132116
def testCdataCharacters(self):
133117
runSerializerTest([["StartTag", "http://www.w3.org/1999/xhtml", "style", {}], ["Characters", "\u0101"]],
134-
["<style>&amacr;"], None, {"encoding": "iso-8859-1"})
118+
["<style>&amacr;"], {"encoding": "iso-8859-1"})
135119

136120
def testCharacters(self):
137121
runSerializerTest([["Characters", "\u0101"]],
138-
["&amacr;"], None, {"encoding": "iso-8859-1"})
122+
["&amacr;"], {"encoding": "iso-8859-1"})
139123

140124
def testStartTagName(self):
141125
self.throwsWithLatin1([["StartTag", "http://www.w3.org/1999/xhtml", "\u0101", []]])
@@ -149,7 +133,7 @@ def testAttributeName(self):
149133
def testAttributeValue(self):
150134
runSerializerTest([["StartTag", "http://www.w3.org/1999/xhtml", "span",
151135
[{"namespace": None, "name": "potato", "value": "\u0101"}]]],
152-
["<span potato=&amacr;>"], None, {"encoding": "iso-8859-1"})
136+
["<span potato=&amacr;>"], {"encoding": "iso-8859-1"})
153137

154138
def testEndTagName(self):
155139
self.throwsWithLatin1([["EndTag", "http://www.w3.org/1999/xhtml", "\u0101"]])
@@ -190,7 +174,4 @@ def test_serializer():
190174
tests = json.load(fp)
191175
test_name = os.path.basename(filename).replace('.test','')
192176
for index, test in enumerate(tests['tests']):
193-
xhtml = test.get("xhtml", test["expected"])
194-
if test_name == 'optionaltags':
195-
xhtml = None
196-
yield runSerializerTest, test["input"], test["expected"], xhtml, test.get("options", {})
177+
yield runSerializerTest, test["input"], test["expected"], test.get("options", {})

0 commit comments

Comments
 (0)