Skip to content

Commit e682b7b

Browse files
committed
Setup last position when jumping to a previous resource
1 parent acc3eae commit e682b7b

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

Sources/Shared/Publication/Services/Content/HTMLResourceContentIterator.swift

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,12 @@ public class HTMLResourceContentIterator : ContentIterator {
2525
.readAsString()
2626
.eraseToAnyError()
2727
.tryMap { try SwiftSoup.parse($0) }
28-
.tryMap { document -> ContentParser in
29-
let parser = ContentParser(
30-
baseLocator: locator,
31-
startElement: try locator.locations.cssSelector
32-
.flatMap {
33-
// The JS third-party library used to generate the CSS Selector sometimes adds
34-
// :root >, which doesn't work with JSoup.
35-
try document.select($0.removingPrefix(":root > ")).first()
36-
}
37-
)
38-
try document.traverse(parser)
39-
return parser
28+
.tryMap { document -> (content: [Content], startingIndex: Int) in
29+
try ContentParser.parse(document: document, locator: locator)
4030
}
4131

4232
content = result.map { $0.content }
43-
startingIndex = result.map { $0.startIndex }.get(or: 0)
33+
startingIndex = result.map { $0.startingIndex }.get(or: 0)
4434
}
4535

4636
public func close() {}
@@ -72,6 +62,31 @@ public class HTMLResourceContentIterator : ContentIterator {
7262
}
7363

7464
private class ContentParser: NodeVisitor {
65+
66+
static func parse(document: Document, locator: Locator) throws -> (content: [Content], startingIndex: Int) {
67+
let parser = ContentParser(
68+
baseLocator: locator,
69+
startElement: try locator.locations.cssSelector
70+
.flatMap {
71+
// The JS third-party library used to generate the CSS Selector sometimes adds
72+
// :root >, which doesn't work with JSoup.
73+
try document.select($0.removingPrefix(":root > ")).first()
74+
}
75+
)
76+
try document.traverse(parser)
77+
78+
var result = (
79+
content: parser.content,
80+
startingIndex: parser.startIndex
81+
)
82+
83+
if locator.locations.progression == 1.0 {
84+
result.startingIndex = result.content.count - 1
85+
}
86+
87+
return result
88+
}
89+
7590
private let baseLocator: Locator
7691
private let startElement: Element?
7792

@@ -87,7 +102,7 @@ public class HTMLResourceContentIterator : ContentIterator {
87102
private var currentCSSSelector: String?
88103
private var ignoredNode: Node?
89104

90-
init(baseLocator: Locator, startElement: Element?) {
105+
private init(baseLocator: Locator, startElement: Element?) {
91106
self.baseLocator = baseLocator
92107
self.startElement = startElement
93108
}

Sources/Shared/Publication/Services/Content/PublicationContentIterator.swift

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,31 @@ public class PublicationContentIterator: ContentIterator, Loggable {
8787

8888
private func loadIterator(from index: Int, by delta: Int) -> (index: Int, ContentIterator)? {
8989
let i = index + delta
90-
guard publication.readingOrder.indices.contains(i) else {
90+
guard
91+
let link = publication.readingOrder.getOrNil(i),
92+
var locator = publication.locate(link)
93+
else {
9194
return nil
9295
}
93-
guard let iterator = loadIterator(at: i) else {
94-
return loadIterator(from: i, by: delta)
95-
}
96-
return (i, iterator)
97-
}
98-
99-
private func loadIterator(at index: Int) -> ContentIterator? {
100-
let link = publication.readingOrder[index]
101-
guard var locator = publication.locate(link) else {
102-
return nil
103-
}
104-
96+
10597
if let start = startLocator.pop() {
10698
locator = locator.copy(
10799
locations: { $0 = start.locations },
108100
text: { $0 = start.text }
109101
)
102+
} else if delta < 0 {
103+
locator = locator.copy(
104+
locations: { $0.progression = 1.0 }
105+
)
106+
}
107+
108+
guard let iterator = loadIterator(at: link, locator: locator) else {
109+
return loadIterator(from: i, by: delta)
110110
}
111+
return (i, iterator)
112+
}
111113

114+
private func loadIterator(at link: Link, locator: Locator) -> ContentIterator? {
112115
let resource = publication.get(link)
113116
for factory in resourceContentIteratorFactories {
114117
if let iterator = factory(resource, locator) {

0 commit comments

Comments
 (0)