Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

html: add PrependChild and InsertAfter #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 49 additions & 1 deletion html/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,37 @@ func (n *Node) InsertBefore(newChild, oldChild *Node) {
newChild.NextSibling = next
}

// AppendChild adds a node c as a child of n.
// InsertAfter inserts newChild as a child of n, immediately after oldChild
// in the sequence of n's children. oldChild may be nil, in which case newChild
// is prepended to the beginning of n's children.
//
// It will panic if newChild already has a parent or siblings.
func (n *Node) InsertAfter(newChild, oldChild *Node) {
if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
panic("html: InsertAfter called for an attached child Node")
}
var prev, next *Node
if oldChild != nil {
prev, next = oldChild, oldChild.NextSibling
} else {
next = n.FirstChild
}
if next != nil {
next.PrevSibling = newChild
} else {
n.LastChild = newChild
}
if prev != nil {
prev.NextSibling = newChild
} else {
n.FirstChild = newChild
}
newChild.Parent = n
newChild.PrevSibling = prev
newChild.NextSibling = next
}

// AppendChild adds a node c as the last child of n.
//
// It will panic if c already has a parent or siblings.
func (n *Node) AppendChild(c *Node) {
Expand All @@ -99,6 +129,24 @@ func (n *Node) AppendChild(c *Node) {
c.PrevSibling = last
}

// PrependChild adds a node c as the first child of n.
//
// It will panic if c already has a parent or siblings.
func (n *Node) PrependChild(c *Node) {
if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
panic("html: PrependChild called for an attached child Node")
}
first := n.FirstChild
if first != nil {
first.PrevSibling = c
} else {
n.LastChild = c
}
n.FirstChild = c
c.Parent = n
c.NextSibling = first
}

// RemoveChild removes a node c that is a child of n. Afterwards, c will have
// no parent and no siblings.
//
Expand Down