diff --git a/tests/test_script_escaping.py b/tests/test_script_escaping.py new file mode 100644 index 0000000..5a0d05c --- /dev/null +++ b/tests/test_script_escaping.py @@ -0,0 +1,90 @@ +"""Values interpolated into a ", re.S) +# Entities that are meaningful in JS source and never legal inside it. +BROKEN = re.compile(r"&(?:#34|quot|#39|apos|amp|lt|gt);") + + +def _site(tmp_path, **config): + root = str(tmp_path / "index") + os.makedirs(root) + init_repo(root) + write_config(root, **config) + write_descriptor(root, "alpha", "widget") + commit(root, "add widget", date="2026-01-01") + out = str(tmp_path / "site") + site, config = build(root, offline=True) + render(site, config, out) + return out + + +def _inline_scripts(site): + for dirpath, _, names in os.walk(site): + for n in names: + if not n.endswith(".html"): + continue + path = os.path.join(dirpath, n) + with open(path, encoding="utf-8") as f: + html = f.read() + for m in SCRIPT.finditer(html): + yield os.path.relpath(path, site), m.group(1) + + +@pytest.mark.parametrize("style", ["auto", "dark", "light"]) +def test_inline_scripts_carry_no_html_entities(tmp_path, style): + site = _site(tmp_path, theme={"style": style}) + for page, body in _inline_scripts(site): + found = BROKEN.search(body) + assert not found, ( + f"{page}: inline script contains {found.group(0)!r}; HTML entities " + f"do not decode inside