1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 // A NodeType is the type of a Node.
15 ErrorNode NodeType = iota
24 // Section 12.2.3.3 says "scope markers are inserted when entering applet
25 // elements, buttons, object elements, marquees, table cells, and table
26 // captions, and are used to prevent formatting from 'leaking'".
27 var scopeMarker = Node{Type: scopeMarkerNode}
29 // A Node consists of a NodeType and some Data (tag name for element nodes,
30 // content for text) and are part of a tree of Nodes. Element nodes may also
31 // have a Namespace and contain a slice of Attributes. Data is unescaped, so
32 // that it looks like "a<b" rather than "a<b". For element nodes, DataAtom
33 // is the atom for Data, or zero if Data is not a known tag name.
35 // An empty Namespace implies a "http://www.w3.org/1999/xhtml" namespace.
36 // Similarly, "math" is short for "http://www.w3.org/1998/Math/MathML", and
37 // "svg" is short for "http://www.w3.org/2000/svg".
39 Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
48 // InsertBefore inserts newChild as a child of n, immediately before oldChild
49 // in the sequence of n's children. oldChild may be nil, in which case newChild
50 // is appended to the end of n's children.
52 // It will panic if newChild already has a parent or siblings.
53 func (n *Node) InsertBefore(newChild, oldChild *Node) {
54 if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
55 panic("html: InsertBefore called for an attached child Node")
59 prev, next = oldChild.PrevSibling, oldChild
64 prev.NextSibling = newChild
66 n.FirstChild = newChild
69 next.PrevSibling = newChild
71 n.LastChild = newChild
74 newChild.PrevSibling = prev
75 newChild.NextSibling = next
78 // AppendChild adds a node c as a child of n.
80 // It will panic if c already has a parent or siblings.
81 func (n *Node) AppendChild(c *Node) {
82 if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
83 panic("html: AppendChild called for an attached child Node")
96 // RemoveChild removes a node c that is a child of n. Afterwards, c will have
97 // no parent and no siblings.
99 // It will panic if c's parent is not n.
100 func (n *Node) RemoveChild(c *Node) {
102 panic("html: RemoveChild called for a non-child Node")
104 if n.FirstChild == c {
105 n.FirstChild = c.NextSibling
107 if c.NextSibling != nil {
108 c.NextSibling.PrevSibling = c.PrevSibling
110 if n.LastChild == c {
111 n.LastChild = c.PrevSibling
113 if c.PrevSibling != nil {
114 c.PrevSibling.NextSibling = c.NextSibling
121 // reparentChildren reparents all of src's child nodes to dst.
122 func reparentChildren(dst, src *Node) {
124 child := src.FirstChild
128 src.RemoveChild(child)
129 dst.AppendChild(child)
133 // clone returns a new node with the same type, data and attributes.
134 // The clone has no parent, no siblings and no children.
135 func (n *Node) clone() *Node {
138 DataAtom: n.DataAtom,
140 Attr: make([]Attribute, len(n.Attr)),
146 // nodeStack is a stack of nodes.
147 type nodeStack []*Node
149 // pop pops the stack. It will panic if s is empty.
150 func (s *nodeStack) pop() *Node {
157 // top returns the most recently pushed node, or nil if s is empty.
158 func (s *nodeStack) top() *Node {
159 if i := len(*s); i > 0 {
165 // index returns the index of the top-most occurrence of n in the stack, or -1
166 // if n is not present.
167 func (s *nodeStack) index(n *Node) int {
168 for i := len(*s) - 1; i >= 0; i-- {
176 // insert inserts a node at the given index.
177 func (s *nodeStack) insert(i int, n *Node) {
178 (*s) = append(*s, nil)
179 copy((*s)[i+1:], (*s)[i:])
183 // remove removes a node from the stack. It is a no-op if n is not present.
184 func (s *nodeStack) remove(n *Node) {
189 copy((*s)[i:], (*s)[i+1:])