Skip to content

Commit 1d0dfc0

Browse files
smithdc1felixxm
authored andcommitted
Refs #30686 -- Moved Parser.SELF_CLOSING_TAGS to django.utils.html.VOID_ELEMENTS
1 parent 6f1b8c0 commit 1d0dfc0

File tree

3 files changed

+27
-44
lines changed

3 files changed

+27
-44
lines changed

django/test/html.py

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import html
33
from html.parser import HTMLParser
44

5+
from django.utils.html import VOID_ELEMENTS
56
from django.utils.regex_helper import _lazy_re_compile
67

78
# 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):
200201

201202

202203
class Parser(HTMLParser):
203-
# https://html.spec.whatwg.org/#void-elements
204-
SELF_CLOSING_TAGS = {
205-
"area",
206-
"base",
207-
"br",
208-
"col",
209-
"embed",
210-
"hr",
211-
"img",
212-
"input",
213-
"link",
214-
"meta",
215-
"param",
216-
"source",
217-
"track",
218-
"wbr",
219-
# Deprecated tags
220-
"frame",
221-
"spacer",
222-
}
223-
224204
def __init__(self):
225205
super().__init__()
226206
self.root = RootElement()
@@ -248,14 +228,14 @@ def current(self):
248228

249229
def handle_startendtag(self, tag, attrs):
250230
self.handle_starttag(tag, attrs)
251-
if tag not in self.SELF_CLOSING_TAGS:
231+
if tag not in VOID_ELEMENTS:
252232
self.handle_endtag(tag)
253233

254234
def handle_starttag(self, tag, attrs):
255235
attrs = normalize_attributes(attrs)
256236
element = Element(tag, attrs)
257237
self.current.append(element)
258-
if tag not in self.SELF_CLOSING_TAGS:
238+
if tag not in VOID_ELEMENTS:
259239
self.open_tags.append(element)
260240
self.element_positions[element] = self.getpos()
261241

django/utils/html.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@
1515
from django.utils.safestring import SafeData, SafeString, mark_safe
1616
from django.utils.text import normalize_newlines
1717

18+
# https://html.spec.whatwg.org/#void-elements
19+
VOID_ELEMENTS = {
20+
"area",
21+
"base",
22+
"br",
23+
"col",
24+
"embed",
25+
"hr",
26+
"img",
27+
"input",
28+
"link",
29+
"meta",
30+
"param",
31+
"source",
32+
"track",
33+
"wbr",
34+
# Deprecated tags.
35+
"frame",
36+
"spacer",
37+
}
38+
1839

1940
@keep_lazy(SafeString)
2041
def escape(text):

tests/test_utils/tests.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
)
4747
from django.urls import NoReverseMatch, path, reverse, reverse_lazy
4848
from django.utils.deprecation import RemovedInDjango51Warning
49+
from django.utils.html import VOID_ELEMENTS
4950
from django.utils.version import PY311
5051

5152
from .models import Car, Person, PossessedCar
@@ -657,27 +658,8 @@ def test_parse_html_in_script(self):
657658
self.assertEqual(len(dom.children), 1)
658659
self.assertEqual(dom.children[0], "<p>foo</p> '</scr'+'ipt>' <span>bar</span>")
659660

660-
def test_self_closing_tags(self):
661-
self_closing_tags = [
662-
"area",
663-
"base",
664-
"br",
665-
"col",
666-
"embed",
667-
"hr",
668-
"img",
669-
"input",
670-
"link",
671-
"meta",
672-
"param",
673-
"source",
674-
"track",
675-
"wbr",
676-
# Deprecated tags
677-
"frame",
678-
"spacer",
679-
]
680-
for tag in self_closing_tags:
661+
def test_void_elements(self):
662+
for tag in VOID_ELEMENTS:
681663
with self.subTest(tag):
682664
dom = parse_html("<p>Hello <%s> world</p>" % tag)
683665
self.assertEqual(len(dom.children), 3)

0 commit comments

Comments
 (0)