Skip to content

Commit

Permalink
Add content type detection when none given
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Wiegman committed Oct 10, 2016
1 parent 310b1f6 commit 6d3d0a6
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions microdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,19 @@ func walkNodes(n *html.Node, f func(*html.Node)) {
// ParseHTML parses the HTML document available in the given reader and returns
// the microdata. The given url is used to resolve the URLs in the
// attributes. The given contentType is used convert the content of r to UTF-8.
// When the given contentType is equal to "", the content type will be detected
// using `http.DetectContentType`.
func ParseHTML(r io.Reader, contentType string, u *url.URL) (*Microdata, error) {
if contentType == "" {
b := make([]byte, 512)
_, err := r.Read(b)
if err != nil {
return nil, err
}
contentType = http.DetectContentType(b)
r = io.MultiReader(bytes.NewReader(b), r)
}

p, err := newParser(r, contentType, u)
if err != nil {
return nil, err
Expand Down

0 comments on commit 6d3d0a6

Please sign in to comment.