diff options
author | Frédéric Guillot <fred@miniflux.net> | 2020-01-04 15:56:00 -0800 |
---|---|---|
committer | Frédéric Guillot <fred@miniflux.net> | 2020-01-04 16:07:06 -0800 |
commit | 61f0c8aa6628efe91cb4ebc7e03ec17a0d4eb03d (patch) | |
tree | 7adf64d3f22dc274bd784a20ec2c42b5ef9c82c3 | |
parent | bf632fad2e19e9ece4db5957f05727f373541917 (diff) |
Allow application/xhtml+xml links as comments URL in Atom replies
-rw-r--r-- | reader/atom/atom_10.go | 6 | ||||
-rw-r--r-- | reader/atom/atom_10_test.go | 85 | ||||
-rw-r--r-- | reader/atom/atom_common.go | 10 |
3 files changed, 95 insertions, 6 deletions
diff --git a/reader/atom/atom_10.go b/reader/atom/atom_10.go index 708cc9f..5323070 100644 --- a/reader/atom/atom_10.go +++ b/reader/atom/atom_10.go @@ -194,9 +194,11 @@ func (a *atom10Entry) entryEnclosures() model.EnclosureList { return enclosures } -// See https://tools.ietf.org/html/rfc4685#section-3 +// See https://tools.ietf.org/html/rfc4685#section-4 +// If the type attribute of the atom:link is omitted, its value is assumed to be "application/atom+xml". +// We accept only HTML or XHTML documents for now since the intention is to have the same behavior as RSS. func (a *atom10Entry) entryCommentsURL() string { - commentsURL := a.Links.firstLinkWithRelationAndType("replies", "text/html") + commentsURL := a.Links.firstLinkWithRelationAndType("replies", "text/html", "application/xhtml+xml") if url.IsAbsoluteURL(commentsURL) { return commentsURL } diff --git a/reader/atom/atom_10_test.go b/reader/atom/atom_10_test.go index d614691..3129e29 100644 --- a/reader/atom/atom_10_test.go +++ b/reader/atom/atom_10_test.go @@ -734,7 +734,7 @@ A website: http://example.org/</media:description> } } -func TestParseRepliesLinkRelation(t *testing.T) { +func TestParseRepliesLinkRelationWithHTMLType(t *testing.T) { data := `<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0"> @@ -778,6 +778,89 @@ func TestParseRepliesLinkRelation(t *testing.T) { } } +func TestParseRepliesLinkRelationWithXHTMLType(t *testing.T) { + data := `<?xml version="1.0" encoding="utf-8"?> + <feed xmlns="http://www.w3.org/2005/Atom" + xmlns:thr="http://purl.org/syndication/thread/1.0"> + <id>http://www.example.org/myfeed</id> + <title>My Example Feed</title> + <updated>2005-07-28T12:00:00Z</updated> + <link href="http://www.example.org/myfeed" /> + <author><name>James</name></author> + <entry> + <id>tag:entries.com,2005:1</id> + <title>My original entry</title> + <updated>2006-03-01T12:12:12Z</updated> + <link href="http://www.example.org/entries/1" /> + <link rel="replies" + type="application/atom+xml" + href="http://www.example.org/mycommentsfeed.xml" + thr:count="10" thr:updated="2005-07-28T12:10:00Z" /> + <link rel="replies" + type="application/xhtml+xml" + href="http://www.example.org/comments.xhtml" + thr:count="10" thr:updated="2005-07-28T12:10:00Z" /> + <summary>This is my original entry</summary> + </entry> + </feed>` + + feed, err := Parse(bytes.NewBufferString(data)) + if err != nil { + t.Fatal(err) + } + + if len(feed.Entries) != 1 { + t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) + } + + if feed.Entries[0].URL != "http://www.example.org/entries/1" { + t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) + } + + if feed.Entries[0].CommentsURL != "http://www.example.org/comments.xhtml" { + t.Errorf("Incorrect entry comments URL, got: %s", feed.Entries[0].CommentsURL) + } +} + +func TestParseRepliesLinkRelationWithNoType(t *testing.T) { + data := `<?xml version="1.0" encoding="utf-8"?> + <feed xmlns="http://www.w3.org/2005/Atom" + xmlns:thr="http://purl.org/syndication/thread/1.0"> + <id>http://www.example.org/myfeed</id> + <title>My Example Feed</title> + <updated>2005-07-28T12:00:00Z</updated> + <link href="http://www.example.org/myfeed" /> + <author><name>James</name></author> + <entry> + <id>tag:entries.com,2005:1</id> + <title>My original entry</title> + <updated>2006-03-01T12:12:12Z</updated> + <link href="http://www.example.org/entries/1" /> + <link rel="replies" + href="http://www.example.org/mycommentsfeed.xml" + thr:count="10" thr:updated="2005-07-28T12:10:00Z" /> + <summary>This is my original entry</summary> + </entry> + </feed>` + + feed, err := Parse(bytes.NewBufferString(data)) + if err != nil { + t.Fatal(err) + } + + if len(feed.Entries) != 1 { + t.Errorf("Incorrect number of entries, got: %d", len(feed.Entries)) + } + + if feed.Entries[0].URL != "http://www.example.org/entries/1" { + t.Errorf("Incorrect entry URL, got: %s", feed.Entries[0].URL) + } + + if feed.Entries[0].CommentsURL != "" { + t.Errorf("Incorrect entry comments URL, got: %s", feed.Entries[0].CommentsURL) + } +} + func TestAbsoluteCommentsURL(t *testing.T) { data := `<?xml version="1.0" encoding="utf-8"?> <feed xmlns="http://www.w3.org/2005/Atom" diff --git a/reader/atom/atom_common.go b/reader/atom/atom_common.go index 85e6b29..511777b 100644 --- a/reader/atom/atom_common.go +++ b/reader/atom/atom_common.go @@ -57,10 +57,14 @@ func (a atomLinks) firstLinkWithRelation(relation string) string { return "" } -func (a atomLinks) firstLinkWithRelationAndType(relation, contentType string) string { +func (a atomLinks) firstLinkWithRelationAndType(relation string, contentTypes ...string) string { for _, link := range a { - if strings.ToLower(link.Rel) == relation && strings.ToLower(link.Type) == contentType { - return strings.TrimSpace(link.URL) + if strings.ToLower(link.Rel) == relation { + for _, contentType := range contentTypes { + if strings.ToLower(link.Type) == contentType { + return strings.TrimSpace(link.URL) + } + } } } |