123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- package goquery
- import (
- "testing"
- )
- const (
- wrapHtml = "<div id=\"ins\">test string<div><p><em><b></b></em></p></div></div>"
- )
- func TestAfter(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").After("#nf6")
- assertLength(t, doc.Find("#main #nf6").Nodes, 0)
- assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
- assertLength(t, doc.Find("#main + #nf6").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestAfterMany(t *testing.T) {
- doc := Doc2Clone()
- doc.Find(".one").After("#nf6")
- assertLength(t, doc.Find("#foot #nf6").Nodes, 1)
- assertLength(t, doc.Find("#main #nf6").Nodes, 1)
- assertLength(t, doc.Find(".one + #nf6").Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestAfterWithRemoved(t *testing.T) {
- doc := Doc2Clone()
- s := doc.Find("#main").Remove()
- s.After("#nf6")
- assertLength(t, s.Find("#nf6").Nodes, 0)
- assertLength(t, doc.Find("#nf6").Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestAfterSelection(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").AfterSelection(doc.Find("#nf1, #nf2"))
- assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)
- assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)
- assertLength(t, doc.Find("#main + #nf1, #nf1 + #nf2").Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestAfterHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").AfterHtml("<strong>new node</strong>")
- assertLength(t, doc.Find("#main + strong").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestAppend(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").Append("#nf6")
- assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
- assertLength(t, doc.Find("#main #nf6").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestAppendBody(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("body").Append("#nf6")
- assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
- assertLength(t, doc.Find("#main #nf6").Nodes, 0)
- assertLength(t, doc.Find("body > #nf6").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestAppendSelection(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").AppendSelection(doc.Find("#nf1, #nf2"))
- assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
- assertLength(t, doc.Find("#foot #nf2").Nodes, 0)
- assertLength(t, doc.Find("#main #nf1").Nodes, 1)
- assertLength(t, doc.Find("#main #nf2").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestAppendSelectionExisting(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").AppendSelection(doc.Find("#n1, #n2"))
- assertClass(t, doc.Find("#main :nth-child(1)"), "three")
- assertClass(t, doc.Find("#main :nth-child(5)"), "one")
- assertClass(t, doc.Find("#main :nth-child(6)"), "two")
- printSel(t, doc.Selection)
- }
- func TestAppendClone(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#n1").AppendSelection(doc.Find("#nf1").Clone())
- assertLength(t, doc.Find("#foot #nf1").Nodes, 1)
- assertLength(t, doc.Find("#main #nf1").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestAppendHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("div").AppendHtml("<strong>new node</strong>")
- assertLength(t, doc.Find("strong").Nodes, 14)
- printSel(t, doc.Selection)
- }
- func TestBefore(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").Before("#nf6")
- assertLength(t, doc.Find("#main #nf6").Nodes, 0)
- assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
- assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestBeforeWithRemoved(t *testing.T) {
- doc := Doc2Clone()
- s := doc.Find("#main").Remove()
- s.Before("#nf6")
- assertLength(t, s.Find("#nf6").Nodes, 0)
- assertLength(t, doc.Find("#nf6").Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestBeforeSelection(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").BeforeSelection(doc.Find("#nf1, #nf2"))
- assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)
- assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)
- assertLength(t, doc.Find("body > #nf1:first-child, #nf1 + #nf2").Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestBeforeHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").BeforeHtml("<strong>new node</strong>")
- assertLength(t, doc.Find("body > strong:first-child").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestEmpty(t *testing.T) {
- doc := Doc2Clone()
- s := doc.Find("#main").Empty()
- assertLength(t, doc.Find("#main").Children().Nodes, 0)
- assertLength(t, s.Filter("div").Nodes, 6)
- printSel(t, doc.Selection)
- }
- func TestPrepend(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").Prepend("#nf6")
- assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
- assertLength(t, doc.Find("#main #nf6:first-child").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestPrependBody(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("body").Prepend("#nf6")
- assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
- assertLength(t, doc.Find("#main #nf6").Nodes, 0)
- assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestPrependSelection(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").PrependSelection(doc.Find("#nf1, #nf2"))
- assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
- assertLength(t, doc.Find("#foot #nf2").Nodes, 0)
- assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)
- assertLength(t, doc.Find("#main #nf2:nth-child(2)").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestPrependSelectionExisting(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").PrependSelection(doc.Find("#n5, #n6"))
- assertClass(t, doc.Find("#main :nth-child(1)"), "five")
- assertClass(t, doc.Find("#main :nth-child(2)"), "six")
- assertClass(t, doc.Find("#main :nth-child(5)"), "three")
- assertClass(t, doc.Find("#main :nth-child(6)"), "four")
- printSel(t, doc.Selection)
- }
- func TestPrependClone(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#n1").PrependSelection(doc.Find("#nf1").Clone())
- assertLength(t, doc.Find("#foot #nf1:first-child").Nodes, 1)
- assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestPrependHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("div").PrependHtml("<strong>new node</strong>")
- assertLength(t, doc.Find("strong:first-child").Nodes, 14)
- printSel(t, doc.Selection)
- }
- func TestRemove(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#nf1").Remove()
- assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestRemoveAll(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("*").Remove()
- assertLength(t, doc.Find("*").Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestRemoveRoot(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("html").Remove()
- assertLength(t, doc.Find("html").Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestRemoveFiltered(t *testing.T) {
- doc := Doc2Clone()
- nf6 := doc.Find("#nf6")
- s := doc.Find("div").RemoveFiltered("#nf6")
- assertLength(t, doc.Find("#nf6").Nodes, 0)
- assertLength(t, s.Nodes, 1)
- if nf6.Nodes[0] != s.Nodes[0] {
- t.Error("Removed node does not match original")
- }
- printSel(t, doc.Selection)
- }
- func TestReplaceWith(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#nf6").ReplaceWith("#main")
- assertLength(t, doc.Find("#foot #main:last-child").Nodes, 1)
- printSel(t, doc.Selection)
- doc.Find("#foot").ReplaceWith("#main")
- assertLength(t, doc.Find("#foot").Nodes, 0)
- assertLength(t, doc.Find("#main").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestReplaceWithHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main, #foot").ReplaceWithHtml("<div id=\"replace\"></div>")
- assertLength(t, doc.Find("#replace").Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestSetHtml(t *testing.T) {
- doc := Doc2Clone()
- q := doc.Find("#main, #foot")
- q.SetHtml(`<div id="replace">test</div>`)
- assertLength(t, doc.Find("#replace").Nodes, 2)
- assertLength(t, doc.Find("#main, #foot").Nodes, 2)
- if q.Text() != "testtest" {
- t.Errorf("Expected text to be %v, found %v", "testtest", q.Text())
- }
- printSel(t, doc.Selection)
- }
- func TestSetHtmlNoMatch(t *testing.T) {
- doc := Doc2Clone()
- q := doc.Find("#notthere")
- q.SetHtml(`<div id="replace">test</div>`)
- assertLength(t, doc.Find("#replace").Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestSetHtmlEmpty(t *testing.T) {
- doc := Doc2Clone()
- q := doc.Find("#main")
- q.SetHtml(``)
- assertLength(t, doc.Find("#main").Nodes, 1)
- assertLength(t, doc.Find("#main").Children().Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestSetText(t *testing.T) {
- doc := Doc2Clone()
- q := doc.Find("#main, #foot")
- repl := "<div id=\"replace\">test</div>"
- q.SetText(repl)
- assertLength(t, doc.Find("#replace").Nodes, 0)
- assertLength(t, doc.Find("#main, #foot").Nodes, 2)
- if q.Text() != (repl + repl) {
- t.Errorf("Expected text to be %v, found %v", (repl + repl), q.Text())
- }
- h, err := q.Html()
- if err != nil {
- t.Errorf("Error: %v", err)
- }
- esc := "<div id="replace">test</div>"
- if h != esc {
- t.Errorf("Expected html to be %v, found %v", esc, h)
- }
- printSel(t, doc.Selection)
- }
- func TestReplaceWithSelection(t *testing.T) {
- doc := Doc2Clone()
- sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5"))
- assertSelectionIs(t, sel, "#nf6")
- assertLength(t, doc.Find("#nf6").Nodes, 0)
- assertLength(t, doc.Find("#nf5").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestUnwrap(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#nf5").Unwrap()
- assertLength(t, doc.Find("#foot").Nodes, 0)
- assertLength(t, doc.Find("body > #nf1").Nodes, 1)
- assertLength(t, doc.Find("body > #nf5").Nodes, 1)
- printSel(t, doc.Selection)
- doc = Doc2Clone()
- doc.Find("#nf5, #n1").Unwrap()
- assertLength(t, doc.Find("#foot").Nodes, 0)
- assertLength(t, doc.Find("#main").Nodes, 0)
- assertLength(t, doc.Find("body > #n1").Nodes, 1)
- assertLength(t, doc.Find("body > #nf5").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestUnwrapBody(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#main").Unwrap()
- assertLength(t, doc.Find("body").Nodes, 1)
- assertLength(t, doc.Find("body > #main").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestUnwrapHead(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("title").Unwrap()
- assertLength(t, doc.Find("head").Nodes, 0)
- assertLength(t, doc.Find("head > title").Nodes, 0)
- assertLength(t, doc.Find("title").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestUnwrapHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("head").Unwrap()
- assertLength(t, doc.Find("html").Nodes, 0)
- assertLength(t, doc.Find("html head").Nodes, 0)
- assertLength(t, doc.Find("head").Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestWrap(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#nf1").Wrap("#nf2")
- nf1 := doc.Find("#foot #nf2 #nf1")
- assertLength(t, nf1.Nodes, 1)
- nf2 := doc.Find("#nf2")
- assertLength(t, nf2.Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestWrapEmpty(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#nf1").Wrap("#doesnt-exist")
- origHtml, _ := Doc2().Html()
- newHtml, _ := doc.Html()
- if origHtml != newHtml {
- t.Error("Expected the two documents to be identical.")
- }
- printSel(t, doc.Selection)
- }
- func TestWrapHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find(".odd").WrapHtml(wrapHtml)
- nf2 := doc.Find("#ins #nf2")
- assertLength(t, nf2.Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestWrapSelection(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#nf1").WrapSelection(doc.Find("#nf2"))
- nf1 := doc.Find("#foot #nf2 #nf1")
- assertLength(t, nf1.Nodes, 1)
- nf2 := doc.Find("#nf2")
- assertLength(t, nf2.Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestWrapAll(t *testing.T) {
- doc := Doc2Clone()
- doc.Find(".odd").WrapAll("#nf1")
- nf1 := doc.Find("#main #nf1")
- assertLength(t, nf1.Nodes, 1)
- sel := nf1.Find("#n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
- assertLength(t, sel.Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestWrapAllHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find(".odd").WrapAllHtml(wrapHtml)
- nf1 := doc.Find("#main div#ins div p em b #n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
- assertLength(t, nf1.Nodes, 1)
- printSel(t, doc.Selection)
- }
- func TestWrapInnerNoContent(t *testing.T) {
- doc := Doc2Clone()
- doc.Find(".one").WrapInner(".two")
- twos := doc.Find(".two")
- assertLength(t, twos.Nodes, 4)
- assertLength(t, doc.Find(".one .two").Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestWrapInnerWithContent(t *testing.T) {
- doc := Doc3Clone()
- doc.Find(".one").WrapInner(".two")
- twos := doc.Find(".two")
- assertLength(t, twos.Nodes, 4)
- assertLength(t, doc.Find(".one .two").Nodes, 2)
- printSel(t, doc.Selection)
- }
- func TestWrapInnerNoWrapper(t *testing.T) {
- doc := Doc2Clone()
- doc.Find(".one").WrapInner(".not-exist")
- twos := doc.Find(".two")
- assertLength(t, twos.Nodes, 2)
- assertLength(t, doc.Find(".one").Nodes, 2)
- assertLength(t, doc.Find(".one .two").Nodes, 0)
- printSel(t, doc.Selection)
- }
- func TestWrapInnerHtml(t *testing.T) {
- doc := Doc2Clone()
- doc.Find("#foot").WrapInnerHtml(wrapHtml)
- foot := doc.Find("#foot div#ins div p em b #nf1 ~ #nf2 ~ #nf3")
- assertLength(t, foot.Nodes, 1)
- printSel(t, doc.Selection)
- }
|