@@ -1287,7 +1287,15 @@ def check(p, expected, namespaces=None):
12871287 {'' : 'http://www.w3.org/2001/XMLSchema' ,
12881288 'ns' : 'http://www.w3.org/2001/XMLSchema' })
12891289
1290- def test_processinginstruction (self ):
1290+ def test_comment_serialization (self ):
1291+ comm = ET .Comment ('<spam> & ham' )
1292+ # comments are not escaped
1293+ self .assertEqual (ET .tostring (comm ), b'<!--<spam> & ham-->' )
1294+ self .assertEqual (ET .tostring (comm , method = 'html' ), b'<!--<spam> & ham-->' )
1295+ # no comments in text serialization
1296+ self .assertEqual (ET .tostring (comm , method = 'text' ), b'' )
1297+
1298+ def test_processinginstruction_serialization (self ):
12911299 # Test ProcessingInstruction directly
12921300
12931301 self .assertEqual (ET .tostring (ET .ProcessingInstruction ('test' , 'instruction' )),
@@ -1296,12 +1304,32 @@ def test_processinginstruction(self):
12961304 b'<?test instruction?>' )
12971305
12981306 # Issue #2746
1299-
1307+ # processing instructions are not escaped
13001308 self .assertEqual (ET .tostring (ET .PI ('test' , '<testing&>' )),
13011309 b'<?test <testing&>?>' )
13021310 self .assertEqual (ET .tostring (ET .PI ('test' , '<testing&>\xe3 ' ), 'latin-1' ),
13031311 b"<?xml version='1.0' encoding='latin-1'?>\n "
13041312 b"<?test <testing&>\xe3 ?>" )
1313+ pi = ET .PI ('test' , 'ham & eggs < spam' )
1314+ self .assertEqual (ET .tostring (pi ), b'<?test ham & eggs < spam?>' )
1315+ self .assertEqual (ET .tostring (pi , method = 'html' ), b'<?test ham & eggs < spam?>' )
1316+ # no processing instructions in text serialization
1317+ self .assertEqual (ET .tostring (pi , method = 'text' ), b'' )
1318+
1319+ def test_empty_attribute_serialization (self ):
1320+ # empty attrs only work in html
1321+ elem = ET .Element ('tag' , attrib = {'attr' : None })
1322+ self .assertRaises (TypeError , ET .tostring , elem )
1323+ self .assertEqual (ET .tostring (elem , method = 'html' ), b'<tag attr></tag>' )
1324+
1325+ @support .subTests ('tag' , ("script" , "style" , "xmp" , "iframe" , "noembed" , "noframes" ))
1326+ def test_html_cdata_elems_serialization (self , tag ):
1327+ # content of raw text elements is not escaped in html
1328+ tag = tag .title ()
1329+ elem = ET .Element (tag )
1330+ elem .text = '<spam>&ham'
1331+ self .assertEqual (ET .tostring (elem , method = 'html' ),
1332+ ('<%s><spam>&ham</%s>' % (tag , tag )).encode ())
13051333
13061334 def test_html_empty_elems_serialization (self ):
13071335 # issue 15970
@@ -1317,6 +1345,14 @@ def test_html_empty_elems_serialization(self):
13171345 method = 'html' )
13181346 self .assertEqual (serialized , expected )
13191347
1348+ def test_html_plaintext_serialization (self ):
1349+ # content of plaintext is not escaped in html
1350+ # no end tag for plaintext
1351+ elem = ET .Element ('PlainText' )
1352+ elem .text = '<spam>&ham'
1353+ self .assertEqual (ET .tostring (elem , method = 'html' ),
1354+ b'<PlainText><spam>&ham' )
1355+
13201356 def test_dump_attribute_order (self ):
13211357 # See BPO 34160
13221358 e = ET .Element ('cirriculum' , status = 'public' , company = 'example' )
0 commit comments