mirror of
				https://github.com/nkanaev/yarr.git
				synced 2025-11-04 00:38:46 +00:00 
			
		
		
		
	fix parsing opml with encoding
This commit is contained in:
		@@ -3,6 +3,8 @@ package opml
 | 
				
			|||||||
import (
 | 
					import (
 | 
				
			||||||
	"encoding/xml"
 | 
						"encoding/xml"
 | 
				
			||||||
	"io"
 | 
						"io"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"golang.org/x/net/html/charset"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type opml struct {
 | 
					type opml struct {
 | 
				
			||||||
@@ -45,6 +47,7 @@ func Parse(r io.Reader) (Folder, error) {
 | 
				
			|||||||
	decoder := xml.NewDecoder(r)
 | 
						decoder := xml.NewDecoder(r)
 | 
				
			||||||
	decoder.Entity = xml.HTMLEntity
 | 
						decoder.Entity = xml.HTMLEntity
 | 
				
			||||||
	decoder.Strict = false
 | 
						decoder.Strict = false
 | 
				
			||||||
 | 
						decoder.CharsetReader = charset.NewReaderLabel
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	err := decoder.Decode(&val)
 | 
						err := decoder.Decode(&val)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package opml
 | 
					package opml
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
@@ -87,3 +88,41 @@ func TestParseFallback(t *testing.T) {
 | 
				
			|||||||
		t.Fatal("invalid opml")
 | 
							t.Fatal("invalid opml")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func TestParseWithEncoding(t *testing.T) {
 | 
				
			||||||
 | 
						file, err := os.Open("sample_win1251.xml")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						have, err := Parse(file)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							t.Fatal(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						want := Folder{
 | 
				
			||||||
 | 
							Title: "",
 | 
				
			||||||
 | 
							Feeds: []Feed{
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									Title:   "пример1",
 | 
				
			||||||
 | 
									FeedUrl: "https://baz.com/feed.xml",
 | 
				
			||||||
 | 
									SiteUrl: "https://baz.com/",
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							Folders: []Folder{
 | 
				
			||||||
 | 
								{
 | 
				
			||||||
 | 
									Title: "папка",
 | 
				
			||||||
 | 
									Feeds: []Feed{
 | 
				
			||||||
 | 
										{
 | 
				
			||||||
 | 
											Title:   "пример2",
 | 
				
			||||||
 | 
											FeedUrl: "https://foo.com/feed.xml",
 | 
				
			||||||
 | 
											SiteUrl: "https://foo.com/",
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if !reflect.DeepEqual(want, have) {
 | 
				
			||||||
 | 
							t.Logf("want: %#v", want)
 | 
				
			||||||
 | 
							t.Logf("have: %#v", have)
 | 
				
			||||||
 | 
							t.Fatal("invalid opml")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								src/server/opml/sample_win1251.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/server/opml/sample_win1251.xml
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					<?xml version="1.0" encoding="windows-1251"?>
 | 
				
			||||||
 | 
					<opml version="1.1">
 | 
				
			||||||
 | 
					<head><title><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></title></head>
 | 
				
			||||||
 | 
					<body>
 | 
				
			||||||
 | 
					    <outline text="<22><><EFBFBD><EFBFBD><EFBFBD>">
 | 
				
			||||||
 | 
					        <outline type="rss" text="<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2" description="<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>2" xmlUrl="https://foo.com/feed.xml" htmlUrl="https://foo.com/"/>
 | 
				
			||||||
 | 
					    </outline>
 | 
				
			||||||
 | 
					    <outline type="rss" text="<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1" description="<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1" xmlUrl="https://baz.com/feed.xml" htmlUrl="https://baz.com/"/>
 | 
				
			||||||
 | 
					</body>
 | 
				
			||||||
 | 
					</opml>
 | 
				
			||||||
		Reference in New Issue
	
	Block a user