diff --git a/CHANGELOG.md b/CHANGELOG.md index 77dcde7..bc21627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Recover inline base64 data missing its `data:` scheme prefix (relative, root-relative, HTTP-resolved, `///...`, and `data://...`) across URL, HTML, and CSS rewriting, skip `__wb_orig_*` and JS module notifications for `data:` URLs, and avoid splitting `srcset` entries on commas inside inline data URIs (openzim/warc2zim#472, #420) + ## [5.6.0] - 2026-11-20 ### Added diff --git a/src/zimscraperlib/rewriting/html.py b/src/zimscraperlib/rewriting/html.py index 35247d4..ae9b10d 100644 --- a/src/zimscraperlib/rewriting/html.py +++ b/src/zimscraperlib/rewriting/html.py @@ -35,6 +35,7 @@ HTTP_EQUIV_REDIRECT_RE = re.compile( r"^\s*(?P.*?)\s*;\s*url\s*=\s*(?P.*?)\s*$" ) +_SRCSET_BASE64_MARKER_RE = re.compile(r";base64,", re.IGNORECASE) class RewritenHtml(NamedTuple): @@ -86,9 +87,10 @@ def get_original_attr_values( (openzim/warc2zim#413). Nothing is recorded for an attribute that had no value, that rewriting left - alone, or that already carries its original. That last is HTML rewritten - twice: the attribute already present is the true original, and a second - would both duplicate it and win, since a browser takes the first. + alone, that already carries its original, or whose rewritten value starts + with ``data:`` (a recovered ``data:`` URI would otherwise duplicate a large + inline payload and cause wombat's ``getAttribute`` override to return the + broken original). """ recorded: list[AttrNameAndValue] = [] for attr_name in WB_ORIG_ATTRIBUTES: @@ -99,7 +101,7 @@ def get_original_attr_values( if not original: continue rewritten = get_attr_value_from(rewritten_attrs, attr_name) - if rewritten is None or rewritten == original: + if rewritten is None or rewritten == original or rewritten.startswith("data:"): continue recorded.append((orig_name, original)) return recorded @@ -659,20 +661,55 @@ def rewrite_href_src_attributes( # noqa: PLR0917 if ( attr_name not in ("href", "src", "poster") or not attr_value - or attr_value.startswith("data:") + or (attr_value[:5].lower() == "data:" and attr_value[:7].lower() != "data://") ): return + result = url_rewriter(attr_value, base_href=base_href, rewrite_all_url=tag != "a") if ( notify_js_module + and result.zim_path is not None and get_html_rewrite_context(tag=tag, attrs=attrs) == "js-module" ): - notify_js_module(url_rewriter.get_item_path(attr_value, base_href=base_href)) - return ( - attr_name, - url_rewriter( - attr_value, base_href=base_href, rewrite_all_url=tag != "a" - ).rewriten_url, - ) + notify_js_module(result.zim_path) + return (attr_name, result.rewriten_url) + + +def _split_srcset_entries(srcset: str) -> list[str]: + """Split a srcset attribute into individual image candidate strings. + + Unlike ``str.split(",")``, commas inside a ``data:`` URI or introducing the + payload of an unprefixed ``;base64,`` URI are not treated + as candidate separators. + """ + entries: list[str] = [] + pos = 0 + length = len(srcset) + while pos < length: + while pos < length and srcset[pos] in " \t\n\r\f,": + pos += 1 + if pos >= length: + break + start = pos + ws = pos + while ws < length and srcset[ws] not in " \t\n\r\f": + ws += 1 + url_chunk = srcset[start:ws] + if marker := _SRCSET_BASE64_MARKER_RE.search(url_chunk): + search_from = start + marker.end() + elif url_chunk[:5].lower() == "data:": + search_from = ws + while search_from > start and srcset[search_from - 1] == ",": + search_from -= 1 + else: + search_from = start + + sep = srcset.find(",", search_from) + if sep == -1: + entries.append(srcset[start:].strip()) + break + entries.append(srcset[start:sep].strip()) + pos = sep + 1 + return entries @rules.rewrite_attribute() @@ -685,10 +722,12 @@ def rewrite_srcset_attribute( """Rewrite srcset attributes""" if attr_name != "srcset" or not attr_value: return - value_list = attr_value.split(",") + value_list = _split_srcset_entries(attr_value) + if not value_list: + return new_value_list: list[str] = [] for value in value_list: - url, *other = value.strip().split(" ", maxsplit=1) + url, *other = value.split(maxsplit=1) new_url = url_rewriter(url, base_href=base_href).rewriten_url new_value = " ".join([new_url, *other]) new_value_list.append(new_value) diff --git a/src/zimscraperlib/rewriting/url_rewriting.py b/src/zimscraperlib/rewriting/url_rewriting.py index 0136204..ea1dc76 100644 --- a/src/zimscraperlib/rewriting/url_rewriting.py +++ b/src/zimscraperlib/rewriting/url_rewriting.py @@ -165,6 +165,97 @@ def _requote_path(path: str) -> str: return "%2F".join(quote(part, safe="/") for part in path.split("%2F")) +_BASE64_MARKER_RE = re.compile(r";base64,", re.IGNORECASE) +_MIME_SUBTYPE_RE = re.compile(r"[A-Za-z0-9][A-Za-z0-9.+-]*") +_MIME_PARAM_RE = re.compile(r"[A-Za-z0-9-]+(?:=[^\s;]+)?") +_NON_B64_RE = re.compile(r"[^A-Za-z0-9+/]") + +# Official IANA top-level media types seen in inline data URIs. +_VALID_MEDIA_TYPES = frozenset( + { + "application", + "audio", + "example", + "font", + "haptics", + "image", + "message", + "model", + "multipart", + "text", + "video", + } +) + + +def _recover_missing_data_url(item_url: str) -> str | None: + """Recover inline base64 data missing its ``data:`` scheme prefix. + + Detects relative (``image/jpeg;base64,...``), root-relative + (``/image/png;base64,...``), HTTP-resolved + (``https://example.com/node/image/jpeg;base64,...``), and malformed + ``data://...`` values, stripping any fake prefix and stray whitespace in the + base64 payload. Returns ``None`` when ``item_url`` already has a valid + ``data:`` scheme or is not a recognizable malformed inline base64 payload. + """ + prefix_lower = item_url[:7].lower() + if prefix_lower.startswith("data:") and not prefix_lower.startswith("data://"): + return None + + marker_match = _BASE64_MARKER_RE.search(item_url) + if not marker_match: + return None + + before = item_url[: marker_match.start()] + payload = item_url[marker_match.end() :] + + if "?" in before or "#" in before: + return None + + parts = urlsplit(before) + if parts.scheme == "data" and before[:7].lower() == "data://": + candidate_path = before[7:].lstrip("/") + elif parts.scheme == "" and parts.netloc.lower() in _VALID_MEDIA_TYPES: + candidate_path = parts.netloc + parts.path + elif parts.scheme in ("", "http", "https"): + candidate_path = parts.path + else: + return None + + prefix, sep, subtype_and_params = candidate_path.rpartition("/") + if not sep or not subtype_and_params: + return None + mime_type = prefix.rpartition("/")[2] + if not mime_type: + return None + mediatype = f"{mime_type}/{subtype_and_params}" + + subtype_essence, has_params, mime_params = subtype_and_params.partition(";") + if mime_type.lower() not in _VALID_MEDIA_TYPES: + return None + if not _MIME_SUBTYPE_RE.fullmatch(subtype_essence): + return None + if has_params and ( + not mime_params + or not all(_MIME_PARAM_RE.fullmatch(param) for param in mime_params.split(";")) + ): + return None + + stripped_payload = "".join(payload.split()) + if not stripped_payload: + return None + if stripped_payload.endswith("=="): + core = stripped_payload[:-2] + elif stripped_payload.endswith("="): + core = stripped_payload[:-1] + else: + core = stripped_payload + if not core or _NON_B64_RE.search(core): + return None + + return f"data:{mediatype};base64,{stripped_payload}" + + def _remove_dot_segments(path: str) -> str: """Remove '.' and '..' segments from a path, per RFC 3986 section 5.2.4 @@ -386,6 +477,13 @@ def __call__( try: item_url = item_url.strip() + if (recovered := _recover_missing_data_url(item_url)) is not None: + return RewriteResult( + absolute_url=recovered, + rewriten_url=recovered, + zim_path=None, + ) + item_absolute_url = _urljoin( _urljoin(self.article_url.value, base_href), item_url ) diff --git a/tests/rewriting/test_css_rewriting.py b/tests/rewriting/test_css_rewriting.py index 96399c8..f8a8c67 100644 --- a/tests/rewriting/test_css_rewriting.py +++ b/tests/rewriting/test_css_rewriting.py @@ -280,3 +280,49 @@ def test_unterminated_string_ending_in_escaped_quote(): rewriter.rewrite(r'.test { content: "unterminated\" }') == r'.test { content: "unterminated\" }}' ) + + +@pytest.mark.parametrize( + "input_css, expected_css", + [ + pytest.param( + "p { background: url(image/png;base64,iVBORw0KGgo=); }", + "p { background: url(data:image/png;base64,iVBORw0KGgo=); }", + id="unquoted_relative_base64", + ), + pytest.param( + 'p { background: url("image/png;base64,iVBORw0KGgo="); }', + 'p { background: url("data:image/png;base64,iVBORw0KGgo="); }', + id="quoted_relative_base64", + ), + pytest.param( + "p { background: " + "url('https://istmat.org/node/image/jpeg;base64,/9j/4AAQSkZJRg=='); }", + 'p { background: url("data:image/jpeg;base64,/9j/4AAQSkZJRg=="); }', + id="quoted_http_resolved_base64", + ), + pytest.param( + "p { background: url(data://image/png;base64,iVBORw0KGgo=); }", + "p { background: url(data:image/png;base64,iVBORw0KGgo=); }", + id="data_double_slash_base64", + ), + pytest.param( + 'p { background: url("image/png;base64,iVBOR w0KG go="); }', + 'p { background: url("data:image/png;base64,iVBORw0KGgo="); }', + id="whitespace_in_base64", + ), + pytest.param( + "p { width= } div { background: url(image/png;base64,iVBORw0KGgo=)}", + "p { width= } div { background: url(data:image/png;base64,iVBORw0KGgo=)}", + id="fallback_regex_rewriter_base64", + ), + ], +) +def test_css_recover_missing_data_prefix(input_css: str, expected_css: str): + assert ( + CssRewriter( + ArticleUrlRewriter(article_url=HttpUrl("https://istmat.org/node/42")), + base_href=None, + ).rewrite(input_css) + == expected_css + ) diff --git a/tests/rewriting/test_html_rewriting.py b/tests/rewriting/test_html_rewriting.py index 71df415..92f03f5 100644 --- a/tests/rewriting/test_html_rewriting.py +++ b/tests/rewriting/test_html_rewriting.py @@ -1735,3 +1735,197 @@ def test_rewritten_url_attributes_keep_their_original( .content == expected_str ) + + +@pytest.mark.parametrize( + "input_str, expected_str", + [ + pytest.param( + '', + '', + id="img_src_relative", + ), + pytest.param( + '', + '', + id="img_src_root_relative", + ), + pytest.param( + '', + '', + id="img_src_http_resolved", + ), + pytest.param( + '', + '', + id="img_src_data_double_slash", + ), + pytest.param( + '', + '', + id="img_src_whitespace_in_base64", + ), + pytest.param( + 'dl', + 'dl', + id="a_href_relative", + ), + pytest.param( + 'dl', + 'dl', + id="a_href_http_resolved", + ), + pytest.param( + '', + '', + id="video_poster_relative", + ), + pytest.param( + '', + '', + id="video_poster_data_double_slash", + ), + pytest.param( + '', + '', + id="srcset_single_unprefixed", + ), + pytest.param( + '', + '', + id="srcset_valid_data_and_http", + ), + pytest.param( + '', + '', + id="srcset_multiple_unprefixed", + ), + pytest.param( + '', + '', + id="srcset_http_then_unprefixed", + ), + pytest.param( + '', + '', + id="srcset_no_descriptor_no_space_after_comma", + ), + pytest.param( + '', + '', + id="srcset_data_double_slash", + ), + pytest.param( + '', + '', + id="srcset_only_commas_and_spaces", + ), + pytest.param( + '', + '', + id="srcset_trailing_comma", + ), + pytest.param( + '', + '', + id="srcset_non_base64_data_with_commas", + ), + pytest.param( + '', + '', + id="srcset_non_base64_data_with_commas_and_descriptor", + ), + pytest.param( + '', + '', + id="srcset_non_base64_data_with_commas_no_descriptor", + ), + pytest.param( + "", + "", + id="inline_style_tag_css_url", + ), + pytest.param( + '
', + '
', + id="inline_style_attr_css_url", + ), + ], +) +def test_html_recover_missing_data_prefix(input_str: str, expected_str: str): + assert ( + HtmlRewriter( + ArticleUrlRewriter(article_url=HttpUrl("https://istmat.org/node/42")), + None, + None, + None, + ) + .rewrite(input_str) + .content + == expected_str + ) + + +@pytest.mark.parametrize( + "input_str, expected_str", + [ + pytest.param( + '', + '', + id="script_module_unprefixed", + ), + pytest.param( + '', + '', + id="script_module_data_double_slash", + ), + pytest.param( + '', + '', + id="modulepreload_unprefixed", + ), + pytest.param( + '', + '', + id="script_module_valid_data", + ), + ], +) +def test_js_module_inline_data_does_not_notify(input_str: str, expected_str: str): + js_modules: list[ZimPath] = [] + + def custom_notify(zim_path: ZimPath): + js_modules.append(zim_path) + + rewritten = ( + HtmlRewriter( + url_rewriter=ArticleUrlRewriter( + article_url=HttpUrl("https://istmat.org/node/42") + ), + pre_head_insert=None, + post_head_insert=None, + notify_js_module=custom_notify, + ) + .rewrite(input_str) + .content + ) + + assert rewritten == expected_str + assert js_modules == [] diff --git a/tests/rewriting/test_url_rewriting.py b/tests/rewriting/test_url_rewriting.py index c599b49..0b7ef00 100644 --- a/tests/rewriting/test_url_rewriting.py +++ b/tests/rewriting/test_url_rewriting.py @@ -1608,3 +1608,279 @@ def _resolve_relative_reference(base_url: str, reference: str) -> str: base = urlsplit(base_url) merged_path = base.path.rpartition("/")[0] + "/" + reference.partition("#")[0] return f"{REPLAY_MARKER}{base.netloc}{_remove_dot_segments(merged_path)}" + + +class TestMissingDataPrefixRecovery: + @pytest.mark.parametrize( + "item_url, expected_data_url", + [ + pytest.param( + "image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + id="relative_image_jpeg", + ), + pytest.param( + "/image/png;base64,iVBORw0KGgo=", + "data:image/png;base64,iVBORw0KGgo=", + id="root_relative_image_png", + ), + pytest.param( + "./image/png;base64,iVBORw0KGgo=", + "data:image/png;base64,iVBORw0KGgo=", + id="dot_relative_image_png", + ), + pytest.param( + "../image/png;base64,iVBORw0KGgo=", + "data:image/png;base64,iVBORw0KGgo=", + id="parent_relative_image_png", + ), + pytest.param( + "https://istmat.org/node/image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + id="http_resolved_https", + ), + pytest.param( + "http://istmat.org/image/png;base64,iVBORw0KGgo=", + "data:image/png;base64,iVBORw0KGgo=", + id="http_resolved_http", + ), + pytest.param( + "//istmat.org/node/image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + id="protocol_relative_resolved", + ), + pytest.param( + "//image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + id="protocol_relative_media_type", + ), + pytest.param( + "data://image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + id="malformed_data_double_slash", + ), + pytest.param( + "data:///image/png;base64,iVBORw0KGgo=", + "data:image/png;base64,iVBORw0KGgo=", + id="malformed_data_triple_slash", + ), + pytest.param( + "DATA://image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + id="malformed_data_uppercase_scheme", + ), + pytest.param( + "image/svg+xml;base64,PHN2Zy8+", + "data:image/svg+xml;base64,PHN2Zy8+", + id="svg_plus_xml_mediatype", + ), + pytest.param( + "text/plain;charset=utf-8;base64,SGVsbG8=", + "data:text/plain;charset=utf-8;base64,SGVsbG8=", + id="mediatype_with_charset_param", + ), + pytest.param( + "application/vnd.ms-fontobject;base64,AAAA", + "data:application/vnd.ms-fontobject;base64,AAAA", + id="vendor_mediatype", + ), + pytest.param( + " image/jpeg;base64,/9j/4AAQ\n SkZJ\r\n\tRg== ", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + id="whitespace_in_base64_payload", + ), + pytest.param( + "image/png;BASE64,iVBORw0KGgo=", + "data:image/png;base64,iVBORw0KGgo=", + id="uppercase_base64_marker", + ), + pytest.param( + "https://istmat.org/node/image/jpeg;base64," + "A" * 200_000 + "==", + "data:image/jpeg;base64," + "A" * 200_000 + "==", + id="large_200kb_payload", + ), + ], + ) + @pytest.mark.parametrize("rewrite_all_url", [True, False]) + def test_recover_missing_data_prefix( + self, + item_url: str, + expected_data_url: str, + *, + rewrite_all_url: bool, + ): + rewriter = ArticleUrlRewriter( + article_url=HttpUrl("https://istmat.org/node/42"), + ) + assert rewriter( + item_url, base_href=None, rewrite_all_url=rewrite_all_url + ) == RewriteResult( + absolute_url=expected_data_url, + rewriten_url=expected_data_url, + zim_path=None, + ) + + @pytest.mark.parametrize( + "item_url, expected_absolute_url, expected_rewriten_url, expected_zim_path", + [ + pytest.param( + "https://example.com/foo/bar;base64,SGVsbG8=", + "https://example.com/foo/bar;base64,SGVsbG8=", + "../../example.com/foo/bar%3Bbase64,SGVsbG8=", + "example.com/foo/bar;base64,SGVsbG8=", + id="invalid_media_type", + ), + pytest.param( + "https://example.com/image/jpeg;base64,", + "https://example.com/image/jpeg;base64,", + "../../example.com/image/jpeg%3Bbase64,", + "example.com/image/jpeg;base64,", + id="empty_payload", + ), + pytest.param( + "https://example.com/image/jpeg;base64, ", + "https://example.com/image/jpeg;base64,", + "../../example.com/image/jpeg%3Bbase64,", + "example.com/image/jpeg;base64,", + id="whitespace_only_payload", + ), + pytest.param( + "https://example.com/image/jpeg;base64,index.html", + "https://example.com/image/jpeg;base64,index.html", + "../../example.com/image/jpeg%3Bbase64,index.html", + "example.com/image/jpeg;base64,index.html", + id="invalid_base64_dot", + ), + pytest.param( + "https://example.com/image/jpeg;base64,abc===", + "https://example.com/image/jpeg;base64,abc===", + "../../example.com/image/jpeg%3Bbase64,abc===", + "example.com/image/jpeg;base64,abc===", + id="too_many_padding_equals", + ), + pytest.param( + "https://example.com/image/jpeg;base64,==", + "https://example.com/image/jpeg;base64,==", + "../../example.com/image/jpeg%3Bbase64,==", + "example.com/image/jpeg;base64,==", + id="only_padding_equals", + ), + pytest.param( + "https://example.com/image/jpeg;base64,SGVsbG8=?w=100", + "https://example.com/image/jpeg;base64,SGVsbG8=?w=100", + "../../example.com/image/jpeg%3Bbase64,SGVsbG8=%3Fw=100", + "example.com/image/jpeg;base64,SGVsbG8=?w=100", + id="query_after_payload", + ), + pytest.param( + "https://example.com/search?q=image/jpeg;base64,SGVsbG8=", + "https://example.com/search?q=image/jpeg;base64,SGVsbG8=", + "../../example.com/search%3Fq=image/jpeg%3Bbase64,SGVsbG8=", + "example.com/search?q=image/jpeg;base64,SGVsbG8=", + id="marker_inside_query", + ), + pytest.param( + "https://example.com/page#image/jpeg;base64,SGVsbG8=", + "https://example.com/page#image/jpeg;base64,SGVsbG8=", + "../../example.com/page#image/jpeg;base64,SGVsbG8=", + "example.com/page", + id="marker_inside_fragment", + ), + pytest.param( + "https://example.com/image/jpeg?;base64,SGVsbG8=", + "https://example.com/image/jpeg?;base64,SGVsbG8=", + "../../example.com/image/jpeg%3F%3Bbase64,SGVsbG8=", + "example.com/image/jpeg?;base64,SGVsbG8=", + id="question_mark_before_marker", + ), + pytest.param( + "https://example.com/image/;base64,SGVsbG8=", + "https://example.com/image/;base64,SGVsbG8=", + "../../example.com/image/%3Bbase64,SGVsbG8=", + "example.com/image/;base64,SGVsbG8=", + id="missing_subtype", + ), + pytest.param( + "/image;base64,SGVsbG8=", + "https://istmat.org/image;base64,SGVsbG8=", + "../image%3Bbase64,SGVsbG8=", + "istmat.org/image;base64,SGVsbG8=", + id="no_subtype_segment", + ), + pytest.param( + "https://example.com/image/jpeg/;base64,SGVsbG8=", + "https://example.com/image/jpeg/;base64,SGVsbG8=", + "../../example.com/image/jpeg/%3Bbase64,SGVsbG8=", + "example.com/image/jpeg/;base64,SGVsbG8=", + id="trailing_slash_before_marker", + ), + pytest.param( + "https://example.com/image/jpeg%20;base64,SGVsbG8=", + "https://example.com/image/jpeg%20;base64,SGVsbG8=", + "../../example.com/image/jpeg%20%3Bbase64,SGVsbG8=", + "example.com/image/jpeg ;base64,SGVsbG8=", + id="invalid_mime_subtype", + ), + pytest.param( + "https://example.com/text/plain;;base64,SGVsbG8=", + "https://example.com/text/plain;;base64,SGVsbG8=", + "../../example.com/text/plain%3B%3Bbase64,SGVsbG8=", + "example.com/text/plain;;base64,SGVsbG8=", + id="empty_mime_param", + ), + pytest.param( + "https://example.com/image/jpeg;base64," + "A" * 200_000 + "!", + "https://example.com/image/jpeg;base64," + "A" * 200_000 + "!", + "../../example.com/image/jpeg%3Bbase64," + "A" * 200_000 + "%21", + "example.com/image/jpeg;base64," + "A" * 200_000 + "!", + id="large_200kb_invalid_payload", + ), + pytest.param( + "mailto:image/jpeg;base64,SGVsbG8=", + "mailto:image/jpeg;base64,SGVsbG8=", + "mailto:image/jpeg;base64,SGVsbG8=", + None, + id="non_http_mailto_scheme", + ), + pytest.param( + "blob:https://example.com/image/jpeg;base64,SGVsbG8=", + "blob:https://example.com/image/jpeg;base64,SGVsbG8=", + "blob:https://example.com/image/jpeg;base64,SGVsbG8=", + None, + id="non_http_blob_scheme", + ), + pytest.param( + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + "data:image/jpeg;base64,/9j/4AAQSkZJRg==", + None, + id="already_valid_data_url", + ), + pytest.param( + "https://example.com/image/jpeg;base64", + "https://example.com/image/jpeg;base64", + "../../example.com/image/jpeg%3Bbase64", + "example.com/image/jpeg;base64", + id="no_comma_after_base64", + ), + ], + ) + def test_negative_cases_not_rewritten_as_data( + self, + item_url: str, + expected_absolute_url: str, + expected_rewriten_url: str, + expected_zim_path: str | None, + ): + rewriter = ArticleUrlRewriter( + article_url=HttpUrl("https://istmat.org/node/42"), + ) + assert rewriter(item_url, base_href=None) == RewriteResult( + absolute_url=expected_absolute_url, + rewriten_url=expected_rewriten_url, + zim_path=( + ZimPath(REPLAY_MARKER + expected_zim_path) + if expected_zim_path + else None + ), + )