summaryrefslogtreecommitdiffhomepage
path: root/libs/html5lib/treebuilders/etree.py
diff options
context:
space:
mode:
Diffstat (limited to 'libs/html5lib/treebuilders/etree.py')
-rw-r--r--libs/html5lib/treebuilders/etree.py27
1 files changed, 15 insertions, 12 deletions
diff --git a/libs/html5lib/treebuilders/etree.py b/libs/html5lib/treebuilders/etree.py
index cb1d4aef5..086bed4ee 100644
--- a/libs/html5lib/treebuilders/etree.py
+++ b/libs/html5lib/treebuilders/etree.py
@@ -5,6 +5,8 @@ from six import text_type
import re
+from copy import copy
+
from . import base
from .. import _ihatexml
from .. import constants
@@ -61,16 +63,17 @@ def getETreeBuilder(ElementTreeImplementation, fullTree=False):
return self._element.attrib
def _setAttributes(self, attributes):
- # Delete existing attributes first
- # XXX - there may be a better way to do this...
- for key in list(self._element.attrib.keys()):
- del self._element.attrib[key]
- for key, value in attributes.items():
- if isinstance(key, tuple):
- name = "{%s}%s" % (key[2], key[1])
- else:
- name = key
- self._element.set(name, value)
+ el_attrib = self._element.attrib
+ el_attrib.clear()
+ if attributes:
+ # calling .items _always_ allocates, and the above truthy check is cheaper than the
+ # allocation on average
+ for key, value in attributes.items():
+ if isinstance(key, tuple):
+ name = "{%s}%s" % (key[2], key[1])
+ else:
+ name = key
+ el_attrib[name] = value
attributes = property(_getAttributes, _setAttributes)
@@ -129,8 +132,8 @@ def getETreeBuilder(ElementTreeImplementation, fullTree=False):
def cloneNode(self):
element = type(self)(self.name, self.namespace)
- for name, value in self.attributes.items():
- element.attributes[name] = value
+ if self._element.attrib:
+ element._element.attrib = copy(self._element.attrib)
return element
def reparentChildren(self, newParent):