Example #1
0
// Replace the given node's children with the given string.
func setNodeText(node *html.Node, s string) {
	// remove all existing children
	for node.FirstChild != nil {
		node.RemoveChild(node.FirstChild)
	}
	// add the text
	node.AppendChild(&html.Node{
		Type: html.TextNode,
		Data: s,
	})
}
Example #2
0
func RemoveAttr(n *html.Node, key string) {
	for i, a := range n.Attr {
		if a.Key == key {
			copy(n.Attr[i:], n.Attr[i+1:])
			n.Attr = n.Attr[:len(n.Attr)-1]
			return
		}
	}
}
Example #3
0
// removes an attribute from a node
func removeAttr(node *html.Node, attrName string) {
	for i := 0; i < len(node.Attr); i++ {
		if node.Attr[i].Key == attrName {
			last := len(node.Attr) - 1
			node.Attr[i] = node.Attr[last] // overwrite the target with the last attribute
			node.Attr = node.Attr[:last]   // then slice off the last attribute
			i--
		}
	}
}
Example #4
0
func InsertBefore(n *html.Node, nodes ...*html.Node) {
	i := childIndex(n)
	p := n.Parent
	c := make([]*html.Node, len(p.Child)+len(nodes))
	copy(c, p.Child[:i])
	copy(c[i:], nodes)
	copy(c[i+len(nodes):], p.Child[i:])
	for _, n := range nodes {
		n.Parent.Remove(n)
		n.Parent = p
	}
	p.Child = c
}