summaryrefslogtreecommitdiff
path: root/django/test/html.py
diff options
context:
space:
mode:
authorBaptiste Mispelon <bmispelon@gmail.com>2021-03-19 00:43:38 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-03-19 20:41:57 +0100
commit41e6b2a3c5e723256506b9ff49437d52a1f3bf43 (patch)
tree5afcbf95e26a870e4ddcfbbd2a0e96b6aaed1dd6 /django/test/html.py
parent98abf80cde0a7b6846f2612ee8ec9189adccdc3c (diff)
Fixed #32556 -- Fixed handling empty string as non-boolean attributes value by assertHTMLEqual().
Diffstat (limited to 'django/test/html.py')
-rw-r--r--django/test/html.py25
1 files changed, 19 insertions, 6 deletions
diff --git a/django/test/html.py b/django/test/html.py
index 4620b133dd..f1cbadfc54 100644
--- a/django/test/html.py
+++ b/django/test/html.py
@@ -9,6 +9,16 @@ from django.utils.regex_helper import _lazy_re_compile
# https://infra.spec.whatwg.org/#ascii-whitespace
ASCII_WHITESPACE = _lazy_re_compile(r'[\t\n\f\r ]+')
+# https://html.spec.whatwg.org/#attributes-3
+BOOLEAN_ATTRIBUTES = {
+ 'allowfullscreen', 'async', 'autofocus', 'autoplay', 'checked', 'controls',
+ 'default', 'defer ', 'disabled', 'formnovalidate', 'hidden', 'ismap',
+ 'itemscope', 'loop', 'multiple', 'muted', 'nomodule', 'novalidate', 'open',
+ 'playsinline', 'readonly', 'required', 'reversed', 'selected',
+ # Attributes for deprecated tags.
+ 'truespeed',
+}
+
def normalize_whitespace(string):
return ASCII_WHITESPACE.sub(' ', string)
@@ -23,11 +33,14 @@ def normalize_attributes(attributes):
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
+ # Boolean attributes without a value is same as attribute with value
+ # that equals the attributes name. For example:
+ # <input checked> == <input checked="checked">
+ if name in BOOLEAN_ATTRIBUTES:
+ if not value or value == name:
+ value = None
+ elif value is None:
+ value = ''
normalized.append((name, value))
return normalized
@@ -131,7 +144,7 @@ class Element:
def __str__(self):
output = '<%s' % self.name
for key, value in self.attributes:
- if value:
+ if value is not None:
output += ' %s="%s"' % (key, value)
else:
output += ' %s' % key