16
16
import re
17
17
import sys
18
18
19
+ try :
20
+ from collections .abc import MutableMapping
21
+ except ImportError :
22
+ from collections import MutableMapping
23
+
19
24
from . import base
20
25
from ..constants import DataLossWarning
21
26
from .. import constants
@@ -185,26 +190,34 @@ def __init__(self, namespaceHTMLElements, fullTree=False):
185
190
infosetFilter = self .infosetFilter = _ihatexml .InfosetFilter (preventDoubleDashComments = True )
186
191
self .namespaceHTMLElements = namespaceHTMLElements
187
192
188
- class Attributes (dict ):
189
- def __init__ (self , element , value = None ):
190
- if value is None :
191
- value = {}
193
+ class Attributes (MutableMapping ):
194
+ def __init__ (self , element ):
192
195
self ._element = element
193
- dict .__init__ (self , value ) # pylint:disable=non-parent-init-called
194
- for key , value in self .items ():
195
- if isinstance (key , tuple ):
196
- name = "{%s}%s" % (key [2 ], infosetFilter .coerceAttribute (key [1 ]))
197
- else :
198
- name = infosetFilter .coerceAttribute (key )
199
- self ._element ._element .attrib [name ] = value
200
196
201
- def __setitem__ (self , key , value ):
202
- dict .__setitem__ (self , key , value )
197
+ def _coerceKey (self , key ):
203
198
if isinstance (key , tuple ):
204
199
name = "{%s}%s" % (key [2 ], infosetFilter .coerceAttribute (key [1 ]))
205
200
else :
206
201
name = infosetFilter .coerceAttribute (key )
207
- self ._element ._element .attrib [name ] = value
202
+ return name
203
+
204
+ def __getitem__ (self , key ):
205
+ return self ._element ._element .attrib [self ._coerceKey (key )]
206
+
207
+ def __setitem__ (self , key , value ):
208
+ self ._element ._element .attrib [self ._coerceKey (key )] = value
209
+
210
+ def __delitem__ (self , key ):
211
+ del self ._element ._element .attrib [self ._coerceKey (key )]
212
+
213
+ def __iter__ (self ):
214
+ return iter (self ._element ._element .attrib )
215
+
216
+ def __len__ (self ):
217
+ return len (self ._element ._element .attrib )
218
+
219
+ def clear (self ):
220
+ return self ._element ._element .attrib .clear ()
208
221
209
222
class Element (builder .Element ):
210
223
def __init__ (self , name , namespace ):
@@ -225,17 +238,22 @@ def _getName(self):
225
238
def _getAttributes (self ):
226
239
return self ._attributes
227
240
228
- def _setAttributes (self , attributes ):
229
- self ._attributes = Attributes (self , attributes )
241
+ def _setAttributes (self , value ):
242
+ attributes = self .attributes
243
+ attributes .clear ()
244
+ attributes .update (value )
230
245
231
246
attributes = property (_getAttributes , _setAttributes )
232
247
233
248
def insertText (self , data , insertBefore = None ):
234
249
data = infosetFilter .coerceCharacters (data )
235
250
builder .Element .insertText (self , data , insertBefore )
236
251
237
- def appendChild (self , child ):
238
- builder .Element .appendChild (self , child )
252
+ def cloneNode (self ):
253
+ element = type (self )(self .name , self .namespace )
254
+ if self ._element .attrib :
255
+ element ._element .attrib .update (self ._element .attrib )
256
+ return element
239
257
240
258
class Comment (builder .Comment ):
241
259
def __init__ (self , data ):
0 commit comments