// RemoveChildren removes all of the child nodes of the node given func RemoveChildren(node dom.Node) dom.Node { n := node.Underlying() for n.Call("hasChildNodes").Bool() { n.Call("removeChild", n.Get("lastChild")) } return node }
// SetInnerText removes all child nodes from the given node and sets a single // Text Node with the given string func SetInnerText(node dom.Node, text string) dom.Node { n := node.Underlying() for n.Call("hasChildNodes").Bool() { n.Call("removeChild", n.Get("lastChild")) } n.Call("appendChild", js.Global.Get("document").Call("createTextNode", text)) return node }
// RootElement returns the root parent element that for the provided element. func RootElement(elem dom.Node) dom.Node { var parent dom.Node parent = elem for parent.ParentNode() != nil { parent = parent.ParentNode() } return parent }
// SetPreText does similar to SetInnerText, but linebreaks are converted to <br />s func SetPreText(node dom.Node, text string) dom.Node { n := node.Underlying() for n.Call("hasChildNodes").Bool() { n.Call("removeChild", n.Get("lastChild")) } for i, part := range strings.Split(text, "\n") { if i > 0 { n.Call("appendChild", js.Global.Get("document").Call("createElement", "br")) } n.Call("appendChild", js.Global.Get("document").Call("createTextNode", part)) } return node }
// GetShadowRoot retrieves the shadowRoot connected to the pass dom.Node, else // returns false as the second argument if the node has no shadowRoot. func GetShadowRoot(elem dom.Node) (dom.DocumentFragment, bool) { if elem == nil { return nil, false } var root *js.Object if root = elem.Underlying().Get("shadowRoot"); root == nil { if root = elem.Underlying().Get("root"); root == nil { return nil, false } } return dom.WrapDocumentFragment(root), true }
// Observe registers a DOM Node to receive events for func (m *Observer) Observe(n dom.Node, i ObserverInit) { j := js.Global.Get("Object").Get("constructor").New() j.Set("childList", i.ChildList) j.Set("attributes", i.Attributes) j.Set("characterData", i.CharacterData) j.Set("subtree", i.Subtree) j.Set("attributeOldValue", i.AttributeOldValue) j.Set("characterDataOldValue", i.CharacterDataOldValue) if i.Attributes { a := js.Get("Array").New(len(i.AttributeFilter)) for i, f := range i.AttributeFilter { a.SetIndex(i, f) } j.Set("attributeFilter", a) } m.Call("observe", n.Underlying(), j) }
// AppendChildren appends all the given children to the parent. func AppendChildren(parent dom.Node, children ...dom.Node) dom.Node { for _, child := range children { parent.AppendChild(child) } return parent }