2017-02-15 05:39:42 +01:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from tools.lib.pretty_print import pretty_print_html
|
2021-12-02 13:19:19 +01:00
|
|
|
from tools.lib.template_parser import validate
|
2017-02-15 05:39:42 +01:00
|
|
|
|
|
|
|
# Note that GOOD_HTML isn't necessarily beautiful HTML. Apart
|
|
|
|
# from adjusting indentation, we mostly leave things alone to
|
|
|
|
# respect whatever line-wrapping styles were in place before.
|
|
|
|
|
|
|
|
BAD_HTML = """
|
|
|
|
<!-- test -->
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
|
|
|
<html>
|
|
|
|
<!-- test -->
|
|
|
|
<head>
|
|
|
|
<title>Test</title>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<link rel="stylesheet" href="style.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
2021-12-02 13:19:19 +01:00
|
|
|
<div><p>Hello<br />world!</p></div>
|
|
|
|
<p>Goodbye<!-- test -->world!</p>
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td>5</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2017-02-15 05:39:42 +01:00
|
|
|
<pre>
|
|
|
|
print 'hello world'
|
|
|
|
</pre>
|
2021-12-02 13:19:19 +01:00
|
|
|
<div class = "foo"
|
2017-02-15 05:39:42 +01:00
|
|
|
id = "bar"
|
2021-12-02 13:19:19 +01:00
|
|
|
role = "whatever">{{ bla }}
|
|
|
|
</div>
|
2017-02-15 05:39:42 +01:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
<!-- test -->
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML = """
|
|
|
|
<!-- test -->
|
|
|
|
<!DOCTYPE html>
|
|
|
|
|
|
|
|
<html>
|
|
|
|
<!-- test -->
|
|
|
|
<head>
|
|
|
|
<title>Test</title>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<link rel="stylesheet" href="style.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
2021-04-21 00:46:14 +02:00
|
|
|
<div><p>Hello<br />world!</p></div>
|
2017-02-15 05:39:42 +01:00
|
|
|
<p>Goodbye<!-- test -->world!</p>
|
|
|
|
<table>
|
|
|
|
<tr>
|
|
|
|
<td>5</td>
|
|
|
|
</tr>
|
|
|
|
</table>
|
2017-02-23 18:12:52 +01:00
|
|
|
<pre>
|
|
|
|
print 'hello world'
|
|
|
|
</pre>
|
2017-02-15 05:39:42 +01:00
|
|
|
<div class = "foo"
|
2018-04-05 08:38:46 +02:00
|
|
|
id = "bar"
|
2021-12-02 13:19:19 +01:00
|
|
|
role = "whatever">{{ bla }}
|
|
|
|
</div>
|
2017-02-15 05:39:42 +01:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
<!-- test -->
|
|
|
|
"""
|
|
|
|
|
|
|
|
BAD_HTML1 = """
|
|
|
|
<html>
|
2021-12-02 13:19:19 +01:00
|
|
|
<body>
|
|
|
|
foobarfoobarfoo<b>bar</b>
|
|
|
|
</body>
|
2017-02-15 05:39:42 +01:00
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML1 = """
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
foobarfoobarfoo<b>bar</b>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
BAD_HTML2 = """
|
|
|
|
<html>
|
2021-12-02 13:19:19 +01:00
|
|
|
<body>
|
2017-02-15 05:39:42 +01:00
|
|
|
{{# foobar area}}
|
|
|
|
foobarfoobarfoo<b>bar</b>
|
2021-12-02 13:19:19 +01:00
|
|
|
{{/ foobar}}
|
|
|
|
</body>
|
2017-02-15 05:39:42 +01:00
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML2 = """
|
|
|
|
<html>
|
|
|
|
<body>
|
|
|
|
{{# foobar area}}
|
|
|
|
foobarfoobarfoo<b>bar</b>
|
2021-12-02 13:19:19 +01:00
|
|
|
{{/ foobar}}
|
2017-02-15 05:39:42 +01:00
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
"""
|
|
|
|
|
2021-11-23 18:56:11 +01:00
|
|
|
# The old GOOD_HTML3 test was flawed.
|
2017-02-15 05:39:42 +01:00
|
|
|
|
|
|
|
BAD_HTML4 = """
|
|
|
|
<div>
|
2021-12-02 13:19:19 +01:00
|
|
|
foo
|
|
|
|
<p>hello</p>
|
|
|
|
bar
|
2017-02-15 05:39:42 +01:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML4 = """
|
|
|
|
<div>
|
|
|
|
foo
|
|
|
|
<p>hello</p>
|
|
|
|
bar
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
BAD_HTML5 = """
|
|
|
|
<div>
|
2021-12-02 13:19:19 +01:00
|
|
|
foo
|
|
|
|
{{#if foobar}}
|
|
|
|
hello
|
|
|
|
{{else}}
|
|
|
|
bye
|
|
|
|
{{/if}}
|
|
|
|
bar
|
2017-02-15 05:39:42 +01:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML5 = """
|
|
|
|
<div>
|
|
|
|
foo
|
|
|
|
{{#if foobar}}
|
|
|
|
hello
|
|
|
|
{{else}}
|
|
|
|
bye
|
|
|
|
{{/if}}
|
|
|
|
bar
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
BAD_HTML6 = """
|
|
|
|
<div>
|
2021-12-02 13:19:19 +01:00
|
|
|
<p> <strong> <span class = "whatever">foobar </span> </strong></p>
|
2017-02-15 05:39:42 +01:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML6 = """
|
|
|
|
<div>
|
|
|
|
<p> <strong> <span class = "whatever">foobar </span> </strong></p>
|
|
|
|
</div>
|
|
|
|
"""
|
2017-02-16 20:25:53 +01:00
|
|
|
|
|
|
|
BAD_HTML7 = """
|
|
|
|
<div class="foobar">
|
|
|
|
<input type="foobar" name="temp" value="{{dyn_name}}"
|
|
|
|
{{#unless invite_only}}checked="checked"{{/unless}} /> {{dyn_name}}
|
2018-10-15 16:25:18 +02:00
|
|
|
{{#if invite_only}}<i class="fa fa-lock"></i>{{/if}}
|
2017-02-16 20:25:53 +01:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML7 = """
|
|
|
|
<div class="foobar">
|
|
|
|
<input type="foobar" name="temp" value="{{dyn_name}}"
|
2018-04-03 10:07:23 +02:00
|
|
|
{{#unless invite_only}}checked="checked"{{/unless}} /> {{dyn_name}}
|
2018-10-15 16:25:18 +02:00
|
|
|
{{#if invite_only}}<i class="fa fa-lock"></i>{{/if}}
|
2017-02-16 20:25:53 +01:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
BAD_HTML8 = """
|
|
|
|
{{#each test}}
|
2021-12-02 13:19:19 +01:00
|
|
|
{{#with this}}
|
|
|
|
{{#if foobar}}
|
|
|
|
<div class="anything">{{{test}}}</div>
|
|
|
|
{{/if}}
|
|
|
|
{{#if foobar2}}
|
|
|
|
{{> teststuff}}
|
|
|
|
{{/if}}
|
|
|
|
{{/with}}
|
2017-02-16 20:25:53 +01:00
|
|
|
{{/each}}
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML8 = """
|
|
|
|
{{#each test}}
|
|
|
|
{{#with this}}
|
2021-11-20 13:25:41 +01:00
|
|
|
{{#if foobar}}
|
2017-02-16 20:25:53 +01:00
|
|
|
<div class="anything">{{{test}}}</div>
|
2021-11-20 13:25:41 +01:00
|
|
|
{{/if}}
|
|
|
|
{{#if foobar2}}
|
|
|
|
{{> teststuff}}
|
|
|
|
{{/if}}
|
2017-02-16 20:25:53 +01:00
|
|
|
{{/with}}
|
|
|
|
{{/each}}
|
|
|
|
"""
|
2017-02-23 18:12:52 +01:00
|
|
|
|
|
|
|
BAD_HTML9 = """
|
|
|
|
<form id="foobar" class="whatever">
|
|
|
|
{{! <div class="anothertest"> }}
|
|
|
|
<input value="test" />
|
|
|
|
<button type="button"><i class="test"></i></button>
|
|
|
|
<button type="button"><i class="test"></i></button>
|
|
|
|
{{! </div> }}
|
|
|
|
<div class="test"></div>
|
|
|
|
</form>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML9 = """
|
|
|
|
<form id="foobar" class="whatever">
|
|
|
|
{{! <div class="anothertest"> }}
|
|
|
|
<input value="test" />
|
|
|
|
<button type="button"><i class="test"></i></button>
|
|
|
|
<button type="button"><i class="test"></i></button>
|
|
|
|
{{! </div> }}
|
|
|
|
<div class="test"></div>
|
|
|
|
</form>
|
|
|
|
"""
|
|
|
|
|
|
|
|
BAD_HTML10 = """
|
|
|
|
{% block portico_content %}
|
|
|
|
<div class="test">
|
|
|
|
<i class='test'></i> foobar
|
|
|
|
</div>
|
|
|
|
<div class="test1">
|
|
|
|
{% for row in data %}
|
|
|
|
<div class="test2">
|
|
|
|
{% for group in (row[0:2], row[2:4]) %}
|
|
|
|
<div class="test2">
|
|
|
|
</div>
|
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
{% endblock %}
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML10 = """
|
|
|
|
{% block portico_content %}
|
|
|
|
<div class="test">
|
|
|
|
<i class='test'></i> foobar
|
|
|
|
</div>
|
|
|
|
<div class="test1">
|
|
|
|
{% for row in data %}
|
|
|
|
<div class="test2">
|
|
|
|
{% for group in (row[0:2], row[2:4]) %}
|
|
|
|
<div class="test2">
|
|
|
|
</div>
|
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
{% endblock %}
|
|
|
|
"""
|
|
|
|
|
|
|
|
BAD_HTML11 = """
|
|
|
|
<div class="test1">
|
2021-12-02 13:19:19 +01:00
|
|
|
<div class="test2">
|
2017-02-23 18:12:52 +01:00
|
|
|
foobar
|
2021-12-02 13:19:19 +01:00
|
|
|
<div class="test2">
|
|
|
|
</div>
|
2017-02-23 18:12:52 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
|
|
|
GOOD_HTML11 = """
|
|
|
|
<div class="test1">
|
|
|
|
<div class="test2">
|
|
|
|
foobar
|
|
|
|
<div class="test2">
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
|
2018-04-03 10:07:23 +02:00
|
|
|
|
2021-12-02 13:19:19 +01:00
|
|
|
def pretty_print(html: str) -> str:
|
|
|
|
tokens = validate(fn=None, text=html)
|
|
|
|
return pretty_print_html(html, tokens)
|
2018-04-05 08:38:46 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-02-15 05:39:42 +01:00
|
|
|
class TestPrettyPrinter(unittest.TestCase):
|
2017-12-13 06:45:46 +01:00
|
|
|
def compare(self, a: str, b: str) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(a.split("\n"), b.split("\n"))
|
2017-02-15 05:39:42 +01:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_pretty_print(self) -> None:
|
2021-12-02 13:19:19 +01:00
|
|
|
self.compare(pretty_print(GOOD_HTML), GOOD_HTML)
|
|
|
|
self.compare(pretty_print(BAD_HTML), GOOD_HTML)
|
|
|
|
self.compare(pretty_print(BAD_HTML1), GOOD_HTML1)
|
|
|
|
self.compare(pretty_print(BAD_HTML2), GOOD_HTML2)
|
|
|
|
self.compare(pretty_print(BAD_HTML4), GOOD_HTML4)
|
|
|
|
self.compare(pretty_print(BAD_HTML5), GOOD_HTML5)
|
|
|
|
self.compare(pretty_print(BAD_HTML6), GOOD_HTML6)
|
|
|
|
self.compare(pretty_print(BAD_HTML7), GOOD_HTML7)
|
|
|
|
self.compare(pretty_print(BAD_HTML8), GOOD_HTML8)
|
|
|
|
self.compare(pretty_print(BAD_HTML9), GOOD_HTML9)
|
|
|
|
self.compare(pretty_print(BAD_HTML10), GOOD_HTML10)
|
|
|
|
self.compare(pretty_print(BAD_HTML11), GOOD_HTML11)
|