示例#1
0
文件: mutate.go 项目: dunmatt/goquery
// 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,
	})
}
示例#2
0
文件: htmlutil.go 项目: nf/htmlclean
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
		}
	}
}
示例#3
0
文件: mutate.go 项目: dunmatt/goquery
// 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--
		}
	}
}
示例#4
0
文件: htmlutil.go 项目: nf/htmlclean
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
}