Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Carry a script's top-level `const`, `let` and `class` declarations across the wombat block, so other scripts on the page can still see them (#329)
- Keep a rewritten `<script>`'s original `src` in `__wb_orig_src`, so wombat can return it from `getAttribute("src")` and bundlers that identify chunks by it (Next.js/Turbopack, Vite) still work (openzim/warc2zim#473)
- Keep URL-encoded `#` characters of the path or querystring when rewriting document URIs, instead of dropping everything after them (#341)
- Find the language of an HTML page in a `content-language` `<meta>` that is not the first `<meta>` of the page (#356)
- Rewrite the `poster` attribute (e.g. of `<video>`) when rewriting HTML, like `src` and `href` (#339)
- Do not collapse consecutive slashes `//` into a single `/` when normalizing URLs into ZIM paths, in both Python and JS rewriting (#340)
- `Creator.finish()` no longer silently discards errors raised by libzim while finalizing the ZIM, so a failure during finalization is no longer reported as a successful run (openzim/warc2zim#462)
Expand Down
5 changes: 3 additions & 2 deletions src/zimscraperlib/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def find_language_in(content: str | BinaryIO | TextIO, mime_type: str) -> str:
soup = BeautifulSoup(content, "lxml")
for nodename, keylist in mapping.items():
for key in keylist:
node = soup.find(nodename)
if node:
# all nodes, not only the first: the content-language <meta> is
# often not the first one (e.g. after <meta charset>)
for node in soup.find_all(nodename):
if not isinstance(
node,
element.Tag, # pyright:ignore[reportUnnecessaryIsInstance]
Expand Down
8 changes: 8 additions & 0 deletions tests/html/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ def test_find_language(tmp_path: pathlib.Path, html_page: str):
),
"en-UK",
),
(
(
"<html><head><meta charset='utf-8'>"
"<meta http-equiv='content-language' content='en-UK'>"
"</head><body></body></html>"
),
"en-UK",
),
],
)
def test_find_language_order(html_string: str, expected_language: str):
Expand Down