Skip to content

Commit ce135a7

Browse files
committed
#105 add new CharsetReader option that supports UTF8, UTF16 and more decoding
1 parent 299e4b3 commit ce135a7

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

options.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package xmlquery
22

33
import (
44
"encoding/xml"
5+
"io"
56
)
67

7-
type ParserOptions struct{
8+
type ParserOptions struct {
89
Decoder *DecoderOptions
910
}
1011

@@ -17,14 +18,16 @@ func (options ParserOptions) apply(parser *parser) {
1718
// DecoderOptions implement the very same options than the standard
1819
// encoding/xml package. Please refer to this documentation:
1920
// https://golang.org/pkg/encoding/xml/#Decoder
20-
type DecoderOptions struct{
21-
Strict bool
22-
AutoClose []string
23-
Entity map[string]string
21+
type DecoderOptions struct {
22+
Strict bool
23+
AutoClose []string
24+
Entity map[string]string
25+
CharsetReader func(charset string, input io.Reader) (io.Reader, error)
2426
}
2527

2628
func (options DecoderOptions) apply(decoder *xml.Decoder) {
2729
decoder.Strict = options.Strict
2830
decoder.AutoClose = options.AutoClose
2931
decoder.Entity = options.Entity
32+
decoder.CharsetReader = options.CharsetReader
3033
}

parse.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ func createParser(r io.Reader) *parser {
6969
level: 0,
7070
reader: reader,
7171
}
72-
p.decoder.CharsetReader = charset.NewReaderLabel
72+
if p.decoder.CharsetReader == nil {
73+
p.decoder.CharsetReader = charset.NewReaderLabel
74+
}
7375
p.prev = p.doc
7476
return p
7577
}

0 commit comments

Comments
 (0)