Paginated scrollToPosition(1) asks for an offset one page past the last page

Open
#948 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ios, javascript, swift
Domain
mobile

Research direction

Start in Sources/Navigator/EPUB/Scripts/src/utils.js, comparing scrollToPosition() with scrollRight() and snapOffset(), which already clamps to documentWidth - pageWidth. Clamp the paginated offset in scrollToPosition() so it never exceeds the last page's start, and mirror it for RTL. Done when window.scrollX equals scrollWidth - innerWidth after goToIndex(..., location: .end), and the reproduction case shows progression below 1. Because the failure is intermittent on iOS, verify with a repeated UI test rather than one run.

Written by the indexing model from the issue text.

Description

Describe the bug

In a reflowable EPUB in paginated mode, going back from the first page of a resource to the previous one (previous page, or a swipe back) sometimes leaves that resource in a state where its last page is drawn, but its scripts see nothing on screen: the navigator reports the resource as read past its end (progression: 1), and an app that asks the page for the first visible word gets none.

Expected: the navigator shows the previous resource's last page, with window.scrollX at the start of that page.

Cause: EPUBReflowableSpreadView.scroll(toProgression:) with progression 1 (goToIndex(..., location: .end)) calls readium.scrollToPosition(1, dir, animated). In paginated mode scrollToPosition (Sources/Navigator/EPUB/Scripts/src/utils.js) asks for

var documentWidth = document.scrollingElement.scrollWidth;
let offset = documentWidth * position * factor;   // position 1 -> scrollWidth
scrollTo({ left: snapOffset(offset), animated });

When the resource is a whole number of pages wide, snapOffset(scrollWidth) is scrollWidth, one page past the last one (the largest valid offset is scrollWidth - innerWidth). Most of the time WebKit clamps it, which hides the problem. On iOS 27 we saw WebKit draw the last page (the UIScrollView of the web view at the last page, e.g. 3120) while keeping the requested offset in the page (window.scrollX == 3510 == scrollWidth). From then on the page's scripts see no element in the viewport, and the progression is 1.

scrollRight() in the same file already clamps to documentWidth - pageWidth (and scrollLeft() to -(documentWidth - pageWidth) in RTL); scrollToPosition() doesn't.

Suggested fix: clamp the paginated offset in scrollToPosition() the same way, e.g. Math.min(snapOffset(offset), documentWidth - pageWidth) (and the mirrored Math.max(..., -(documentWidth - pageWidth)) for RTL).

How to reproduce?
  1. Open a reflowable EPUB in paginated mode whose resources are several pages long.
  2. From the first page of a resource, go to the previous page (goBackward) or swipe back, so the navigator opens the previous resource at location: .end.
  3. In the web view, compare window.scrollX with document.scrollingElement.scrollWidth - window.innerWidth: scrollTo was asked for scrollWidth. Intermittently (2 of 8 runs in our UI test on an iOS 27.2 simulator) window.scrollX stays at scrollWidth and locationDidChange reports progression: 1.

We reproduced this in our own app's UI tests, not in the Test App. We work around it by wrapping readium.scrollToPosition in a user script that asks for the start of the last page when position >= 1 in paginated mode.

Readium version

3.11.0 (scrollToPosition() and snapOffset() are unchanged on develop, b968f54)

OS version

iOS 27.2

Testing device

iPhone 17e (simulator)

Environment
Xcode 27.2
platform: arm64
Additional context

Possibly related to #946 (same snapOffset() path, different failure).

Dominant language
Swift
Stars
557
Forks
249
Avg merge
3h 52m
Merged PRs (30d)
19

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from readium/swift-toolkit

All issues in readium/swift-toolkit

Similar issues

More Swift issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.