Skip to content

Commit 1857415

Browse files
authored
HTMLTextDocument() now preserves the order of serialized dependencies (#95)
* Update unit test to check for order of dependencies * Preserve order of dependencies when deduping * Update changelog
1 parent 3005d6a commit 1857415

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [UNRELEASED]
99

10-
10+
* Fixed an issue with `HTMLTextDocument()` returning extracted `HTMLDependency()`s in a non-determistic order. (#95)
1111

1212
## [0.5.3] 2024-07-18
1313

htmltools/_core.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,19 +1215,24 @@ def _static_extract_serialized_html_deps(
12151215
# HTMLdependency.get_tag_representation()
12161216
pattern = r'<script type="application/json" data-html-dependency="">((?:.|\r|\n)*?)</script>'
12171217
dep_strs = re.findall(pattern, html)
1218-
# Deduplicate dependencies. htmltools normally would dedupe dependencies, but
1219-
# with HTMLTextDocuments, the input HTML would usually have been generated by
1220-
# something else (like Quarto) and may not have the dependencies deduped.
1221-
dep_strs = list(set(dep_strs))
12221218

12231219
# Remove the serialized HTML dependencies from the HTML string
12241220
html = re.sub(pattern, "", html)
12251221

1222+
# Reconstitute the HTMLDependency objects
1223+
#
1224+
# Note: htmltools normally would dedupe dependencies, but
1225+
# with HTMLTextDocuments, the input HTML would usually have been generated by
1226+
# something else (like Quarto) and may not have the dependencies deduped.
1227+
seen_deps: set[str] = set()
12261228
deps: list[HTMLDependency] = []
12271229
for dep_str in dep_strs:
1230+
if dep_str in seen_deps:
1231+
continue
12281232
args = json.loads(dep_str)
12291233
dep = HTMLDependency(**args)
12301234
deps.append(dep)
1235+
seen_deps.add(dep_str)
12311236

12321237
return (html, deps)
12331238

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)