@@ -431,13 +431,13 @@ open class EPUBNavigatorViewController: UIViewController,
431
431
}
432
432
433
433
/// Mapping between reading order hrefs and the table of contents title.
434
- private lazy var tableOfContentsTitleByHref : [ String : String ] = {
435
- func fulfill( linkList: [ Link ] ) -> [ String : String ] {
436
- var result = [ String : String] ( )
434
+ private lazy var tableOfContentsTitleByHref : [ AnyURL : String ] = {
435
+ func fulfill( linkList: [ Link ] ) -> [ AnyURL : String ] {
436
+ var result = [ AnyURL : String] ( )
437
437
438
438
for link in linkList {
439
439
if let title = link. title {
440
- result [ link. href ] = title
440
+ result [ link. url ( ) ] = title
441
441
}
442
442
let subResult = fulfill ( linkList: link. children)
443
443
result. merge ( subResult) { current, _ -> String in
@@ -565,7 +565,7 @@ open class EPUBNavigatorViewController: UIViewController,
565
565
return nil
566
566
}
567
567
568
- return readingOrder. firstIndex ( withHREF : spreads [ currentSpreadIndex] . left. href )
568
+ return readingOrder. firstIndexWithHREF ( spreads [ currentSpreadIndex] . left. url ( ) )
569
569
}
570
570
571
571
private let reloadSpreadsCompletions = CompletionList ( )
@@ -615,7 +615,7 @@ open class EPUBNavigatorViewController: UIViewController,
615
615
)
616
616
617
617
let initialIndex : Int = {
618
- if let href = locator? . href, let foundIndex = self . spreads. firstIndex ( withHref : href) {
618
+ if let href = locator? . href, let foundIndex = self . spreads. firstIndexWithHREF ( href) {
619
619
return foundIndex
620
620
} else {
621
621
return 0
@@ -633,10 +633,10 @@ open class EPUBNavigatorViewController: UIViewController,
633
633
}
634
634
}
635
635
636
- private func loadedSpreadView ( forHREF href: String ) -> EPUBSpreadView ? {
636
+ private func loadedSpreadViewForHREF < T : URLConvertible > ( _ href: T ) -> EPUBSpreadView ? {
637
637
paginationView. loadedViews
638
638
. compactMap { _, view in view as? EPUBSpreadView }
639
- . first { $0. spread. links. first ( withHREF : href) != nil }
639
+ . first { $0. spread. links. firstWithHREF ( href) != nil }
640
640
}
641
641
642
642
// MARK: - Navigator
@@ -671,21 +671,21 @@ open class EPUBNavigatorViewController: UIViewController,
671
671
}
672
672
673
673
let link = spreadView. focusedResource ?? spreadView. spread. leading
674
- let href = link. href
674
+ let href = link. url ( )
675
675
let progression = min ( max ( spreadView. progression ( in: href) , 0.0 ) , 1.0 )
676
676
677
677
if
678
678
// The positions are not always available, for example a Readium
679
679
// WebPub doesn't have any unless a Publication Positions Web
680
680
// Service is provided
681
- let index = readingOrder. firstIndex ( withHREF : href) ,
681
+ let index = readingOrder. firstIndexWithHREF ( href) ,
682
682
let positionList = positionsByReadingOrder. getOrNil ( index) ,
683
683
positionList. count > 0
684
684
{
685
685
// Gets the current locator from the positionList, and fill its missing data.
686
686
let positionIndex = Int ( ceil ( progression * Double( positionList. count - 1 ) ) )
687
687
return positionList [ positionIndex] . copy (
688
- title: tableOfContentsTitleByHref [ href] ,
688
+ title: tableOfContentsTitleByHref [ equivalent : href] ,
689
689
locations: { $0. progression = progression }
690
690
)
691
691
} else {
@@ -726,8 +726,10 @@ open class EPUBNavigatorViewController: UIViewController,
726
726
}
727
727
728
728
public func go( to locator: Locator , animated: Bool , completion: @escaping ( ) -> Void ) -> Bool {
729
+ let locator = publication. normalizeLocator ( locator)
730
+
729
731
guard
730
- let spreadIndex = spreads. firstIndex ( withHref : locator. href) ,
732
+ let spreadIndex = spreads. firstIndexWithHREF ( locator. href) ,
731
733
on ( . jump( locator) )
732
734
else {
733
735
return false
@@ -796,7 +798,11 @@ open class EPUBNavigatorViewController: UIViewController,
796
798
797
799
public func apply( decorations: [ Decoration ] , in group: String ) {
798
800
let source = self . decorations [ group] ?? [ ]
799
- let target = decorations. map { DiffableDecoration ( decoration: $0) }
801
+ let target = decorations. map { d in
802
+ var d = d
803
+ d. locator = publication. normalizeLocator ( d. locator)
804
+ return DiffableDecoration ( decoration: d)
805
+ }
800
806
801
807
self . decorations [ group] = target
802
808
@@ -815,7 +821,7 @@ open class EPUBNavigatorViewController: UIViewController,
815
821
guard let script = changes. javascript ( forGroup: group, styles: config. decorationTemplates) else {
816
822
continue
817
823
}
818
- loadedSpreadView ( forHREF : href) ? . evaluateScript ( script, inHREF: href)
824
+ loadedSpreadViewForHREF ( href) ? . evaluateScript ( script, inHREF: href)
819
825
}
820
826
}
821
827
}
@@ -922,7 +928,7 @@ extension EPUBNavigatorViewController: EPUBNavigatorViewModelDelegate {
922
928
for (_, view) in paginationView. loadedViews {
923
929
guard
924
930
let view = view as? EPUBSpreadView ,
925
- view. spread. links. first ( withHREF : href) != nil
931
+ view. spread. links. firstWithHREF ( href) != nil
926
932
else {
927
933
continue
928
934
}
@@ -966,10 +972,10 @@ extension EPUBNavigatorViewController: EPUBSpreadViewDelegate {
966
972
967
973
spreadView. evaluateScript ( " (function() { \n \( script) \n })(); " ) { _ in
968
974
for link in spreadView. spread. links {
969
- let href = link. href
975
+ let href = link. url ( )
970
976
for (group, decorations) in self . decorations {
971
977
let decorations = decorations
972
- . filter { $0. decoration. locator. href == href }
978
+ . filter { $0. decoration. locator. href. isEquivalentTo ( href) }
973
979
. map { DecorationChange . add ( $0. decoration) }
974
980
975
981
guard let script = decorations. javascript ( forGroup: group, styles: self . config. decorationTemplates) else {
@@ -1018,7 +1024,10 @@ extension EPUBNavigatorViewController: EPUBSpreadViewDelegate {
1018
1024
}
1019
1025
1020
1026
func spreadView( _ spreadView: EPUBSpreadView , didTapOnInternalLink href: String , clickEvent: ClickEvent ? ) {
1021
- guard var link = publication. link ( withHREF: href) else {
1027
+ guard
1028
+ let url = AnyURL ( string: href) ,
1029
+ var link = publication. linkWithHREF ( url)
1030
+ else {
1022
1031
log ( . warning, " Cannot find link with HREF: \( href) " )
1023
1032
return
1024
1033
}
0 commit comments