Просмотр исходного кода

fix(opml): trim whitespace around feed URLs and category names

A padded xmlUrl failed URL validation, which aborted the whole import,
and missed the duplicate feed check. Padded folder names created a
separate category.
Frédéric Guillot 9 часов назад
Родитель
Сommit
595e3ce920
2 измененных файлов с 38 добавлено и 0 удалено
  1. 5 0
      internal/reader/opml/opml.go
  2. 33 0
      internal/reader/opml/parser_test.go

+ 5 - 0
internal/reader/opml/opml.go

@@ -80,6 +80,11 @@ func (o *opmlOutline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
 		return err
 	}
 
+	o.Title = strings.TrimSpace(o.Title)
+	o.Text = strings.TrimSpace(o.Text)
+	o.FeedURL = strings.TrimSpace(o.FeedURL)
+	o.SiteURL = strings.TrimSpace(o.SiteURL)
+
 	for _, attr := range start.Attr {
 		if attr.Name.Space == minifluxOPMLNamespace {
 			if err := o.setMinifluxAttribute(attr.Name.Local, attr.Value); err != nil {

+ 33 - 0
internal/reader/opml/parser_test.go

@@ -88,6 +88,39 @@ func TestParseOpmlWithCategories(t *testing.T) {
 	}
 }
 
+func TestParseOpmlTrimsWhitespace(t *testing.T) {
+	data := `<?xml version="1.0" encoding="utf-8"?>
+	<opml version="2.0">
+		<body>
+			<outline text="  My Category  ">
+				<outline title="  Feed 1  " xmlUrl="  http://example.org/feed1/  " htmlUrl="  http://example.org/1  "/>
+				<outline title="   " xmlUrl="&#10;http://example.org/feed2/&#9;"/>
+			</outline>
+		</body>
+	</opml>
+	`
+
+	expected := []subcription{
+		{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: "My Category"},
+		{Title: "http://example.org/feed2/", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/feed2/", CategoryName: "My Category"},
+	}
+
+	subscriptions, err := parse(bytes.NewBufferString(data))
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if len(subscriptions) != len(expected) {
+		t.Fatalf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), len(expected))
+	}
+
+	for i := range len(subscriptions) {
+		if subscriptions[i] != expected[i] {
+			t.Errorf(`Subscription is different: "%v" vs "%v"`, subscriptions[i], expected[i])
+		}
+	}
+}
+
 func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) {
 	data := `<?xml version="1.0" encoding="ISO-8859-1"?>
 	<opml version="2.0">