From 951514dd7dd2fafb67e9e9c6930d31b9295741c0 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sat, 18 Apr 2020 09:38:31 +0000 Subject: [PATCH] check-templates: Be stricter about singleton tags. We now forbid tags of the form `` in most places, and we also forbid it even for several void tags. We make exceptions for tags that are already formatted in two different ways in our codebase. This is mostly svg tags, plus these common cases: - br - hr - img - input It would be nice to lock down a convention for these, even though the HTML specification is unopinionated on these. We'll probably want to stay flexible for svg tags, since they are sometimes copy/pasted from other sources (although it's probably rare enough for them that we can tolerate just doing minor edits as needed). --- tools/lib/template_parser.py | 19 +++++++++++++++++-- tools/tests/test_html_branches.py | 4 ++-- tools/tests/test_pretty_print.py | 8 ++++---- tools/tests/test_template_parser.py | 2 +- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/tools/lib/template_parser.py b/tools/lib/template_parser.py index eb3d507960..4b94a49d81 100644 --- a/tools/lib/template_parser.py +++ b/tools/lib/template_parser.py @@ -301,7 +301,21 @@ def validate(fn: Optional[str] = None, text: Optional[str] = None, check_indent: def is_special_html_tag(s: str, tag: str) -> bool: return tag in ['link', 'meta', '!DOCTYPE'] +OPTIONAL_CLOSING_TAGS = [ + 'br', + 'circle', + 'hr', + 'img', + 'input', + 'path', + 'polygon', +] + def is_self_closing_html_tag(s: Text, tag: Text) -> bool: + if s.endswith('/>'): + if tag in OPTIONAL_CLOSING_TAGS: + return True + raise TokenizationException('Singleton tag not allowed', tag) self_closing_tag = tag in [ 'area', 'base', @@ -316,8 +330,9 @@ def is_self_closing_html_tag(s: Text, tag: Text) -> bool: 'track', 'wbr', ] - singleton_tag = s.endswith('/>') - return self_closing_tag or singleton_tag + if self_closing_tag: + return True + return False def is_django_block_tag(tag: str) -> bool: return tag in [ diff --git a/tools/tests/test_html_branches.py b/tools/tests/test_html_branches.py index ccce328923..45dbafad32 100644 --- a/tools/tests/test_html_branches.py +++ b/tools/tests/test_html_branches.py @@ -41,7 +41,7 @@ class TestHtmlBranches(unittest.TestCase): -

Hello
world!

+

Hello
world!

Goodbyeworld!

@@ -83,7 +83,7 @@ class TestHtmlBranches(unittest.TestCase): -

Hello
world!

+

Hello
world!

Goodbyeworld!

diff --git a/tools/tests/test_pretty_print.py b/tools/tests/test_pretty_print.py index ec3f620221..efc250af12 100644 --- a/tools/tests/test_pretty_print.py +++ b/tools/tests/test_pretty_print.py @@ -20,7 +20,7 @@ BAD_HTML = """ -

Hello
world!

+

Hello
world!

Goodbyeworld!

@@ -52,7 +52,7 @@ GOOD_HTML = """ -

Hello
world!

+

Hello
world!

Goodbyeworld!

@@ -338,7 +338,7 @@ BAD_HTML13 = """ {{#if this.is_realm_emoji}} {{else}} -
+
{{/if}} {{/if}}
{{this.count}}
@@ -353,7 +353,7 @@ GOOD_HTML13 = """ {{#if this.is_realm_emoji}} {{else}} -
+
{{/if}} {{/if}}
{{this.count}}
diff --git a/tools/tests/test_template_parser.py b/tools/tests/test_template_parser.py index 894058251f..5928101e47 100644 --- a/tools/tests/test_template_parser.py +++ b/tools/tests/test_template_parser.py @@ -249,7 +249,7 @@ class ParserTest(unittest.TestCase): self.assertEqual(token.kind, 'html_start') self.assertEqual(token.tag, 'a') - tag = '
bla' + tag = '
bla' token = tokenize(tag)[0] self.assertEqual(token.kind, 'html_singleton') self.assertEqual(token.tag, 'br')