@@ -75,6 +75,15 @@ def _convertAttrib(self, attribs):
75
75
return attrs
76
76
77
77
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
+
78
87
class TestCase (unittest .TestCase ):
79
88
def addTest (cls , name , description , input , expected , xhtml , options ):
80
89
func = lambda self : self .mockTest (input , options , expected , xhtml )
@@ -83,29 +92,63 @@ def addTest(cls, name, description, input, expected, xhtml, options):
83
92
addTest = classmethod (addTest )
84
93
85
94
def mockTest (self , input , options , expected , xhtml ):
86
- result = self . serialize_html (input , options )
95
+ result = serialize_html (input , options )
87
96
if len (expected ) == 1 :
88
97
self .assertEquals (expected [0 ], result , "Expected:\n %s\n Actual:\n %s\n Options\n xhtml:False\n %s" % (expected [0 ], result , str (options )))
89
98
elif result not in expected :
90
99
self .fail ("Expected: %s, Received: %s" % (expected , result ))
91
100
92
101
if not xhtml : return
93
102
94
- result = self . serialize_xhtml (input , options )
103
+ result = serialize_xhtml (input , options )
95
104
if len (xhtml ) == 1 :
96
105
self .assertEquals (xhtml [0 ], result , "Expected:\n %s\n Actual:\n %s\n Options\n xhtml:True\n %s" % (xhtml [0 ], result , str (options )))
97
106
elif result not in xhtml :
98
107
self .fail ("Expected: %s, Received: %s" % (xhtml , result ))
99
108
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 )))
104
109
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>ā" , 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 ("ā" , 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=ā>" , 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
+
109
152
110
153
class LxmlTestCase (unittest .TestCase ):
111
154
def setUp (self ):
@@ -146,6 +189,7 @@ def buildBasicTestSuite():
146
189
147
190
def buildTestSuite ():
148
191
allTests = [buildBasicTestSuite ()]
192
+ allTests .append (unittest .TestLoader ().loadTestsFromTestCase (EncodingTestCase ))
149
193
if "lxml" in optionals_loaded :
150
194
allTests .append (unittest .TestLoader ().loadTestsFromTestCase (LxmlTestCase ))
151
195
0 commit comments