node.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. // Node trees may be navigated using the link fields (Parent,
  36. // FirstChild, and so on) or a range loop over iterators such as
  37. // [Node.Descendants].
  38. //
  39. // An empty Namespace implies a "http://www.w3.org/1999/xhtml" namespace.
  40. // Similarly, "math" is short for "http://www.w3.org/1998/Math/MathML", and
  41. // "svg" is short for "http://www.w3.org/2000/svg".
  42. type Node struct {
  43. Parent, FirstChild, LastChild, PrevSibling, NextSibling *Node
  44. Type NodeType
  45. DataAtom atom.Atom
  46. Data string
  47. Namespace string
  48. Attr []Attribute
  49. }
  50. // InsertBefore inserts newChild as a child of n, immediately before oldChild
  51. // in the sequence of n's children. oldChild may be nil, in which case newChild
  52. // is appended to the end of n's children.
  53. //
  54. // It will panic if newChild already has a parent or siblings.
  55. func (n *Node) InsertBefore(newChild, oldChild *Node) {
  56. if newChild.Parent != nil || newChild.PrevSibling != nil || newChild.NextSibling != nil {
  57. panic("html: InsertBefore called for an attached child Node")
  58. }
  59. var prev, next *Node
  60. if oldChild != nil {
  61. prev, next = oldChild.PrevSibling, oldChild
  62. } else {
  63. prev = n.LastChild
  64. }
  65. if prev != nil {
  66. prev.NextSibling = newChild
  67. } else {
  68. n.FirstChild = newChild
  69. }
  70. if next != nil {
  71. next.PrevSibling = newChild
  72. } else {
  73. n.LastChild = newChild
  74. }
  75. newChild.Parent = n
  76. newChild.PrevSibling = prev
  77. newChild.NextSibling = next
  78. }
  79. // AppendChild adds a node c as a child of n.
  80. //
  81. // It will panic if c already has a parent or siblings.
  82. func (n *Node) AppendChild(c *Node) {
  83. if c.Parent != nil || c.PrevSibling != nil || c.NextSibling != nil {
  84. panic("html: AppendChild called for an attached child Node")
  85. }
  86. last := n.LastChild
  87. if last != nil {
  88. last.NextSibling = c
  89. } else {
  90. n.FirstChild = c
  91. }
  92. n.LastChild = c
  93. c.Parent = n
  94. c.PrevSibling = last
  95. }
  96. // RemoveChild removes a node c that is a child of n. Afterwards, c will have
  97. // no parent and no siblings.
  98. //
  99. // It will panic if c's parent is not n.
  100. func (n *Node) RemoveChild(c *Node) {
  101. if c.Parent != n {
  102. panic("html: RemoveChild called for a non-child Node")
  103. }
  104. if n.FirstChild == c {
  105. n.FirstChild = c.NextSibling
  106. }
  107. if c.NextSibling != nil {
  108. c.NextSibling.PrevSibling = c.PrevSibling
  109. }
  110. if n.LastChild == c {
  111. n.LastChild = c.PrevSibling
  112. }
  113. if c.PrevSibling != nil {
  114. c.PrevSibling.NextSibling = c.NextSibling
  115. }
  116. c.Parent = nil
  117. c.PrevSibling = nil
  118. c.NextSibling = nil
  119. }
  120. // reparentChildren reparents all of src's child nodes to dst.
  121. func reparentChildren(dst, src *Node) {
  122. for {
  123. child := src.FirstChild
  124. if child == nil {
  125. break
  126. }
  127. src.RemoveChild(child)
  128. dst.AppendChild(child)
  129. }
  130. }
  131. // clone returns a new node with the same type, data and attributes.
  132. // The clone has no parent, no siblings and no children.
  133. func (n *Node) clone() *Node {
  134. m := &Node{
  135. Type: n.Type,
  136. DataAtom: n.DataAtom,
  137. Data: n.Data,
  138. Attr: make([]Attribute, len(n.Attr)),
  139. }
  140. copy(m.Attr, n.Attr)
  141. return m
  142. }
  143. // nodeStack is a stack of nodes.
  144. type nodeStack []*Node
  145. // pop pops the stack. It will panic if s is empty.
  146. func (s *nodeStack) pop() *Node {
  147. i := len(*s)
  148. n := (*s)[i-1]
  149. *s = (*s)[:i-1]
  150. return n
  151. }
  152. // top returns the most recently pushed node, or nil if s is empty.
  153. func (s *nodeStack) top() *Node {
  154. if i := len(*s); i > 0 {
  155. return (*s)[i-1]
  156. }
  157. return nil
  158. }
  159. // index returns the index of the top-most occurrence of n in the stack, or -1
  160. // if n is not present.
  161. func (s *nodeStack) index(n *Node) int {
  162. for i := len(*s) - 1; i >= 0; i-- {
  163. if (*s)[i] == n {
  164. return i
  165. }
  166. }
  167. return -1
  168. }
  169. // contains returns whether a is within s.
  170. func (s *nodeStack) contains(a atom.Atom) bool {
  171. for _, n := range *s {
  172. if n.DataAtom == a && n.Namespace == "" {
  173. return true
  174. }
  175. }
  176. return false
  177. }
  178. // insert inserts a node at the given index.
  179. func (s *nodeStack) insert(i int, n *Node) {
  180. (*s) = append(*s, nil)
  181. copy((*s)[i+1:], (*s)[i:])
  182. (*s)[i] = n
  183. }
  184. // remove removes a node from the stack. It is a no-op if n is not present.
  185. func (s *nodeStack) remove(n *Node) {
  186. i := s.index(n)
  187. if i == -1 {
  188. return
  189. }
  190. copy((*s)[i:], (*s)[i+1:])
  191. j := len(*s) - 1
  192. (*s)[j] = nil
  193. *s = (*s)[:j]
  194. }
  195. type insertionModeStack []insertionMode
  196. func (s *insertionModeStack) pop() (im insertionMode) {
  197. i := len(*s)
  198. im = (*s)[i-1]
  199. *s = (*s)[:i-1]
  200. return im
  201. }
  202. func (s *insertionModeStack) top() insertionMode {
  203. if i := len(*s); i > 0 {
  204. return (*s)[i-1]
  205. }
  206. return nil
  207. }