node.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package domquery
  2. import (
  3. "strings"
  4. )
  5. type Node struct {
  6. token string
  7. Children []*Node
  8. Parent *Node
  9. }
  10. func (node *Node) TagName() string {
  11. return getTagName(node.token)
  12. }
  13. func (node *Node) OuterHTML() string {
  14. str := node.token
  15. str += node.InnerHTML()
  16. str += getCloseNode(node).token
  17. return str
  18. }
  19. func (node *Node) SetOuterHTML(str string) {
  20. closeTag := getCloseNode(node)
  21. closeTag.token = ""
  22. //use the token to insert str
  23. node.token = str
  24. node.Children = []*Node{}
  25. // rebuild parent
  26. tokList := GetTokenList(node.Parent.InnerHTML())
  27. node.Parent.Children = []*Node{}
  28. node.Parent = BuildTree(tokList, node.Parent)
  29. }
  30. func (node *Node) InnerHTML() string {
  31. str := ""
  32. for _, child := range node.Children {
  33. str += child.token
  34. str += child.InnerHTML()
  35. }
  36. return str
  37. }
  38. func (node *Node) SetInnerHTML(str string) {
  39. node.Children = []*Node{}
  40. tokList := GetTokenList(str)
  41. BuildTree(tokList, node)
  42. }
  43. func (node *Node) QuerySelector(str string) *Node {
  44. found := &Node{}
  45. for _, child := range node.Children {
  46. if matchSelector(child, str) {
  47. return child
  48. }
  49. found := child.QuerySelector(str)
  50. if found.token != "" {
  51. return found
  52. }
  53. }
  54. return found
  55. }
  56. func (node *Node) QuerySelectorAll(str string) []*Node {
  57. found := []*Node{}
  58. for _, child := range node.Children {
  59. if getTagName(child.token) == str && (getTokType(child.token) == "open" || getTokType(child.token) == "selfclosing") {
  60. found = append(found, child)
  61. }
  62. if str == "*" && (getTokType(child.token) == "open" || getTokType(child.token) == "selfclosing") {
  63. found = append(found, child)
  64. }
  65. found = append(found, child.QuerySelectorAll(str)...)
  66. }
  67. return found
  68. }
  69. func (node *Node) GetAttribute(str string) string {
  70. for key, val := range node.AttributeList() {
  71. if key == str {
  72. return val
  73. }
  74. }
  75. return ""
  76. }
  77. func (node *Node) HasAttribute(str string) bool {
  78. for key := range node.AttributeList() {
  79. if key == str {
  80. return true
  81. }
  82. }
  83. return false
  84. }
  85. func (node *Node) SetAttribute(attr string, val string) {
  86. attributes := node.AttributeList()
  87. attributes[attr] = val
  88. attrStr := ""
  89. for key, val := range attributes {
  90. attrStr += key
  91. if val != "" {
  92. attrStr += "=" + "\"" + val + "\""
  93. }
  94. attrStr += " "
  95. }
  96. attrStr = strings.Trim(attrStr, " ")
  97. node.token = "<" + getTagName(node.token) + " " + attrStr + ">"
  98. }
  99. func (node *Node) RemoveAttribute(attr string) {
  100. attributes := node.AttributeList()
  101. attrStr := ""
  102. for key, val := range attributes {
  103. if key == attr {
  104. continue
  105. }
  106. attrStr += key
  107. if val != "" {
  108. attrStr += "=" + "\"" + val + "\""
  109. }
  110. attrStr += " "
  111. }
  112. attrStr = strings.Trim(attrStr, " ")
  113. node.token = "<" + getTagName(node.token) + " " + attrStr + ">"
  114. }
  115. func (node *Node) AttributeList() map[string]string {
  116. attributesList := make(map[string]string)
  117. if getTokType(node.token) != "open" && getTokType(node.token) != "selfclosing" {
  118. return attributesList
  119. }
  120. attrStr := strings.Replace(node.token, "<"+getTagName(node.token), "", 1)
  121. attrStr = strings.Trim(attrStr, "<> ")
  122. tok := ""
  123. state := "key"
  124. strKey := ""
  125. for i := 0; i < len(attrStr); i++ {
  126. chr := string(attrStr[i])
  127. if state == "key" {
  128. if i == len(attrStr)-1 {
  129. tok += chr
  130. strKey = strings.Trim(tok, " ")
  131. attributesList[strKey] = ""
  132. continue
  133. }
  134. if chr == "=" {
  135. strKey = strings.Trim(tok, " ")
  136. state = "qual"
  137. tok = ""
  138. continue
  139. }
  140. if chr == " " {
  141. strKey = strings.Trim(tok, " ")
  142. if strKey != "" {
  143. attributesList[strKey] = ""
  144. }
  145. strKey = ""
  146. tok = ""
  147. continue
  148. }
  149. }
  150. if state == "qual" {
  151. if chr == "\"" {
  152. state = "val"
  153. tok = ""
  154. continue
  155. }
  156. }
  157. if state == "val" {
  158. if chr == "\"" {
  159. attributesList[strKey] = strings.Trim(tok, " ")
  160. state = "key"
  161. tok = ""
  162. continue
  163. }
  164. }
  165. tok += chr
  166. }
  167. return attributesList
  168. }
  169. func (node *Node) ClassList() []string {
  170. classStr := node.GetAttribute("class")
  171. classList := strings.Split(classStr, " ")
  172. return classList
  173. }
  174. func (node *Node) ClassListAdd(classStr string) {
  175. classList := append(node.ClassList(), classStr)
  176. str := strings.Join(classList, " ")
  177. str = strings.Trim(str, " ")
  178. node.SetAttribute("class", str)
  179. }
  180. func (node *Node) ClassListRemove(classStr string) {
  181. classList := []string{}
  182. for _, class := range node.ClassList() {
  183. if class != classStr {
  184. classList = append(classList, class)
  185. }
  186. }
  187. str := strings.Join(classList, " ")
  188. str = strings.Trim(str, " ")
  189. node.SetAttribute("class", str)
  190. }
  191. func (node *Node) Remove() {
  192. closeNode := getCloseNode(node)
  193. i := 0
  194. for _, n := range node.Parent.Children {
  195. if n == closeNode {
  196. node.Parent.Children = append(node.Parent.Children[:i], node.Parent.Children[i+1:]...)
  197. break
  198. }
  199. i++
  200. }
  201. i = 0
  202. for _, n := range node.Parent.Children {
  203. if n == node {
  204. node.Parent.Children = append(node.Parent.Children[:i], node.Parent.Children[i+1:]...)
  205. break
  206. }
  207. i++
  208. }
  209. }
  210. func (node *Node) CreateElement(tagName string) *Node {
  211. newNode := &Node{}
  212. newNode.token = "<" + tagName + ">"
  213. return newNode
  214. }
  215. func (node *Node) Append(newNode *Node) {
  216. newNode.Parent = node
  217. node.Children = append(node.Children, newNode)
  218. if getTokType(newNode.token) != "selfclosing" {
  219. closeNode := &Node{}
  220. closeNode.token = "</" + getTagName(newNode.token) + ">"
  221. node.Children = append(node.Children, closeNode)
  222. }
  223. }