3
[Golang] Remove HTML Inline Style
source link: http://siongui.github.io/2018/01/16/go-remove-html-inline-style/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
[Golang] Remove HTML Inline Style
January 16, 2018
Remove HTML inline style, i.e., remove style attribute from HTML node via Go net/html package.
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) }
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK