func doHtmlAssert()

in asserts/asserts.go [131:157]


func doHtmlAssert(value string, rule specs.HtmlAssert) string {
	// Use net/html to parse first because it can handle some malformed HTML.
	root, err := html.Parse(strings.NewReader(value))
	if err != nil {
		return "Failed to parse HTML content"
	}

	// Reconstruct well-formed XML for xmlpath library.
	var b bytes.Buffer
	html.Render(&b, root)
	fixedHtml := b.String()
	xmlRoot, err := xmlpath.ParseHTML(strings.NewReader(fixedHtml))
	if err != nil {
		return "Failed to fix and parse HTML to XML"
	}

	if rule.Title != nil {
		title, ok := xmlpath.MustCompile("/html/head/title").String(xmlRoot)
		if !ok {
			return "HTML document contains no title"
		}
		if msg := doStringAssert(title, *rule.Title); msg != "" {
			return MessageWithContext(msg, "Title")
		}
	}
	return ""
}