node.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.
  4. package html
  5. import (
  6. "golang.org/x/net/html/atom"
  7. )
  8. // A NodeType is the type of a Node.
  9. type NodeType uint32
  10. const (
  11. ErrorNode NodeType = iota
  12. TextNode
  13. DocumentNode
  14. ElementNode
  15. CommentNode
  16. DoctypeNode
  17. // RawNode nodes are not returned by the parser, but can be part of the
  18. // Node tree passed to func Render to insert raw HTML (without escaping).
  19. // If so, this package makes no guarantee that the rendered HTML is secure
  20. // (from e.g. Cross Site Scripting attacks) or well-formed.
  21. RawNode
  22. scopeMarkerNode
  23. )
  24. // Section 12.2.4.3 says "The markers are inserted when entering applet,
  25. // object, marquee, template, td, th, and caption elements, and are used
  26. // to prevent formatting from "leaking" into applet, object, marquee,
  27. // template, td, th, and caption elements".
  28. 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&lt;b". For element nodes, DataAtom
  33. // is the atom for Data, or zero if Data is not a known tag name.
  34. //
  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".
  38. type Node struct {
  39. Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
  40. Type NodeType
  41. DataAtom atom.Atom
  42. Data string
  43. Namespace string
  44. Attr []Attribute
  45. }
  46. // InsertBefore inserts newChild as a child of n, immediately before oldChild
  47. // in the sequence of n's children. oldChild may be nil, in which case newChild
  48. // is appended to the end of n's children.
  49. //
  50. // It will panic if newChild already has a parent or siblings.
  51. func (n *Node) InsertBefore(newChild, oldChild *Node) {
  52. if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
  53. panic("html: InsertBefore called for an attached child Node")
  54. }
  55. var prev, next *Node
  56. if oldChild != nil {
  57. prev, next = oldChild.PrevSibling, oldChild
  58. } else {
  59. prev = n.LastChild
  60. }
  61. if prev != nil {
  62. prev.NextSibling = newChild
  63. } else {
  64. n.FirstChild = newChild
  65. }
  66. if next != nil {
  67. next.PrevSibling = newChild
  68. } else {
  69. n.LastChild = newChild
  70. }
  71. newChild.Parent = n
  72. newChild.PrevSibling = prev
  73. newChild.NextSibling = next
  74. }
  75. // AppendChild adds a node c as a child of n.
  76. //
  77. // It will panic if c already has a parent or siblings.
  78. func (n *Node) AppendChild(c *Node) {
  79. if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
  80. panic("html: AppendChild called for an attached child Node")
  81. }
  82. last := n.LastChild
  83. if last != nil {
  84. last.NextSibling = c
  85. } else {
  86. n.FirstChild = c
  87. }
  88. n.LastChild = c
  89. c.Parent = n
  90. c.PrevSibling = last
  91. }
  92. // RemoveChild removes a node c that is a child of n. Afterwards, c will have
  93. // no parent and no siblings.
  94. //
  95. // It will panic if c's parent is not n.
  96. func (n *Node) RemoveChild(c *Node) {
  97. if c.Parent != n {
  98. panic("html: RemoveChild called for a non-child Node")
  99. }
  100. if n.FirstChild == c {
  101. n.FirstChild = c.NextSibling
  102. }
  103. if c.NextSibling != nil {
  104. c.NextSibling.PrevSibling = c.PrevSibling
  105. }
  106. if n.LastChild == c {
  107. n.LastChild = c.PrevSibling
  108. }
  109. if c.PrevSibling != nil {
  110. c.PrevSibling.NextSibling = c.NextSibling
  111. }
  112. c.Parent = nil
  113. c.PrevSibling = nil
  114. c.NextSibling = nil
  115. }
  116. // reparentChildren reparents all of src's child nodes to dst.
  117. func reparentChildren(dst, src *Node) {
  118. for {
  119. child := src.FirstChild
  120. if child == nil {
  121. break
  122. }
  123. src.RemoveChild(child)
  124. dst.AppendChild(child)
  125. }
  126. }
  127. // clone returns a new node with the same type, data and attributes.
  128. // The clone has no parent, no siblings and no children.
  129. func (n *Node) clone() *Node {
  130. m := &Node{
  131. Type: n.Type,
  132. DataAtom: n.DataAtom,
  133. Data: n.Data,
  134. Attr: make([]Attribute, len(n.Attr)),
  135. }
  136. copy(m.Attr, n.Attr)
  137. return m
  138. }
  139. // nodeStack is a stack of nodes.
  140. type nodeStack []*Node
  141. // pop pops the stack. It will panic if s is empty.
  142. func (s *nodeStack) pop() *Node {
  143. i := len(*s)
  144. n := (*s)[i-1]
  145. *s = (*s)[:i-1]
  146. return n
  147. }
  148. // top returns the most recently pushed node, or nil if s is empty.
  149. func (s *nodeStack) top() *Node {
  150. if i := len(*s); i > 0 {
  151. return (*s)[i-1]
  152. }
  153. return nil
  154. }
  155. // index returns the index of the top-most occurrence of n in the stack, or -1
  156. // if n is not present.
  157. func (s *nodeStack) index(n *Node) int {
  158. for i := len(*s) - 1; i >= 0; i-- {
  159. if (*s)[i] == n {
  160. return i
  161. }
  162. }
  163. return -1
  164. }
  165. // contains returns whether a is within s.
  166. func (s *nodeStack) contains(a atom.Atom) bool {
  167. for _, n := range *s {
  168. if n.DataAtom == a && n.Namespace == "" {
  169. return true
  170. }
  171. }
  172. return false
  173. }
  174. // insert inserts a node at the given index.
  175. func (s *nodeStack) insert(i int, n *Node) {
  176. (*s) = append(*s, nil)
  177. copy((*s)[i+1:], (*s)[i:])
  178. (*s)[i] = n
  179. }
  180. // remove removes a node from the stack. It is a no-op if n is not present.
  181. func (s *nodeStack) remove(n *Node) {
  182. i := s.index(n)
  183. if i == -1 {
  184. return
  185. }
  186. copy((*s)[i:], (*s)[i+1:])
  187. j := len(*s) - 1
  188. (*s)[j] = nil
  189. *s = (*s)[:j]
  190. }
  191. type insertionModeStack []insertionMode
  192. func (s *insertionModeStack) pop() (im insertionMode) {
  193. i := len(*s)
  194. im = (*s)[i-1]
  195. *s = (*s)[:i-1]
  196. return im
  197. }
  198. func (s *insertionModeStack) top() insertionMode {
  199. if i := len(*s); i > 0 {
  200. return (*s)[i-1]
  201. }
  202. return nil
  203. }