Skip to content

Commit c2eecb5

Browse files
committed
Add tests that we throw when we should and escape when we should when serializing at ISO-8859-1
1 parent 114ab64 commit c2eecb5

File tree

1 file changed

+54
-10
lines changed

1 file changed

+54
-10
lines changed

html5lib/tests/test_serializer.py

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ def _convertAttrib(self, attribs):
7575
return attrs
7676

7777

78+
def serialize_html(input, options):
79+
options = dict([(str(k),v) for k,v in options.iteritems()])
80+
return serializer.HTMLSerializer(**options).render(JsonWalker(input),options.get("encoding",None))
81+
82+
def serialize_xhtml(input, options):
83+
options = dict([(str(k),v) for k,v in options.iteritems()])
84+
return serializer.XHTMLSerializer(**options).render(JsonWalker(input),options.get("encoding",None))
85+
86+
7887
class TestCase(unittest.TestCase):
7988
def addTest(cls, name, description, input, expected, xhtml, options):
8089
func = lambda self: self.mockTest(input, options, expected, xhtml)
@@ -83,29 +92,63 @@ def addTest(cls, name, description, input, expected, xhtml, options):
8392
addTest = classmethod(addTest)
8493

8594
def mockTest(self, input, options, expected, xhtml):
86-
result = self.serialize_html(input, options)
95+
result = serialize_html(input, options)
8796
if len(expected) == 1:
8897
self.assertEquals(expected[0], result, "Expected:\n%s\nActual:\n%s\nOptions\nxhtml:False\n%s"%(expected[0], result, str(options)))
8998
elif result not in expected:
9099
self.fail("Expected: %s, Received: %s" % (expected, result))
91100

92101
if not xhtml: return
93102

94-
result = self.serialize_xhtml(input, options)
103+
result = serialize_xhtml(input, options)
95104
if len(xhtml) == 1:
96105
self.assertEquals(xhtml[0], result, "Expected:\n%s\nActual:\n%s\nOptions\nxhtml:True\n%s"%(xhtml[0], result, str(options)))
97106
elif result not in xhtml:
98107
self.fail("Expected: %s, Received: %s" % (xhtml, result))
99108

100-
def serialize_html(self, input, options):
101-
options = dict([(str(k),v) for k,v in options.iteritems()])
102-
return u''.join(serializer.HTMLSerializer(**options).
103-
serialize(JsonWalker(input),options.get("encoding",None)))
104109

105-
def serialize_xhtml(self, input, options):
106-
options = dict([(str(k),v) for k,v in options.iteritems()])
107-
return u''.join(serializer.XHTMLSerializer(**options).
108-
serialize(JsonWalker(input),options.get("encoding",None)))
110+
class EncodingTestCase(unittest.TestCase):
111+
def throwsWithLatin1(self, input):
112+
self.assertRaises(UnicodeEncodeError, serialize_html, input, {"encoding": "iso-8859-1"})
113+
114+
def testDoctypeName(self):
115+
self.throwsWithLatin1([["Doctype", u"\u0101"]])
116+
117+
def testDoctypePublicId(self):
118+
self.throwsWithLatin1([["Doctype", u"potato", u"\u0101"]])
119+
120+
def testDoctypeSystemId(self):
121+
self.throwsWithLatin1([["Doctype", u"potato", u"potato", u"\u0101"]])
122+
123+
def testCdataCharacters(self):
124+
self.assertEquals("<style>&amacr;", serialize_html([["StartTag", "http://www.w3.org/1999/xhtml", "style", {}],
125+
["Characters", u"\u0101"]],
126+
{"encoding": "iso-8859-1"}))
127+
128+
def testCharacters(self):
129+
self.assertEquals("&amacr;", serialize_html([["Characters", u"\u0101"]],
130+
{"encoding": "iso-8859-1"}))
131+
132+
def testStartTagName(self):
133+
self.throwsWithLatin1([["StartTag", u"http://www.w3.org/1999/xhtml", u"\u0101", []]])
134+
135+
def testEmptyTagName(self):
136+
self.throwsWithLatin1([["EmptyTag", u"http://www.w3.org/1999/xhtml", u"\u0101", []]])
137+
138+
def testAttributeName(self):
139+
self.throwsWithLatin1([["StartTag", u"http://www.w3.org/1999/xhtml", u"span", [{"namespace": None, "name": u"\u0101", "value": u"potato"}]]])
140+
141+
def testAttributeValue(self):
142+
self.assertEquals("<span potato=&amacr;>", serialize_html([["StartTag", u"http://www.w3.org/1999/xhtml", u"span",
143+
[{"namespace": None, "name": u"potato", "value": u"\u0101"}]]],
144+
{"encoding": "iso-8859-1"}))
145+
146+
def testEndTagName(self):
147+
self.throwsWithLatin1([["EndTag", u"http://www.w3.org/1999/xhtml", u"\u0101"]])
148+
149+
def testComment(self):
150+
self.throwsWithLatin1([["Comment", u"\u0101"]])
151+
109152

110153
class LxmlTestCase(unittest.TestCase):
111154
def setUp(self):
@@ -146,6 +189,7 @@ def buildBasicTestSuite():
146189

147190
def buildTestSuite():
148191
allTests = [buildBasicTestSuite()]
192+
allTests.append(unittest.TestLoader().loadTestsFromTestCase(EncodingTestCase))
149193
if "lxml" in optionals_loaded:
150194
allTests.append(unittest.TestLoader().loadTestsFromTestCase(LxmlTestCase))
151195

0 commit comments

Comments
 (0)