Remove HTML inline style, i.e., remove style attribute from HTML node via Go
net/html package.
Run Code on Go Playground
package main
import (
"golang.org/x/net/html"
"os"
"strings"
)
var testhtml = `
<table style="s1">
<tr><td id="foo" style="s2">R1, C1</td><td>R1, C2</td></tr>
<tr><td alt="hi">R2, C1</td><td style="s3">R2, C2</td></tr>
</table>`
func RemoveStyleAttr(n *html.Node) {
i := -1
for index, attr := range n.Attr {
if attr.Key == "style" {
i = index
break
}
}
if i != -1 {
n.Attr = append(n.Attr[:i], n.Attr[i+1:]...)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
RemoveStyleAttr(c)
}
}
func main() {
doc, err := html.Parse(strings.NewReader(testhtml))
if err != nil {
panic(err)
}
RemoveStyleAttr(doc)
html.Render(os.Stdout, doc)
}
Tested on: Go Playground
References: