Simplify 'default_attributes' handling a bit

This commit is contained in:
Eduard Braun 2018-04-15 18:33:46 +02:00
parent 20dcbcbe64
commit 0ec0732447

View file

@ -1866,19 +1866,16 @@ default_attributes = [
DefaultAttribute('yChannelSelector', 'A', elements=['feDisplacementMap']) DefaultAttribute('yChannelSelector', 'A', elements=['feDisplacementMap'])
] ]
default_attributes_restricted_by_tag = defaultdict(list) # split to increase lookup performance
default_attributes_unrestricted = [] default_attributes_universal = [] # list containing attributes valid for all elements
default_attributes_per_element = defaultdict(list) # dict containing lists of attributes valid for individual elements
for attr in default_attributes: for default_attribute in default_attributes:
if attr.elements is None: if default_attribute.elements is None:
# Applies to all tags default_attributes_universal.append(default_attribute)
default_attributes_unrestricted.append(attr)
continue
if type(attr.elements) is str:
default_attributes_restricted_by_tag[attr.elements].append(attr)
else: else:
for tag in attr.elements: for element in default_attribute.elements:
default_attributes_restricted_by_tag[tag].append(attr) default_attributes_per_element[element].append(default_attribute)
print(len(default_attributes_universal))
def taint(taintedSet, taintedAttribute): def taint(taintedSet, taintedAttribute):
@ -1896,6 +1893,8 @@ def taint(taintedSet, taintedAttribute):
def removeDefaultAttributeValue(node, attribute): def removeDefaultAttributeValue(node, attribute):
""" """
Removes the DefaultAttribute 'attribute' from 'node' if specified conditions are fulfilled Removes the DefaultAttribute 'attribute' from 'node' if specified conditions are fulfilled
Warning: Does NOT check if the attribute is actually valid for the passed element type for increased preformance!
""" """
if not node.hasAttribute(attribute.name): if not node.hasAttribute(attribute.name):
return 0 return 0
@ -1928,16 +1927,15 @@ def removeDefaultAttributeValues(node, options, tainted=set()):
if node.nodeType != Node.ELEMENT_NODE: if node.nodeType != Node.ELEMENT_NODE:
return 0 return 0
# Remove all default attributes. The remoteDefaultAttributeValue # Conditionally remove all default attributes defined in 'default_attributes' (a list of 'DefaultAttribute's)
# function deals with "if/when" we are allowed to remove the #
# attribute as long as we supply it only with attributes that are # For increased performance do not iterate the whole list for each element but run only on valid subsets
# applicable for this given node. That part is handled by using # - 'default_attributes_universal' (attributes valid for all elements)
# default_attributes_unrestricted and # - 'default_attributes_per_element' (attributes specific to one specific element type)
# default_attributes_restricted_by_tag for attribute in default_attributes_universal:
for attribute in default_attributes_unrestricted:
num += removeDefaultAttributeValue(node, attribute) num += removeDefaultAttributeValue(node, attribute)
if node.nodeName in default_attributes_restricted_by_tag: if node.nodeName in default_attributes_per_element:
for attribute in default_attributes_restricted_by_tag[node.nodeName]: for attribute in default_attributes_per_element[node.nodeName]:
num += removeDefaultAttributeValue(node, attribute) num += removeDefaultAttributeValue(node, attribute)
# Summarily get rid of default properties # Summarily get rid of default properties