go - How to compare HTML markup in Golang? -
i'm trying come test suite checks html fragments/files canonically equivalent 1 another. surprised see if parse same string or file, https://godoc.org/golang.org/x/net/html#node comparing different. missing?
hopefully demonstrates issue:
package main import ( "fmt" "strings" "golang.org/x/net/html" ) func main() { s := `<h1> test </h1><p>foo</p>` // s2 := `<h1>test</h1><p>foo</p>` doc, _ := html.parse(strings.newreader(s)) doc2, _ := html.parse(strings.newreader(s)) if doc == doc2 { fmt.println("html same") // expected } else { fmt.println("html not same") // got } }
html not same
the simplest way use reflection, since html.parse
returns *node
object. comparing 2 of objects in go need reflect.deepequal
.
if reflect.deepequal(doc, doc2) { fmt.println("html same") } else { fmt.println("html not same") }
this prints out "html same".
Comments
Post a Comment