Paginated scrollToPosition(1) asks for an offset one page past the last page
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?
- Open a reflowable EPUB in paginated mode whose resources are several pages long.
- From the first page of a resource, go to the previous page (
goBackward) or swipe back, so the navigator opens the previous resource atlocation: .end. - In the web view, compare
window.scrollXwithdocument.scrollingElement.scrollWidth - window.innerWidth:scrollTowas asked forscrollWidth. Intermittently (2 of 8 runs in our UI test on an iOS 27.2 simulator)window.scrollXstays atscrollWidthandlocationDidChangereportsprogression: 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
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from readium/swift-toolkit
-
triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
readium/swift-toolkit#954 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
readium/swift-toolkit#841 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 57/100
readium/swift-toolkit#961 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
readium/swift-toolkit#959 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
readium/swift-toolkit#953 ·
Maintainers usually reply within 1 day
All issues in readium/swift-toolkit
Similar issues
-
area: models enhancement needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
rexmhall09/TUFF#69 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
nikitabobko/AeroSpace#2427 · 1 comment ·
Maintainers usually reply within 8 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 63/100
Maintainers usually reply within 1 day
-
documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
A second click on the Control Center widget doesn't close Control CenterPossibly taken @MaHofseth claimed this today. Openneeds-triage
Difficulty 2/5 1-3 hours Newbie friendliness 78/100