Do not fallback to InnerXML if XHTML title is empty

This commit is contained in:
Frédéric Guillot 2022-03-04 14:18:43 -08:00
parent 1e357d3ced
commit c9e0f0b3e4
2 changed files with 31 additions and 29 deletions

View file

@ -227,6 +227,9 @@ type atom10Text struct {
XHTMLRootElement atomXHTMLRootElement `xml:"http://www.w3.org/1999/xhtml div"` XHTMLRootElement atomXHTMLRootElement `xml:"http://www.w3.org/1999/xhtml div"`
} }
// Text: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.1
// HTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.2
// XHTML: https://datatracker.ietf.org/doc/html/rfc4287#section-3.1.1.3
func (a *atom10Text) String() string { func (a *atom10Text) String() string {
var content string var content string
switch { switch {
@ -237,11 +240,7 @@ func (a *atom10Text) String() string {
content = a.InnerXML content = a.InnerXML
} }
case a.Type == "xhtml": case a.Type == "xhtml":
if a.XHTMLRootElement.InnerXML != "" {
content = a.XHTMLRootElement.InnerXML content = a.XHTMLRootElement.InnerXML
} else {
content = a.InnerXML
}
default: default:
content = a.CharData content = a.CharData
} }

View file

@ -339,14 +339,6 @@ func TestParseEntryWithXHTMLTitle(t *testing.T) {
<title>Example Feed</title> <title>Example Feed</title>
<link href="http://example.org/"/> <link href="http://example.org/"/>
<entry>
<title type="xhtml"><code>Test</code> Test</title>
<link href="http://example.org/a"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
<entry> <entry>
<title type="xhtml"> <title type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml"> <div xmlns="http://www.w3.org/1999/xhtml">
@ -366,11 +358,34 @@ func TestParseEntryWithXHTMLTitle(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if feed.Entries[0].Title != `<code>Test</code> Test` { if feed.Entries[0].Title != `This is <b>XHTML</b> content.` {
t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title) t.Errorf("Incorrect entry title, got: %q", feed.Entries[1].Title)
}
} }
if feed.Entries[1].Title != `This is <b>XHTML</b> content.` { func TestParseEntryWithEmptyXHTMLTitle(t *testing.T) {
data := `<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<link href="http://example.org/"/>
<entry>
<title type="xhtml">
<div xmlns="http://www.w3.org/1999/xhtml"/>
</title>
<link href="http://example.org/entry"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
</entry>
</feed>`
feed, err := Parse("https://example.org/", bytes.NewBufferString(data))
if err != nil {
t.Fatal(err)
}
if feed.Entries[0].Title != `http://example.org/entry` {
t.Errorf("Incorrect entry title, got: %q", feed.Entries[1].Title) t.Errorf("Incorrect entry title, got: %q", feed.Entries[1].Title)
} }
} }
@ -433,14 +448,6 @@ func TestParseEntryWithXHTMLSummary(t *testing.T) {
<title>Example Feed</title> <title>Example Feed</title>
<link href="http://example.org/"/> <link href="http://example.org/"/>
<entry>
<title type="xhtml">Example</title>
<link href="http://example.org/2003/12/13/atom03"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary type="xhtml"><p>Some text.</p></summary>
</entry>
<entry> <entry>
<title type="xhtml">Example</title> <title type="xhtml">Example</title>
<link href="http://example.org/2003/12/13/atom03"/> <link href="http://example.org/2003/12/13/atom03"/>
@ -456,11 +463,7 @@ func TestParseEntryWithXHTMLSummary(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if feed.Entries[0].Content != `<p>Some text.</p>` { if feed.Entries[0].Content != `<p>Test: <code>std::unique_ptr&lt;S&gt;</code></p>` {
t.Errorf("Incorrect entry content, got: %s", feed.Entries[0].Content)
}
if feed.Entries[1].Content != `<p>Test: <code>std::unique_ptr&lt;S&gt;</code></p>` {
t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content) t.Errorf("Incorrect entry content, got: %s", feed.Entries[1].Content)
} }
} }