You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

453 lines
11 KiB

  1. package goquery
  2. import (
  3. "testing"
  4. )
  5. const (
  6. wrapHtml = "<div id=\"ins\">test string<div><p><em><b></b></em></p></div></div>"
  7. )
  8. func TestAfter(t *testing.T) {
  9. doc := Doc2Clone()
  10. doc.Find("#main").After("#nf6")
  11. assertLength(t, doc.Find("#main #nf6").Nodes, 0)
  12. assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
  13. assertLength(t, doc.Find("#main + #nf6").Nodes, 1)
  14. printSel(t, doc.Selection)
  15. }
  16. func TestAfterMany(t *testing.T) {
  17. doc := Doc2Clone()
  18. doc.Find(".one").After("#nf6")
  19. assertLength(t, doc.Find("#foot #nf6").Nodes, 1)
  20. assertLength(t, doc.Find("#main #nf6").Nodes, 1)
  21. assertLength(t, doc.Find(".one + #nf6").Nodes, 2)
  22. printSel(t, doc.Selection)
  23. }
  24. func TestAfterWithRemoved(t *testing.T) {
  25. doc := Doc2Clone()
  26. s := doc.Find("#main").Remove()
  27. s.After("#nf6")
  28. assertLength(t, s.Find("#nf6").Nodes, 0)
  29. assertLength(t, doc.Find("#nf6").Nodes, 0)
  30. printSel(t, doc.Selection)
  31. }
  32. func TestAfterSelection(t *testing.T) {
  33. doc := Doc2Clone()
  34. doc.Find("#main").AfterSelection(doc.Find("#nf1, #nf2"))
  35. assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)
  36. assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)
  37. assertLength(t, doc.Find("#main + #nf1, #nf1 + #nf2").Nodes, 2)
  38. printSel(t, doc.Selection)
  39. }
  40. func TestAfterHtml(t *testing.T) {
  41. doc := Doc2Clone()
  42. doc.Find("#main").AfterHtml("<strong>new node</strong>")
  43. assertLength(t, doc.Find("#main + strong").Nodes, 1)
  44. printSel(t, doc.Selection)
  45. }
  46. func TestAppend(t *testing.T) {
  47. doc := Doc2Clone()
  48. doc.Find("#main").Append("#nf6")
  49. assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
  50. assertLength(t, doc.Find("#main #nf6").Nodes, 1)
  51. printSel(t, doc.Selection)
  52. }
  53. func TestAppendBody(t *testing.T) {
  54. doc := Doc2Clone()
  55. doc.Find("body").Append("#nf6")
  56. assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
  57. assertLength(t, doc.Find("#main #nf6").Nodes, 0)
  58. assertLength(t, doc.Find("body > #nf6").Nodes, 1)
  59. printSel(t, doc.Selection)
  60. }
  61. func TestAppendSelection(t *testing.T) {
  62. doc := Doc2Clone()
  63. doc.Find("#main").AppendSelection(doc.Find("#nf1, #nf2"))
  64. assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
  65. assertLength(t, doc.Find("#foot #nf2").Nodes, 0)
  66. assertLength(t, doc.Find("#main #nf1").Nodes, 1)
  67. assertLength(t, doc.Find("#main #nf2").Nodes, 1)
  68. printSel(t, doc.Selection)
  69. }
  70. func TestAppendSelectionExisting(t *testing.T) {
  71. doc := Doc2Clone()
  72. doc.Find("#main").AppendSelection(doc.Find("#n1, #n2"))
  73. assertClass(t, doc.Find("#main :nth-child(1)"), "three")
  74. assertClass(t, doc.Find("#main :nth-child(5)"), "one")
  75. assertClass(t, doc.Find("#main :nth-child(6)"), "two")
  76. printSel(t, doc.Selection)
  77. }
  78. func TestAppendClone(t *testing.T) {
  79. doc := Doc2Clone()
  80. doc.Find("#n1").AppendSelection(doc.Find("#nf1").Clone())
  81. assertLength(t, doc.Find("#foot #nf1").Nodes, 1)
  82. assertLength(t, doc.Find("#main #nf1").Nodes, 1)
  83. printSel(t, doc.Selection)
  84. }
  85. func TestAppendHtml(t *testing.T) {
  86. doc := Doc2Clone()
  87. doc.Find("div").AppendHtml("<strong>new node</strong>")
  88. assertLength(t, doc.Find("strong").Nodes, 14)
  89. printSel(t, doc.Selection)
  90. }
  91. func TestBefore(t *testing.T) {
  92. doc := Doc2Clone()
  93. doc.Find("#main").Before("#nf6")
  94. assertLength(t, doc.Find("#main #nf6").Nodes, 0)
  95. assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
  96. assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)
  97. printSel(t, doc.Selection)
  98. }
  99. func TestBeforeWithRemoved(t *testing.T) {
  100. doc := Doc2Clone()
  101. s := doc.Find("#main").Remove()
  102. s.Before("#nf6")
  103. assertLength(t, s.Find("#nf6").Nodes, 0)
  104. assertLength(t, doc.Find("#nf6").Nodes, 0)
  105. printSel(t, doc.Selection)
  106. }
  107. func TestBeforeSelection(t *testing.T) {
  108. doc := Doc2Clone()
  109. doc.Find("#main").BeforeSelection(doc.Find("#nf1, #nf2"))
  110. assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)
  111. assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)
  112. assertLength(t, doc.Find("body > #nf1:first-child, #nf1 + #nf2").Nodes, 2)
  113. printSel(t, doc.Selection)
  114. }
  115. func TestBeforeHtml(t *testing.T) {
  116. doc := Doc2Clone()
  117. doc.Find("#main").BeforeHtml("<strong>new node</strong>")
  118. assertLength(t, doc.Find("body > strong:first-child").Nodes, 1)
  119. printSel(t, doc.Selection)
  120. }
  121. func TestEmpty(t *testing.T) {
  122. doc := Doc2Clone()
  123. s := doc.Find("#main").Empty()
  124. assertLength(t, doc.Find("#main").Children().Nodes, 0)
  125. assertLength(t, s.Filter("div").Nodes, 6)
  126. printSel(t, doc.Selection)
  127. }
  128. func TestPrepend(t *testing.T) {
  129. doc := Doc2Clone()
  130. doc.Find("#main").Prepend("#nf6")
  131. assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
  132. assertLength(t, doc.Find("#main #nf6:first-child").Nodes, 1)
  133. printSel(t, doc.Selection)
  134. }
  135. func TestPrependBody(t *testing.T) {
  136. doc := Doc2Clone()
  137. doc.Find("body").Prepend("#nf6")
  138. assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
  139. assertLength(t, doc.Find("#main #nf6").Nodes, 0)
  140. assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)
  141. printSel(t, doc.Selection)
  142. }
  143. func TestPrependSelection(t *testing.T) {
  144. doc := Doc2Clone()
  145. doc.Find("#main").PrependSelection(doc.Find("#nf1, #nf2"))
  146. assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
  147. assertLength(t, doc.Find("#foot #nf2").Nodes, 0)
  148. assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)
  149. assertLength(t, doc.Find("#main #nf2:nth-child(2)").Nodes, 1)
  150. printSel(t, doc.Selection)
  151. }
  152. func TestPrependSelectionExisting(t *testing.T) {
  153. doc := Doc2Clone()
  154. doc.Find("#main").PrependSelection(doc.Find("#n5, #n6"))
  155. assertClass(t, doc.Find("#main :nth-child(1)"), "five")
  156. assertClass(t, doc.Find("#main :nth-child(2)"), "six")
  157. assertClass(t, doc.Find("#main :nth-child(5)"), "three")
  158. assertClass(t, doc.Find("#main :nth-child(6)"), "four")
  159. printSel(t, doc.Selection)
  160. }
  161. func TestPrependClone(t *testing.T) {
  162. doc := Doc2Clone()
  163. doc.Find("#n1").PrependSelection(doc.Find("#nf1").Clone())
  164. assertLength(t, doc.Find("#foot #nf1:first-child").Nodes, 1)
  165. assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)
  166. printSel(t, doc.Selection)
  167. }
  168. func TestPrependHtml(t *testing.T) {
  169. doc := Doc2Clone()
  170. doc.Find("div").PrependHtml("<strong>new node</strong>")
  171. assertLength(t, doc.Find("strong:first-child").Nodes, 14)
  172. printSel(t, doc.Selection)
  173. }
  174. func TestRemove(t *testing.T) {
  175. doc := Doc2Clone()
  176. doc.Find("#nf1").Remove()
  177. assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
  178. printSel(t, doc.Selection)
  179. }
  180. func TestRemoveAll(t *testing.T) {
  181. doc := Doc2Clone()
  182. doc.Find("*").Remove()
  183. assertLength(t, doc.Find("*").Nodes, 0)
  184. printSel(t, doc.Selection)
  185. }
  186. func TestRemoveRoot(t *testing.T) {
  187. doc := Doc2Clone()
  188. doc.Find("html").Remove()
  189. assertLength(t, doc.Find("html").Nodes, 0)
  190. printSel(t, doc.Selection)
  191. }
  192. func TestRemoveFiltered(t *testing.T) {
  193. doc := Doc2Clone()
  194. nf6 := doc.Find("#nf6")
  195. s := doc.Find("div").RemoveFiltered("#nf6")
  196. assertLength(t, doc.Find("#nf6").Nodes, 0)
  197. assertLength(t, s.Nodes, 1)
  198. if nf6.Nodes[0] != s.Nodes[0] {
  199. t.Error("Removed node does not match original")
  200. }
  201. printSel(t, doc.Selection)
  202. }
  203. func TestReplaceWith(t *testing.T) {
  204. doc := Doc2Clone()
  205. doc.Find("#nf6").ReplaceWith("#main")
  206. assertLength(t, doc.Find("#foot #main:last-child").Nodes, 1)
  207. printSel(t, doc.Selection)
  208. doc.Find("#foot").ReplaceWith("#main")
  209. assertLength(t, doc.Find("#foot").Nodes, 0)
  210. assertLength(t, doc.Find("#main").Nodes, 1)
  211. printSel(t, doc.Selection)
  212. }
  213. func TestReplaceWithHtml(t *testing.T) {
  214. doc := Doc2Clone()
  215. doc.Find("#main, #foot").ReplaceWithHtml("<div id=\"replace\"></div>")
  216. assertLength(t, doc.Find("#replace").Nodes, 2)
  217. printSel(t, doc.Selection)
  218. }
  219. func TestReplaceWithSelection(t *testing.T) {
  220. doc := Doc2Clone()
  221. sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5"))
  222. assertSelectionIs(t, sel, "#nf6")
  223. assertLength(t, doc.Find("#nf6").Nodes, 0)
  224. assertLength(t, doc.Find("#nf5").Nodes, 1)
  225. printSel(t, doc.Selection)
  226. }
  227. func TestUnwrap(t *testing.T) {
  228. doc := Doc2Clone()
  229. doc.Find("#nf5").Unwrap()
  230. assertLength(t, doc.Find("#foot").Nodes, 0)
  231. assertLength(t, doc.Find("body > #nf1").Nodes, 1)
  232. assertLength(t, doc.Find("body > #nf5").Nodes, 1)
  233. printSel(t, doc.Selection)
  234. doc = Doc2Clone()
  235. doc.Find("#nf5, #n1").Unwrap()
  236. assertLength(t, doc.Find("#foot").Nodes, 0)
  237. assertLength(t, doc.Find("#main").Nodes, 0)
  238. assertLength(t, doc.Find("body > #n1").Nodes, 1)
  239. assertLength(t, doc.Find("body > #nf5").Nodes, 1)
  240. printSel(t, doc.Selection)
  241. }
  242. func TestUnwrapBody(t *testing.T) {
  243. doc := Doc2Clone()
  244. doc.Find("#main").Unwrap()
  245. assertLength(t, doc.Find("body").Nodes, 1)
  246. assertLength(t, doc.Find("body > #main").Nodes, 1)
  247. printSel(t, doc.Selection)
  248. }
  249. func TestUnwrapHead(t *testing.T) {
  250. doc := Doc2Clone()
  251. doc.Find("title").Unwrap()
  252. assertLength(t, doc.Find("head").Nodes, 0)
  253. assertLength(t, doc.Find("head > title").Nodes, 0)
  254. assertLength(t, doc.Find("title").Nodes, 1)
  255. printSel(t, doc.Selection)
  256. }
  257. func TestUnwrapHtml(t *testing.T) {
  258. doc := Doc2Clone()
  259. doc.Find("head").Unwrap()
  260. assertLength(t, doc.Find("html").Nodes, 0)
  261. assertLength(t, doc.Find("html head").Nodes, 0)
  262. assertLength(t, doc.Find("head").Nodes, 1)
  263. printSel(t, doc.Selection)
  264. }
  265. func TestWrap(t *testing.T) {
  266. doc := Doc2Clone()
  267. doc.Find("#nf1").Wrap("#nf2")
  268. nf1 := doc.Find("#foot #nf2 #nf1")
  269. assertLength(t, nf1.Nodes, 1)
  270. nf2 := doc.Find("#nf2")
  271. assertLength(t, nf2.Nodes, 2)
  272. printSel(t, doc.Selection)
  273. }
  274. func TestWrapEmpty(t *testing.T) {
  275. doc := Doc2Clone()
  276. doc.Find("#nf1").Wrap("#doesnt-exist")
  277. origHtml, _ := Doc2().Html()
  278. newHtml, _ := doc.Html()
  279. if origHtml != newHtml {
  280. t.Error("Expected the two documents to be identical.")
  281. }
  282. printSel(t, doc.Selection)
  283. }
  284. func TestWrapHtml(t *testing.T) {
  285. doc := Doc2Clone()
  286. doc.Find(".odd").WrapHtml(wrapHtml)
  287. nf2 := doc.Find("#ins #nf2")
  288. assertLength(t, nf2.Nodes, 1)
  289. printSel(t, doc.Selection)
  290. }
  291. func TestWrapSelection(t *testing.T) {
  292. doc := Doc2Clone()
  293. doc.Find("#nf1").WrapSelection(doc.Find("#nf2"))
  294. nf1 := doc.Find("#foot #nf2 #nf1")
  295. assertLength(t, nf1.Nodes, 1)
  296. nf2 := doc.Find("#nf2")
  297. assertLength(t, nf2.Nodes, 2)
  298. printSel(t, doc.Selection)
  299. }
  300. func TestWrapAll(t *testing.T) {
  301. doc := Doc2Clone()
  302. doc.Find(".odd").WrapAll("#nf1")
  303. nf1 := doc.Find("#main #nf1")
  304. assertLength(t, nf1.Nodes, 1)
  305. sel := nf1.Find("#n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
  306. assertLength(t, sel.Nodes, 1)
  307. printSel(t, doc.Selection)
  308. }
  309. func TestWrapAllHtml(t *testing.T) {
  310. doc := Doc2Clone()
  311. doc.Find(".odd").WrapAllHtml(wrapHtml)
  312. nf1 := doc.Find("#main div#ins div p em b #n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
  313. assertLength(t, nf1.Nodes, 1)
  314. printSel(t, doc.Selection)
  315. }
  316. func TestWrapInnerNoContent(t *testing.T) {
  317. doc := Doc2Clone()
  318. doc.Find(".one").WrapInner(".two")
  319. twos := doc.Find(".two")
  320. assertLength(t, twos.Nodes, 4)
  321. assertLength(t, doc.Find(".one .two").Nodes, 2)
  322. printSel(t, doc.Selection)
  323. }
  324. func TestWrapInnerWithContent(t *testing.T) {
  325. doc := Doc3Clone()
  326. doc.Find(".one").WrapInner(".two")
  327. twos := doc.Find(".two")
  328. assertLength(t, twos.Nodes, 4)
  329. assertLength(t, doc.Find(".one .two").Nodes, 2)
  330. printSel(t, doc.Selection)
  331. }
  332. func TestWrapInnerNoWrapper(t *testing.T) {
  333. doc := Doc2Clone()
  334. doc.Find(".one").WrapInner(".not-exist")
  335. twos := doc.Find(".two")
  336. assertLength(t, twos.Nodes, 2)
  337. assertLength(t, doc.Find(".one").Nodes, 2)
  338. assertLength(t, doc.Find(".one .two").Nodes, 0)
  339. printSel(t, doc.Selection)
  340. }
  341. func TestWrapInnerHtml(t *testing.T) {
  342. doc := Doc2Clone()
  343. doc.Find("#foot").WrapInnerHtml(wrapHtml)
  344. foot := doc.Find("#foot div#ins div p em b #nf1 ~ #nf2 ~ #nf3")
  345. assertLength(t, foot.Nodes, 1)
  346. printSel(t, doc.Selection)
  347. }