"""Pure MIME, HTML and CSS transformations for a portable browser archive.""" from __future__ import annotations import base64 import hashlib import mimetypes import re from email import encoders, policy from email.parser import BytesParser from html import escape, unescape from html.parser import HTMLParser from pathlib import Path from urllib.parse import urljoin, urlsplit import tinycss2 from .document_resources import resolve_local_reference def _assign_content_ids(parts) -> dict[str, str]: aliases = {} for part in parts: location = part.get("Content-Location", "") if location: identifier = "mdhelper-" + hashlib.sha256(location.encode()).hexdigest()[:24] if "Content-ID" in part: identifier = part["Content-ID"].strip("<>") else: part["Content-ID"] = f"<{identifier}>" aliases[location] = "cid:" + identifier return aliases class _ArchiveResources: def __init__(self, aliases: dict[str, str]): self.aliases = aliases def css_resource(self, url: str, base: str) -> str: if url.startswith(("#", "data:", "cid:")): return url absolute = urljoin(base, url) if absolute in self.aliases: return self.aliases[absolute] scheme = urlsplit(absolute).scheme if scheme == "file": path = resolve_local_reference(Path.cwd(), absolute) # Bundled styles list legacy font fallbacks not shipped with the app. if not path.is_file(): return "data:," mime = mimetypes.guess_type(path.name)[0] or "application/octet-stream" return f"data:{mime};base64," + base64.b64encode(path.read_bytes()).decode("ascii") if scheme in {"http", "https"}: raise ValueError("以下样式资源未能包含在离线归档中,请改用本地资源后重试:" + absolute) return absolute def css_text(self, text: str, base: str) -> str: tokens = tinycss2.parse_stylesheet(text) self._visit_css(tokens, base) return tinycss2.serialize(tokens) def _visit_css(self, items, base): for token in items: if token.type == "url": value = self.css_resource(token.value, base) token.value = value token.representation = 'url("' + value.replace('"', '%22') + '")' elif token.type == "function" and token.lower_name == "url": arguments = [item for item in token.arguments if item.type not in {"whitespace", "comment"}] if len(arguments) == 1 and arguments[0].type == "string": value = arguments[0].value else: value = tinycss2.serialize(token.arguments).strip() quoted = '"' + self.css_resource(value, base).replace('"', '%22') + '"' token.arguments = tinycss2.parse_component_value_list(quoted) else: if token.type == "at-rule" and token.lower_at_keyword == "import": for value in token.prelude: if value.type == "string": value.value = self.css_resource(value.value, base) value.representation = '"' + value.value + '"' for name in ("content", "prelude", "arguments"): if getattr(token, name, None): self._visit_css(getattr(token, name), base) class _HtmlResources(HTMLParser): def __init__(self, text, aliases): super().__init__(convert_charrefs=False) self.aliases = aliases self.changes = [] self.offsets = [0] + [m.end() for m in re.finditer("\n", text)] self.feed(text) def handle_starttag(self, tag, attrs): raw = self.get_starttag_text() line, column = self.getpos() offset = self.offsets[line - 1] + column for match in re.finditer(r'''\b(?:src|href|poster)\s*=\s*(["'])(.*?)\1''', raw, re.S): value = unescape(match[2]) if value in self.aliases: self.changes.append((offset + match.start(2), offset + match.end(2), escape(self.aliases[value], quote=True))) def _rewrite_text_part(part, resources: _ArchiveResources) -> None: kind = part.get_content_type() if kind not in {"text/html", "text/css"}: return text = part.get_payload(decode=True).decode(part.get_content_charset() or "utf-8") if kind == "text/css": text = resources.css_text(text, part.get("Content-Location", "")) else: chunks, offset = [], 0 for start, end, replacement in _HtmlResources(text, resources.aliases).changes: chunks.extend((text[offset:start], replacement)) offset = end chunks.append(text[offset:]) text = "".join(chunks) if "Content-Transfer-Encoding" in part: del part["Content-Transfer-Encoding"] part.set_param("charset", "utf-8") part.set_payload(text.encode("utf-8")) encoders.encode_base64(part) def portable_archive(data: bytes) -> bytes: """Use archive IDs instead of file URLs and embed CSS font dependencies. Chromium can block file: subresources even when present in an MHTML archive. Native page saving also omits local fonts that were not painted on the page. """ message = BytesParser(policy=policy.default).parsebytes(data) parts = [part for part in message.walk() if not part.is_multipart()] resources = _ArchiveResources(_assign_content_ids(parts)) for part in parts: _rewrite_text_part(part, resources) # Chromium expects literal Content-Location values, not RFC 2047 folded URLs. return message.as_bytes(policy=policy.SMTP.clone(max_line_length=0, refold_source="none", utf8=True))