Skip to content

Commit c8917de

Browse files
authoredMar 18, 2025
Fix PDFNavigatorViewController.go(to: Link) (#563)
1 parent b1698b5 commit c8917de

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed
 

‎CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ All notable changes to this project will be documented in this file. Take a look
2323

2424
* A `self` link is not required anymore when parsing a RWPM.
2525

26+
### Fixed
27+
28+
#### Navigator
29+
30+
* Fixed going to a link containing a fragment in the PDF navigator, for example from the table of contents.
31+
2632

2733
## [3.1.0]
2834

‎Sources/Navigator/PDF/PDFNavigatorViewController.swift

+23-17
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
294294
if let locator = locator {
295295
await go(to: locator, isJump: false)
296296
} else if let link = publication.readingOrder.first {
297-
await go(to: link, pageNumber: 0, isJump: false)
297+
await go(to: link.url(), pageNumber: 0, isJump: false)
298298
} else {
299299
log(.error, "No initial location and empty reading order")
300300
}
@@ -384,25 +384,24 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
384384
private func go(to locator: Locator, isJump: Bool) async -> Bool {
385385
let locator = publication.normalizeLocator(locator)
386386

387-
guard let link = findLink(at: locator) else {
387+
let href: AnyURL? = {
388+
if isPDFFile {
389+
return publication.readingOrder.first?.url()
390+
} else {
391+
return publication.readingOrder.firstWithHREF(locator.href)?.url()
392+
}
393+
}()
394+
guard let href = href else {
388395
return false
389396
}
390397

391398
return await go(
392-
to: link,
399+
to: href,
393400
pageNumber: pageNumber(for: locator),
394401
isJump: isJump
395402
)
396403
}
397404

398-
private func findLink(at locator: Locator) -> Link? {
399-
if isPDFFile {
400-
return publication.readingOrder.first
401-
} else {
402-
return publication.readingOrder.firstWithHREF(locator.href)
403-
}
404-
}
405-
406405
/// Historically, the reading order of a standalone PDF file contained a
407406
/// single link with the HREF `"/<asset filename>"`. This was fragile if
408407
/// the asset named changed, or was different on other devices.
@@ -415,20 +414,23 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
415414
publication.readingOrder.count == 1 && publication.readingOrder[0].href == "publication.pdf"
416415

417416
@discardableResult
418-
private func go(to link: Link, pageNumber: Int?, isJump: Bool) async -> Bool {
419-
guard let pdfView = pdfView, let index = publication.readingOrder.firstIndex(of: link) else {
417+
private func go<HREF: URLConvertible>(to href: HREF, pageNumber: Int?, isJump: Bool) async -> Bool {
418+
guard
419+
let pdfView = pdfView,
420+
let url = publicationBaseURL.resolve(href),
421+
let index = publication.readingOrder.firstIndexWithHREF(href)
422+
else {
420423
return false
421424
}
422425

423426
if currentResourceIndex != index {
424-
let url = link.url(relativeTo: publicationBaseURL)
425427
guard let document = PDFDocument(url: url.url) else {
426-
log(.error, "Can't open PDF document at \(link)")
428+
log(.error, "Can't open PDF document at \(url)")
427429
return false
428430
}
429431

430432
currentResourceIndex = index
431-
documentHolder.set(document, at: link.url())
433+
documentHolder.set(document, at: href)
432434
pdfView.document = document
433435
updateScaleFactors()
434436
}
@@ -613,7 +615,11 @@ open class PDFNavigatorViewController: UIViewController, VisualNavigator, Select
613615
}
614616

615617
public func go(to link: Link, options: NavigatorGoOptions) async -> Bool {
616-
await go(to: link, pageNumber: nil, isJump: true)
618+
guard let locator = await publication.locate(link) else {
619+
return false
620+
}
621+
622+
return await go(to: locator, options: options)
617623
}
618624

619625
public func goForward(options: NavigatorGoOptions) async -> Bool {

0 commit comments

Comments
 (0)