1package goquery 2 3import ( 4 "testing" 5) 6 7const ( 8 wrapHtml = "<div id=\"ins\">test string<div><p><em><b></b></em></p></div></div>" 9) 10 11func TestAfter(t *testing.T) { 12 doc := Doc2Clone() 13 doc.Find("#main").After("#nf6") 14 15 assertLength(t, doc.Find("#main #nf6").Nodes, 0) 16 assertLength(t, doc.Find("#foot #nf6").Nodes, 0) 17 assertLength(t, doc.Find("#main + #nf6").Nodes, 1) 18 printSel(t, doc.Selection) 19} 20 21func TestAfterMany(t *testing.T) { 22 doc := Doc2Clone() 23 doc.Find(".one").After("#nf6") 24 25 assertLength(t, doc.Find("#foot #nf6").Nodes, 1) 26 assertLength(t, doc.Find("#main #nf6").Nodes, 1) 27 assertLength(t, doc.Find(".one + #nf6").Nodes, 2) 28 printSel(t, doc.Selection) 29} 30 31func TestAfterWithRemoved(t *testing.T) { 32 doc := Doc2Clone() 33 s := doc.Find("#main").Remove() 34 s.After("#nf6") 35 36 assertLength(t, s.Find("#nf6").Nodes, 0) 37 assertLength(t, doc.Find("#nf6").Nodes, 0) 38 printSel(t, doc.Selection) 39} 40 41func TestAfterSelection(t *testing.T) { 42 doc := Doc2Clone() 43 doc.Find("#main").AfterSelection(doc.Find("#nf1, #nf2")) 44 45 assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0) 46 assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0) 47 assertLength(t, doc.Find("#main + #nf1, #nf1 + #nf2").Nodes, 2) 48 printSel(t, doc.Selection) 49} 50 51func TestAfterHtml(t *testing.T) { 52 doc := Doc2Clone() 53 doc.Find("#main").AfterHtml("<strong>new node</strong>") 54 55 assertLength(t, doc.Find("#main + strong").Nodes, 1) 56 printSel(t, doc.Selection) 57} 58 59func TestAppend(t *testing.T) { 60 doc := Doc2Clone() 61 doc.Find("#main").Append("#nf6") 62 63 assertLength(t, doc.Find("#foot #nf6").Nodes, 0) 64 assertLength(t, doc.Find("#main #nf6").Nodes, 1) 65 printSel(t, doc.Selection) 66} 67 68func TestAppendBody(t *testing.T) { 69 doc := Doc2Clone() 70 doc.Find("body").Append("#nf6") 71 72 assertLength(t, doc.Find("#foot #nf6").Nodes, 0) 73 assertLength(t, doc.Find("#main #nf6").Nodes, 0) 74 assertLength(t, doc.Find("body > #nf6").Nodes, 1) 75 printSel(t, doc.Selection) 76} 77 78func TestAppendSelection(t *testing.T) { 79 doc := Doc2Clone() 80 doc.Find("#main").AppendSelection(doc.Find("#nf1, #nf2")) 81 82 assertLength(t, doc.Find("#foot #nf1").Nodes, 0) 83 assertLength(t, doc.Find("#foot #nf2").Nodes, 0) 84 assertLength(t, doc.Find("#main #nf1").Nodes, 1) 85 assertLength(t, doc.Find("#main #nf2").Nodes, 1) 86 printSel(t, doc.Selection) 87} 88 89func TestAppendSelectionExisting(t *testing.T) { 90 doc := Doc2Clone() 91 doc.Find("#main").AppendSelection(doc.Find("#n1, #n2")) 92 93 assertClass(t, doc.Find("#main :nth-child(1)"), "three") 94 assertClass(t, doc.Find("#main :nth-child(5)"), "one") 95 assertClass(t, doc.Find("#main :nth-child(6)"), "two") 96 printSel(t, doc.Selection) 97} 98 99func TestAppendClone(t *testing.T) { 100 doc := Doc2Clone() 101 doc.Find("#n1").AppendSelection(doc.Find("#nf1").Clone()) 102 103 assertLength(t, doc.Find("#foot #nf1").Nodes, 1) 104 assertLength(t, doc.Find("#main #nf1").Nodes, 1) 105 printSel(t, doc.Selection) 106} 107 108func TestAppendHtml(t *testing.T) { 109 doc := Doc2Clone() 110 doc.Find("div").AppendHtml("<strong>new node</strong>") 111 112 assertLength(t, doc.Find("strong").Nodes, 14) 113 printSel(t, doc.Selection) 114} 115 116func TestBefore(t *testing.T) { 117 doc := Doc2Clone() 118 doc.Find("#main").Before("#nf6") 119 120 assertLength(t, doc.Find("#main #nf6").Nodes, 0) 121 assertLength(t, doc.Find("#foot #nf6").Nodes, 0) 122 assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1) 123 printSel(t, doc.Selection) 124} 125 126func TestBeforeWithRemoved(t *testing.T) { 127 doc := Doc2Clone() 128 s := doc.Find("#main").Remove() 129 s.Before("#nf6") 130 131 assertLength(t, s.Find("#nf6").Nodes, 0) 132 assertLength(t, doc.Find("#nf6").Nodes, 0) 133 printSel(t, doc.Selection) 134} 135 136func TestBeforeSelection(t *testing.T) { 137 doc := Doc2Clone() 138 doc.Find("#main").BeforeSelection(doc.Find("#nf1, #nf2")) 139 140 assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0) 141 assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0) 142 assertLength(t, doc.Find("body > #nf1:first-child, #nf1 + #nf2").Nodes, 2) 143 printSel(t, doc.Selection) 144} 145 146func TestBeforeHtml(t *testing.T) { 147 doc := Doc2Clone() 148 doc.Find("#main").BeforeHtml("<strong>new node</strong>") 149 150 assertLength(t, doc.Find("body > strong:first-child").Nodes, 1) 151 printSel(t, doc.Selection) 152} 153 154func TestEmpty(t *testing.T) { 155 doc := Doc2Clone() 156 s := doc.Find("#main").Empty() 157 158 assertLength(t, doc.Find("#main").Children().Nodes, 0) 159 assertLength(t, s.Filter("div").Nodes, 6) 160 printSel(t, doc.Selection) 161} 162 163func TestPrepend(t *testing.T) { 164 doc := Doc2Clone() 165 doc.Find("#main").Prepend("#nf6") 166 167 assertLength(t, doc.Find("#foot #nf6").Nodes, 0) 168 assertLength(t, doc.Find("#main #nf6:first-child").Nodes, 1) 169 printSel(t, doc.Selection) 170} 171 172func TestPrependBody(t *testing.T) { 173 doc := Doc2Clone() 174 doc.Find("body").Prepend("#nf6") 175 176 assertLength(t, doc.Find("#foot #nf6").Nodes, 0) 177 assertLength(t, doc.Find("#main #nf6").Nodes, 0) 178 assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1) 179 printSel(t, doc.Selection) 180} 181 182func TestPrependSelection(t *testing.T) { 183 doc := Doc2Clone() 184 doc.Find("#main").PrependSelection(doc.Find("#nf1, #nf2")) 185 186 assertLength(t, doc.Find("#foot #nf1").Nodes, 0) 187 assertLength(t, doc.Find("#foot #nf2").Nodes, 0) 188 assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1) 189 assertLength(t, doc.Find("#main #nf2:nth-child(2)").Nodes, 1) 190 printSel(t, doc.Selection) 191} 192 193func TestPrependSelectionExisting(t *testing.T) { 194 doc := Doc2Clone() 195 doc.Find("#main").PrependSelection(doc.Find("#n5, #n6")) 196 197 assertClass(t, doc.Find("#main :nth-child(1)"), "five") 198 assertClass(t, doc.Find("#main :nth-child(2)"), "six") 199 assertClass(t, doc.Find("#main :nth-child(5)"), "three") 200 assertClass(t, doc.Find("#main :nth-child(6)"), "four") 201 printSel(t, doc.Selection) 202} 203 204func TestPrependClone(t *testing.T) { 205 doc := Doc2Clone() 206 doc.Find("#n1").PrependSelection(doc.Find("#nf1").Clone()) 207 208 assertLength(t, doc.Find("#foot #nf1:first-child").Nodes, 1) 209 assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1) 210 printSel(t, doc.Selection) 211} 212 213func TestPrependHtml(t *testing.T) { 214 doc := Doc2Clone() 215 doc.Find("div").PrependHtml("<strong>new node</strong>") 216 217 assertLength(t, doc.Find("strong:first-child").Nodes, 14) 218 printSel(t, doc.Selection) 219} 220 221func TestRemove(t *testing.T) { 222 doc := Doc2Clone() 223 doc.Find("#nf1").Remove() 224 225 assertLength(t, doc.Find("#foot #nf1").Nodes, 0) 226 printSel(t, doc.Selection) 227} 228 229func TestRemoveAll(t *testing.T) { 230 doc := Doc2Clone() 231 doc.Find("*").Remove() 232 233 assertLength(t, doc.Find("*").Nodes, 0) 234 printSel(t, doc.Selection) 235} 236 237func TestRemoveRoot(t *testing.T) { 238 doc := Doc2Clone() 239 doc.Find("html").Remove() 240 241 assertLength(t, doc.Find("html").Nodes, 0) 242 printSel(t, doc.Selection) 243} 244 245func TestRemoveFiltered(t *testing.T) { 246 doc := Doc2Clone() 247 nf6 := doc.Find("#nf6") 248 s := doc.Find("div").RemoveFiltered("#nf6") 249 250 assertLength(t, doc.Find("#nf6").Nodes, 0) 251 assertLength(t, s.Nodes, 1) 252 if nf6.Nodes[0] != s.Nodes[0] { 253 t.Error("Removed node does not match original") 254 } 255 printSel(t, doc.Selection) 256} 257 258func TestReplaceWith(t *testing.T) { 259 doc := Doc2Clone() 260 261 doc.Find("#nf6").ReplaceWith("#main") 262 assertLength(t, doc.Find("#foot #main:last-child").Nodes, 1) 263 printSel(t, doc.Selection) 264 265 doc.Find("#foot").ReplaceWith("#main") 266 assertLength(t, doc.Find("#foot").Nodes, 0) 267 assertLength(t, doc.Find("#main").Nodes, 1) 268 269 printSel(t, doc.Selection) 270} 271 272func TestReplaceWithHtml(t *testing.T) { 273 doc := Doc2Clone() 274 doc.Find("#main, #foot").ReplaceWithHtml("<div id=\"replace\"></div>") 275 276 assertLength(t, doc.Find("#replace").Nodes, 2) 277 278 printSel(t, doc.Selection) 279} 280 281func TestSetHtml(t *testing.T) { 282 doc := Doc2Clone() 283 q := doc.Find("#main, #foot") 284 q.SetHtml(`<div id="replace">test</div>`) 285 286 assertLength(t, doc.Find("#replace").Nodes, 2) 287 assertLength(t, doc.Find("#main, #foot").Nodes, 2) 288 289 if q.Text() != "testtest" { 290 t.Errorf("Expected text to be %v, found %v", "testtest", q.Text()) 291 } 292 293 printSel(t, doc.Selection) 294} 295 296func TestSetHtmlNoMatch(t *testing.T) { 297 doc := Doc2Clone() 298 q := doc.Find("#notthere") 299 q.SetHtml(`<div id="replace">test</div>`) 300 301 assertLength(t, doc.Find("#replace").Nodes, 0) 302 303 printSel(t, doc.Selection) 304} 305 306func TestSetHtmlEmpty(t *testing.T) { 307 doc := Doc2Clone() 308 q := doc.Find("#main") 309 q.SetHtml(``) 310 311 assertLength(t, doc.Find("#main").Nodes, 1) 312 assertLength(t, doc.Find("#main").Children().Nodes, 0) 313 printSel(t, doc.Selection) 314} 315 316func TestSetText(t *testing.T) { 317 doc := Doc2Clone() 318 q := doc.Find("#main, #foot") 319 repl := "<div id=\"replace\">test</div>" 320 q.SetText(repl) 321 322 assertLength(t, doc.Find("#replace").Nodes, 0) 323 assertLength(t, doc.Find("#main, #foot").Nodes, 2) 324 325 if q.Text() != (repl + repl) { 326 t.Errorf("Expected text to be %v, found %v", (repl + repl), q.Text()) 327 } 328 329 h, err := q.Html() 330 if err != nil { 331 t.Errorf("Error: %v", err) 332 } 333 esc := "<div id="replace">test</div>" 334 if h != esc { 335 t.Errorf("Expected html to be %v, found %v", esc, h) 336 } 337 338 printSel(t, doc.Selection) 339} 340 341func TestReplaceWithSelection(t *testing.T) { 342 doc := Doc2Clone() 343 sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5")) 344 345 assertSelectionIs(t, sel, "#nf6") 346 assertLength(t, doc.Find("#nf6").Nodes, 0) 347 assertLength(t, doc.Find("#nf5").Nodes, 1) 348 349 printSel(t, doc.Selection) 350} 351 352func TestUnwrap(t *testing.T) { 353 doc := Doc2Clone() 354 355 doc.Find("#nf5").Unwrap() 356 assertLength(t, doc.Find("#foot").Nodes, 0) 357 assertLength(t, doc.Find("body > #nf1").Nodes, 1) 358 assertLength(t, doc.Find("body > #nf5").Nodes, 1) 359 360 printSel(t, doc.Selection) 361 362 doc = Doc2Clone() 363 364 doc.Find("#nf5, #n1").Unwrap() 365 assertLength(t, doc.Find("#foot").Nodes, 0) 366 assertLength(t, doc.Find("#main").Nodes, 0) 367 assertLength(t, doc.Find("body > #n1").Nodes, 1) 368 assertLength(t, doc.Find("body > #nf5").Nodes, 1) 369 370 printSel(t, doc.Selection) 371} 372 373func TestUnwrapBody(t *testing.T) { 374 doc := Doc2Clone() 375 376 doc.Find("#main").Unwrap() 377 assertLength(t, doc.Find("body").Nodes, 1) 378 assertLength(t, doc.Find("body > #main").Nodes, 1) 379 380 printSel(t, doc.Selection) 381} 382 383func TestUnwrapHead(t *testing.T) { 384 doc := Doc2Clone() 385 386 doc.Find("title").Unwrap() 387 assertLength(t, doc.Find("head").Nodes, 0) 388 assertLength(t, doc.Find("head > title").Nodes, 0) 389 assertLength(t, doc.Find("title").Nodes, 1) 390 391 printSel(t, doc.Selection) 392} 393 394func TestUnwrapHtml(t *testing.T) { 395 doc := Doc2Clone() 396 397 doc.Find("head").Unwrap() 398 assertLength(t, doc.Find("html").Nodes, 0) 399 assertLength(t, doc.Find("html head").Nodes, 0) 400 assertLength(t, doc.Find("head").Nodes, 1) 401 402 printSel(t, doc.Selection) 403} 404 405func TestWrap(t *testing.T) { 406 doc := Doc2Clone() 407 doc.Find("#nf1").Wrap("#nf2") 408 nf1 := doc.Find("#foot #nf2 #nf1") 409 assertLength(t, nf1.Nodes, 1) 410 411 nf2 := doc.Find("#nf2") 412 assertLength(t, nf2.Nodes, 2) 413 414 printSel(t, doc.Selection) 415} 416 417func TestWrapEmpty(t *testing.T) { 418 doc := Doc2Clone() 419 doc.Find("#nf1").Wrap("#doesnt-exist") 420 421 origHtml, _ := Doc2().Html() 422 newHtml, _ := doc.Html() 423 424 if origHtml != newHtml { 425 t.Error("Expected the two documents to be identical.") 426 } 427 428 printSel(t, doc.Selection) 429} 430 431func TestWrapHtml(t *testing.T) { 432 doc := Doc2Clone() 433 doc.Find(".odd").WrapHtml(wrapHtml) 434 nf2 := doc.Find("#ins #nf2") 435 assertLength(t, nf2.Nodes, 1) 436 printSel(t, doc.Selection) 437} 438 439func TestWrapSelection(t *testing.T) { 440 doc := Doc2Clone() 441 doc.Find("#nf1").WrapSelection(doc.Find("#nf2")) 442 nf1 := doc.Find("#foot #nf2 #nf1") 443 assertLength(t, nf1.Nodes, 1) 444 445 nf2 := doc.Find("#nf2") 446 assertLength(t, nf2.Nodes, 2) 447 448 printSel(t, doc.Selection) 449} 450 451func TestWrapAll(t *testing.T) { 452 doc := Doc2Clone() 453 doc.Find(".odd").WrapAll("#nf1") 454 nf1 := doc.Find("#main #nf1") 455 assertLength(t, nf1.Nodes, 1) 456 457 sel := nf1.Find("#n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6") 458 assertLength(t, sel.Nodes, 1) 459 460 printSel(t, doc.Selection) 461} 462 463func TestWrapAllHtml(t *testing.T) { 464 doc := Doc2Clone() 465 doc.Find(".odd").WrapAllHtml(wrapHtml) 466 nf1 := doc.Find("#main div#ins div p em b #n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6") 467 assertLength(t, nf1.Nodes, 1) 468 printSel(t, doc.Selection) 469} 470 471func TestWrapInnerNoContent(t *testing.T) { 472 doc := Doc2Clone() 473 doc.Find(".one").WrapInner(".two") 474 475 twos := doc.Find(".two") 476 assertLength(t, twos.Nodes, 4) 477 assertLength(t, doc.Find(".one .two").Nodes, 2) 478 479 printSel(t, doc.Selection) 480} 481 482func TestWrapInnerWithContent(t *testing.T) { 483 doc := Doc3Clone() 484 doc.Find(".one").WrapInner(".two") 485 486 twos := doc.Find(".two") 487 assertLength(t, twos.Nodes, 4) 488 assertLength(t, doc.Find(".one .two").Nodes, 2) 489 490 printSel(t, doc.Selection) 491} 492 493func TestWrapInnerNoWrapper(t *testing.T) { 494 doc := Doc2Clone() 495 doc.Find(".one").WrapInner(".not-exist") 496 497 twos := doc.Find(".two") 498 assertLength(t, twos.Nodes, 2) 499 assertLength(t, doc.Find(".one").Nodes, 2) 500 assertLength(t, doc.Find(".one .two").Nodes, 0) 501 502 printSel(t, doc.Selection) 503} 504 505func TestWrapInnerHtml(t *testing.T) { 506 doc := Doc2Clone() 507 doc.Find("#foot").WrapInnerHtml(wrapHtml) 508 509 foot := doc.Find("#foot div#ins div p em b #nf1 ~ #nf2 ~ #nf3") 510 assertLength(t, foot.Nodes, 1) 511 512 printSel(t, doc.Selection) 513} 514