template_parser: Add parsing support for self closing tags as per HTML5.

In this commit we add support for some tags which are also called
void-elements according to
http://w3c.github.io/html/syntax.html#void-elements to be parsed by
our template parser and get tagged as singleton_html_tags.

Fixes: #8387.
This commit is contained in:
Aditya Bansal
2018-02-16 02:46:40 +05:30
committed by showell
parent 271cfd4d7a
commit 6fce1d7834
9 changed files with 46 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
from typing import Callable, List, Optional
from typing import Callable, List, Optional, Text
class TemplateParserException(Exception):
def __init__(self, message):
@@ -112,7 +112,7 @@ def tokenize(text):
if is_special_html_tag(s, tag):
kind = 'html_special'
elif s.endswith('/>'):
elif is_self_closing_html_tag(s, tag):
kind = 'html_singleton'
else:
kind = 'html_start'
@@ -272,6 +272,24 @@ def is_special_html_tag(s, tag):
# type: (str, str) -> bool
return tag in ['link', 'meta', '!DOCTYPE']
def is_self_closing_html_tag(s: Text, tag: Text) -> bool:
self_closing_tag = tag in [
'area',
'base',
'br',
'col',
'embed',
'hr',
'img',
'input',
'param',
'source',
'track',
'wbr',
]
singleton_tag = s.endswith('/>')
return self_closing_tag or singleton_tag
def is_django_block_tag(tag):
# type: (str) -> bool
return tag in [