Skip to content
Open
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
202 changes: 99 additions & 103 deletions packages/app-expo/assets/reader/reader.html

Large diffs are not rendered by default.

14 changes: 5 additions & 9 deletions packages/app-expo/assets/reader/reader.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -4108,18 +4108,14 @@
window.goLeft = function () { if (view) view.goLeft(); };
window.goRight = function () { if (view) view.goRight(); };
window.goToHref = function (href) { if (view) view.goTo(href); };
window.goToSection = async function (sectionIndex) {
window.goToSection = function (sectionIndex) {
if (!view) return;
const index = Number(sectionIndex);
if (!Number.isInteger(index)) return;
try {
const resolved = typeof view.resolveNavigation === 'function'
? await Promise.resolve(view.resolveNavigation(index))
: { index };
return view.goTo(resolved);
} catch (e) {
try { return view.goTo({ index }); } catch (_) {}
}
// Hand the index straight to view.goTo: pre-resolving it here produced an
// object that goTo could not handle, and its try/catch never fired
// because goTo swallows navigation errors itself.
return view.goTo(index);
};
window.goToCFI = function (cfi) { if (view) view.goTo(cfi); };
window.goToProgress = function (p) { if (view) view.goToFraction(p); };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, it } from "vitest";

const repositoryRoot = resolve(dirname(fileURLToPath(import.meta.url)), "../../../../..");

function readSource(relativePath: string): string {
return readFileSync(resolve(repositoryRoot, relativePath), "utf8");
}

describe("section index navigation", () => {
it("decodes only string targets so numeric indices survive goTo", () => {
const view = readSource("packages/foliate-js/view.js");

expect(view).toContain('if (typeof target === "string") target = decodeURIComponent(target);');
expect(view).not.toMatch(/^\s*target = decodeURIComponent\(target\);$/m);
expect(view).toContain('if (typeof target === "number") return { index: target };');
});

it("documents why an unguarded decode broke index navigation", () => {
// decodeURIComponent coerces its argument with ToString, which is what
// turned a section index into a href that resolves to nothing.
expect(decodeURIComponent(5 as unknown as string)).toBe("5");
expect(decodeURIComponent({ index: 5 } as unknown as string)).toBe("[object Object]");
});

it("passes the raw section index from the WebView bridge", () => {
const template = readSource("packages/app-expo/assets/reader/reader.template.html");
const builtReader = readSource("packages/app-expo/assets/reader/reader.html");

for (const source of [template, builtReader]) {
expect(source).toMatch(
/window\.goToSection = function \(sectionIndex\) \{[\s\S]*?return view\.goTo\(index\);[\s\S]*?\};/,
);
expect(source).not.toContain("await Promise.resolve(view.resolveNavigation(index))");
}
});
});
7 changes: 6 additions & 1 deletion packages/foliate-js/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,12 @@ export class View extends HTMLElement {
}
}
async goTo(target) {
target = decodeURIComponent(target);
// Only hrefs and CFIs are percent-encoded. Decoding unconditionally would
// stringify a section index (5 -> "5") or a resolved target
// ({ index: 5 } -> "[object Object]"), so resolveNavigation could never
// take its `typeof target === "number"` branch and the jump failed
// silently.
if (typeof target === "string") target = decodeURIComponent(target);
const resolved = this.resolveNavigation(target);
try {
await this.renderer.goTo(resolved);
Expand Down