Skip to content

Commit 1aa5fac

Browse files
authored
Implement parsing of the <icon> element in OPDS 1.x feeds (#656)
1 parent 343c3ae commit 1aa5fac

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

Sources/OPDS/OPDS1Parser.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ public class OPDS1Parser: Loggable {
105105
feed.metadata.itemsPerPage = Int(itemsPerPage)
106106
}
107107

108+
if let iconValue = root.firstChild(tag: "icon")?.stringValue,
109+
let href = URLHelper.getAbsolute(href: iconValue, base: feedURL)
110+
{
111+
let iconLink = Link(href: href, rel: .icon)
112+
feed.links.append(iconLink)
113+
}
114+
108115
for entry in root.children(tag: "entry") {
109116
var isNavigation = true
110117
var collectionLink: Link?

Sources/Shared/Publication/LinkRelation.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public struct LinkRelation: Sendable {
6868
public static let next = LinkRelation("next")
6969
/// Refers to a resource that provides a preview of the link's context.
7070
public static let preview = LinkRelation("preview")
71+
/// Refers to an icon representing the link's context.
72+
public static let icon = LinkRelation("icon")
7173

7274
// OPDS – https://specs.opds.io/opds-1.2.html
7375

Tests/OPDSTests/Samples/wiki_1_1.opds

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
type="application/atom+xml;profile=opds-catalog;kind=navigation"/>
1919

2020
<title>Unpopular Publications</title>
21+
<icon>/images/favicon.ico?t=1516986276</icon>
2122
<updated>2010-01-10T10:01:11Z</updated>
2223
<author>
2324
<name>Spec Writer</name>

Tests/OPDSTests/readium_opds1_1_test.swift

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,52 @@ class readium_opds1_1_test: XCTestCase {
5151
}
5252

5353
func testLinks() {
54-
XCTAssertEqual(feed.links.count, 4)
55-
XCTAssertEqual(feed.links[0].rels, ["related"])
56-
XCTAssertEqual(feed.links[1].mediaType, MediaType("application/atom+xml;profile=opds-catalog;kind=acquisition")!)
57-
XCTAssertEqual(feed.links[2].href, "http://test.com/opds-catalogs/root.xml")
54+
XCTAssertEqual(feed.links.count, 5)
55+
56+
// Has a "related" link
57+
let expectedRelatedLink = Link(
58+
href: "http://test.com/opds-catalogs/vampire.farming.xml",
59+
mediaType: MediaType("application/atom+xml;profile=opds-catalog;kind=acquisition")!,
60+
rels: ["related"]
61+
)
62+
let relatedLink = feed?.links.first { $0.rels.contains("related") }
63+
XCTAssertEqual(relatedLink, expectedRelatedLink)
64+
65+
// Has a "self" link
66+
let expectedSelfLink = Link(
67+
href: "http://test.com/opds-catalogs/unpopular.xml",
68+
mediaType: MediaType("application/atom+xml;profile=opds-catalog;kind=acquisition")!,
69+
rels: ["self"]
70+
)
71+
let selfLink = feed?.links.first { $0.rels.contains("self") }
72+
XCTAssertEqual(selfLink, expectedSelfLink)
73+
74+
// Has a "start" link
75+
let expectedStartLink = Link(
76+
href: "http://test.com/opds-catalogs/root.xml",
77+
mediaType: MediaType("application/atom+xml;profile=opds-catalog;kind=navigation")!,
78+
rels: ["start"]
79+
)
80+
let startLink = feed?.links.first { $0.rels.contains("start") }
81+
XCTAssertEqual(startLink, expectedStartLink)
82+
83+
// Has an "up" link
84+
let expectedUpLink = Link(
85+
href: "http://test.com/opds-catalogs/root.xml",
86+
mediaType: MediaType("application/atom+xml;profile=opds-catalog;kind=navigation")!,
87+
rels: ["up"]
88+
)
89+
let upLink = feed?.links.first { $0.rels.contains("up") }
90+
XCTAssertEqual(upLink, expectedUpLink)
91+
92+
// Has an "icon" link
93+
let expectedIconLink = Link(
94+
href: "http://test.com/images/favicon.ico?t=1516986276",
95+
rels: ["icon"]
96+
)
97+
let iconLink = feed?.links.first { $0.rels.contains("icon") }
98+
XCTAssertEqual(iconLink, expectedIconLink)
99+
58100
// TODO: add more tests...
59101
}
60102

0 commit comments

Comments
 (0)