mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
check-templates: Complain about stray text.
We disallow this HTML:
junk-text-before-open-tag<p>
This is a paragraph.
</p>
We rarely see the above mistake, but we want to eliminate
the possibility to be somewhat rigorous, and so that we
can eliminate a pretty-printer mis-feature.
This commit is contained in:
@@ -442,8 +442,8 @@
|
|||||||
<li>
|
<li>
|
||||||
<div class="list-content">
|
<div class="list-content">
|
||||||
Use topics to manage support workflows, answer
|
Use topics to manage support workflows, answer
|
||||||
questions, and collaborate to investigate issues. <a
|
questions, and collaborate to investigate issues.
|
||||||
href="/help/resolve-a-topic">Mark the topic ✓
|
<a href="/help/resolve-a-topic">Mark the topic ✓
|
||||||
resolved</a> when done!
|
resolved</a> when done!
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
|||||||
@@ -88,11 +88,9 @@
|
|||||||
both the app and the website is extremely positive!
|
both the app and the website is extremely positive!
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<div class="author">
|
<div class="author">
|
||||||
— <a
|
— <a href="https://www.imperial.ac.uk/people/k.buzzard">Kevin Buzzard</a>,
|
||||||
href="https://www.imperial.ac.uk/people/k.buzzard">Kevin
|
Professor of Pure Mathematics at
|
||||||
Buzzard</a>, Professor of Pure Mathematics at <a
|
<a href="https://www.imperial.ac.uk/">Imperial College London</a>
|
||||||
href="https://www.imperial.ac.uk/">Imperial College
|
|
||||||
London</a>
|
|
||||||
</div>
|
</div>
|
||||||
<a class="case-study-link" href="/case-studies/lean/"
|
<a class="case-study-link" href="/case-studies/lean/"
|
||||||
target="_blank">How the Lean prover
|
target="_blank">How the Lean prover
|
||||||
|
|||||||
@@ -223,6 +223,10 @@ HTML_VOID_TAGS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def indent_level(s: str) -> int:
|
||||||
|
return len(s) - len(s.lstrip())
|
||||||
|
|
||||||
|
|
||||||
def validate(
|
def validate(
|
||||||
fn: Optional[str] = None, text: Optional[str] = None, check_indent: bool = True
|
fn: Optional[str] = None, text: Optional[str] = None, check_indent: bool = True
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -235,6 +239,8 @@ def validate(
|
|||||||
with open(fn) as f:
|
with open(fn) as f:
|
||||||
text = f.read()
|
text = f.read()
|
||||||
|
|
||||||
|
lines = text.split("\n")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
tokens = tokenize(text)
|
tokens = tokenize(text)
|
||||||
except FormattedException as e:
|
except FormattedException as e:
|
||||||
@@ -294,6 +300,15 @@ def validate(
|
|||||||
elif check_indent and (end_line > start_line + max_lines):
|
elif check_indent and (end_line > start_line + max_lines):
|
||||||
if end_col != start_col:
|
if end_col != start_col:
|
||||||
problem = "Bad indentation."
|
problem = "Bad indentation."
|
||||||
|
|
||||||
|
if end_line >= start_line + 2:
|
||||||
|
# We have 3+ lines in the tag's block.
|
||||||
|
start_row_text = lines[start_line - 1]
|
||||||
|
start_indent = indent_level(start_row_text)
|
||||||
|
if start_indent != start_col - 1 and start_row_text[start_indent] not in "<{":
|
||||||
|
junk = start_row_text[start_indent : start_col - 1]
|
||||||
|
problem = f"There is junk before the start tag: {junk}"
|
||||||
|
|
||||||
if problem:
|
if problem:
|
||||||
raise TemplateParserException(
|
raise TemplateParserException(
|
||||||
f"""
|
f"""
|
||||||
|
|||||||
@@ -106,33 +106,7 @@ GOOD_HTML2 = """
|
|||||||
</html>
|
</html>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
BAD_HTML3 = """
|
# The old GOOD_HTML3 test was flawed.
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
{{# foobar area}}
|
|
||||||
foobarfoobar<blockquote>
|
|
||||||
<p>
|
|
||||||
FOOBAR
|
|
||||||
</p>
|
|
||||||
</blockquote>
|
|
||||||
{{/ foobar area}}
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
"""
|
|
||||||
|
|
||||||
GOOD_HTML3 = """
|
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
{{# foobar area}}
|
|
||||||
foobarfoobar<blockquote>
|
|
||||||
<p>
|
|
||||||
FOOBAR
|
|
||||||
</p>
|
|
||||||
</blockquote>
|
|
||||||
{{/ foobar area}}
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
"""
|
|
||||||
|
|
||||||
BAD_HTML4 = """
|
BAD_HTML4 = """
|
||||||
<div>
|
<div>
|
||||||
@@ -478,7 +452,6 @@ class TestPrettyPrinter(unittest.TestCase):
|
|||||||
self.compare(pretty_print_html(BAD_HTML), GOOD_HTML)
|
self.compare(pretty_print_html(BAD_HTML), GOOD_HTML)
|
||||||
self.compare(pretty_print_html(BAD_HTML1), GOOD_HTML1)
|
self.compare(pretty_print_html(BAD_HTML1), GOOD_HTML1)
|
||||||
self.compare(pretty_print_html(BAD_HTML2), GOOD_HTML2)
|
self.compare(pretty_print_html(BAD_HTML2), GOOD_HTML2)
|
||||||
self.compare(pretty_print_html(BAD_HTML3), GOOD_HTML3)
|
|
||||||
self.compare(pretty_print_html(BAD_HTML4), GOOD_HTML4)
|
self.compare(pretty_print_html(BAD_HTML4), GOOD_HTML4)
|
||||||
self.compare(pretty_print_html(BAD_HTML5), GOOD_HTML5)
|
self.compare(pretty_print_html(BAD_HTML5), GOOD_HTML5)
|
||||||
self.compare(pretty_print_html(BAD_HTML6), GOOD_HTML6)
|
self.compare(pretty_print_html(BAD_HTML6), GOOD_HTML6)
|
||||||
|
|||||||
Reference in New Issue
Block a user