Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix broken deserialization of deps #65

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions htmltools/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,7 @@ def _static_extract_serialized_html_deps(
) -> tuple[str, list[HTMLDependency]]:
# Scan for HTML dependencies that were serialized via
# HTMLdependency.get_tag_representation()
pattern = (
r'<script type="application/json" data-html-dependency="">(.*?)</script>'
)
pattern = r'<script type="application/json" data-html-dependency="">((?:.|\r|\n)*?)</script>'
dep_strs = re.findall(pattern, html)
# html = re.sub(pattern, "", html)

Expand Down Expand Up @@ -1446,7 +1444,7 @@ def as_html_tags(
scripts = [Tag("script", **s) for s in d["script"]]
return TagList(*metas, *links, *scripts, self.head)

def serialize_to_script_json(self, indent: int = 0, eol: str = "\n") -> Tag:
def serialize_to_script_json(self, indent: int | None = None) -> Tag:
res = {
"name": self.name,
"version": str(self.version),
Expand Down
39 changes: 39 additions & 0 deletions tests/test_html_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from tempfile import TemporaryDirectory
from typing import Union

import htmltools as ht
from htmltools import (
HTMLDependency,
HTMLDocument,
Expand Down Expand Up @@ -263,3 +264,41 @@ def tagify(self):
testdep_files = os.listdir(os.path.join(tmpdir, "mylib", "testdep"))
testdep_files.sort()
assert testdep_files == ["testdep.css", "testdep.js"]


def test_json_roundtrip():
testdep = HTMLDependency(
"testdep",
"1.0",
source={"package": "htmltools", "subdir": "libtest/testdep"},
script={"src": "testdep.js"},
stylesheet={"href": "testdep.css"},
)
testdep2 = HTMLDependency(
"testdep2",
"1.0",
source={"package": "htmltools", "subdir": "libtest/testdep"},
script={"src": "testdep.js"},
stylesheet={"href": "testdep.css"},
)

old_mode = ht.html_dependency_render_mode
ht.html_dependency_render_mode = "json"
try:
x = ht.TagList(
[
ht.HTML('<meta data-foo="">'),
div("hello world", testdep),
# Also make sure it would work even with indents
ht.HTML(testdep2.serialize_to_script_json(indent=2)),
]
)
x_str = str(x)
rendered = ht.HTMLTextDocument(
x_str, deps_replace_pattern='<meta data-foo="">'
).render()
assert "testdep" in [d.name for d in rendered["dependencies"]]
assert "testdep2" in [d.name for d in rendered["dependencies"]]

finally:
ht.html_dependency_render_mode = old_mode