瀏覽代碼

Refs #30686 -- Moved Parser.SELF_CLOSING_TAGS to django.utils.html.VOID_ELEMENTS

David Smith 1 年之前
父節點
當前提交
1d0dfc0b92
共有 3 個文件被更改,包括 27 次插入44 次删除
  1. 3 23
      django/test/html.py
  2. 21 0
      django/utils/html.py
  3. 3 21
      tests/test_utils/tests.py

+ 3 - 23
django/test/html.py

@@ -2,6 +2,7 @@
 import html
 from html.parser import HTMLParser
 
+from django.utils.html import VOID_ELEMENTS
 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
@@ -200,27 +201,6 @@ 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",
-        # Deprecated tags
-        "frame",
-        "spacer",
-    }
-
     def __init__(self):
         super().__init__()
         self.root = RootElement()
@@ -248,14 +228,14 @@ class Parser(HTMLParser):
 
     def handle_startendtag(self, tag, attrs):
         self.handle_starttag(tag, attrs)
-        if tag not in self.SELF_CLOSING_TAGS:
+        if tag not in VOID_ELEMENTS:
             self.handle_endtag(tag)
 
     def handle_starttag(self, tag, attrs):
         attrs = normalize_attributes(attrs)
         element = Element(tag, attrs)
         self.current.append(element)
-        if tag not in self.SELF_CLOSING_TAGS:
+        if tag not in VOID_ELEMENTS:
             self.open_tags.append(element)
         self.element_positions[element] = self.getpos()
 

+ 21 - 0
django/utils/html.py

@@ -15,6 +15,27 @@ from django.utils.regex_helper import _lazy_re_compile
 from django.utils.safestring import SafeData, SafeString, mark_safe
 from django.utils.text import normalize_newlines
 
+# https://html.spec.whatwg.org/#void-elements
+VOID_ELEMENTS = {
+    "area",
+    "base",
+    "br",
+    "col",
+    "embed",
+    "hr",
+    "img",
+    "input",
+    "link",
+    "meta",
+    "param",
+    "source",
+    "track",
+    "wbr",
+    # Deprecated tags.
+    "frame",
+    "spacer",
+}
+
 
 @keep_lazy(SafeString)
 def escape(text):

+ 3 - 21
tests/test_utils/tests.py

@@ -46,6 +46,7 @@ from django.test.utils import (
 )
 from django.urls import NoReverseMatch, path, reverse, reverse_lazy
 from django.utils.deprecation import RemovedInDjango51Warning
+from django.utils.html import VOID_ELEMENTS
 from django.utils.version import PY311
 
 from .models import Car, Person, PossessedCar
@@ -657,27 +658,8 @@ class HTMLEqualTests(SimpleTestCase):
         self.assertEqual(len(dom.children), 1)
         self.assertEqual(dom.children[0], "<p>foo</p> '</scr'+'ipt>' <span>bar</span>")
 
-    def test_self_closing_tags(self):
-        self_closing_tags = [
-            "area",
-            "base",
-            "br",
-            "col",
-            "embed",
-            "hr",
-            "img",
-            "input",
-            "link",
-            "meta",
-            "param",
-            "source",
-            "track",
-            "wbr",
-            # Deprecated tags
-            "frame",
-            "spacer",
-        ]
-        for tag in self_closing_tags:
+    def test_void_elements(self):
+        for tag in VOID_ELEMENTS:
             with self.subTest(tag):
                 dom = parse_html("<p>Hello <%s> world</p>" % tag)
                 self.assertEqual(len(dom.children), 3)