Пример #1
0
// sanitizeRemove traverses pre-order over the nodes,
// removing any element nodes that are not whitelisted
// and and removing any attributes that are not whitelisted
// from a given element node
func (w *Whitelist) sanitizeRemove(n *html.Node) error {
	return w.sanitizeNode(n, func(n *html.Node) bool {
		if !w.HasElement(n.Data) {
			if n.Parent != nil {
				nextSibling := n.NextSibling
				n.Parent.RemoveChild(n)

				// reset next sibling to support continuation
				// of linked-list style traversal of parent node's children
				n.NextSibling = nextSibling
			}
			return false
		}
		return true
	})
}
Пример #2
0
// sanitizeUnwrap traverses pre-order over the nodes, reattaching
// the whitelisted children of any element nodes that are not
// whitelisted to the parent of the unwhitelisted node
func (w *Whitelist) sanitizeUnwrap(n *html.Node) error {
	return w.sanitizeNode(n, func(n *html.Node) bool {
		if w.HasElement(n.Data) || n.Parent == nil {
			return true
		}

		insertBefore := n.NextSibling
		firstChild := n.FirstChild
		for c := n.FirstChild; c != nil; {
			nodeToUnwrap := c
			c = c.NextSibling

			n.RemoveChild(nodeToUnwrap)
			n.Parent.InsertBefore(nodeToUnwrap, insertBefore)
		}
		n.Parent.RemoveChild(n)

		// reset next sibling to support continuation
		// of linked-list style traversal of parent node's children
		n.NextSibling = firstChild
		return false
	})
}