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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,4 @@ src/zimscraperlib/rewriting/statics/wombatSetup.js

# wombat.js is installed from online source
src/zimscraperlib/rewriting/statics/wombat.js
.python-version
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `ArticleUrlRewriter.normalize()` no longer decodes a percent-encoded `/` (`%2F`) in the path into a literal `/`, fixing a content collision between e.g. `/foo%2Fbar` (one segment, containing a literal `/`) and `/foo/bar` (two segments), which used to normalize to the same ZIM path (#396)
- `ArticleUrlRewriter.escape_dot_segments()` now also escapes a literal `_.`/`_..` (and further escaped forms like `__.`/`__..`) segment with one more leading `_`, fixing a content collision with the escaped form of an actual `.`/`..` dot-segment, which used to normalize to the same ZIM path (#396)
- `ArticleUrlRewriter.normalize()` now prefixes every ZIM path with a constant `replay://` marker, and `wombatSetup.js`'s `urlRewriteFunction`/`head_insert.html` do the same, fixing wombat.js computing an empty or single-`/` replay prefix ("`wb_rel_prefix`") for a ZIM served with no extra "collection" path segment (e.g. a bare `kiwix.app/<path>`), which corrupted any `innerHTML`/`outerHTML` read on the page by deleting arbitrary `/text/` spans, and caused related, quieter bugs in `rewriteUrl`/`rewriteCookie` (#396, openzim/zimit#589)
- Include values that violate metadata length restriction and names that violate custom metadata name conventions in `ValueError` messages (#405)

### Changed

Expand Down
10 changes: 7 additions & 3 deletions src/zimscraperlib/zim/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ def get_name(self) -> str:
and not self.meta_name.startswith("X-")
and self.matches_reserved_name(self.meta_name)
):
raise ValueError("Custom metdata name must be X- prefixed")
raise ValueError(
f"Custom metadata name ('{self.meta_name}') must be X- prefixed"
)
if (
self.oz_x_prefixed
and APPLY_RECOMMENDATIONS
Expand Down Expand Up @@ -270,9 +272,11 @@ def get_cleaned_value(self, value: str) -> str:

# max-length is openZIM recommendation
if getattr(self, "oz_max_length", 0) and APPLY_RECOMMENDATIONS:
if nb_grapheme_for(value) > self.oz_max_length:
nb_graphemes = nb_grapheme_for(value)
if nb_graphemes > self.oz_max_length:
raise ValueError(
f"{self.name} value is too long ({self.oz_max_length})"
f"{self.name} value ('{value}') is too long "
f"({nb_graphemes}/{self.oz_max_length})"
)
return value

Expand Down
26 changes: 19 additions & 7 deletions tests/zim/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ def test_validate_short_grapheme_title_check_enabled():


def test_validate_too_long_title_check_enabled():
with pytest.raises(ValueError, match="Title value is too long"):
assert metadata.TitleMetadata("T" * 31)
value = "T" * 31
with pytest.raises(
ValueError, match=rf"Title value \('{value}'\) is too long \(31/30\)"
):
assert metadata.TitleMetadata(value)


def test_validate_too_long_title_check_disabled(
Expand All @@ -155,8 +158,11 @@ def test_validate_short_grapheme_description_check_enabled():


def test_validate_too_long_description_check_enabled():
with pytest.raises(ValueError, match="Description value is too long"):
assert metadata.DescriptionMetadata("T" * 81)
value = "T" * 81
with pytest.raises(
ValueError, match=rf"Description value \('{value}'\) is too long"
):
assert metadata.DescriptionMetadata(value)


def test_validate_too_long_description_check_disabled(
Expand All @@ -174,8 +180,11 @@ def test_validate_short_grapheme_longdescription_check_enabled():


def test_validate_too_long_longdescription_check_enabled():
with pytest.raises(ValueError, match="LongDescription value is too long"):
assert metadata.LongDescriptionMetadata("T" * 4001)
value = "T" * 4001
with pytest.raises(
ValueError, match=rf"LongDescription value \('{value}'\) is too long"
):
assert metadata.LongDescriptionMetadata(value)


def test_validate_too_long_longdescription_check_disabled(
Expand Down Expand Up @@ -287,7 +296,10 @@ def metadata_init(request: pytest.FixtureRequest) -> MetadataInitConfig:
],
)
def test_reserved_names(metadata_name: str, metadata_type: type):
with pytest.raises(ValueError, match="Custom metdata name must be X- prefixed"):
with pytest.raises(
ValueError,
match=rf"Custom metadata name \('{metadata_name}'\) must be X- prefixed",
):
value = "x" if metadata_type == metadata.CustomTextMetadata else b"x"
metadata_type(name=metadata_name, value=value)

Expand Down