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
27 changes: 25 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,31 @@ async function attachReaderToWebview(
(item) => item.uri.path.toLowerCase() === currentSource.path.toLowerCase()
) ?? -1;

const currentFileName = currentSource.path.slice(currentSource.path.lastIndexOf('/') + 1).toLowerCase();
const currentFileName = currentSource.path.slice(currentSource.path.lastIndexOf('/') + 1);
const isLinear = spineIndex >= 0 ? (epubPackage?.spine[spineIndex]?.linear !== false) : true;

const spineItems = epubPackage?.spine.map((item, idx) => {
const itemFileName = item.uri.path.slice(item.uri.path.lastIndexOf('/') + 1);
const isActive = item.uri.path.toLowerCase() === currentSource.path.toLowerCase();
const tocMatch = epubPackage.toc.find(
(t) => t.uri.path.toLowerCase() === item.uri.path.toLowerCase()
);
return {
index: idx + 1,
idref: item.idref,
href: item.href,
fileName: itemFileName,
linear: item.linear !== false,
active: isActive,
label: tocMatch?.label
};
});

const currentFileNameLower = currentFileName.toLowerCase();
const tocWithActive = epubPackage?.toc.map((item) => {
const itemFileName = item.href.split(/[?#]/, 1)[0].split('/').pop()?.toLowerCase();
const isActive = item.uri.path.toLowerCase() === currentSource.path.toLowerCase()
|| (itemFileName && itemFileName === currentFileName);
|| (itemFileName && itemFileName === currentFileNameLower);
return {
label: item.label,
href: item.href,
Expand All @@ -240,6 +260,9 @@ async function attachReaderToWebview(
creator: epubPackage?.creator,
chapterIndex: spineIndex >= 0 ? spineIndex + 1 : undefined,
totalChapters: epubPackage?.spine.length,
currentFileName,
isLinear,
spineItems,
toc: tocWithActive,
historyBackInfo
};
Expand Down
281 changes: 267 additions & 14 deletions src/reader-shell.ts

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions test/epub.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,57 @@ test('parseOpfXml extracts title, creator, manifest, and spine in sequence', asy
assert.equal(parsed.spine[2].idref, 'ch2');
});

test('parseOpfXml strictly respects spine itemref order and linear attribute over manifest declaration order', async () => {
const epub = await import('../dist/epub.js');
const opfXml = `<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="pub-id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Unordered Spine Demo</dc:title>
</metadata>
<manifest>
<item id="a-ch1" href="a_chapter1.xhtml" media-type="application/xhtml+xml"/>
<item id="b-ch2" href="b_chapter2.xhtml" media-type="application/xhtml+xml"/>
<item id="cover" href="z_cover.xhtml" media-type="application/xhtml+xml"/>
<item id="prologue" href="m_prologue.xhtml" media-type="application/xhtml+xml"/>
</manifest>
<spine>
<itemref idref="cover" linear="no"/>
<itemref idref="prologue"/>
<itemref idref="b-ch2"/>
<itemref idref="a-ch1"/>
</spine>
</package>`;

const mockOpfUri = {
path: '/project/OEBPS/content.opf',
with(change) { return { ...this, ...change }; }
};
const mockRootUri = {
path: '/project',
with(change) { return { ...this, ...change }; }
};

const parsed = epub.parseOpfXml(opfXml, mockOpfUri, mockRootUri);
assert.equal(parsed.spine.length, 4);

// Verify exact order in spine: cover (z_cover) -> prologue (m_prologue) -> ch2 (b_chapter2) -> ch1 (a_chapter1)
assert.equal(parsed.spine[0].idref, 'cover');
assert.equal(parsed.spine[0].href, 'z_cover.xhtml');
assert.equal(parsed.spine[0].linear, false); // linear="no"

assert.equal(parsed.spine[1].idref, 'prologue');
assert.equal(parsed.spine[1].href, 'm_prologue.xhtml');
assert.equal(parsed.spine[1].linear, true);

assert.equal(parsed.spine[2].idref, 'b-ch2');
assert.equal(parsed.spine[2].href, 'b_chapter2.xhtml');
assert.equal(parsed.spine[2].linear, true);

assert.equal(parsed.spine[3].idref, 'a-ch1');
assert.equal(parsed.spine[3].href, 'a_chapter1.xhtml');
assert.equal(parsed.spine[3].linear, true);
});

test('parseNavHtml extracts TOC items from EPUB 3 Navigation document', async () => {
const epub = await import('../dist/epub.js');
const navHtml = `<!DOCTYPE html>
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/unordered-spine/META-INF/container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
17 changes: 17 additions & 0 deletions test/fixtures/unordered-spine/OEBPS/a_chapter1.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bab 1: Kilas Balik Memori</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<h1>Bab 1: Kilas Balik Memori (a_chapter1.xhtml)</h1>
<p>
Ini adalah Spine Item urutan ke-4 (Spine 4/4) sekaligus bab terakhir dalam buku ini.
</p>
<p>
Meskipun nama berkasnya berawalan <code>a_</code> (yang biasanya paling atas di folder explorer), urutan baca canonical di spine menempatkannya di paling akhir.
</p>
</body>
</html>
17 changes: 17 additions & 0 deletions test/fixtures/unordered-spine/OEBPS/b_chapter2.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bab 2: Loncatan Waktu</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<h1>Bab 2: Loncatan Waktu (b_chapter2.xhtml)</h1>
<p>
Ini adalah Spine Item urutan ke-3 (Spine 3/4). Bab 2 sengaja diletakkan sebelum Bab 1 di urutan spine untuk menguji alur cerita non-kronologis.
</p>
<p>
Jika tombol <strong>Next Chapter (→)</strong> ditekan berikutnya, reader WAJIB membuka <code>a_chapter1.xhtml</code>.
</p>
</body>
</html>
22 changes: 22 additions & 0 deletions test/fixtures/unordered-spine/OEBPS/content.opf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="pub-id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title>Unordered Spine Test Fixture</dc:title>
<dc:creator>XHTML Reader Previewer</dc:creator>
<dc:language>id</dc:language>
</metadata>
<manifest>
<item id="ch1" href="a_chapter1.xhtml" media-type="application/xhtml+xml"/>
<item id="ch2" href="b_chapter2.xhtml" media-type="application/xhtml+xml"/>
<item id="prologue" href="m_prologue.xhtml" media-type="application/xhtml+xml"/>
<item id="cover" href="z_cover.xhtml" media-type="application/xhtml+xml"/>
<item id="style" href="style.css" media-type="text/css"/>
<item id="toc" href="toc.xhtml" properties="nav" media-type="application/xhtml+xml"/>
</manifest>
<spine>
<itemref idref="cover" linear="no"/>
<itemref idref="prologue"/>
<itemref idref="ch2"/>
<itemref idref="ch1"/>
</spine>
</package>
17 changes: 17 additions & 0 deletions test/fixtures/unordered-spine/OEBPS/m_prologue.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Prolog: Awal Mula</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<h1>Prolog: Awal Mula (m_prologue.xhtml)</h1>
<p>
Ini adalah Spine Item urutan ke-2 (Spine 2/4). Secara alfabetis nama berkas, huruf 'm' berada di tengah, namun pembaca resmi membaca dokumen ini tepat setelah cover.
</p>
<p>
Jika tombol <strong>Next Chapter (→)</strong> ditekan, reader WAJIB membuka <code>b_chapter2.xhtml</code> sesuai urutan spine OPF, bukan <code>z_cover.xhtml</code>.
</p>
</body>
</html>
17 changes: 17 additions & 0 deletions test/fixtures/unordered-spine/OEBPS/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
body {
font-family: system-ui, -apple-system, sans-serif;
line-height: 1.8;
padding: 1.5rem;
}

h1 {
border-bottom: 2px solid #3b82f6;
padding-bottom: 0.5rem;
margin-bottom: 1.5rem;
}

code {
background: rgba(156, 163, 175, 0.2);
padding: 0.2rem 0.4rem;
border-radius: 4px;
}
19 changes: 19 additions & 0 deletions test/fixtures/unordered-spine/OEBPS/toc.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<title>Table of Contents</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<nav epub:type="toc" id="toc">
<h1>Table of Contents</h1>
<ol>
<li><a href="z_cover.xhtml">Sampul Buku (Non-linear)</a></li>
<li><a href="m_prologue.xhtml">Prolog: Awal Mula</a></li>
<li><a href="b_chapter2.xhtml">Bab 2: Loncatan Waktu</a></li>
<li><a href="a_chapter1.xhtml">Bab 1: Kilas Balik Memori</a></li>
</ol>
</nav>
</body>
</html>
15 changes: 15 additions & 0 deletions test/fixtures/unordered-spine/OEBPS/z_cover.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Cover Page</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div style="text-align: center; padding: 4rem 1rem;">
<h1>Cover Page (z_cover.xhtml)</h1>
<p><em>Item ini memiliki atribut <code>linear="no"</code> di spine.</em></p>
<p>Urutan spine sesungguhnya adalah Spine 1 dari 4, meskipun secara alfabetis file ini berawalan 'z'.</p>
</div>
</body>
</html>
28 changes: 28 additions & 0 deletions test/reader-shell.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,31 @@ test('readerShell contains reading navigation history return pill and keyboard b
// Verify keyboard navigation priority for Alt+Left
assert.match(extensionBundle, /data-can-go-back/);
});

test('readerShell contains spine order inspector tab and toolbar spine metadata', async () => {
const fs = await import('node:fs');
const extensionBundle = fs.readFileSync('dist/extension.js', 'utf8');

// Verify sidebar tab switcher elements
assert.match(extensionBundle, /class=["']sidebar-tabs["']/);
assert.match(extensionBundle, /id=["']tab-btn-toc["']/);
assert.match(extensionBundle, /id=["']tab-btn-spine["']/);
assert.match(extensionBundle, /id=["']tab-pane-toc["']/);
assert.match(extensionBundle, /id=["']tab-pane-spine["']/);

// Verify spine order list items and linear badge
assert.match(extensionBundle, /class=["']toc-item spine-item/);
assert.match(extensionBundle, /spine-item-idx/);
assert.match(extensionBundle, /spine-item-file/);
assert.match(extensionBundle, /spine-item-badge non-linear/);

// Verify toolbar spine metadata & non-linear indicator
assert.match(extensionBundle, /chapter-badge-file/);
assert.match(extensionBundle, /badge-non-linear/);

// Verify client-side tab switching script logic
assert.match(extensionBundle, /sidebar-tab-btn/);
assert.match(extensionBundle, /tabPanes\.spine/);
assert.match(extensionBundle, /tabPanes\.toc/);
});

Loading