@@ -75,6 +75,15 @@ def _convertAttrib(self, attribs):
7575return 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+
7887class TestCase (unittest .TestCase ):
7988def addTest (cls ,name ,description ,input ,expected ,xhtml ,options ):
8089func = lambda self :self .mockTest (input ,options ,expected ,xhtml )
@@ -83,29 +92,63 @@ def addTest(cls, name, description, input, expected, xhtml, options):
8392addTest = classmethod (addTest )
8493
8594def mockTest (self ,input ,options ,expected ,xhtml ):
86- result = self . serialize_html (input ,options )
95+ result = serialize_html (input ,options )
8796if len (expected )== 1 :
8897self .assertEquals (expected [0 ],result ,"Expected:\n %s\n Actual:\n %s\n Options\n xhtml:False\n %s" % (expected [0 ],result ,str (options )))
8998elif result not in expected :
9099self .fail ("Expected: %s, Received: %s" % (expected ,result ))
91100
92101if not xhtml :return
93102
94- result = self . serialize_xhtml (input ,options )
103+ result = serialize_xhtml (input ,options )
95104if len (xhtml )== 1 :
96105self .assertEquals (xhtml [0 ],result ,"Expected:\n %s\n Actual:\n %s\n Options\n xhtml:True\n %s" % (xhtml [0 ],result ,str (options )))
97106elif result not in xhtml :
98107self .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>ā" ,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+
109152
110153class LxmlTestCase (unittest .TestCase ):
111154def setUp (self ):
@@ -146,6 +189,7 @@ def buildBasicTestSuite():
146189
147190def buildTestSuite ():
148191allTests = [buildBasicTestSuite ()]
192+ allTests .append (unittest .TestLoader ().loadTestsFromTestCase (EncodingTestCase ))
149193if "lxml" in optionals_loaded :
150194allTests .append (unittest .TestLoader ().loadTestsFromTestCase (LxmlTestCase ))
151195