2017-01-06 15:11:15 +01:00
|
|
|
import os
|
2020-06-11 00:54:34 +02:00
|
|
|
import unittest
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
import tools.lib.template_parser
|
|
|
|
from tools.lib.html_branches import (
|
2021-04-21 00:46:14 +02:00
|
|
|
Node,
|
2020-06-11 00:54:34 +02:00
|
|
|
build_id_dict,
|
2016-09-11 20:23:29 +02:00
|
|
|
get_tag_info,
|
|
|
|
html_branches,
|
|
|
|
html_tag_tree,
|
2017-01-06 15:11:15 +01:00
|
|
|
split_for_id_and_class,
|
2016-09-11 20:23:29 +02:00
|
|
|
)
|
|
|
|
|
2017-01-18 22:04:32 +01:00
|
|
|
ZULIP_PATH = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
TEST_TEMPLATES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_template_data")
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
class TestHtmlBranches(unittest.TestCase):
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_get_tag_info(self) -> None:
|
2021-12-01 18:03:31 +01:00
|
|
|
html = """<p id="test" class="test1 test2">foo</p>"""
|
2016-09-11 20:23:29 +02:00
|
|
|
|
2021-12-01 18:03:31 +01:00
|
|
|
start_tag, text, end_tag = tools.lib.template_parser.tokenize(html)
|
2016-09-11 20:23:29 +02:00
|
|
|
|
|
|
|
start_tag_info = get_tag_info(start_tag)
|
|
|
|
end_tag_info = get_tag_info(end_tag)
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(start_tag_info.text(), "p.test1.test2#test")
|
|
|
|
self.assertEqual(end_tag_info.text(), "p")
|
2021-12-01 18:03:31 +01:00
|
|
|
self.assertEqual(text.s, "foo")
|
2016-09-11 20:23:29 +02:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_html_tag_tree(self) -> None:
|
2016-09-11 20:23:29 +02:00
|
|
|
html = """
|
|
|
|
<!-- test -->
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<!-- test -->
|
|
|
|
<head>
|
|
|
|
<title>Test</title>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<link rel="stylesheet" href="style.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
2020-04-18 11:38:31 +02:00
|
|
|
<p>Hello<br>world!</p>
|
2016-09-11 20:23:29 +02:00
|
|
|
<p>Goodbye<!-- test -->world!</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
<!-- test -->
|
|
|
|
"""
|
|
|
|
|
|
|
|
tree = html_tag_tree(html)
|
|
|
|
|
2021-04-21 00:46:14 +02:00
|
|
|
def serialize(node: Node) -> object:
|
|
|
|
return (
|
|
|
|
node.token and (node.token.kind, node.token.tag),
|
|
|
|
[serialize(child) for child in node.children],
|
|
|
|
)
|
2016-09-11 20:23:29 +02:00
|
|
|
|
2021-04-21 00:46:14 +02:00
|
|
|
expected = (
|
|
|
|
None,
|
|
|
|
[
|
|
|
|
(
|
|
|
|
("html_start", "html"),
|
|
|
|
[
|
|
|
|
(
|
|
|
|
("html_start", "head"),
|
|
|
|
[
|
|
|
|
(("html_start", "title"), []),
|
|
|
|
(("html_singleton", "meta"), []),
|
|
|
|
(("html_singleton", "link"), []),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
("html_start", "body"),
|
|
|
|
[
|
|
|
|
(
|
|
|
|
("html_start", "p"),
|
|
|
|
[(("html_start", "br"), []), (("html_start", "p"), [])],
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-04-21 00:46:14 +02:00
|
|
|
self.assertEqual(serialize(tree), expected)
|
2016-09-11 20:23:29 +02:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_html_branches(self) -> None:
|
2016-09-11 20:23:29 +02:00
|
|
|
html = """
|
|
|
|
<!-- test -->
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<!-- test -->
|
|
|
|
<head>
|
|
|
|
<title>Test</title>
|
|
|
|
<meta charset="utf-8" />
|
|
|
|
<link rel="stylesheet" href="style.css" />
|
|
|
|
</head>
|
|
|
|
<body>
|
2020-04-18 11:38:31 +02:00
|
|
|
<p>Hello<br>world!</p>
|
2016-09-11 20:23:29 +02:00
|
|
|
<p>Goodbye<!-- test -->world!</p>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
<!-- test -->
|
|
|
|
"""
|
|
|
|
|
|
|
|
branches = html_branches(html)
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-04-21 00:46:14 +02:00
|
|
|
[(branch.text(), branch.staircase_text()) for branch in branches],
|
|
|
|
[
|
|
|
|
("html head title", "\n html\n head\n title\n"),
|
|
|
|
("html head meta", "\n html\n head\n meta\n"),
|
|
|
|
("html head link", "\n html\n head\n link\n"),
|
|
|
|
("html body p br", "\n html\n body\n p\n br\n"),
|
|
|
|
("html body p p", "\n html\n body\n p\n p\n"),
|
|
|
|
],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-01-06 15:11:15 +01:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_build_id_dict(self) -> None:
|
2017-01-06 15:11:15 +01:00
|
|
|
templates = ["test_template1.html", "test_template2.html"]
|
|
|
|
templates = [os.path.join(TEST_TEMPLATES_DIR, fn) for fn in templates]
|
|
|
|
|
|
|
|
template_id_dict = build_id_dict(templates)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
set(template_id_dict.keys()), {"below_navbar", "hello_{{ message }}", "intro"}
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
template_id_dict["hello_{{ message }}"],
|
2021-02-12 08:19:30 +01:00
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
f"Line 12:{ZULIP_PATH}/tools/tests/test_template_data/test_template1.html",
|
|
|
|
f"Line 12:{ZULIP_PATH}/tools/tests/test_template_data/test_template2.html",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
template_id_dict["intro"],
|
2021-02-12 08:19:30 +01:00
|
|
|
[
|
2021-02-12 08:20:45 +01:00
|
|
|
f"Line 10:{ZULIP_PATH}/tools/tests/test_template_data/test_template1.html",
|
|
|
|
f"Line 11:{ZULIP_PATH}/tools/tests/test_template_data/test_template1.html",
|
|
|
|
f"Line 11:{ZULIP_PATH}/tools/tests/test_template_data/test_template2.html",
|
2021-02-12 08:19:30 +01:00
|
|
|
],
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
2021-02-12 08:20:45 +01:00
|
|
|
template_id_dict["below_navbar"],
|
|
|
|
[f"Line 10:{ZULIP_PATH}/tools/tests/test_template_data/test_template2.html"],
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-01-06 15:11:15 +01:00
|
|
|
|
2017-12-13 06:45:46 +01:00
|
|
|
def test_split_for_id_and_class(self) -> None:
|
2017-01-06 15:11:15 +01:00
|
|
|
id1 = "{{ red|blue }}"
|
|
|
|
id2 = "search_box_{{ page }}"
|
|
|
|
|
|
|
|
class1 = "chat_box message"
|
|
|
|
class2 = "stream_{{ topic }}"
|
|
|
|
class3 = "foo {{ a|b|c }} bar"
|
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(split_for_id_and_class(id1), ["{{ red|blue }}"])
|
|
|
|
self.assertEqual(split_for_id_and_class(id2), ["search_box_{{ page }}"])
|
2017-01-06 15:11:15 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
self.assertEqual(split_for_id_and_class(class1), ["chat_box", "message"])
|
|
|
|
self.assertEqual(split_for_id_and_class(class2), ["stream_{{ topic }}"])
|
|
|
|
self.assertEqual(split_for_id_and_class(class3), ["foo", "{{ a|b|c }}", "bar"])
|