manipulation_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 TestSetHtml(t *testing.T) {
  220. doc := Doc2Clone()
  221. q := doc.Find("#main, #foot")
  222. q.SetHtml(`<div id="replace">test</div>`)
  223. assertLength(t, doc.Find("#replace").Nodes, 2)
  224. assertLength(t, doc.Find("#main, #foot").Nodes, 2)
  225. if q.Text() != "testtest" {
  226. t.Errorf("Expected text to be %v, found %v", "testtest", q.Text())
  227. }
  228. printSel(t, doc.Selection)
  229. }
  230. func TestSetHtmlNoMatch(t *testing.T) {
  231. doc := Doc2Clone()
  232. q := doc.Find("#notthere")
  233. q.SetHtml(`<div id="replace">test</div>`)
  234. assertLength(t, doc.Find("#replace").Nodes, 0)
  235. printSel(t, doc.Selection)
  236. }
  237. func TestSetHtmlEmpty(t *testing.T) {
  238. doc := Doc2Clone()
  239. q := doc.Find("#main")
  240. q.SetHtml(``)
  241. assertLength(t, doc.Find("#main").Nodes, 1)
  242. assertLength(t, doc.Find("#main").Children().Nodes, 0)
  243. printSel(t, doc.Selection)
  244. }
  245. func TestSetText(t *testing.T) {
  246. doc := Doc2Clone()
  247. q := doc.Find("#main, #foot")
  248. repl := "<div id=\"replace\">test</div>"
  249. q.SetText(repl)
  250. assertLength(t, doc.Find("#replace").Nodes, 0)
  251. assertLength(t, doc.Find("#main, #foot").Nodes, 2)
  252. if q.Text() != (repl + repl) {
  253. t.Errorf("Expected text to be %v, found %v", (repl + repl), q.Text())
  254. }
  255. h, err := q.Html()
  256. if err != nil {
  257. t.Errorf("Error: %v", err)
  258. }
  259. esc := "&lt;div id=&#34;replace&#34;&gt;test&lt;/div&gt;"
  260. if h != esc {
  261. t.Errorf("Expected html to be %v, found %v", esc, h)
  262. }
  263. printSel(t, doc.Selection)
  264. }
  265. func TestReplaceWithSelection(t *testing.T) {
  266. doc := Doc2Clone()
  267. sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5"))
  268. assertSelectionIs(t, sel, "#nf6")
  269. assertLength(t, doc.Find("#nf6").Nodes, 0)
  270. assertLength(t, doc.Find("#nf5").Nodes, 1)
  271. printSel(t, doc.Selection)
  272. }
  273. func TestUnwrap(t *testing.T) {
  274. doc := Doc2Clone()
  275. doc.Find("#nf5").Unwrap()
  276. assertLength(t, doc.Find("#foot").Nodes, 0)
  277. assertLength(t, doc.Find("body > #nf1").Nodes, 1)
  278. assertLength(t, doc.Find("body > #nf5").Nodes, 1)
  279. printSel(t, doc.Selection)
  280. doc = Doc2Clone()
  281. doc.Find("#nf5, #n1").Unwrap()
  282. assertLength(t, doc.Find("#foot").Nodes, 0)
  283. assertLength(t, doc.Find("#main").Nodes, 0)
  284. assertLength(t, doc.Find("body > #n1").Nodes, 1)
  285. assertLength(t, doc.Find("body > #nf5").Nodes, 1)
  286. printSel(t, doc.Selection)
  287. }
  288. func TestUnwrapBody(t *testing.T) {
  289. doc := Doc2Clone()
  290. doc.Find("#main").Unwrap()
  291. assertLength(t, doc.Find("body").Nodes, 1)
  292. assertLength(t, doc.Find("body > #main").Nodes, 1)
  293. printSel(t, doc.Selection)
  294. }
  295. func TestUnwrapHead(t *testing.T) {
  296. doc := Doc2Clone()
  297. doc.Find("title").Unwrap()
  298. assertLength(t, doc.Find("head").Nodes, 0)
  299. assertLength(t, doc.Find("head > title").Nodes, 0)
  300. assertLength(t, doc.Find("title").Nodes, 1)
  301. printSel(t, doc.Selection)
  302. }
  303. func TestUnwrapHtml(t *testing.T) {
  304. doc := Doc2Clone()
  305. doc.Find("head").Unwrap()
  306. assertLength(t, doc.Find("html").Nodes, 0)
  307. assertLength(t, doc.Find("html head").Nodes, 0)
  308. assertLength(t, doc.Find("head").Nodes, 1)
  309. printSel(t, doc.Selection)
  310. }
  311. func TestWrap(t *testing.T) {
  312. doc := Doc2Clone()
  313. doc.Find("#nf1").Wrap("#nf2")
  314. nf1 := doc.Find("#foot #nf2 #nf1")
  315. assertLength(t, nf1.Nodes, 1)
  316. nf2 := doc.Find("#nf2")
  317. assertLength(t, nf2.Nodes, 2)
  318. printSel(t, doc.Selection)
  319. }
  320. func TestWrapEmpty(t *testing.T) {
  321. doc := Doc2Clone()
  322. doc.Find("#nf1").Wrap("#doesnt-exist")
  323. origHtml, _ := Doc2().Html()
  324. newHtml, _ := doc.Html()
  325. if origHtml != newHtml {
  326. t.Error("Expected the two documents to be identical.")
  327. }
  328. printSel(t, doc.Selection)
  329. }
  330. func TestWrapHtml(t *testing.T) {
  331. doc := Doc2Clone()
  332. doc.Find(".odd").WrapHtml(wrapHtml)
  333. nf2 := doc.Find("#ins #nf2")
  334. assertLength(t, nf2.Nodes, 1)
  335. printSel(t, doc.Selection)
  336. }
  337. func TestWrapSelection(t *testing.T) {
  338. doc := Doc2Clone()
  339. doc.Find("#nf1").WrapSelection(doc.Find("#nf2"))
  340. nf1 := doc.Find("#foot #nf2 #nf1")
  341. assertLength(t, nf1.Nodes, 1)
  342. nf2 := doc.Find("#nf2")
  343. assertLength(t, nf2.Nodes, 2)
  344. printSel(t, doc.Selection)
  345. }
  346. func TestWrapAll(t *testing.T) {
  347. doc := Doc2Clone()
  348. doc.Find(".odd").WrapAll("#nf1")
  349. nf1 := doc.Find("#main #nf1")
  350. assertLength(t, nf1.Nodes, 1)
  351. sel := nf1.Find("#n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
  352. assertLength(t, sel.Nodes, 1)
  353. printSel(t, doc.Selection)
  354. }
  355. func TestWrapAllHtml(t *testing.T) {
  356. doc := Doc2Clone()
  357. doc.Find(".odd").WrapAllHtml(wrapHtml)
  358. nf1 := doc.Find("#main div#ins div p em b #n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
  359. assertLength(t, nf1.Nodes, 1)
  360. printSel(t, doc.Selection)
  361. }
  362. func TestWrapInnerNoContent(t *testing.T) {
  363. doc := Doc2Clone()
  364. doc.Find(".one").WrapInner(".two")
  365. twos := doc.Find(".two")
  366. assertLength(t, twos.Nodes, 4)
  367. assertLength(t, doc.Find(".one .two").Nodes, 2)
  368. printSel(t, doc.Selection)
  369. }
  370. func TestWrapInnerWithContent(t *testing.T) {
  371. doc := Doc3Clone()
  372. doc.Find(".one").WrapInner(".two")
  373. twos := doc.Find(".two")
  374. assertLength(t, twos.Nodes, 4)
  375. assertLength(t, doc.Find(".one .two").Nodes, 2)
  376. printSel(t, doc.Selection)
  377. }
  378. func TestWrapInnerNoWrapper(t *testing.T) {
  379. doc := Doc2Clone()
  380. doc.Find(".one").WrapInner(".not-exist")
  381. twos := doc.Find(".two")
  382. assertLength(t, twos.Nodes, 2)
  383. assertLength(t, doc.Find(".one").Nodes, 2)
  384. assertLength(t, doc.Find(".one .two").Nodes, 0)
  385. printSel(t, doc.Selection)
  386. }
  387. func TestWrapInnerHtml(t *testing.T) {
  388. doc := Doc2Clone()
  389. doc.Find("#foot").WrapInnerHtml(wrapHtml)
  390. foot := doc.Find("#foot div#ins div p em b #nf1 ~ #nf2 ~ #nf3")
  391. assertLength(t, foot.Nodes, 1)
  392. printSel(t, doc.Selection)
  393. }