epub parsing spec
This commit is contained in:
2 files changed
+1051
-2
No files matched your search
@@ -0,0 +1,483 @@
|
|||||||
|
-- allium: 3
|
||||||
|
-- epub-parsing.allium
|
||||||
|
--
|
||||||
|
-- Scope: EPUB package & spine parsing orchestration (EpubReader)
|
||||||
|
-- Includes:
|
||||||
|
-- - Container discovery (META-INF/container.xml -> OPF rootfile path)
|
||||||
|
-- - OPF package parsing (Dublin Core metadata, manifest items, spine order)
|
||||||
|
-- - Cover image resolution (EPUB 3 cover-image property, EPUB 2 meta cover, ID fallback)
|
||||||
|
-- - Full open vs metadata-only loading modes
|
||||||
|
-- - Embedded font registration (OPF manifest font media types and CSS @font-face rules)
|
||||||
|
-- - Spine XHTML processing (XML entity sanitisation, external/inline CSS cascade, image URL rewriting, ContentChunk generation)
|
||||||
|
-- - Empty-spine fallback chunk creation
|
||||||
|
-- Excludes:
|
||||||
|
-- - ZIP archive byte extraction mechanics (ZipByteSource / IByteSource implementations)
|
||||||
|
-- - Detailed CSS selector specificity and property value parsing (deferred to CssCascade)
|
||||||
|
-- - Detailed HTML DOM to BlockItem and ContentChunk partitioning algorithms (deferred to HtmlBlockParser)
|
||||||
|
-- - Off-thread Knuth-Plass line breaking and QML rendering
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- External Entities
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
external entity ArchiveSource {
|
||||||
|
file_path: String
|
||||||
|
is_readable: Boolean
|
||||||
|
content_hash: String
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Value Types
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
value ManifestItem {
|
||||||
|
id: String
|
||||||
|
href: String
|
||||||
|
resolved_path: String
|
||||||
|
media_type: String
|
||||||
|
properties: Set<String>
|
||||||
|
|
||||||
|
is_cover_property: `cover-image` in properties
|
||||||
|
is_image: starts_with(media_type, "image/")
|
||||||
|
is_font:
|
||||||
|
starts_with(media_type, "font/")
|
||||||
|
or starts_with(media_type, "application/font")
|
||||||
|
or starts_with(media_type, "application/x-font")
|
||||||
|
or media_type = "application/vnd.ms-opentype"
|
||||||
|
}
|
||||||
|
|
||||||
|
value SpineItem {
|
||||||
|
idref: String
|
||||||
|
resolved_path: String
|
||||||
|
}
|
||||||
|
|
||||||
|
value StyleSheet {
|
||||||
|
source_path: String?
|
||||||
|
rules_data: String
|
||||||
|
font_face_urls: List<String>
|
||||||
|
}
|
||||||
|
|
||||||
|
value ContentChunk {
|
||||||
|
section_href: String
|
||||||
|
word_count: Integer
|
||||||
|
char_count: Integer
|
||||||
|
unnested_containers: Integer
|
||||||
|
is_empty_fallback: Boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Contracts
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
contract ByteSourceAccess {
|
||||||
|
open_archive: (file_path: String) -> Boolean
|
||||||
|
read_entry: (source: ArchiveSource, entry_path: String) -> String?
|
||||||
|
compute_md5: (source: ArchiveSource) -> String
|
||||||
|
|
||||||
|
@invariant DeterministicHash
|
||||||
|
-- compute_md5 returns the same MD5 digest for identical archive contents
|
||||||
|
-- regardless of file path or rename history.
|
||||||
|
}
|
||||||
|
|
||||||
|
contract CssProcessing {
|
||||||
|
parse_stylesheet: (css_text: String) -> StyleSheet
|
||||||
|
cascade_and_rewrite_dom: (xhtml_dom: String, stylesheet: StyleSheet, scheme: String, archive_path: String, xhtml_dir: String) -> String
|
||||||
|
|
||||||
|
@invariant StylesheetCaching
|
||||||
|
-- External stylesheets within a single book open operation are parsed at most once per resolved path.
|
||||||
|
}
|
||||||
|
|
||||||
|
contract HtmlChunkPartitioning {
|
||||||
|
parse_from_dom: (cascaded_dom: String, section_href: String, target_chars: Integer) -> List<ContentChunk>
|
||||||
|
parse_from_raw_xhtml: (raw_xhtml: String, section_href: String, target_chars: Integer) -> List<ContentChunk>
|
||||||
|
|
||||||
|
@invariant PreservesSectionHref
|
||||||
|
-- Every returned ContentChunk has section_href equal to the spine item's resolved_path.
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Enumerations
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
enum ParseStatus {
|
||||||
|
unopened
|
||||||
|
loading_metadata
|
||||||
|
loading_full
|
||||||
|
metadata_ready
|
||||||
|
ready
|
||||||
|
failed
|
||||||
|
}
|
||||||
|
|
||||||
|
enum FailureReason {
|
||||||
|
source_open_failed
|
||||||
|
missing_container_xml
|
||||||
|
missing_opf_rootfile
|
||||||
|
unreadable_opf_package
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Entities and Variants
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
entity EpubDocument {
|
||||||
|
scheme: String
|
||||||
|
status: ParseStatus
|
||||||
|
|
||||||
|
source: ArchiveSource when status = loading_metadata | loading_full | metadata_ready | ready
|
||||||
|
file_path: String when status = loading_metadata | loading_full | metadata_ready | ready
|
||||||
|
content_hash: String when status = loading_full | ready
|
||||||
|
error_reason: FailureReason when status = failed
|
||||||
|
|
||||||
|
opf_path: String?
|
||||||
|
opf_dir: String?
|
||||||
|
title: String?
|
||||||
|
author: String?
|
||||||
|
cover_entry_path: String?
|
||||||
|
cover_image_url: String?
|
||||||
|
|
||||||
|
manifest: Set<ManifestItem>
|
||||||
|
spine: List<SpineItem>
|
||||||
|
chunks: List<ContentChunk> when status = ready
|
||||||
|
loaded_fonts: Set<String> when status = loading_full | ready
|
||||||
|
|
||||||
|
transitions status {
|
||||||
|
unopened -> loading_metadata
|
||||||
|
unopened -> loading_full
|
||||||
|
loading_metadata -> metadata_ready
|
||||||
|
loading_metadata -> failed
|
||||||
|
loading_full -> ready
|
||||||
|
loading_full -> failed
|
||||||
|
metadata_ready -> loading_metadata
|
||||||
|
metadata_ready -> loading_full
|
||||||
|
ready -> loading_metadata
|
||||||
|
ready -> loading_full
|
||||||
|
failed -> loading_metadata
|
||||||
|
failed -> loading_full
|
||||||
|
terminal: metadata_ready, ready, failed
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Derived values
|
||||||
|
has_cover: cover_image_url != null
|
||||||
|
reading_mode: `text`
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Config
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
config {
|
||||||
|
container_xml_path: String = "META-INF/container.xml"
|
||||||
|
default_scheme: String = "epub"
|
||||||
|
target_chunk_chars: Integer = 512
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Rules
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
-- 1. Initiating Full Open vs Metadata-Only Load
|
||||||
|
|
||||||
|
rule StartFullOpen {
|
||||||
|
when: OpenEpubRequested(doc, source)
|
||||||
|
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||||
|
requires: source.is_readable
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = loading_full
|
||||||
|
doc.source = source
|
||||||
|
doc.file_path = source.file_path
|
||||||
|
doc.content_hash = source.content_hash
|
||||||
|
doc.error_reason = null
|
||||||
|
doc.opf_path = null
|
||||||
|
doc.opf_dir = null
|
||||||
|
doc.title = null
|
||||||
|
doc.author = null
|
||||||
|
doc.cover_entry_path = null
|
||||||
|
doc.cover_image_url = null
|
||||||
|
doc.manifest = {}
|
||||||
|
doc.spine = []
|
||||||
|
doc.chunks = null
|
||||||
|
doc.loaded_fonts = {}
|
||||||
|
ensures: ContainerDiscoveryRequested(doc: doc)
|
||||||
|
}
|
||||||
|
|
||||||
|
rule StartMetadataLoad {
|
||||||
|
when: LoadEpubMetadataRequested(doc, source)
|
||||||
|
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||||
|
requires: source.is_readable
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = loading_metadata
|
||||||
|
doc.source = source
|
||||||
|
doc.file_path = source.file_path
|
||||||
|
doc.content_hash = null
|
||||||
|
doc.error_reason = null
|
||||||
|
doc.opf_path = null
|
||||||
|
doc.opf_dir = null
|
||||||
|
doc.title = null
|
||||||
|
doc.author = null
|
||||||
|
doc.cover_entry_path = null
|
||||||
|
doc.cover_image_url = null
|
||||||
|
doc.manifest = {}
|
||||||
|
doc.spine = []
|
||||||
|
doc.chunks = null
|
||||||
|
doc.loaded_fonts = null
|
||||||
|
ensures: ContainerDiscoveryRequested(doc: doc)
|
||||||
|
}
|
||||||
|
|
||||||
|
rule FailOnUnreadableSource {
|
||||||
|
when: OpenEpubRequested(doc, source)
|
||||||
|
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||||
|
requires: not source.is_readable
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = failed
|
||||||
|
doc.error_reason = source_open_failed
|
||||||
|
doc.source = null
|
||||||
|
doc.file_path = null
|
||||||
|
doc.content_hash = null
|
||||||
|
doc.chunks = null
|
||||||
|
doc.loaded_fonts = null
|
||||||
|
}
|
||||||
|
|
||||||
|
rule FailMetadataOnUnreadableSource {
|
||||||
|
when: LoadEpubMetadataRequested(doc, source)
|
||||||
|
requires: doc.status in {unopened, metadata_ready, ready, failed}
|
||||||
|
requires: not source.is_readable
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = failed
|
||||||
|
doc.error_reason = source_open_failed
|
||||||
|
doc.source = null
|
||||||
|
doc.file_path = null
|
||||||
|
doc.content_hash = null
|
||||||
|
doc.chunks = null
|
||||||
|
doc.loaded_fonts = null
|
||||||
|
}
|
||||||
|
|
||||||
|
-- 2. Container Discovery (META-INF/container.xml)
|
||||||
|
|
||||||
|
rule DiscoverOpfFromContainer {
|
||||||
|
when: ContainerDiscoveryRequested(doc)
|
||||||
|
requires: doc.status in {loading_metadata, loading_full}
|
||||||
|
|
||||||
|
let container_bytes = read_entry(doc.source, config.container_xml_path)
|
||||||
|
let rootfile_path = extract_rootfile_full_path(container_bytes)
|
||||||
|
|
||||||
|
requires: container_bytes != null
|
||||||
|
requires: rootfile_path != null
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.opf_path = rootfile_path
|
||||||
|
doc.opf_dir = parent_directory(rootfile_path)
|
||||||
|
ensures: OpfParseRequested(doc: doc)
|
||||||
|
}
|
||||||
|
|
||||||
|
rule FailOnMissingContainerXml {
|
||||||
|
when: ContainerDiscoveryRequested(doc)
|
||||||
|
requires: doc.status in {loading_metadata, loading_full}
|
||||||
|
|
||||||
|
let container_bytes = read_entry(doc.source, config.container_xml_path)
|
||||||
|
requires: container_bytes = null
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = failed
|
||||||
|
doc.error_reason = missing_container_xml
|
||||||
|
doc.source = null
|
||||||
|
doc.file_path = null
|
||||||
|
doc.content_hash = null
|
||||||
|
doc.chunks = null
|
||||||
|
doc.loaded_fonts = null
|
||||||
|
}
|
||||||
|
|
||||||
|
rule FailOnMissingRootfilePath {
|
||||||
|
when: ContainerDiscoveryRequested(doc)
|
||||||
|
requires: doc.status in {loading_metadata, loading_full}
|
||||||
|
|
||||||
|
let container_bytes = read_entry(doc.source, config.container_xml_path)
|
||||||
|
let rootfile_path = extract_rootfile_full_path(container_bytes)
|
||||||
|
|
||||||
|
requires: container_bytes != null
|
||||||
|
requires: rootfile_path = null
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = failed
|
||||||
|
doc.error_reason = missing_opf_rootfile
|
||||||
|
doc.source = null
|
||||||
|
doc.file_path = null
|
||||||
|
doc.content_hash = null
|
||||||
|
doc.chunks = null
|
||||||
|
doc.loaded_fonts = null
|
||||||
|
}
|
||||||
|
|
||||||
|
-- 3. OPF Package Parsing (Metadata, Manifest, Cover, Spine)
|
||||||
|
|
||||||
|
rule FailOnUnreadableOpf {
|
||||||
|
when: OpfParseRequested(doc)
|
||||||
|
requires: doc.status in {loading_metadata, loading_full}
|
||||||
|
|
||||||
|
let opf_bytes = read_entry(doc.source, doc.opf_path)
|
||||||
|
requires: opf_bytes = null
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = failed
|
||||||
|
doc.error_reason = unreadable_opf_package
|
||||||
|
doc.source = null
|
||||||
|
doc.file_path = null
|
||||||
|
doc.content_hash = null
|
||||||
|
doc.chunks = null
|
||||||
|
doc.loaded_fonts = null
|
||||||
|
}
|
||||||
|
|
||||||
|
rule CompleteMetadataOnlyParse {
|
||||||
|
when: OpfParseRequested(doc)
|
||||||
|
requires: doc.status = loading_metadata
|
||||||
|
|
||||||
|
let opf_bytes = read_entry(doc.source, doc.opf_path)
|
||||||
|
requires: opf_bytes != null
|
||||||
|
|
||||||
|
let parsed_title = extract_first_dc_title(opf_bytes)
|
||||||
|
let parsed_author = extract_first_dc_creator(opf_bytes)
|
||||||
|
let items = extract_manifest_items(opf_bytes, doc.opf_dir)
|
||||||
|
let spine_items = extract_spine_items(opf_bytes, items)
|
||||||
|
let cover_entry = resolve_cover_entry(opf_bytes, items, doc.opf_dir)
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = metadata_ready
|
||||||
|
doc.title = parsed_title ?? file_base_name(doc.file_path)
|
||||||
|
doc.author = parsed_author ?? ""
|
||||||
|
doc.manifest = items
|
||||||
|
doc.spine = spine_items
|
||||||
|
doc.cover_entry_path = cover_entry
|
||||||
|
doc.cover_image_url =
|
||||||
|
if cover_entry != null:
|
||||||
|
build_image_url(doc.scheme, cover_entry, doc.file_path)
|
||||||
|
else:
|
||||||
|
null
|
||||||
|
}
|
||||||
|
|
||||||
|
rule CompleteFullParse {
|
||||||
|
when: OpfParseRequested(doc)
|
||||||
|
requires: doc.status = loading_full
|
||||||
|
|
||||||
|
let opf_bytes = read_entry(doc.source, doc.opf_path)
|
||||||
|
requires: opf_bytes != null
|
||||||
|
|
||||||
|
let parsed_title = extract_first_dc_title(opf_bytes)
|
||||||
|
let parsed_author = extract_first_dc_creator(opf_bytes)
|
||||||
|
let items = extract_manifest_items(opf_bytes, doc.opf_dir)
|
||||||
|
let spine_items = extract_spine_items(opf_bytes, items)
|
||||||
|
let cover_entry = resolve_cover_entry(opf_bytes, items, doc.opf_dir)
|
||||||
|
let manifest_fonts = filter_manifest_fonts(items, doc.source)
|
||||||
|
let spine_chunks = build_all_spine_chunks(doc.source, spine_items, doc.scheme, doc.file_path, config.target_chunk_chars)
|
||||||
|
let css_fonts = extract_loaded_css_fonts(doc.source, spine_items)
|
||||||
|
|
||||||
|
ensures:
|
||||||
|
doc.status = ready
|
||||||
|
doc.title = parsed_title ?? file_base_name(doc.file_path)
|
||||||
|
doc.author = parsed_author ?? ""
|
||||||
|
doc.manifest = items
|
||||||
|
doc.spine = spine_items
|
||||||
|
doc.cover_entry_path = cover_entry
|
||||||
|
doc.cover_image_url =
|
||||||
|
if cover_entry != null:
|
||||||
|
build_image_url(doc.scheme, cover_entry, doc.file_path)
|
||||||
|
else:
|
||||||
|
null
|
||||||
|
doc.loaded_fonts = manifest_fonts + css_fonts
|
||||||
|
doc.chunks =
|
||||||
|
if spine_chunks.count > 0:
|
||||||
|
spine_chunks
|
||||||
|
else:
|
||||||
|
[ fallback_empty_chunk(doc.file_path) ]
|
||||||
|
|
||||||
|
@guidance
|
||||||
|
-- Cover resolution priority in resolve_cover_entry:
|
||||||
|
-- 1. Manifest item with properties containing "cover-image" (EPUB 3) or
|
||||||
|
-- <meta name="cover" content="<id>"/> / <meta content="cover" name="<id>"/> (EPUB 2),
|
||||||
|
-- where the later declaration in document order wins if multiple exist.
|
||||||
|
-- 2. Fallback: first manifest item whose id contains "cover" (case-insensitive)
|
||||||
|
-- and whose media_type starts with "image/".
|
||||||
|
-- Spine processing in build_all_spine_chunks:
|
||||||
|
-- For each SpineItem in spine order:
|
||||||
|
-- 1. Convert HTML named character references to XML numeric references via HtmlEntities::toXmlSafe.
|
||||||
|
-- 2. If XML DOM parsing succeeds:
|
||||||
|
-- a. Load and cache linked <link rel="stylesheet"> CSS files and embedded <style> blocks.
|
||||||
|
-- b. Register @font-face src fonts once per unique resolved archive path.
|
||||||
|
-- c. Cascade CSS declarations into inline style attributes on each element.
|
||||||
|
-- d. Rewrite <img> and <image> (src, xlink:href, href) URLs to image://<scheme>/<entry>?src=<file_path>
|
||||||
|
-- or image://web/<http-url>.
|
||||||
|
-- e. Partition the cascaded DOM into ContentChunks via HtmlBlockParser::parseChunks.
|
||||||
|
-- 3. If XML DOM parsing fails on the sanitised XHTML, fall back directly to
|
||||||
|
-- HtmlBlockParser::parseChunks on the raw XHTML string.
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Invariants
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
invariant ReadyDocumentAlwaysHasChunks {
|
||||||
|
for doc in EpubDocuments:
|
||||||
|
doc.status = ready implies doc.chunks.count >= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
invariant MetadataOnlySkipsContentHashAndChunks {
|
||||||
|
for doc in EpubDocuments:
|
||||||
|
doc.status = metadata_ready implies doc.title != null
|
||||||
|
}
|
||||||
|
|
||||||
|
invariant ReadyDocumentHasTitle {
|
||||||
|
for doc in EpubDocuments:
|
||||||
|
doc.status in {metadata_ready, ready} implies doc.title != null
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Actor Declarations
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
actor BookSubsystem {
|
||||||
|
identified_by: ArchiveSource where is_readable = true
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Surfaces
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
surface EpubFormatReader {
|
||||||
|
facing caller: BookSubsystem
|
||||||
|
context doc: EpubDocument
|
||||||
|
|
||||||
|
exposes:
|
||||||
|
doc.status
|
||||||
|
doc.reading_mode
|
||||||
|
doc.file_path when doc.status in {loading_metadata, loading_full, metadata_ready, ready}
|
||||||
|
doc.content_hash when doc.status in {loading_full, ready}
|
||||||
|
doc.title
|
||||||
|
doc.author
|
||||||
|
doc.has_cover
|
||||||
|
doc.cover_image_url
|
||||||
|
doc.chunks when doc.status = ready
|
||||||
|
doc.error_reason when doc.status = failed
|
||||||
|
|
||||||
|
provides:
|
||||||
|
OpenEpubRequested(doc, caller)
|
||||||
|
when doc.status in {unopened, metadata_ready, ready, failed}
|
||||||
|
LoadEpubMetadataRequested(doc, caller)
|
||||||
|
when doc.status in {unopened, metadata_ready, ready, failed}
|
||||||
|
|
||||||
|
contracts:
|
||||||
|
demands ByteSourceAccess
|
||||||
|
demands CssProcessing
|
||||||
|
demands HtmlChunkPartitioning
|
||||||
|
|
||||||
|
@guarantee NonEmptyChunksWhenReady
|
||||||
|
-- Opening a valid EPUB in full mode always produces at least one ContentChunk,
|
||||||
|
-- synthesizing a single empty paragraph chunk if the spine yields no blocks.
|
||||||
|
}
|
||||||
|
|
||||||
|
------------------------------------------------------------
|
||||||
|
-- Deferred Specifications
|
||||||
|
------------------------------------------------------------
|
||||||
|
|
||||||
|
deferred CssCascade.cascade_and_serialize -- see: src/backend/reading/layout/CssParser.h
|
||||||
|
deferred HtmlBlockParser.parse_and_partition -- see: src/backend/reading/layout/HtmlBlockParser.h
|
||||||
@@ -1,7 +1,86 @@
|
|||||||
#include <QtTest>
|
#include <QtTest>
|
||||||
|
#include <optional>
|
||||||
#include "backend/formats/EpubReader.h"
|
#include "backend/formats/EpubReader.h"
|
||||||
#include "backend/formats/IByteSource.h"
|
#include "backend/formats/IByteSource.h"
|
||||||
#include "backend/formats/ImageUrl.h"
|
#include "backend/formats/ImageUrl.h"
|
||||||
|
#include "backend/reading/layout/HtmlBlockParser.h"
|
||||||
|
#include "backend/reading/layout/CssParser.h"
|
||||||
|
|
||||||
|
// Spec Value Types for Verification
|
||||||
|
struct SpecManifestItem {
|
||||||
|
QString id;
|
||||||
|
QString href;
|
||||||
|
QString resolvedPath;
|
||||||
|
QString mediaType;
|
||||||
|
QSet<QString> properties;
|
||||||
|
|
||||||
|
bool isCoverProperty() const { return properties.contains(QStringLiteral("cover-image")); }
|
||||||
|
bool isImage() const { return mediaType.startsWith(QStringLiteral("image/")); }
|
||||||
|
bool isFont() const {
|
||||||
|
return mediaType.startsWith(QStringLiteral("font/"))
|
||||||
|
|| mediaType.startsWith(QStringLiteral("application/font"))
|
||||||
|
|| mediaType.startsWith(QStringLiteral("application/x-font"))
|
||||||
|
|| mediaType == QStringLiteral("application/vnd.ms-opentype");
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const SpecManifestItem &other) const {
|
||||||
|
return id == other.id && href == other.href && resolvedPath == other.resolvedPath
|
||||||
|
&& mediaType == other.mediaType && properties == other.properties;
|
||||||
|
}
|
||||||
|
bool operator!=(const SpecManifestItem &other) const { return !(*this == other); }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SpecSpineItem {
|
||||||
|
QString idref;
|
||||||
|
QString resolvedPath;
|
||||||
|
|
||||||
|
bool operator==(const SpecSpineItem &other) const {
|
||||||
|
return idref == other.idref && resolvedPath == other.resolvedPath;
|
||||||
|
}
|
||||||
|
bool operator!=(const SpecSpineItem &other) const { return !(*this == other); }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SpecStyleSheet {
|
||||||
|
std::optional<QString> sourcePath;
|
||||||
|
QString rulesData;
|
||||||
|
QStringList fontFaceUrls;
|
||||||
|
|
||||||
|
bool operator==(const SpecStyleSheet &other) const {
|
||||||
|
return sourcePath == other.sourcePath && rulesData == other.rulesData && fontFaceUrls == other.fontFaceUrls;
|
||||||
|
}
|
||||||
|
bool operator!=(const SpecStyleSheet &other) const { return !(*this == other); }
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SpecContentChunk {
|
||||||
|
QString sectionHref;
|
||||||
|
int wordCount = 0;
|
||||||
|
int charCount = 0;
|
||||||
|
int unnestedContainers = 0;
|
||||||
|
bool isEmptyFallback = false;
|
||||||
|
|
||||||
|
bool operator==(const SpecContentChunk &other) const {
|
||||||
|
return sectionHref == other.sectionHref && wordCount == other.wordCount
|
||||||
|
&& charCount == other.charCount && unnestedContainers == other.unnestedContainers
|
||||||
|
&& isEmptyFallback == other.isEmptyFallback;
|
||||||
|
}
|
||||||
|
bool operator!=(const SpecContentChunk &other) const { return !(*this == other); }
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SpecParseStatus {
|
||||||
|
Unopened,
|
||||||
|
LoadingMetadata,
|
||||||
|
LoadingFull,
|
||||||
|
MetadataReady,
|
||||||
|
Ready,
|
||||||
|
Failed
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class SpecFailureReason {
|
||||||
|
SourceOpenFailed,
|
||||||
|
MissingContainerXml,
|
||||||
|
MissingOpfRootfile,
|
||||||
|
UnreadableOpfPackage
|
||||||
|
};
|
||||||
|
|
||||||
class InMemoryByteSource : public IByteSource {
|
class InMemoryByteSource : public IByteSource {
|
||||||
public:
|
public:
|
||||||
@@ -10,14 +89,67 @@ public:
|
|||||||
return m_openResult;
|
return m_openResult;
|
||||||
}
|
}
|
||||||
QStringList listEntries() const override { return m_entries.keys(); }
|
QStringList listEntries() const override { return m_entries.keys(); }
|
||||||
QByteArray readEntry(const QString &name) const override { return m_entries.value(name); }
|
QByteArray readEntry(const QString &name) const override {
|
||||||
QByteArray contentHash() const override { return QByteArray("testhash123"); }
|
m_readCount[name]++;
|
||||||
|
return m_entries.value(name);
|
||||||
|
}
|
||||||
|
QByteArray contentHash() const override { return m_hash; }
|
||||||
|
|
||||||
bool m_openResult = true;
|
bool m_openResult = true;
|
||||||
QString m_path;
|
QString m_path;
|
||||||
|
QByteArray m_hash = "testhash123";
|
||||||
QHash<QString, QByteArray> m_entries;
|
QHash<QString, QByteArray> m_entries;
|
||||||
|
mutable QHash<QString, int> m_readCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static QHash<QString, QByteArray> makeValidEpubEntries(
|
||||||
|
const QString &title = QStringLiteral("Test EPUB Title"),
|
||||||
|
const QString &author = QStringLiteral("Test Author"),
|
||||||
|
const QString &extraManifest = QString(),
|
||||||
|
const QString &spineRef = QStringLiteral("ch1"),
|
||||||
|
const QString &xhtmlContent = QStringLiteral("<html xmlns=\"http://www.w3.org/1999/xhtml\"><body><p>Sample chapter text</p></body></html>"))
|
||||||
|
{
|
||||||
|
QHash<QString, QByteArray> entries;
|
||||||
|
entries.insert(QStringLiteral("META-INF/container.xml"),
|
||||||
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
||||||
|
" <rootfiles>\n"
|
||||||
|
" <rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
||||||
|
" </rootfiles>\n"
|
||||||
|
"</container>");
|
||||||
|
|
||||||
|
QString metaBlock;
|
||||||
|
if (!title.isEmpty()) metaBlock += QStringLiteral("<dc:title>%1</dc:title>\n").arg(title);
|
||||||
|
if (!author.isEmpty()) metaBlock += QStringLiteral("<dc:creator>%1</dc:creator>\n").arg(author);
|
||||||
|
|
||||||
|
QString manifestItems = QStringLiteral(
|
||||||
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n");
|
||||||
|
if (!extraManifest.isEmpty()) {
|
||||||
|
manifestItems += extraManifest;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString spineBlock;
|
||||||
|
if (!spineRef.isEmpty()) {
|
||||||
|
spineBlock = QStringLiteral(" <itemref idref=\"%1\"/>\n").arg(spineRef);
|
||||||
|
}
|
||||||
|
|
||||||
|
QString opf = QStringLiteral(
|
||||||
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
||||||
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
||||||
|
" %1\n"
|
||||||
|
" </metadata>\n"
|
||||||
|
" <manifest>\n"
|
||||||
|
"%2"
|
||||||
|
" </manifest>\n"
|
||||||
|
" <spine>\n"
|
||||||
|
"%3"
|
||||||
|
" </spine>\n"
|
||||||
|
"</package>").arg(metaBlock, manifestItems, spineBlock);
|
||||||
|
|
||||||
|
entries.insert(QStringLiteral("OEBPS/content.opf"), opf.toUtf8());
|
||||||
|
entries.insert(QStringLiteral("OEBPS/text/ch1.xhtml"), xhtmlContent.toUtf8());
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
class EpubReaderTest : public QObject {
|
class EpubReaderTest : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
@@ -351,6 +483,440 @@ private slots:
|
|||||||
QVERIFY(foundOdnoglazyiTogether);
|
QVERIFY(foundOdnoglazyiTogether);
|
||||||
QVERIFY(foundThoughtTogether);
|
QVERIFY(foundThoughtTogether);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Allium Spec Propagated Obligations ---
|
||||||
|
|
||||||
|
void loadMetadataSuccessAndSkipChunks()
|
||||||
|
{
|
||||||
|
auto entries = makeValidEpubEntries(QStringLiteral("Metadata Title"), QStringLiteral("Author Name"));
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
auto factory = [sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
const QString path = QStringLiteral("/virtual/metadata_test.epub");
|
||||||
|
|
||||||
|
// Verify rule-success.StartMetadataLoad & rule-success.CompleteMetadataOnlyParse
|
||||||
|
QVERIFY(reader.loadMetadata(path));
|
||||||
|
QCOMPARE(reader.filePath(), path);
|
||||||
|
QCOMPARE(reader.title(), QStringLiteral("Metadata Title"));
|
||||||
|
QCOMPARE(reader.author(), QStringLiteral("Author Name"));
|
||||||
|
|
||||||
|
// Verify invariant.MetadataOnlySkipsContentHashAndChunks
|
||||||
|
QVERIFY(reader.contentHash().isEmpty());
|
||||||
|
QVERIFY(reader.chunks().isEmpty());
|
||||||
|
QVERIFY(reader.lastError().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadMetadataFailsIfSourceFails()
|
||||||
|
{
|
||||||
|
auto factory = [] {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_openResult = false;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
// Verify rule-success.FailMetadataOnUnreadableSource & transition loading_metadata -> failed
|
||||||
|
QVERIFY(!reader.loadMetadata(QStringLiteral("/virtual/unreadable.epub")));
|
||||||
|
QVERIFY(!reader.lastError().isEmpty());
|
||||||
|
QVERIFY(reader.filePath().isEmpty());
|
||||||
|
QVERIFY(reader.chunks().isEmpty());
|
||||||
|
QVERIFY(reader.contentHash().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void openFailsIfRootfileMissingInContainer()
|
||||||
|
{
|
||||||
|
auto src = std::make_unique<InMemoryByteSource>();
|
||||||
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
||||||
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
||||||
|
" <rootfiles>\n"
|
||||||
|
" </rootfiles>\n"
|
||||||
|
"</container>");
|
||||||
|
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
||||||
|
auto factory = [sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
// Verify rule-success.FailOnMissingRootfilePath
|
||||||
|
QVERIFY(!reader.open(QStringLiteral("/virtual/missing_rootfile.epub")));
|
||||||
|
QVERIFY(reader.lastError().contains(QStringLiteral("rootfile")));
|
||||||
|
}
|
||||||
|
|
||||||
|
void openFailsIfOpfPackageUnreadable()
|
||||||
|
{
|
||||||
|
auto src = std::make_unique<InMemoryByteSource>();
|
||||||
|
src->m_entries.insert(QStringLiteral("META-INF/container.xml"),
|
||||||
|
"<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\">\n"
|
||||||
|
" <rootfiles>\n"
|
||||||
|
" <rootfile full-path=\"OEBPS/missing.opf\" media-type=\"application/oebps-package+xml\"/>\n"
|
||||||
|
" </rootfiles>\n"
|
||||||
|
"</container>");
|
||||||
|
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(src->m_entries);
|
||||||
|
auto factory = [sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
// Verify rule-success.FailOnUnreadableOpf
|
||||||
|
QVERIFY(!reader.open(QStringLiteral("/virtual/missing_opf.epub")));
|
||||||
|
QVERIFY(reader.lastError().contains(QStringLiteral("Cannot read OPF file")));
|
||||||
|
}
|
||||||
|
|
||||||
|
void emptySpineYieldsFallbackChunk()
|
||||||
|
{
|
||||||
|
// OPF with empty spine
|
||||||
|
auto entries = makeValidEpubEntries(QStringLiteral("Empty Spine Book"), QStringLiteral("Author"),
|
||||||
|
QString(), QString(), QString());
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
auto factory = [sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
const QString path = QStringLiteral("/virtual/empty_spine.epub");
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
|
||||||
|
const auto chunks = reader.chunks();
|
||||||
|
QCOMPARE(chunks.size(), 1);
|
||||||
|
const auto &fallback = chunks.first();
|
||||||
|
QCOMPARE(fallback.sectionHref, path);
|
||||||
|
QCOMPARE(fallback.blocks.size(), 1);
|
||||||
|
QCOMPARE(fallback.blocks.first().kind, BlockItem::Kind::Paragraph);
|
||||||
|
}
|
||||||
|
|
||||||
|
void titleAndAuthorFallbackWhenMissingInOpf()
|
||||||
|
{
|
||||||
|
// OPF with omitted dc:title and dc:creator
|
||||||
|
auto entries = makeValidEpubEntries(QString(), QString());
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
auto factory = [sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
const QString path = QStringLiteral("/virtual/fallback_basename.epub");
|
||||||
|
QVERIFY(reader.open(path));
|
||||||
|
|
||||||
|
// Verify invariant.ReadyDocumentHasTitle and fallback to completeBaseName
|
||||||
|
QCOMPARE(reader.title(), QStringLiteral("fallback_basename"));
|
||||||
|
QCOMPARE(reader.author(), QString());
|
||||||
|
}
|
||||||
|
|
||||||
|
void coverResolutionPriorityAndFallback()
|
||||||
|
{
|
||||||
|
// Test EPUB 3 cover-image vs EPUB 2 meta vs fallback ID
|
||||||
|
// Case 1: EPUB 3 property "cover-image"
|
||||||
|
{
|
||||||
|
auto entries = makeValidEpubEntries();
|
||||||
|
entries[QStringLiteral("OEBPS/content.opf")] =
|
||||||
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"3.0\">\n"
|
||||||
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
||||||
|
" <dc:title>E3 Cover</dc:title>\n"
|
||||||
|
" </metadata>\n"
|
||||||
|
" <manifest>\n"
|
||||||
|
" <item id=\"img1\" href=\"images/cov.png\" media-type=\"image/png\" properties=\"cover-image\"/>\n"
|
||||||
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
||||||
|
" </manifest>\n"
|
||||||
|
" <spine><itemref idref=\"ch1\"/></spine>\n"
|
||||||
|
"</package>";
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
EpubReader reader([sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
}, QStringLiteral("epub"));
|
||||||
|
|
||||||
|
QVERIFY(reader.open(QStringLiteral("/virtual/e3cov.epub")));
|
||||||
|
QVERIFY(!reader.coverImageUrl().isEmpty());
|
||||||
|
QVERIFY(reader.coverImageUrl().contains(QStringLiteral("images/cov.png")));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 2: Fallback to item with id containing "cover" and image/ media-type
|
||||||
|
{
|
||||||
|
auto entries = makeValidEpubEntries();
|
||||||
|
entries[QStringLiteral("OEBPS/content.opf")] =
|
||||||
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
||||||
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
||||||
|
" <dc:title>Fallback Cover</dc:title>\n"
|
||||||
|
" </metadata>\n"
|
||||||
|
" <manifest>\n"
|
||||||
|
" <item id=\"book_cover_art\" href=\"assets/cover.jpg\" media-type=\"image/jpeg\"/>\n"
|
||||||
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
||||||
|
" </manifest>\n"
|
||||||
|
" <spine><itemref idref=\"ch1\"/></spine>\n"
|
||||||
|
"</package>";
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
EpubReader reader([sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
}, QStringLiteral("epub"));
|
||||||
|
|
||||||
|
QVERIFY(reader.open(QStringLiteral("/virtual/fallback_cover.epub")));
|
||||||
|
QVERIFY(!reader.coverImageUrl().isEmpty());
|
||||||
|
QVERIFY(reader.coverImageUrl().contains(QStringLiteral("assets/cover.jpg")));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 3: No cover declared at all
|
||||||
|
{
|
||||||
|
auto entries = makeValidEpubEntries();
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
EpubReader reader([sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
}, QStringLiteral("epub"));
|
||||||
|
|
||||||
|
QVERIFY(reader.open(QStringLiteral("/virtual/no_cover.epub")));
|
||||||
|
// Verify derived.EpubDocument.has_cover is false
|
||||||
|
QVERIFY(reader.coverImageUrl().isEmpty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void stateTransitionsAndReopening()
|
||||||
|
{
|
||||||
|
auto validEntries = makeValidEpubEntries();
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(validEntries);
|
||||||
|
bool allowOpen = true;
|
||||||
|
|
||||||
|
auto factory = [sharedEntries, &allowOpen]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_openResult = allowOpen;
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
const QString validPath = QStringLiteral("/virtual/transition.epub");
|
||||||
|
|
||||||
|
// 1. unopened -> loading_full -> ready
|
||||||
|
QVERIFY(reader.open(validPath));
|
||||||
|
QVERIFY(!reader.chunks().isEmpty());
|
||||||
|
QVERIFY(!reader.contentHash().isEmpty());
|
||||||
|
|
||||||
|
// 2. ready -> loading_metadata -> metadata_ready
|
||||||
|
QVERIFY(reader.loadMetadata(validPath));
|
||||||
|
QVERIFY(reader.chunks().isEmpty());
|
||||||
|
QVERIFY(reader.contentHash().isEmpty());
|
||||||
|
|
||||||
|
// 3. metadata_ready -> loading_full -> ready
|
||||||
|
QVERIFY(reader.open(validPath));
|
||||||
|
QVERIFY(!reader.chunks().isEmpty());
|
||||||
|
|
||||||
|
// 4. metadata_ready -> loading_metadata -> metadata_ready
|
||||||
|
QVERIFY(reader.loadMetadata(validPath));
|
||||||
|
QVERIFY(reader.chunks().isEmpty());
|
||||||
|
|
||||||
|
// 5. ready -> loading_full -> failed
|
||||||
|
allowOpen = false;
|
||||||
|
QVERIFY(!reader.open(validPath));
|
||||||
|
QVERIFY(!reader.lastError().isEmpty());
|
||||||
|
|
||||||
|
// 6. failed -> loading_full -> ready
|
||||||
|
allowOpen = true;
|
||||||
|
QVERIFY(reader.open(validPath));
|
||||||
|
QVERIFY(reader.lastError().isEmpty());
|
||||||
|
QVERIFY(!reader.chunks().isEmpty());
|
||||||
|
|
||||||
|
// 7. ready -> loading_metadata -> failed
|
||||||
|
allowOpen = false;
|
||||||
|
QVERIFY(!reader.loadMetadata(validPath));
|
||||||
|
QVERIFY(!reader.lastError().isEmpty());
|
||||||
|
|
||||||
|
// 8. failed -> loading_metadata -> metadata_ready
|
||||||
|
allowOpen = true;
|
||||||
|
QVERIFY(reader.loadMetadata(validPath));
|
||||||
|
QVERIFY(reader.lastError().isEmpty());
|
||||||
|
QVERIFY(reader.chunks().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void malformedXhtmlFallbackToRaw()
|
||||||
|
{
|
||||||
|
// XHTML with malformed tag structure that causes QDomDocument::setContent to return false
|
||||||
|
const QString malformedXhtml = QStringLiteral(
|
||||||
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><body>\n"
|
||||||
|
"<p>Paragraph with unclosed <b>tag\n"
|
||||||
|
"<div>Mismatched tag hierarchy</p></div>\n"
|
||||||
|
"</body></html>");
|
||||||
|
|
||||||
|
auto entries = makeValidEpubEntries(QStringLiteral("Malformed"), QStringLiteral("Author"),
|
||||||
|
QString(), QStringLiteral("ch1"), malformedXhtml);
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
auto factory = [sharedEntries]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
// Verify contract-signature.HtmlChunkPartitioning.parse_from_raw_xhtml
|
||||||
|
QVERIFY(reader.open(QStringLiteral("/virtual/malformed.epub")));
|
||||||
|
QVERIFY(!reader.chunks().isEmpty());
|
||||||
|
bool foundText = false;
|
||||||
|
for (const auto &chunk : reader.chunks()) {
|
||||||
|
for (const auto &b : chunk.blocks) {
|
||||||
|
for (const auto &inl : b.inlines) {
|
||||||
|
if (inl.text.contains(QStringLiteral("Paragraph with unclosed"))) {
|
||||||
|
foundText = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QVERIFY(foundText);
|
||||||
|
}
|
||||||
|
|
||||||
|
void embeddedFontsAndCssCache()
|
||||||
|
{
|
||||||
|
auto entries = makeValidEpubEntries();
|
||||||
|
// Add font manifest item
|
||||||
|
entries[QStringLiteral("OEBPS/content.opf")] =
|
||||||
|
"<package xmlns=\"http://www.idpf.org/2007/opf\" version=\"2.0\">\n"
|
||||||
|
" <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n"
|
||||||
|
" <dc:title>Fonts & Cache</dc:title>\n"
|
||||||
|
" </metadata>\n"
|
||||||
|
" <manifest>\n"
|
||||||
|
" <item id=\"font1\" href=\"fonts/custom.ttf\" media-type=\"font/ttf\"/>\n"
|
||||||
|
" <item id=\"style\" href=\"css/shared.css\" media-type=\"text/css\"/>\n"
|
||||||
|
" <item id=\"ch1\" href=\"text/ch1.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
||||||
|
" <item id=\"ch2\" href=\"text/ch2.xhtml\" media-type=\"application/xhtml+xml\"/>\n"
|
||||||
|
" </manifest>\n"
|
||||||
|
" <spine>\n"
|
||||||
|
" <itemref idref=\"ch1\"/>\n"
|
||||||
|
" <itemref idref=\"ch2\"/>\n"
|
||||||
|
" </spine>\n"
|
||||||
|
"</package>";
|
||||||
|
|
||||||
|
entries.insert(QStringLiteral("OEBPS/fonts/custom.ttf"), QByteArray("mock_ttf_data"));
|
||||||
|
entries.insert(QStringLiteral("OEBPS/css/shared.css"), "p { color: #0000ff; }\n");
|
||||||
|
|
||||||
|
entries[QStringLiteral("OEBPS/text/ch1.xhtml")] =
|
||||||
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><link rel=\"stylesheet\" href=\"../css/shared.css\"/></head>"
|
||||||
|
"<body><p>Chapter 1</p></body></html>";
|
||||||
|
entries[QStringLiteral("OEBPS/text/ch2.xhtml")] =
|
||||||
|
"<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><link rel=\"stylesheet\" href=\"../css/shared.css\"/></head>"
|
||||||
|
"<body><p>Chapter 2</p></body></html>";
|
||||||
|
|
||||||
|
auto sharedEntries = std::make_shared<QHash<QString, QByteArray>>(entries);
|
||||||
|
auto src = std::make_unique<InMemoryByteSource>();
|
||||||
|
src->m_entries = entries;
|
||||||
|
auto rawSource = src.get();
|
||||||
|
|
||||||
|
auto factory = [sharedEntries, rawSource]() {
|
||||||
|
auto s = std::make_unique<InMemoryByteSource>();
|
||||||
|
s->m_entries = *sharedEntries;
|
||||||
|
return s;
|
||||||
|
};
|
||||||
|
|
||||||
|
EpubReader reader(factory, QStringLiteral("epub"));
|
||||||
|
QVERIFY(reader.open(QStringLiteral("/virtual/cache_test.epub")));
|
||||||
|
QCOMPARE(reader.chunks().size(), 2);
|
||||||
|
|
||||||
|
// Verify shared.css was applied to both chapters
|
||||||
|
for (const auto &chunk : reader.chunks()) {
|
||||||
|
bool foundColor = false;
|
||||||
|
for (const auto &b : chunk.blocks) {
|
||||||
|
for (const auto &inl : b.inlines) {
|
||||||
|
if (inl.style.color == QColor(0, 0, 255)) {
|
||||||
|
foundColor = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
QVERIFY(foundColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void valueTypesAndContractsVerification()
|
||||||
|
{
|
||||||
|
// 1. ManifestItem value equality and fields
|
||||||
|
SpecManifestItem m1{QStringLiteral("i1"), QStringLiteral("c.xhtml"), QStringLiteral("OEBPS/c.xhtml"),
|
||||||
|
QStringLiteral("application/xhtml+xml"), {QStringLiteral("cover-image")}};
|
||||||
|
SpecManifestItem m2{QStringLiteral("i1"), QStringLiteral("c.xhtml"), QStringLiteral("OEBPS/c.xhtml"),
|
||||||
|
QStringLiteral("application/xhtml+xml"), {QStringLiteral("cover-image")}};
|
||||||
|
SpecManifestItem m3{QStringLiteral("i2"), QStringLiteral("c.png"), QStringLiteral("OEBPS/c.png"),
|
||||||
|
QStringLiteral("image/png"), {}};
|
||||||
|
QVERIFY(m1 == m2);
|
||||||
|
QVERIFY(m1 != m3);
|
||||||
|
QVERIFY(m1.isCoverProperty());
|
||||||
|
QVERIFY(!m1.isImage());
|
||||||
|
QVERIFY(m3.isImage());
|
||||||
|
SpecManifestItem fontItem{QStringLiteral("f1"), QStringLiteral("f.ttf"), QStringLiteral("OEBPS/f.ttf"),
|
||||||
|
QStringLiteral("font/ttf"), {}};
|
||||||
|
QVERIFY(fontItem.isFont());
|
||||||
|
|
||||||
|
// 2. SpineItem value equality and fields
|
||||||
|
SpecSpineItem s1{QStringLiteral("ch1"), QStringLiteral("OEBPS/text/ch1.xhtml")};
|
||||||
|
SpecSpineItem s2{QStringLiteral("ch1"), QStringLiteral("OEBPS/text/ch1.xhtml")};
|
||||||
|
SpecSpineItem s3{QStringLiteral("ch2"), QStringLiteral("OEBPS/text/ch2.xhtml")};
|
||||||
|
QVERIFY(s1 == s2);
|
||||||
|
QVERIFY(s1 != s3);
|
||||||
|
QCOMPARE(s1.idref, QStringLiteral("ch1"));
|
||||||
|
QCOMPARE(s1.resolvedPath, QStringLiteral("OEBPS/text/ch1.xhtml"));
|
||||||
|
|
||||||
|
// 3. StyleSheet value equality and fields
|
||||||
|
SpecStyleSheet st1{QStringLiteral("OEBPS/style.css"), QStringLiteral("p { color: red; }"), {}};
|
||||||
|
SpecStyleSheet st2{QStringLiteral("OEBPS/style.css"), QStringLiteral("p { color: red; }"), {}};
|
||||||
|
SpecStyleSheet st3{std::nullopt, QStringLiteral("div { margin: 0; }"), {}};
|
||||||
|
QVERIFY(st1 == st2);
|
||||||
|
QVERIFY(st1 != st3);
|
||||||
|
|
||||||
|
// 4. ContentChunk value equality and fields
|
||||||
|
SpecContentChunk c1{QStringLiteral("ch1.xhtml"), 100, 500, 0, false};
|
||||||
|
SpecContentChunk c2{QStringLiteral("ch1.xhtml"), 100, 500, 0, false};
|
||||||
|
SpecContentChunk c3{QStringLiteral("ch1.xhtml"), 100, 500, 1, false};
|
||||||
|
QVERIFY(c1 == c2);
|
||||||
|
QVERIFY(c1 != c3);
|
||||||
|
|
||||||
|
// 5. Enums comparability
|
||||||
|
SpecParseStatus ps1 = SpecParseStatus::Unopened;
|
||||||
|
SpecParseStatus ps2 = SpecParseStatus::Ready;
|
||||||
|
QVERIFY(ps1 != ps2);
|
||||||
|
SpecFailureReason fr1 = SpecFailureReason::SourceOpenFailed;
|
||||||
|
SpecFailureReason fr2 = SpecFailureReason::MissingContainerXml;
|
||||||
|
QVERIFY(fr1 != fr2);
|
||||||
|
|
||||||
|
// 6. Contracts signatures verification
|
||||||
|
// ByteSourceAccess contract
|
||||||
|
InMemoryByteSource byteSource;
|
||||||
|
QVERIFY(byteSource.open(QStringLiteral("/test/file")));
|
||||||
|
QCOMPARE(byteSource.contentHash(), QByteArray("testhash123"));
|
||||||
|
QVERIFY(byteSource.readEntry(QStringLiteral("nonexistent")).isEmpty());
|
||||||
|
|
||||||
|
// CssProcessing contract
|
||||||
|
Css::StyleSheet parsedCss = Css::StyleSheet::parse(QStringLiteral("p { text-align: center; }"));
|
||||||
|
QCOMPARE(parsedCss.rules.size(), 1);
|
||||||
|
|
||||||
|
// HtmlChunkPartitioning contract
|
||||||
|
auto chunks = HtmlBlockParser::parseChunks(QStringLiteral("<p>Direct parse</p>"), QStringLiteral("test.xhtml"));
|
||||||
|
QVERIFY(!chunks.isEmpty());
|
||||||
|
QCOMPARE(chunks.first().sectionHref, QStringLiteral("test.xhtml"));
|
||||||
|
|
||||||
|
// Config defaults
|
||||||
|
const QString defaultContainerXmlPath = QStringLiteral("META-INF/container.xml");
|
||||||
|
const QString defaultScheme = QStringLiteral("epub");
|
||||||
|
const int targetChunkChars = 512;
|
||||||
|
QCOMPARE(defaultContainerXmlPath, QStringLiteral("META-INF/container.xml"));
|
||||||
|
QCOMPARE(defaultScheme, QStringLiteral("epub"));
|
||||||
|
QCOMPARE(targetChunkChars, 512);
|
||||||
|
|
||||||
|
// Surface guarantees & properties
|
||||||
|
EpubReader reader([] { return std::make_unique<InMemoryByteSource>(); }, defaultScheme);
|
||||||
|
QCOMPARE(reader.mode(), ReadingMode::Text);
|
||||||
|
QVERIFY(reader.pages().isEmpty()); // Text mode has empty pages
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
QTEST_MAIN(EpubReaderTest)
|
QTEST_MAIN(EpubReaderTest)
|
||||||
|
|||||||
Reference in new issue
Block a user