Paginated goForward/goBackward can return true without moving the page

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

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
ios, javascript, swift
Domain
backend, mobile

Research direction

Start with EPUBReflowableSpreadView.swift lines 218–233 and the snapOffset implementation in Scripts/src/utils.js, as named in the issue; trace how UIKit’s content offset and WebKit’s scrollX determine page moves. Reproduce with rapid consecutive navigation in a paginated EPUB and compare the positions before and after each call. Done means each call that returns true visibly changes the page without crossing into the wrong resource.

Written by the indexing model from the issue text.

Description

Describe the bug

In a paginated reflowable EPUB, goForward(options:) and goBackward(options:) can return true while the page stays where it was, or cross into the wrong resource.

EPUBReflowableSpreadView.go(to:options:) decides between scrolling inside a resource and crossing to the next one from scrollView.contentOffset.x, then scrolls with window.scrollBy in JavaScript. UIKit's offset and WebKit's scrollX are not always in step. We found two situations.

1. A move made straight after another

contentOffset trails WebKit's scroll position for a moment after scrollBy. A second goForward(options: .none) made in that gap, from the last-but-one page of a resource, computes its target from the stale offset, finds it in range, and calls scrollBy on a page that is already the last one. Nothing moves and the call returns true. Going backward from the second page of a resource, the stale offset can instead make it cross into the previous resource.

2. After entering a resource from its end

Going backward into the previous resource calls readium.scrollToPosition(1). That sets scrollLeft to the full scrollWidth, because snapOffset rounds scrollWidth + 1 down to a page boundary, which is scrollWidth itself. The view shows the last page and contentOffset.x is scrollWidth - innerWidth, but window.scrollX can go on reporting scrollWidth, one page beyond the range. We saw this hold for at least 600 ms on a second navigator kept behind the visible one: scrollX 1902, scrollWidth 1902, innerWidth 951, contentOffset.x 951.

The next goBackward computes a valid target from contentOffset, then scrollBy(-innerWidth) moves WebKit from 1902 to 951, which is where the view already is. Nothing moves and the call returns true.

How to reproduce?
  1. Open an EPUB whose spine items are each two or three pages long, in paginated mode. We used six generated XHTML files of about 14 short paragraphs each, at 951 x 669 points with two columns.
  2. Call await navigator.goForward(options: .none) in a loop, with nothing between the calls.
  3. Before and after each call, read window.scrollX and readium.link.href from the visible web view.

Expected: every call that returns true shows a different page.

Actual: some calls return true with the same href and scrollX as before, mostly on the last page of a resource.

For the second case, step forward into the next resource, step back, wait for the page to settle, and compare window.scrollX with scrollView.contentOffset.x. Then call goBackward(options: .none) once more.

How often it happens depends on the device. Waiting for fonts and two stable animation frames after each move is not enough: on an iPhone 15 Pro one move in six rounds of a twelve-page book still stayed put.

Readium version

3.11.0, with a few local patches that do not touch this code. The same lines are on develop at 3b6b1559: EPUBReflowableSpreadView.swift lines 218 to 233, and snapOffset in Scripts/src/utils.js.

OS version

iOS 27.2 and 27.2.1

Testing device

iPhone 13 Pro Max and iPhone 15 Pro (physical devices)

Environment

No response

Additional context

Workarounds we use, in case they help:

  • Before each move, wait until contentOffset.x equals scrollX. For a right-to-left document the offset to compare is scrollWidth - innerWidth + scrollX, because scrollX counts down from zero while contentOffset.x counts up from the left edge.
  • When scrollX is outside 0...(scrollWidth - innerWidth), call window.scrollTo with the clamped value before the next move.
  • After each move, compare href and scrollX with the values before, and repeat a move that changed neither.

Possible fixes:

  • Decide inside one script: read scrollX, choose between scrolling and crossing, scroll, and return whether it moved. That takes UIKit's offset out of the decision.
  • Or wait for the two positions to agree before deciding.
  • Clamp the target in scrollToPosition to scrollWidth - pageWidth, which removes the second case.

The comment in the source explains that scrollBy was chosen over scrollTo for right-to-left content. An absolute target would also work there: it is scrollX - innerWidth going forward.

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.