Skip to content

Commit 6a094d2

Browse files
authored
Merge branch 'main' into html_not_str
2 parents 53334c5 + 1857415 commit 6a094d2

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2121

2222
* Exported `ReprHtml` protocol class. If an object has a `_repr_html_` method, then it is of instance `ReprHtml`. (#86)
2323

24+
### Bug fixes
25+
26+
* Fixed an issue with `HTMLTextDocument()` returning extracted `HTMLDependency()`s in a non-determistic order. (#95)
27+
2428
## [0.5.3] 2024-07-18
2529

2630
* HTML tags in docstrings are now escaped. (#90)

htmltools/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.5.3"
1+
__version__ = "0.5.3.9000"
22

33
from . import svg, tags
44
from ._core import TagAttrArg # pyright: ignore[reportUnusedImport] # noqa: F401

htmltools/_core.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,19 +1219,24 @@ def _static_extract_serialized_html_deps(
12191219
# HTMLdependency.get_tag_representation()
12201220
pattern = r'<script type="application/json" data-html-dependency="">((?:.|\r|\n)*?)</script>'
12211221
dep_strs = re.findall(pattern, html)
1222-
# Deduplicate dependencies. htmltools normally would dedupe dependencies, but
1223-
# with HTMLTextDocuments, the input HTML would usually have been generated by
1224-
# something else (like Quarto) and may not have the dependencies deduped.
1225-
dep_strs = list(set(dep_strs))
12261222

12271223
# Remove the serialized HTML dependencies from the HTML string
12281224
html = re.sub(pattern, "", html)
12291225

1226+
# Reconstitute the HTMLDependency objects
1227+
#
1228+
# Note: htmltools normally would dedupe dependencies, but
1229+
# with HTMLTextDocuments, the input HTML would usually have been generated by
1230+
# something else (like Quarto) and may not have the dependencies deduped.
1231+
seen_deps: set[str] = set()
12301232
deps: list[HTMLDependency] = []
12311233
for dep_str in dep_strs:
1234+
if dep_str in seen_deps:
1235+
continue
12321236
args = json.loads(dep_str)
12331237
dep = HTMLDependency(**args)
12341238
deps.append(dep)
1239+
seen_deps.add(dep_str)
12351240

12361241
return (html, deps)
12371242

@@ -1554,13 +1559,7 @@ def as_html_tags(
15541559
Render the dependency as a ``TagList()``.
15551560
"""
15561561
d = self.as_dict(lib_prefix=lib_prefix, include_version=include_version)
1557-
metas = [
1558-
Tag(
1559-
"meta",
1560-
**m, # pyright: ignore[reportArgumentType]
1561-
)
1562-
for m in self.meta
1563-
]
1562+
metas = [Tag("meta", **m) for m in d["meta"]]
15641563
links = [Tag("link", **s) for s in d["stylesheet"]]
15651564
scripts = [Tag("script", **s) for s in d["script"]]
15661565
return TagList(*metas, *links, *scripts, self.head)

tests/test_html_document.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,9 @@ def test_json_roundtrip():
322322
x_str, deps_replace_pattern='<meta data-foo="">'
323323
).render()
324324

325-
# Make sure both deps are present.
326-
assert "testdep" in [d.name for d in rendered["dependencies"]]
327-
assert "testdep2" in [d.name for d in rendered["dependencies"]]
325+
# Make sure both deps are present and in the order they appear in x_str.
326+
assert "testdep2" == rendered["dependencies"][0].name
327+
assert "testdep" == rendered["dependencies"][1].name
328328

329329
# Make sure testdep was deduplicated by HTMLTextDocument().render().
330330
assert rendered["dependencies"].count(testdep) == 1

0 commit comments

Comments
 (0)