Skip to content

Commit 6613ac5

Browse files
authored
Merge pull request #357 from Sarthak-Shreshtha01/fix/language-from-any-meta
Find a content-language meta that is not the first meta
2 parents 4ec8322 + d7b7c14 commit 6613ac5

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2020
- 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)
2121
- 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)
2222
- Keep URL-encoded `#` characters of the path or querystring when rewriting document URIs, instead of dropping everything after them (#341)
23+
- Find the language of an HTML page in a `content-language` `<meta>` that is not the first `<meta>` of the page (#356)
2324
- Rewrite the `poster` attribute (e.g. of `<video>`) when rewriting HTML, like `src` and `href` (#339)
2425
- Do not collapse consecutive slashes `//` into a single `/` when normalizing URLs into ZIM paths, in both Python and JS rewriting (#340)
2526
- `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)

‎src/zimscraperlib/html.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ def find_language_in(content: str | BinaryIO | TextIO, mime_type: str) -> str:
3737
soup = BeautifulSoup(content, "lxml")
3838
for nodename, keylist in mapping.items():
3939
for key in keylist:
40-
node = soup.find(nodename)
41-
if node:
40+
# all nodes, not only the first: the content-language <meta> is
41+
# often not the first one (e.g. after <meta charset>)
42+
for node in soup.find_all(nodename):
4243
if not isinstance(
4344
node,
4445
element.Tag, # pyright:ignore[reportUnnecessaryIsInstance]

‎tests/html/test_html.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ def test_find_language(tmp_path: pathlib.Path, html_page: str):
9898
),
9999
"en-UK",
100100
),
101+
(
102+
(
103+
"<html><head><meta charset='utf-8'>"
104+
"<meta http-equiv='content-language' content='en-UK'>"
105+
"</head><body></body></html>"
106+
),
107+
"en-UK",
108+
),
101109
],
102110
)
103111
def test_find_language_order(html_string: str, expected_language: str):

0 commit comments

Comments
 (0)