Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #59 from readium/fixes/navigator-api
Browse files Browse the repository at this point in the history
Create a Locator from a Link (navigator API)
  • Loading branch information
aferditamuriqi authored May 23, 2019
2 parents 350ad28 + 055059a commit 56f47b8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
15 changes: 15 additions & 0 deletions r2-shared-swift/Publication/Locator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,21 @@ public struct Locator: Equatable, CustomStringConvertible, Loggable {
try self.init(json: json)
}

public init(link: Link) {
let components = link.href.split(separator: "#", maxSplits: 1).map(String.init)
var locations: Locations?
if components.count > 1 {
locations = Locations(fragment: String(components[1]))
}

self.init(
href: components.first ?? link.href,
type: link.type ?? "",
title: link.title,
locations: locations
)
}

public var json: [String: Any] {
return makeJSON([
"href": href,
Expand Down
41 changes: 41 additions & 0 deletions r2-shared-swiftTests/Publication/LocatorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,47 @@ class LocatorTests: XCTestCase {
XCTAssertThrowsError(try Locator(json: ""))
}

func testMakeFromFullLink() {
XCTAssertEqual(
Locator(link: Link(
href: "http://locator",
type: "text/html",
title: "Link title"
)),
Locator(
href: "http://locator",
type: "text/html",
title: "Link title"
)
)
}

func testMakeFromMinimalLink() {
XCTAssertEqual(
Locator(link: Link(
href: "http://locator"
)),
Locator(
href: "http://locator",
type: "",
title: nil
)
)
}

func testMakeFromLinkWithFragment() {
XCTAssertEqual(
Locator(link: Link(
href: "http://locator#page=42"
)),
Locator(
href: "http://locator",
type: "",
locations: Locations(fragment: "page=42")
)
)
}

func testGetMinimalJSON() {
AssertJSONEqual(
Locator(
Expand Down

0 comments on commit 56f47b8

Please sign in to comment.