Repository: mmcdole/gofeed
Pull request #156 (2020-09-16 09:25:50)

Ignore all leading space characters in detector
reported in https://github.com/nkanaev/yarr/issues/6: the source xml contains newlines

coveralls: 
[![Coverage Status](https://coveralls.io/builds/33497652/badge)](https://coveralls.io/builds/33497652)

Coverage increased (+8.6%) to 73.323% when pulling **e7eaadcbb03e754ffa1e39d948f53db1fb673faa on nkanaev:master** into **a9ae6739dd913250d5c0942d86e8b366445ae0e7 on mmcdole:master**.

mmcdole: Thanks for your contribution!

Patch:
diff --git a/detector.go b/detector.go
index 898b1af..b3853b8 100644
--- a/detector.go
+++ b/detector.go
@@ -35,15 +35,17 @@ func DetectFeedType(feed io.Reader) FeedType {
 
 	// remove leading whitespace (if exists)
 	var firstChar byte
-	for {
+	loop: for {
 		ch, err := buffer.ReadByte()
 		if err != nil {
 			return FeedTypeUnknown
 		}
-		if ch != ' ' && ch != '\t' {
+		switch ch {
+		case ' ', '\r', '\n', '\t':
+		default:
 			firstChar = ch
 			buffer.UnreadByte()
-			break
+			break loop
 		}
 	}
 


Test patch:
diff --git a/detector_test.go b/detector_test.go
index 8758463..f3eb059 100644
--- a/detector_test.go
+++ b/detector_test.go
@@ -19,6 +19,7 @@ func TestDetectFeedType(t *testing.T) {
 		{"atom03_feed.xml", gofeed.FeedTypeAtom},
 		{"atom10_feed.xml", gofeed.FeedTypeAtom},
 		{"rss_feed.xml", gofeed.FeedTypeRSS},
+		{"rss_feed_leading_spaces.xml", gofeed.FeedTypeRSS},
 		{"rdf_feed.xml", gofeed.FeedTypeRSS},
 		{"unknown_feed.xml", gofeed.FeedTypeUnknown},
 		{"empty_feed.xml", gofeed.FeedTypeUnknown},
diff --git a/parser_test.go b/parser_test.go
index 3aa91b4..b30959c 100644
--- a/parser_test.go
+++ b/parser_test.go
@@ -27,6 +27,7 @@ func TestParser_Parse(t *testing.T) {
 		{"atom03_feed.xml", "atom", "Feed Title", false},
 		{"atom10_feed.xml", "atom", "Feed Title", false},
 		{"rss_feed.xml", "rss", "Feed Title", false},
+		{"rss_feed_leading_spaces.xml", "rss", "Feed Title", false},
 		{"rdf_feed.xml", "rss", "Feed Title", false},
 		{"sample.json", "json", "title", false},
 		{"unknown_feed.xml", "", "", true},
@@ -67,6 +68,7 @@ func TestParser_ParseString(t *testing.T) {
 		{"atom03_feed.xml", "atom", "Feed Title", false},
 		{"atom10_feed.xml", "atom", "Feed Title", false},
 		{"rss_feed.xml", "rss", "Feed Title", false},
+		{"rss_feed_leading_spaces.xml", "rss", "Feed Title", false},
 		{"rdf_feed.xml", "rss", "Feed Title", false},
 		{"sample.json", "json", "title", false},
 		{"unknown_feed.xml", "", "", true},
@@ -107,6 +109,7 @@ func TestParser_ParseURL_Success(t *testing.T) {
 		{"atom03_feed.xml", "atom", "Feed Title", false},
 		{"atom10_feed.xml", "atom", "Feed Title", false},
 		{"rss_feed.xml", "rss", "Feed Title", false},
+		{"rss_feed_leading_spaces.xml", "rss", "Feed Title", false},
 		{"rdf_feed.xml", "rss", "Feed Title", false},
 		{"sample.json", "json", "title", false},
 		{"unknown_feed.xml", "", "", true},
diff --git a/testdata/parser/universal/rss_feed_leading_spaces.xml b/testdata/parser/universal/rss_feed_leading_spaces.xml
new file mode 100644
index 0000000..94d5c0d
--- /dev/null
+++ b/testdata/parser/universal/rss_feed_leading_spaces.xml
@@ -0,0 +1,12 @@
+
+   
+		
+<!--
+Description: rss feed with leading space chars: ` `, `\n`, `\t`, '\n'.
+Careful with editing the file.
+-->
+<rss version="2.0">
+  <channel>
+    <title>Feed Title</title>
+  </channel>
+</rss>