diff options
| author | Baptiste Mispelon <bmispelon@gmail.com> | 2021-03-19 12:30:16 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-03-19 20:41:53 +0100 |
| commit | 98abf80cde0a7b6846f2612ee8ec9189adccdc3c (patch) | |
| tree | a9b78bc835411867e257e85c2d3eb39e69ed5130 /django/test/html.py | |
| parent | d40402cfb023801f0d83f19747e30b13096e3636 (diff) | |
Refs #32556 -- Added django.test.html.normalize_attributes().
Diffstat (limited to 'django/test/html.py')
| -rw-r--r-- | django/test/html.py | 42 |
1 files changed, 20 insertions, 22 deletions
diff --git a/django/test/html.py b/django/test/html.py index b0f63d25d6..4620b133dd 100644 --- a/django/test/html.py +++ b/django/test/html.py @@ -14,6 +14,24 @@ def normalize_whitespace(string): return ASCII_WHITESPACE.sub(' ', string) +def normalize_attributes(attributes): + normalized = [] + for name, value in attributes: + if name == 'class' and value: + # Special case handling of 'class' attribute, so that comparisons + # of DOM instances are not sensitive to ordering of classes. + value = ' '.join(sorted( + value for value in ASCII_WHITESPACE.split(value) if value + )) + # Attributes without a value is same as attribute with value that + # equals the attributes name: + # <input checked> == <input checked="checked"> + if not value or value == name: + value = None + normalized.append((name, value)) + return normalized + + class Element: def __init__(self, name, attributes): self.name = name @@ -55,21 +73,8 @@ class Element: def __eq__(self, element): if not hasattr(element, 'name') or self.name != element.name: return False - if len(self.attributes) != len(element.attributes): - return False if self.attributes != element.attributes: - # attributes without a value is same as attribute with value that - # equals the attributes name: - # <input checked> == <input checked="checked"> - for i in range(len(self.attributes)): - attr, value = self.attributes[i] - other_attr, other_value = element.attributes[i] - if not value: - value = attr - if not other_value: - other_value = other_attr - if attr != other_attr or value != other_value: - return False + return False return self.children == element.children def __hash__(self): @@ -194,14 +199,7 @@ class Parser(HTMLParser): self.handle_endtag(tag) def handle_starttag(self, tag, attrs): - # Special case handling of 'class' attribute, so that comparisons of DOM - # instances are not sensitive to ordering of classes. - attrs = [ - (name, ' '.join(sorted(value for value in ASCII_WHITESPACE.split(value) if value))) - if name == "class" - else (name, value) - for name, value in attrs - ] + attrs = normalize_attributes(attrs) element = Element(tag, attrs) self.current.append(element) if tag not in self.SELF_CLOSING_TAGS: |
