diff options
| author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
| commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
| tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/test/html.py | |
| parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/test/html.py')
| -rw-r--r-- | django/test/html.py | 106 |
1 files changed, 70 insertions, 36 deletions
diff --git a/django/test/html.py b/django/test/html.py index 07e986439b..87e213d651 100644 --- a/django/test/html.py +++ b/django/test/html.py @@ -7,32 +7,52 @@ from django.utils.regex_helper import _lazy_re_compile # ASCII whitespace is U+0009 TAB, U+000A LF, U+000C FF, U+000D CR, or U+0020 # SPACE. # https://infra.spec.whatwg.org/#ascii-whitespace -ASCII_WHITESPACE = _lazy_re_compile(r'[\t\n\f\r ]+') +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', + "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', + "truespeed", } def normalize_whitespace(string): - return ASCII_WHITESPACE.sub(' ', string) + return ASCII_WHITESPACE.sub(" ", string) def normalize_attributes(attributes): normalized = [] for name, value in attributes: - if name == 'class' and value: + 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 - )) + value = " ".join( + sorted(value for value in ASCII_WHITESPACE.split(value) if value) + ) # Boolean attributes without a value is same as attribute with value # that equals the attributes name. For example: # <input checked> == <input checked="checked"> @@ -40,7 +60,7 @@ def normalize_attributes(attributes): if not value or value == name: value = None elif value is None: - value = '' + value = "" normalized.append((name, value)) return normalized @@ -80,11 +100,11 @@ class Element: for i, child in enumerate(self.children): if isinstance(child, str): self.children[i] = child.strip() - elif hasattr(child, 'finalize'): + elif hasattr(child, "finalize"): child.finalize() def __eq__(self, element): - if not hasattr(element, 'name') or self.name != element.name: + if not hasattr(element, "name") or self.name != element.name: return False if self.attributes != element.attributes: return False @@ -142,21 +162,23 @@ class Element: return self.children[key] def __str__(self): - output = '<%s' % self.name + output = "<%s" % self.name for key, value in self.attributes: if value is not None: output += ' %s="%s"' % (key, value) else: - output += ' %s' % key + output += " %s" % key if self.children: - output += '>\n' - output += ''.join([ - html.escape(c) if isinstance(c, str) else str(c) - for c in self.children - ]) - output += '\n</%s>' % self.name + output += ">\n" + output += "".join( + [ + html.escape(c) if isinstance(c, str) else str(c) + for c in self.children + ] + ) + output += "\n</%s>" % self.name else: - output += '>' + output += ">" return output def __repr__(self): @@ -168,10 +190,9 @@ class RootElement(Element): super().__init__(None, ()) def __str__(self): - return ''.join([ - html.escape(c) if isinstance(c, str) else str(c) - for c in self.children - ]) + return "".join( + [html.escape(c) if isinstance(c, str) else str(c) for c in self.children] + ) class HTMLParseError(Exception): @@ -181,10 +202,23 @@ class HTMLParseError(Exception): class Parser(HTMLParser): # https://html.spec.whatwg.org/#void-elements SELF_CLOSING_TAGS = { - 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', - 'param', 'source', 'track', 'wbr', + "area", + "base", + "br", + "col", + "embed", + "hr", + "img", + "input", + "link", + "meta", + "param", + "source", + "track", + "wbr", # Deprecated tags - 'frame', 'spacer', + "frame", + "spacer", } def __init__(self): @@ -201,9 +235,9 @@ class Parser(HTMLParser): position = self.element_positions[element] if position is None: position = self.getpos() - if hasattr(position, 'lineno'): + if hasattr(position, "lineno"): position = position.lineno, position.offset - return 'Line %d, Column %d' % position + return "Line %d, Column %d" % position @property def current(self): @@ -227,13 +261,13 @@ class Parser(HTMLParser): def handle_endtag(self, tag): if not self.open_tags: - self.error("Unexpected end tag `%s` (%s)" % ( - tag, self.format_position())) + self.error("Unexpected end tag `%s` (%s)" % (tag, self.format_position())) element = self.open_tags.pop() while element.name != tag: if not self.open_tags: - self.error("Unexpected end tag `%s` (%s)" % ( - tag, self.format_position())) + self.error( + "Unexpected end tag `%s` (%s)" % (tag, self.format_position()) + ) element = self.open_tags.pop() def handle_data(self, data): |
