1
1
import new
2
2
import re
3
+ import types
3
4
4
5
import _base
5
6
from html5lib import ihatexml
@@ -91,15 +92,15 @@ def _setChildNodes(self, value):
91
92
92
93
def hasContent (self ):
93
94
"""Return true if the node has children or text"""
94
- return bool (self ._element .text or self ._element . getchildren ( ))
95
+ return bool (self ._element .text or len ( self ._element ))
95
96
96
97
def appendChild (self , node ):
97
98
self ._childNodes .append (node )
98
99
self ._element .append (node ._element )
99
100
node .parent = self
100
101
101
102
def insertBefore (self , node , refNode ):
102
- index = self ._element . getchildren ( ).index (refNode ._element )
103
+ index = list ( self ._element ).index (refNode ._element )
103
104
self ._element .insert (index , node ._element )
104
105
node .parent = self
105
106
@@ -119,7 +120,7 @@ def insertText(self, data, insertBefore=None):
119
120
self ._element [- 1 ].tail += data
120
121
else :
121
122
#Insert the text before the specified node
122
- children = self ._element . getchildren ( )
123
+ children = list ( self ._element )
123
124
index = children .index (insertBefore ._element )
124
125
if index > 0 :
125
126
if not self ._element [index - 1 ].tail :
@@ -217,9 +218,10 @@ def serializeElement(element, indent=0):
217
218
rv .append ("|%s\" %s\" " % (' ' * (indent + 2 ), element .text ))
218
219
if element .tail :
219
220
finalText = element .tail
220
- elif type ( element .tag ) == type ( ElementTree .Comment ) :
221
+ elif element .tag == ElementTree .Comment :
221
222
rv .append ("|%s<!-- %s -->" % (' ' * indent , element .text ))
222
223
else :
224
+ assert type (element .tag ) in types .StringTypes , "Expected unicode, got %s" % type (element .tag )
223
225
nsmatch = tag_regexp .match (element .tag )
224
226
225
227
if nsmatch is None :
@@ -247,7 +249,7 @@ def serializeElement(element, indent=0):
247
249
if element .text :
248
250
rv .append ("|%s\" %s\" " % (' ' * (indent + 2 ), element .text ))
249
251
indent += 2
250
- for child in element . getchildren () :
252
+ for child in element :
251
253
serializeElement (child , indent )
252
254
if element .tail :
253
255
rv .append ("|%s\" %s\" " % (' ' * (indent - 2 ), element .tail ))
@@ -281,7 +283,7 @@ def serializeElement(element):
281
283
if element .tail :
282
284
finalText = element .tail
283
285
284
- for child in element . getchildren () :
286
+ for child in element :
285
287
serializeElement (child )
286
288
287
289
elif type (element .tag ) == type (ElementTree .Comment ):
@@ -298,7 +300,7 @@ def serializeElement(element):
298
300
if element .text :
299
301
rv .append (element .text )
300
302
301
- for child in element . getchildren () :
303
+ for child in element :
302
304
serializeElement (child )
303
305
304
306
rv .append ("</%s>" % (element .tag ,))
0 commit comments