...

Source file src/github.com/PuerkitoBio/goquery/traversal_test.go

Documentation: github.com/PuerkitoBio/goquery

     1  package goquery
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  )
     7  
     8  func TestFind(t *testing.T) {
     9  	sel := Doc().Find("div.row-fluid")
    10  	assertLength(t, sel.Nodes, 9)
    11  }
    12  
    13  func TestFindRollback(t *testing.T) {
    14  	sel := Doc().Find("div.row-fluid")
    15  	sel2 := sel.Find("a").End()
    16  	assertEqual(t, sel, sel2)
    17  }
    18  
    19  func TestFindNotSelf(t *testing.T) {
    20  	sel := Doc().Find("h1").Find("h1")
    21  	assertLength(t, sel.Nodes, 0)
    22  }
    23  
    24  func TestFindInvalid(t *testing.T) {
    25  	sel := Doc().Find(":+ ^")
    26  	assertLength(t, sel.Nodes, 0)
    27  }
    28  
    29  func TestFindBig(t *testing.T) {
    30  	doc := DocW()
    31  	sel := doc.Find("li")
    32  	assertLength(t, sel.Nodes, 373)
    33  	sel2 := doc.Find("span")
    34  	assertLength(t, sel2.Nodes, 448)
    35  	sel3 := sel.FindSelection(sel2)
    36  	assertLength(t, sel3.Nodes, 248)
    37  }
    38  
    39  func TestChainedFind(t *testing.T) {
    40  	sel := Doc().Find("div.hero-unit").Find(".row-fluid")
    41  	assertLength(t, sel.Nodes, 4)
    42  }
    43  
    44  func TestChainedFindInvalid(t *testing.T) {
    45  	sel := Doc().Find("div.hero-unit").Find("")
    46  	assertLength(t, sel.Nodes, 0)
    47  }
    48  
    49  func TestChildren(t *testing.T) {
    50  	sel := Doc().Find(".pvk-content").Children()
    51  	assertLength(t, sel.Nodes, 5)
    52  }
    53  
    54  func TestChildrenRollback(t *testing.T) {
    55  	sel := Doc().Find(".pvk-content")
    56  	sel2 := sel.Children().End()
    57  	assertEqual(t, sel, sel2)
    58  }
    59  
    60  func TestContents(t *testing.T) {
    61  	sel := Doc().Find(".pvk-content").Contents()
    62  	assertLength(t, sel.Nodes, 13)
    63  }
    64  
    65  func TestContentsRollback(t *testing.T) {
    66  	sel := Doc().Find(".pvk-content")
    67  	sel2 := sel.Contents().End()
    68  	assertEqual(t, sel, sel2)
    69  }
    70  
    71  func TestChildrenFiltered(t *testing.T) {
    72  	sel := Doc().Find(".pvk-content").ChildrenFiltered(".hero-unit")
    73  	assertLength(t, sel.Nodes, 1)
    74  }
    75  
    76  func TestChildrenFilteredInvalid(t *testing.T) {
    77  	sel := Doc().Find(".pvk-content").ChildrenFiltered("")
    78  	assertLength(t, sel.Nodes, 0)
    79  }
    80  
    81  func TestChildrenFilteredRollback(t *testing.T) {
    82  	sel := Doc().Find(".pvk-content")
    83  	sel2 := sel.ChildrenFiltered(".hero-unit").End()
    84  	assertEqual(t, sel, sel2)
    85  }
    86  
    87  func TestContentsFiltered(t *testing.T) {
    88  	sel := Doc().Find(".pvk-content").ContentsFiltered(".hero-unit")
    89  	assertLength(t, sel.Nodes, 1)
    90  }
    91  
    92  func TestContentsFilteredInvalid(t *testing.T) {
    93  	sel := Doc().Find(".pvk-content").ContentsFiltered("~")
    94  	assertLength(t, sel.Nodes, 0)
    95  }
    96  
    97  func TestContentsFilteredRollback(t *testing.T) {
    98  	sel := Doc().Find(".pvk-content")
    99  	sel2 := sel.ContentsFiltered(".hero-unit").End()
   100  	assertEqual(t, sel, sel2)
   101  }
   102  
   103  func TestChildrenFilteredNone(t *testing.T) {
   104  	sel := Doc().Find(".pvk-content").ChildrenFiltered("a.btn")
   105  	assertLength(t, sel.Nodes, 0)
   106  }
   107  
   108  func TestParent(t *testing.T) {
   109  	sel := Doc().Find(".container-fluid").Parent()
   110  	assertLength(t, sel.Nodes, 3)
   111  }
   112  
   113  func TestParentRollback(t *testing.T) {
   114  	sel := Doc().Find(".container-fluid")
   115  	sel2 := sel.Parent().End()
   116  	assertEqual(t, sel, sel2)
   117  }
   118  
   119  func TestParentBody(t *testing.T) {
   120  	sel := Doc().Find("body").Parent()
   121  	assertLength(t, sel.Nodes, 1)
   122  }
   123  
   124  func TestParentFiltered(t *testing.T) {
   125  	sel := Doc().Find(".container-fluid").ParentFiltered(".hero-unit")
   126  	assertLength(t, sel.Nodes, 1)
   127  	assertClass(t, sel, "hero-unit")
   128  }
   129  
   130  func TestParentFilteredInvalid(t *testing.T) {
   131  	sel := Doc().Find(".container-fluid").ParentFiltered("")
   132  	assertLength(t, sel.Nodes, 0)
   133  }
   134  
   135  func TestParentFilteredRollback(t *testing.T) {
   136  	sel := Doc().Find(".container-fluid")
   137  	sel2 := sel.ParentFiltered(".hero-unit").End()
   138  	assertEqual(t, sel, sel2)
   139  }
   140  
   141  func TestParents(t *testing.T) {
   142  	sel := Doc().Find(".container-fluid").Parents()
   143  	assertLength(t, sel.Nodes, 8)
   144  }
   145  
   146  func TestParentsOrder(t *testing.T) {
   147  	sel := Doc().Find("#cf2").Parents()
   148  	assertLength(t, sel.Nodes, 6)
   149  	assertSelectionIs(t, sel, ".hero-unit", ".pvk-content", "div.row-fluid", "#cf1", "body", "html")
   150  }
   151  
   152  func TestParentsRollback(t *testing.T) {
   153  	sel := Doc().Find(".container-fluid")
   154  	sel2 := sel.Parents().End()
   155  	assertEqual(t, sel, sel2)
   156  }
   157  
   158  func TestParentsFiltered(t *testing.T) {
   159  	sel := Doc().Find(".container-fluid").ParentsFiltered("body")
   160  	assertLength(t, sel.Nodes, 1)
   161  }
   162  
   163  func TestParentsFilteredInvalid(t *testing.T) {
   164  	sel := Doc().Find(".container-fluid").ParentsFiltered("")
   165  	assertLength(t, sel.Nodes, 0)
   166  }
   167  
   168  func TestParentsFilteredRollback(t *testing.T) {
   169  	sel := Doc().Find(".container-fluid")
   170  	sel2 := sel.ParentsFiltered("body").End()
   171  	assertEqual(t, sel, sel2)
   172  }
   173  
   174  func TestParentsUntil(t *testing.T) {
   175  	sel := Doc().Find(".container-fluid").ParentsUntil("body")
   176  	assertLength(t, sel.Nodes, 6)
   177  }
   178  
   179  func TestParentsUntilInvalid(t *testing.T) {
   180  	sel := Doc().Find(".container-fluid").ParentsUntil("")
   181  	assertLength(t, sel.Nodes, 8)
   182  }
   183  
   184  func TestParentsUntilRollback(t *testing.T) {
   185  	sel := Doc().Find(".container-fluid")
   186  	sel2 := sel.ParentsUntil("body").End()
   187  	assertEqual(t, sel, sel2)
   188  }
   189  
   190  func TestParentsUntilSelection(t *testing.T) {
   191  	sel := Doc().Find(".container-fluid")
   192  	sel2 := Doc().Find(".pvk-content")
   193  	sel = sel.ParentsUntilSelection(sel2)
   194  	assertLength(t, sel.Nodes, 3)
   195  }
   196  
   197  func TestParentsUntilSelectionRollback(t *testing.T) {
   198  	sel := Doc().Find(".container-fluid")
   199  	sel2 := Doc().Find(".pvk-content")
   200  	sel2 = sel.ParentsUntilSelection(sel2).End()
   201  	assertEqual(t, sel, sel2)
   202  }
   203  
   204  func TestParentsUntilNodes(t *testing.T) {
   205  	sel := Doc().Find(".container-fluid")
   206  	sel2 := Doc().Find(".pvk-content, .hero-unit")
   207  	sel = sel.ParentsUntilNodes(sel2.Nodes...)
   208  	assertLength(t, sel.Nodes, 2)
   209  }
   210  
   211  func TestParentsUntilNodesRollback(t *testing.T) {
   212  	sel := Doc().Find(".container-fluid")
   213  	sel2 := Doc().Find(".pvk-content, .hero-unit")
   214  	sel2 = sel.ParentsUntilNodes(sel2.Nodes...).End()
   215  	assertEqual(t, sel, sel2)
   216  }
   217  
   218  func TestParentsFilteredUntil(t *testing.T) {
   219  	sel := Doc().Find(".container-fluid").ParentsFilteredUntil(".pvk-content", "body")
   220  	assertLength(t, sel.Nodes, 2)
   221  }
   222  
   223  func TestParentsFilteredUntilInvalid(t *testing.T) {
   224  	sel := Doc().Find(".container-fluid").ParentsFilteredUntil("", "")
   225  	assertLength(t, sel.Nodes, 0)
   226  }
   227  
   228  func TestParentsFilteredUntilRollback(t *testing.T) {
   229  	sel := Doc().Find(".container-fluid")
   230  	sel2 := sel.ParentsFilteredUntil(".pvk-content", "body").End()
   231  	assertEqual(t, sel, sel2)
   232  }
   233  
   234  func TestParentsFilteredUntilSelection(t *testing.T) {
   235  	sel := Doc().Find(".container-fluid")
   236  	sel2 := Doc().Find(".row-fluid")
   237  	sel = sel.ParentsFilteredUntilSelection("div", sel2)
   238  	assertLength(t, sel.Nodes, 3)
   239  }
   240  
   241  func TestParentsFilteredUntilSelectionRollback(t *testing.T) {
   242  	sel := Doc().Find(".container-fluid")
   243  	sel2 := Doc().Find(".row-fluid")
   244  	sel2 = sel.ParentsFilteredUntilSelection("div", sel2).End()
   245  	assertEqual(t, sel, sel2)
   246  }
   247  
   248  func TestParentsFilteredUntilNodes(t *testing.T) {
   249  	sel := Doc().Find(".container-fluid")
   250  	sel2 := Doc().Find(".row-fluid")
   251  	sel = sel.ParentsFilteredUntilNodes("body", sel2.Nodes...)
   252  	assertLength(t, sel.Nodes, 1)
   253  }
   254  
   255  func TestParentsFilteredUntilNodesRollback(t *testing.T) {
   256  	sel := Doc().Find(".container-fluid")
   257  	sel2 := Doc().Find(".row-fluid")
   258  	sel2 = sel.ParentsFilteredUntilNodes("body", sel2.Nodes...).End()
   259  	assertEqual(t, sel, sel2)
   260  }
   261  
   262  func TestSiblings(t *testing.T) {
   263  	sel := Doc().Find("h1").Siblings()
   264  	assertLength(t, sel.Nodes, 1)
   265  }
   266  
   267  func TestSiblingsRollback(t *testing.T) {
   268  	sel := Doc().Find("h1")
   269  	sel2 := sel.Siblings().End()
   270  	assertEqual(t, sel, sel2)
   271  }
   272  
   273  func TestSiblings2(t *testing.T) {
   274  	sel := Doc().Find(".pvk-gutter").Siblings()
   275  	assertLength(t, sel.Nodes, 9)
   276  }
   277  
   278  func TestSiblings3(t *testing.T) {
   279  	sel := Doc().Find("body>.container-fluid").Siblings()
   280  	assertLength(t, sel.Nodes, 0)
   281  }
   282  
   283  func TestSiblingsFiltered(t *testing.T) {
   284  	sel := Doc().Find(".pvk-gutter").SiblingsFiltered(".pvk-content")
   285  	assertLength(t, sel.Nodes, 3)
   286  }
   287  
   288  func TestSiblingsFilteredInvalid(t *testing.T) {
   289  	sel := Doc().Find(".pvk-gutter").SiblingsFiltered("")
   290  	assertLength(t, sel.Nodes, 0)
   291  }
   292  
   293  func TestSiblingsFilteredRollback(t *testing.T) {
   294  	sel := Doc().Find(".pvk-gutter")
   295  	sel2 := sel.SiblingsFiltered(".pvk-content").End()
   296  	assertEqual(t, sel, sel2)
   297  }
   298  
   299  func TestNext(t *testing.T) {
   300  	sel := Doc().Find("h1").Next()
   301  	assertLength(t, sel.Nodes, 1)
   302  }
   303  
   304  func TestNextRollback(t *testing.T) {
   305  	sel := Doc().Find("h1")
   306  	sel2 := sel.Next().End()
   307  	assertEqual(t, sel, sel2)
   308  }
   309  
   310  func TestNext2(t *testing.T) {
   311  	sel := Doc().Find(".close").Next()
   312  	assertLength(t, sel.Nodes, 1)
   313  }
   314  
   315  func TestNextNone(t *testing.T) {
   316  	sel := Doc().Find("small").Next()
   317  	assertLength(t, sel.Nodes, 0)
   318  }
   319  
   320  func TestNextFiltered(t *testing.T) {
   321  	sel := Doc().Find(".container-fluid").NextFiltered("div")
   322  	assertLength(t, sel.Nodes, 2)
   323  }
   324  
   325  func TestNextFilteredInvalid(t *testing.T) {
   326  	sel := Doc().Find(".container-fluid").NextFiltered("")
   327  	assertLength(t, sel.Nodes, 0)
   328  }
   329  
   330  func TestNextFilteredRollback(t *testing.T) {
   331  	sel := Doc().Find(".container-fluid")
   332  	sel2 := sel.NextFiltered("div").End()
   333  	assertEqual(t, sel, sel2)
   334  }
   335  
   336  func TestNextFiltered2(t *testing.T) {
   337  	sel := Doc().Find(".container-fluid").NextFiltered("[ng-view]")
   338  	assertLength(t, sel.Nodes, 1)
   339  }
   340  
   341  func TestPrev(t *testing.T) {
   342  	sel := Doc().Find(".red").Prev()
   343  	assertLength(t, sel.Nodes, 1)
   344  	assertClass(t, sel, "green")
   345  }
   346  
   347  func TestPrevRollback(t *testing.T) {
   348  	sel := Doc().Find(".red")
   349  	sel2 := sel.Prev().End()
   350  	assertEqual(t, sel, sel2)
   351  }
   352  
   353  func TestPrev2(t *testing.T) {
   354  	sel := Doc().Find(".row-fluid").Prev()
   355  	assertLength(t, sel.Nodes, 5)
   356  }
   357  
   358  func TestPrevNone(t *testing.T) {
   359  	sel := Doc().Find("h2").Prev()
   360  	assertLength(t, sel.Nodes, 0)
   361  }
   362  
   363  func TestPrevFiltered(t *testing.T) {
   364  	sel := Doc().Find(".row-fluid").PrevFiltered(".row-fluid")
   365  	assertLength(t, sel.Nodes, 5)
   366  }
   367  
   368  func TestPrevFilteredInvalid(t *testing.T) {
   369  	sel := Doc().Find(".row-fluid").PrevFiltered("")
   370  	assertLength(t, sel.Nodes, 0)
   371  }
   372  
   373  func TestPrevFilteredRollback(t *testing.T) {
   374  	sel := Doc().Find(".row-fluid")
   375  	sel2 := sel.PrevFiltered(".row-fluid").End()
   376  	assertEqual(t, sel, sel2)
   377  }
   378  
   379  func TestNextAll(t *testing.T) {
   380  	sel := Doc().Find("#cf2 div:nth-child(1)").NextAll()
   381  	assertLength(t, sel.Nodes, 3)
   382  }
   383  
   384  func TestNextAllRollback(t *testing.T) {
   385  	sel := Doc().Find("#cf2 div:nth-child(1)")
   386  	sel2 := sel.NextAll().End()
   387  	assertEqual(t, sel, sel2)
   388  }
   389  
   390  func TestNextAll2(t *testing.T) {
   391  	sel := Doc().Find("div[ng-cloak]").NextAll()
   392  	assertLength(t, sel.Nodes, 1)
   393  }
   394  
   395  func TestNextAllNone(t *testing.T) {
   396  	sel := Doc().Find(".footer").NextAll()
   397  	assertLength(t, sel.Nodes, 0)
   398  }
   399  
   400  func TestNextAllFiltered(t *testing.T) {
   401  	sel := Doc().Find("#cf2 .row-fluid").NextAllFiltered("[ng-cloak]")
   402  	assertLength(t, sel.Nodes, 2)
   403  }
   404  
   405  func TestNextAllFilteredInvalid(t *testing.T) {
   406  	sel := Doc().Find("#cf2 .row-fluid").NextAllFiltered("")
   407  	assertLength(t, sel.Nodes, 0)
   408  }
   409  
   410  func TestNextAllFilteredRollback(t *testing.T) {
   411  	sel := Doc().Find("#cf2 .row-fluid")
   412  	sel2 := sel.NextAllFiltered("[ng-cloak]").End()
   413  	assertEqual(t, sel, sel2)
   414  }
   415  
   416  func TestNextAllFiltered2(t *testing.T) {
   417  	sel := Doc().Find(".close").NextAllFiltered("h4")
   418  	assertLength(t, sel.Nodes, 1)
   419  }
   420  
   421  func TestPrevAll(t *testing.T) {
   422  	sel := Doc().Find("[ng-view]").PrevAll()
   423  	assertLength(t, sel.Nodes, 2)
   424  }
   425  
   426  func TestPrevAllOrder(t *testing.T) {
   427  	sel := Doc().Find("[ng-view]").PrevAll()
   428  	assertLength(t, sel.Nodes, 2)
   429  	assertSelectionIs(t, sel, "#cf4", "#cf3")
   430  }
   431  
   432  func TestPrevAllRollback(t *testing.T) {
   433  	sel := Doc().Find("[ng-view]")
   434  	sel2 := sel.PrevAll().End()
   435  	assertEqual(t, sel, sel2)
   436  }
   437  
   438  func TestPrevAll2(t *testing.T) {
   439  	sel := Doc().Find(".pvk-gutter").PrevAll()
   440  	assertLength(t, sel.Nodes, 6)
   441  }
   442  
   443  func TestPrevAllFiltered(t *testing.T) {
   444  	sel := Doc().Find(".pvk-gutter").PrevAllFiltered(".pvk-content")
   445  	assertLength(t, sel.Nodes, 3)
   446  }
   447  
   448  func TestPrevAllFilteredInvalid(t *testing.T) {
   449  	sel := Doc().Find(".pvk-gutter").PrevAllFiltered("")
   450  	assertLength(t, sel.Nodes, 0)
   451  }
   452  
   453  func TestPrevAllFilteredRollback(t *testing.T) {
   454  	sel := Doc().Find(".pvk-gutter")
   455  	sel2 := sel.PrevAllFiltered(".pvk-content").End()
   456  	assertEqual(t, sel, sel2)
   457  }
   458  
   459  func TestNextUntil(t *testing.T) {
   460  	sel := Doc().Find(".alert a").NextUntil("p")
   461  	assertLength(t, sel.Nodes, 1)
   462  	assertSelectionIs(t, sel, "h4")
   463  }
   464  
   465  func TestNextUntilInvalid(t *testing.T) {
   466  	sel := Doc().Find(".alert a").NextUntil("")
   467  	assertLength(t, sel.Nodes, 2)
   468  }
   469  
   470  func TestNextUntil2(t *testing.T) {
   471  	sel := Doc().Find("#cf2-1").NextUntil("[ng-cloak]")
   472  	assertLength(t, sel.Nodes, 1)
   473  	assertSelectionIs(t, sel, "#cf2-2")
   474  }
   475  
   476  func TestNextUntilOrder(t *testing.T) {
   477  	sel := Doc().Find("#cf2-1").NextUntil("#cf2-4")
   478  	assertLength(t, sel.Nodes, 2)
   479  	assertSelectionIs(t, sel, "#cf2-2", "#cf2-3")
   480  }
   481  
   482  func TestNextUntilRollback(t *testing.T) {
   483  	sel := Doc().Find("#cf2-1")
   484  	sel2 := sel.PrevUntil("#cf2-4").End()
   485  	assertEqual(t, sel, sel2)
   486  }
   487  
   488  func TestNextUntilSelection(t *testing.T) {
   489  	sel := Doc2().Find("#n2")
   490  	sel2 := Doc2().Find("#n4")
   491  	sel2 = sel.NextUntilSelection(sel2)
   492  	assertLength(t, sel2.Nodes, 1)
   493  	assertSelectionIs(t, sel2, "#n3")
   494  }
   495  
   496  func TestNextUntilSelectionRollback(t *testing.T) {
   497  	sel := Doc2().Find("#n2")
   498  	sel2 := Doc2().Find("#n4")
   499  	sel2 = sel.NextUntilSelection(sel2).End()
   500  	assertEqual(t, sel, sel2)
   501  }
   502  
   503  func TestNextUntilNodes(t *testing.T) {
   504  	sel := Doc2().Find("#n2")
   505  	sel2 := Doc2().Find("#n5")
   506  	sel2 = sel.NextUntilNodes(sel2.Nodes...)
   507  	assertLength(t, sel2.Nodes, 2)
   508  	assertSelectionIs(t, sel2, "#n3", "#n4")
   509  }
   510  
   511  func TestNextUntilNodesRollback(t *testing.T) {
   512  	sel := Doc2().Find("#n2")
   513  	sel2 := Doc2().Find("#n5")
   514  	sel2 = sel.NextUntilNodes(sel2.Nodes...).End()
   515  	assertEqual(t, sel, sel2)
   516  }
   517  
   518  func TestPrevUntil(t *testing.T) {
   519  	sel := Doc().Find(".alert p").PrevUntil("a")
   520  	assertLength(t, sel.Nodes, 1)
   521  	assertSelectionIs(t, sel, "h4")
   522  }
   523  
   524  func TestPrevUntilInvalid(t *testing.T) {
   525  	sel := Doc().Find(".alert p").PrevUntil("")
   526  	assertLength(t, sel.Nodes, 2)
   527  }
   528  
   529  func TestPrevUntil2(t *testing.T) {
   530  	sel := Doc().Find("[ng-cloak]").PrevUntil(":not([ng-cloak])")
   531  	assertLength(t, sel.Nodes, 1)
   532  	assertSelectionIs(t, sel, "[ng-cloak]")
   533  }
   534  
   535  func TestPrevUntilOrder(t *testing.T) {
   536  	sel := Doc().Find("#cf2-4").PrevUntil("#cf2-1")
   537  	assertLength(t, sel.Nodes, 2)
   538  	assertSelectionIs(t, sel, "#cf2-3", "#cf2-2")
   539  }
   540  
   541  func TestPrevUntilRollback(t *testing.T) {
   542  	sel := Doc().Find("#cf2-4")
   543  	sel2 := sel.PrevUntil("#cf2-1").End()
   544  	assertEqual(t, sel, sel2)
   545  }
   546  
   547  func TestPrevUntilSelection(t *testing.T) {
   548  	sel := Doc2().Find("#n4")
   549  	sel2 := Doc2().Find("#n2")
   550  	sel2 = sel.PrevUntilSelection(sel2)
   551  	assertLength(t, sel2.Nodes, 1)
   552  	assertSelectionIs(t, sel2, "#n3")
   553  }
   554  
   555  func TestPrevUntilSelectionRollback(t *testing.T) {
   556  	sel := Doc2().Find("#n4")
   557  	sel2 := Doc2().Find("#n2")
   558  	sel2 = sel.PrevUntilSelection(sel2).End()
   559  	assertEqual(t, sel, sel2)
   560  }
   561  
   562  func TestPrevUntilNodes(t *testing.T) {
   563  	sel := Doc2().Find("#n5")
   564  	sel2 := Doc2().Find("#n2")
   565  	sel2 = sel.PrevUntilNodes(sel2.Nodes...)
   566  	assertLength(t, sel2.Nodes, 2)
   567  	assertSelectionIs(t, sel2, "#n4", "#n3")
   568  }
   569  
   570  func TestPrevUntilNodesRollback(t *testing.T) {
   571  	sel := Doc2().Find("#n5")
   572  	sel2 := Doc2().Find("#n2")
   573  	sel2 = sel.PrevUntilNodes(sel2.Nodes...).End()
   574  	assertEqual(t, sel, sel2)
   575  }
   576  
   577  func TestNextFilteredUntil(t *testing.T) {
   578  	sel := Doc2().Find(".two").NextFilteredUntil(".even", ".six")
   579  	assertLength(t, sel.Nodes, 4)
   580  	assertSelectionIs(t, sel, "#n3", "#n5", "#nf3", "#nf5")
   581  }
   582  
   583  func TestNextFilteredUntilInvalid(t *testing.T) {
   584  	sel := Doc2().Find(".two").NextFilteredUntil("", "")
   585  	assertLength(t, sel.Nodes, 0)
   586  }
   587  
   588  func TestNextFilteredUntilRollback(t *testing.T) {
   589  	sel := Doc2().Find(".two")
   590  	sel2 := sel.NextFilteredUntil(".even", ".six").End()
   591  	assertEqual(t, sel, sel2)
   592  }
   593  
   594  func TestNextFilteredUntilSelection(t *testing.T) {
   595  	sel := Doc2().Find(".even")
   596  	sel2 := Doc2().Find(".five")
   597  	sel = sel.NextFilteredUntilSelection(".even", sel2)
   598  	assertLength(t, sel.Nodes, 2)
   599  	assertSelectionIs(t, sel, "#n3", "#nf3")
   600  }
   601  
   602  func TestNextFilteredUntilSelectionRollback(t *testing.T) {
   603  	sel := Doc2().Find(".even")
   604  	sel2 := Doc2().Find(".five")
   605  	sel3 := sel.NextFilteredUntilSelection(".even", sel2).End()
   606  	assertEqual(t, sel, sel3)
   607  }
   608  
   609  func TestNextFilteredUntilNodes(t *testing.T) {
   610  	sel := Doc2().Find(".even")
   611  	sel2 := Doc2().Find(".four")
   612  	sel = sel.NextFilteredUntilNodes(".odd", sel2.Nodes...)
   613  	assertLength(t, sel.Nodes, 4)
   614  	assertSelectionIs(t, sel, "#n2", "#n6", "#nf2", "#nf6")
   615  }
   616  
   617  func TestNextFilteredUntilNodesRollback(t *testing.T) {
   618  	sel := Doc2().Find(".even")
   619  	sel2 := Doc2().Find(".four")
   620  	sel3 := sel.NextFilteredUntilNodes(".odd", sel2.Nodes...).End()
   621  	assertEqual(t, sel, sel3)
   622  }
   623  
   624  func TestPrevFilteredUntil(t *testing.T) {
   625  	sel := Doc2().Find(".five").PrevFilteredUntil(".odd", ".one")
   626  	assertLength(t, sel.Nodes, 4)
   627  	assertSelectionIs(t, sel, "#n4", "#n2", "#nf4", "#nf2")
   628  }
   629  
   630  func TestPrevFilteredUntilInvalid(t *testing.T) {
   631  	sel := Doc2().Find(".five").PrevFilteredUntil("", "")
   632  	assertLength(t, sel.Nodes, 0)
   633  }
   634  
   635  func TestPrevFilteredUntilRollback(t *testing.T) {
   636  	sel := Doc2().Find(".four")
   637  	sel2 := sel.PrevFilteredUntil(".odd", ".one").End()
   638  	assertEqual(t, sel, sel2)
   639  }
   640  
   641  func TestPrevFilteredUntilSelection(t *testing.T) {
   642  	sel := Doc2().Find(".odd")
   643  	sel2 := Doc2().Find(".two")
   644  	sel = sel.PrevFilteredUntilSelection(".odd", sel2)
   645  	assertLength(t, sel.Nodes, 2)
   646  	assertSelectionIs(t, sel, "#n4", "#nf4")
   647  }
   648  
   649  func TestPrevFilteredUntilSelectionRollback(t *testing.T) {
   650  	sel := Doc2().Find(".even")
   651  	sel2 := Doc2().Find(".five")
   652  	sel3 := sel.PrevFilteredUntilSelection(".even", sel2).End()
   653  	assertEqual(t, sel, sel3)
   654  }
   655  
   656  func TestPrevFilteredUntilNodes(t *testing.T) {
   657  	sel := Doc2().Find(".even")
   658  	sel2 := Doc2().Find(".four")
   659  	sel = sel.PrevFilteredUntilNodes(".odd", sel2.Nodes...)
   660  	assertLength(t, sel.Nodes, 2)
   661  	assertSelectionIs(t, sel, "#n2", "#nf2")
   662  }
   663  
   664  func TestPrevFilteredUntilNodesRollback(t *testing.T) {
   665  	sel := Doc2().Find(".even")
   666  	sel2 := Doc2().Find(".four")
   667  	sel3 := sel.PrevFilteredUntilNodes(".odd", sel2.Nodes...).End()
   668  	assertEqual(t, sel, sel3)
   669  }
   670  
   671  func TestClosestItself(t *testing.T) {
   672  	sel := Doc2().Find(".three")
   673  	sel2 := sel.Closest(".row")
   674  	assertLength(t, sel2.Nodes, sel.Length())
   675  	assertSelectionIs(t, sel2, "#n3", "#nf3")
   676  }
   677  
   678  func TestClosestNoDupes(t *testing.T) {
   679  	sel := Doc().Find(".span12")
   680  	sel2 := sel.Closest(".pvk-content")
   681  	assertLength(t, sel2.Nodes, 1)
   682  	assertClass(t, sel2, "pvk-content")
   683  }
   684  
   685  func TestClosestNone(t *testing.T) {
   686  	sel := Doc().Find("h4")
   687  	sel2 := sel.Closest("a")
   688  	assertLength(t, sel2.Nodes, 0)
   689  }
   690  
   691  func TestClosestInvalid(t *testing.T) {
   692  	sel := Doc().Find("h4")
   693  	sel2 := sel.Closest("")
   694  	assertLength(t, sel2.Nodes, 0)
   695  }
   696  
   697  func TestClosestMany(t *testing.T) {
   698  	sel := Doc().Find(".container-fluid")
   699  	sel2 := sel.Closest(".pvk-content")
   700  	assertLength(t, sel2.Nodes, 2)
   701  	assertSelectionIs(t, sel2, "#pc1", "#pc2")
   702  }
   703  
   704  func TestClosestRollback(t *testing.T) {
   705  	sel := Doc().Find(".container-fluid")
   706  	sel2 := sel.Closest(".pvk-content").End()
   707  	assertEqual(t, sel, sel2)
   708  }
   709  
   710  func TestClosestSelectionItself(t *testing.T) {
   711  	sel := Doc2().Find(".three")
   712  	sel2 := sel.ClosestSelection(Doc2().Find(".row"))
   713  	assertLength(t, sel2.Nodes, sel.Length())
   714  }
   715  
   716  func TestClosestSelectionNoDupes(t *testing.T) {
   717  	sel := Doc().Find(".span12")
   718  	sel2 := sel.ClosestSelection(Doc().Find(".pvk-content"))
   719  	assertLength(t, sel2.Nodes, 1)
   720  	assertClass(t, sel2, "pvk-content")
   721  }
   722  
   723  func TestClosestSelectionNone(t *testing.T) {
   724  	sel := Doc().Find("h4")
   725  	sel2 := sel.ClosestSelection(Doc().Find("a"))
   726  	assertLength(t, sel2.Nodes, 0)
   727  }
   728  
   729  func TestClosestSelectionMany(t *testing.T) {
   730  	sel := Doc().Find(".container-fluid")
   731  	sel2 := sel.ClosestSelection(Doc().Find(".pvk-content"))
   732  	assertLength(t, sel2.Nodes, 2)
   733  	assertSelectionIs(t, sel2, "#pc1", "#pc2")
   734  }
   735  
   736  func TestClosestSelectionRollback(t *testing.T) {
   737  	sel := Doc().Find(".container-fluid")
   738  	sel2 := sel.ClosestSelection(Doc().Find(".pvk-content")).End()
   739  	assertEqual(t, sel, sel2)
   740  }
   741  
   742  func TestClosestNodesItself(t *testing.T) {
   743  	sel := Doc2().Find(".three")
   744  	sel2 := sel.ClosestNodes(Doc2().Find(".row").Nodes...)
   745  	assertLength(t, sel2.Nodes, sel.Length())
   746  }
   747  
   748  func TestClosestNodesNoDupes(t *testing.T) {
   749  	sel := Doc().Find(".span12")
   750  	sel2 := sel.ClosestNodes(Doc().Find(".pvk-content").Nodes...)
   751  	assertLength(t, sel2.Nodes, 1)
   752  	assertClass(t, sel2, "pvk-content")
   753  }
   754  
   755  func TestClosestNodesNone(t *testing.T) {
   756  	sel := Doc().Find("h4")
   757  	sel2 := sel.ClosestNodes(Doc().Find("a").Nodes...)
   758  	assertLength(t, sel2.Nodes, 0)
   759  }
   760  
   761  func TestClosestNodesMany(t *testing.T) {
   762  	sel := Doc().Find(".container-fluid")
   763  	sel2 := sel.ClosestNodes(Doc().Find(".pvk-content").Nodes...)
   764  	assertLength(t, sel2.Nodes, 2)
   765  	assertSelectionIs(t, sel2, "#pc1", "#pc2")
   766  }
   767  
   768  func TestClosestNodesRollback(t *testing.T) {
   769  	sel := Doc().Find(".container-fluid")
   770  	sel2 := sel.ClosestNodes(Doc().Find(".pvk-content").Nodes...).End()
   771  	assertEqual(t, sel, sel2)
   772  }
   773  
   774  func TestIssue26(t *testing.T) {
   775  	img1 := `<img src="assets/images/gallery/thumb-1.jpg" alt="150x150" />`
   776  	img2 := `<img alt="150x150" src="assets/images/gallery/thumb-1.jpg" />`
   777  	cases := []struct {
   778  		s string
   779  		l int
   780  	}{
   781  		{s: img1 + img2, l: 2},
   782  		{s: img1, l: 1},
   783  		{s: img2, l: 1},
   784  	}
   785  	for _, c := range cases {
   786  		doc, err := NewDocumentFromReader(strings.NewReader(c.s))
   787  		if err != nil {
   788  			t.Fatal(err)
   789  		}
   790  		sel := doc.Find("img[src]")
   791  		assertLength(t, sel.Nodes, c.l)
   792  	}
   793  }
   794  

View as plain text