From 6d3d0a6a4afe79fa4553fbd16bc5b2d7219f6ed8 Mon Sep 17 00:00:00 2001 From: Lars Wiegman Date: Mon, 10 Oct 2016 14:50:47 +0200 Subject: [PATCH] Add content type detection when none given --- microdata.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/microdata.go b/microdata.go index 73c0fe5..e2474c3 100644 --- a/microdata.go +++ b/microdata.go @@ -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