Skip to content

Commit 4681098

Browse files
authored
Support for starting from a progression in the HTML content iterator (#597)
1 parent 68fb1cb commit 4681098

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. Take a look
1111
#### Shared
1212

1313
* Implementation of the [W3C Accessibility Metadata Display Guide](https://w3c.github.io/publ-a11y/a11y-meta-display-guide/2.0/guidelines/) specification to facilitate displaying accessibility metadata to users. [See the dedicated user guide](docs/Guides/Accessibility.md).
14+
* Support for starting from a progression in the HTML content iterator.
1415

1516
#### Navigator
1617

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,16 @@ public class HTMLResourceContentIterator: ContentIterator {
9494
private var currentIndex: Int?
9595

9696
private func elements() async throws -> ParsedElements {
97-
try await elementsTask.value
97+
try await elementsTask.value.get()
9898
}
9999

100100
private lazy var elementsTask = Task {
101-
let result = await resource
101+
await resource
102102
.readAsString()
103103
.eraseToAnyError()
104104
.tryMap { try SwiftSoup.parse($0) }
105105
.tryMap { try parse(document: $0, locator: locator, beforeMaxLength: beforeMaxLength) }
106106
.asyncMap { await adjustProgressions(of: $0) }
107-
return try result.get()
108107
}
109108

110109
private func parse(document: Document, locator: Locator, beforeMaxLength: Int) throws -> ParsedElements {
@@ -141,6 +140,20 @@ public class HTMLResourceContentIterator: ContentIterator {
141140
}
142141
)
143142
}
143+
144+
// Update the `startIndex` if a particular progression was requested.
145+
if
146+
elements.startIndex == 0,
147+
locator.locations.cssSelector == nil,
148+
let progression = locator.locations.progression,
149+
progression > 0, progression < 1
150+
{
151+
elements.startIndex = elements.elements.lastIndex { element in
152+
let elementProgression = element.locator.locations.progression ?? 0
153+
return elementProgression < progression
154+
} ?? 0
155+
}
156+
144157
return elements
145158
}
146159

Tests/SharedTests/Publication/Services/Content/Iterators/HTMLResourceContentIteratorTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,21 @@ class HTMLResourceContentIteratorTest: XCTestCase {
229229
XCTAssertEqual(elements[0], result)
230230
}
231231

232+
func testStartingFromProgression() async throws {
233+
func next(from progression: Double) async throws -> AnyEquatableContentElement? {
234+
try await iterator(html, start: locator(progression: progression)).next()?.equatable()
235+
}
236+
237+
var result = try await next(from: 0.5)
238+
XCTAssertEqual(result, elements[2])
239+
240+
result = try await next(from: 0.21)
241+
XCTAssertEqual(result, elements[1])
242+
243+
result = try await next(from: 0.81)
244+
XCTAssertEqual(result, elements[4])
245+
}
246+
232247
func testStartingFromCSSSelector() async throws {
233248
let iter = iterator(html, start: locator(selector: "#pgepubid00498 > p:nth-child(3)"))
234249

0 commit comments

Comments
 (0)