Skip to content

Commit 2812e44

Browse files
committed
Make DOM treebuilder's AttrList return a MutableMapping
1 parent 143b0d4 commit 2812e44

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

html5lib/treebuilders/dom.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import, division, unicode_literals
22

33

4+
from collections import MutableMapping
45
from xml.dom import minidom, Node
56
import weakref
67

@@ -13,34 +14,41 @@
1314
def getDomBuilder(DomImplementation):
1415
Dom = DomImplementation
1516

16-
class AttrList(object):
17+
class AttrList(MutableMapping):
1718
def __init__(self, element):
1819
self.element = element
1920

2021
def __iter__(self):
21-
return list(self.element.attributes.items()).__iter__()
22+
return iter(self.element.attributes.keys())
2223

2324
def __setitem__(self, name, value):
24-
self.element.setAttribute(name, value)
25+
if isinstance(name, tuple):
26+
raise NotImplementedError
27+
else:
28+
attr = self.element.ownerDocument.createAttribute(name)
29+
attr.value = value
30+
self.element.attributes[name] = attr
2531

2632
def __len__(self):
27-
return len(list(self.element.attributes.items()))
33+
return len(self.element.attributes)
2834

2935
def items(self):
30-
return [(item[0], item[1]) for item in
31-
list(self.element.attributes.items())]
36+
return list(self.element.attributes.items())
3237

33-
def keys(self):
34-
return list(self.element.attributes.keys())
38+
def values(self):
39+
return list(self.element.attributes.values())
3540

3641
def __getitem__(self, name):
37-
return self.element.getAttribute(name)
42+
if isinstance(name, tuple):
43+
raise NotImplementedError
44+
else:
45+
return self.element.attributes[name].value
3846

39-
def __contains__(self, name):
47+
def __delitem__(self, name):
4048
if isinstance(name, tuple):
4149
raise NotImplementedError
4250
else:
43-
return self.element.hasAttribute(name)
51+
del self.element.attributes[name]
4452

4553
class NodeBuilder(_base.Node):
4654
def __init__(self, element):

0 commit comments

Comments
 (0)