Selaa lähdekoodia

Try to use outermost element text when title is empty

lf94 4 vuotta sitten
vanhempi
commit
fa8431c5c6
2 muutettua tiedostoa jossa 36 lisäystä ja 3 poistoa
  1. 8 2
      reader/atom/atom_10.go
  2. 28 1
      reader/atom/atom_10_test.go

+ 8 - 2
reader/atom/atom_10.go

@@ -245,7 +245,12 @@ func (a *atom10Text) String() string {
 			content = a.InnerXML
 		}
 	case a.Type == "xhtml":
-		content = a.XHTMLRootElement.InnerXML
+		var root = a.XHTMLRootElement
+		if root.XMLName.Local == "div" {
+			content = root.InnerXML
+		} else {
+			content = a.InnerXML
+		}
 	default:
 		content = a.CharData
 	}
@@ -254,5 +259,6 @@ func (a *atom10Text) String() string {
 }
 
 type atomXHTMLRootElement struct {
-	InnerXML string `xml:",innerxml"`
+	XMLName  xml.Name `xml:"div"`
+	InnerXML string   `xml:",innerxml"`
 }

+ 28 - 1
reader/atom/atom_10_test.go

@@ -449,7 +449,34 @@ func TestParseEntryWithEmptyXHTMLTitle(t *testing.T) {
 	}
 
 	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[0].Title)
+	}
+}
+
+func TestParseEntryWithXHTMLTitleWithoutDiv(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">
+		  test
+		</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 != `test` {
+		t.Errorf("Incorrect entry title, got: %q", feed.Entries[0].Title)
 	}
 }