...

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

Documentation: github.com/PuerkitoBio/goquery

     1  package goquery
     2  
     3  import (
     4  	"log"
     5  	"testing"
     6  )
     7  
     8  const (
     9  	wrapHtml = "<div id=\"ins\">test string<div><p><em><b></b></em></p></div></div>"
    10  )
    11  
    12  func TestAfter(t *testing.T) {
    13  	doc := Doc2Clone()
    14  	doc.Find("#main").After("#nf6")
    15  
    16  	assertLength(t, doc.Find("#main #nf6").Nodes, 0)
    17  	assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
    18  	assertLength(t, doc.Find("#main + #nf6").Nodes, 1)
    19  	printSel(t, doc.Selection)
    20  }
    21  
    22  func TestAfterMany(t *testing.T) {
    23  	doc := Doc2Clone()
    24  	doc.Find(".one").After("#nf6")
    25  
    26  	assertLength(t, doc.Find("#foot #nf6").Nodes, 1)
    27  	assertLength(t, doc.Find("#main #nf6").Nodes, 1)
    28  	assertLength(t, doc.Find(".one + #nf6").Nodes, 2)
    29  	printSel(t, doc.Selection)
    30  }
    31  
    32  func TestAfterWithRemoved(t *testing.T) {
    33  	doc := Doc2Clone()
    34  	s := doc.Find("#main").Remove()
    35  	s.After("#nf6")
    36  
    37  	assertLength(t, s.Find("#nf6").Nodes, 0)
    38  	assertLength(t, doc.Find("#nf6").Nodes, 0)
    39  	printSel(t, doc.Selection)
    40  }
    41  
    42  func TestAfterSelection(t *testing.T) {
    43  	doc := Doc2Clone()
    44  	doc.Find("#main").AfterSelection(doc.Find("#nf1, #nf2"))
    45  
    46  	assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)
    47  	assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)
    48  	assertLength(t, doc.Find("#main + #nf1, #nf1 + #nf2").Nodes, 2)
    49  	printSel(t, doc.Selection)
    50  }
    51  
    52  func TestAfterHtml(t *testing.T) {
    53  	doc := Doc2Clone()
    54  	doc.Find("#main").AfterHtml("<strong>new node</strong>")
    55  
    56  	assertLength(t, doc.Find("#main + strong").Nodes, 1)
    57  	printSel(t, doc.Selection)
    58  }
    59  
    60  func TestAfterHtmlContext(t *testing.T) {
    61  	doc := loadString(t, `
    62  		<html>
    63  			<body>
    64  				<table>
    65  					<tr>
    66  						<td>Before1</td>
    67  					</tr>
    68  					<tr>
    69  						<td>Before2</td>
    70  					</tr>
    71  				</table>
    72  			</body>
    73  		</html>`)
    74  	doc.Find("table tr td").AfterHtml("<td class='c1'>Test</td><td class='c2'>Again</td>")
    75  	assertLength(t, doc.Find("table tr td").Nodes, 6)
    76  	assertClass(t, doc.Find("table tr td").Last(), "c2")
    77  	printSel(t, doc.Selection)
    78  }
    79  
    80  func TestAppend(t *testing.T) {
    81  	doc := Doc2Clone()
    82  	doc.Find("#main").Append("#nf6")
    83  
    84  	assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
    85  	assertLength(t, doc.Find("#main #nf6").Nodes, 1)
    86  	printSel(t, doc.Selection)
    87  }
    88  
    89  func TestAppendBody(t *testing.T) {
    90  	doc := Doc2Clone()
    91  	doc.Find("body").Append("#nf6")
    92  
    93  	assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
    94  	assertLength(t, doc.Find("#main #nf6").Nodes, 0)
    95  	assertLength(t, doc.Find("body > #nf6").Nodes, 1)
    96  	printSel(t, doc.Selection)
    97  }
    98  
    99  func TestAppendSelection(t *testing.T) {
   100  	doc := Doc2Clone()
   101  	doc.Find("#main").AppendSelection(doc.Find("#nf1, #nf2"))
   102  
   103  	assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
   104  	assertLength(t, doc.Find("#foot #nf2").Nodes, 0)
   105  	assertLength(t, doc.Find("#main #nf1").Nodes, 1)
   106  	assertLength(t, doc.Find("#main #nf2").Nodes, 1)
   107  	printSel(t, doc.Selection)
   108  }
   109  
   110  func TestAppendSelectionExisting(t *testing.T) {
   111  	doc := Doc2Clone()
   112  	doc.Find("#main").AppendSelection(doc.Find("#n1, #n2"))
   113  
   114  	assertClass(t, doc.Find("#main :nth-child(1)"), "three")
   115  	assertClass(t, doc.Find("#main :nth-child(5)"), "one")
   116  	assertClass(t, doc.Find("#main :nth-child(6)"), "two")
   117  	printSel(t, doc.Selection)
   118  }
   119  
   120  func TestAppendClone(t *testing.T) {
   121  	doc := Doc2Clone()
   122  	doc.Find("#n1").AppendSelection(doc.Find("#nf1").Clone())
   123  
   124  	assertLength(t, doc.Find("#foot #nf1").Nodes, 1)
   125  	assertLength(t, doc.Find("#main #nf1").Nodes, 1)
   126  	printSel(t, doc.Selection)
   127  }
   128  
   129  func TestAppendHtml(t *testing.T) {
   130  	doc := Doc2Clone()
   131  	doc.Find("div").AppendHtml("<strong>new node</strong>")
   132  
   133  	assertLength(t, doc.Find("strong").Nodes, 14)
   134  	printSel(t, doc.Selection)
   135  }
   136  
   137  func TestAppendHtmlContext(t *testing.T) {
   138  	doc := loadString(t, `
   139  		<html>
   140  			<body>
   141  				<table>
   142  					<tr>
   143  						<td>Before1</td>
   144  					</tr>
   145  					<tr>
   146  						<td>Before2</td>
   147  					</tr>
   148  				</table>
   149  			</body>
   150  		</html>`)
   151  	doc.Find("table tr").AppendHtml("<td class='c1'>new1</td><td class='c2'>new2</td>")
   152  
   153  	assertLength(t, doc.Find("table td").Nodes, 6)
   154  	assertClass(t, doc.Find("table td").Last(), "c2")
   155  	printSel(t, doc.Selection)
   156  }
   157  
   158  func TestBefore(t *testing.T) {
   159  	doc := Doc2Clone()
   160  	doc.Find("#main").Before("#nf6")
   161  
   162  	assertLength(t, doc.Find("#main #nf6").Nodes, 0)
   163  	assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
   164  	assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)
   165  	printSel(t, doc.Selection)
   166  }
   167  
   168  func TestBeforeWithRemoved(t *testing.T) {
   169  	doc := Doc2Clone()
   170  	s := doc.Find("#main").Remove()
   171  	s.Before("#nf6")
   172  
   173  	assertLength(t, s.Find("#nf6").Nodes, 0)
   174  	assertLength(t, doc.Find("#nf6").Nodes, 0)
   175  	printSel(t, doc.Selection)
   176  }
   177  
   178  func TestBeforeSelection(t *testing.T) {
   179  	doc := Doc2Clone()
   180  	doc.Find("#main").BeforeSelection(doc.Find("#nf1, #nf2"))
   181  
   182  	assertLength(t, doc.Find("#main #nf1, #main #nf2").Nodes, 0)
   183  	assertLength(t, doc.Find("#foot #nf1, #foot #nf2").Nodes, 0)
   184  	assertLength(t, doc.Find("body > #nf1:first-child, #nf1 + #nf2").Nodes, 2)
   185  	printSel(t, doc.Selection)
   186  }
   187  
   188  func TestBeforeHtml(t *testing.T) {
   189  	doc := Doc2Clone()
   190  	doc.Find("#main").BeforeHtml("<strong>new node</strong>")
   191  
   192  	assertLength(t, doc.Find("body > strong:first-child").Nodes, 1)
   193  	printSel(t, doc.Selection)
   194  }
   195  
   196  func TestBeforeHtmlContext(t *testing.T) {
   197  	doc := loadString(t, `
   198  		<html>
   199  			<body>
   200  				<table>
   201  					<tr>
   202  						<td>Before1</td>
   203  					</tr>
   204  					<tr>
   205  						<td>Before2</td>
   206  					</tr>
   207  				</table>
   208  			</body>
   209  		</html>`)
   210  	doc.Find("table tr td:first-child").BeforeHtml("<td class='c1'>new1</td><td class='c2'>new2</td>")
   211  
   212  	assertLength(t, doc.Find("table td").Nodes, 6)
   213  	assertClass(t, doc.Find("table td").First(), "c1")
   214  	printSel(t, doc.Selection)
   215  }
   216  
   217  func TestEmpty(t *testing.T) {
   218  	doc := Doc2Clone()
   219  	s := doc.Find("#main").Empty()
   220  
   221  	assertLength(t, doc.Find("#main").Children().Nodes, 0)
   222  	assertLength(t, s.Filter("div").Nodes, 6)
   223  	printSel(t, doc.Selection)
   224  }
   225  
   226  func TestPrepend(t *testing.T) {
   227  	doc := Doc2Clone()
   228  	doc.Find("#main").Prepend("#nf6")
   229  
   230  	assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
   231  	assertLength(t, doc.Find("#main #nf6:first-child").Nodes, 1)
   232  	printSel(t, doc.Selection)
   233  }
   234  
   235  func TestPrependBody(t *testing.T) {
   236  	doc := Doc2Clone()
   237  	doc.Find("body").Prepend("#nf6")
   238  
   239  	assertLength(t, doc.Find("#foot #nf6").Nodes, 0)
   240  	assertLength(t, doc.Find("#main #nf6").Nodes, 0)
   241  	assertLength(t, doc.Find("body > #nf6:first-child").Nodes, 1)
   242  	printSel(t, doc.Selection)
   243  }
   244  
   245  func TestPrependSelection(t *testing.T) {
   246  	doc := Doc2Clone()
   247  	doc.Find("#main").PrependSelection(doc.Find("#nf1, #nf2"))
   248  
   249  	assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
   250  	assertLength(t, doc.Find("#foot #nf2").Nodes, 0)
   251  	assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)
   252  	assertLength(t, doc.Find("#main #nf2:nth-child(2)").Nodes, 1)
   253  	printSel(t, doc.Selection)
   254  }
   255  
   256  func TestPrependSelectionExisting(t *testing.T) {
   257  	doc := Doc2Clone()
   258  	doc.Find("#main").PrependSelection(doc.Find("#n5, #n6"))
   259  
   260  	assertClass(t, doc.Find("#main :nth-child(1)"), "five")
   261  	assertClass(t, doc.Find("#main :nth-child(2)"), "six")
   262  	assertClass(t, doc.Find("#main :nth-child(5)"), "three")
   263  	assertClass(t, doc.Find("#main :nth-child(6)"), "four")
   264  	printSel(t, doc.Selection)
   265  }
   266  
   267  func TestPrependClone(t *testing.T) {
   268  	doc := Doc2Clone()
   269  	doc.Find("#n1").PrependSelection(doc.Find("#nf1").Clone())
   270  
   271  	assertLength(t, doc.Find("#foot #nf1:first-child").Nodes, 1)
   272  	assertLength(t, doc.Find("#main #nf1:first-child").Nodes, 1)
   273  	printSel(t, doc.Selection)
   274  }
   275  
   276  func TestPrependHtml(t *testing.T) {
   277  	doc := Doc2Clone()
   278  	doc.Find("div").PrependHtml("<strong>new node</strong>")
   279  
   280  	assertLength(t, doc.Find("strong:first-child").Nodes, 14)
   281  	printSel(t, doc.Selection)
   282  }
   283  
   284  func TestPrependHtmlContext(t *testing.T) {
   285  	doc := loadString(t, `
   286  		<html>
   287  			<body>
   288  				<table>
   289  					<tr>
   290  						<td>Before1</td>
   291  					</tr>
   292  					<tr>
   293  						<td>Before2</td>
   294  					</tr>
   295  				</table>
   296  			</body>
   297  		</html>`)
   298  	doc.Find("table tr").PrependHtml("<td class='c1'>new node</td><td class='c2'>other new node</td>")
   299  
   300  	assertLength(t, doc.Find("table td").Nodes, 6)
   301  	assertClass(t, doc.Find("table tr td").First(), "c1")
   302  	printSel(t, doc.Selection)
   303  }
   304  
   305  func TestRemove(t *testing.T) {
   306  	doc := Doc2Clone()
   307  	doc.Find("#nf1").Remove()
   308  
   309  	assertLength(t, doc.Find("#foot #nf1").Nodes, 0)
   310  	printSel(t, doc.Selection)
   311  }
   312  
   313  func TestRemoveAll(t *testing.T) {
   314  	doc := Doc2Clone()
   315  	doc.Find("*").Remove()
   316  
   317  	assertLength(t, doc.Find("*").Nodes, 0)
   318  	printSel(t, doc.Selection)
   319  }
   320  
   321  func TestRemoveRoot(t *testing.T) {
   322  	doc := Doc2Clone()
   323  	doc.Find("html").Remove()
   324  
   325  	assertLength(t, doc.Find("html").Nodes, 0)
   326  	printSel(t, doc.Selection)
   327  }
   328  
   329  func TestRemoveFiltered(t *testing.T) {
   330  	doc := Doc2Clone()
   331  	nf6 := doc.Find("#nf6")
   332  	s := doc.Find("div").RemoveFiltered("#nf6")
   333  
   334  	assertLength(t, doc.Find("#nf6").Nodes, 0)
   335  	assertLength(t, s.Nodes, 1)
   336  	if nf6.Nodes[0] != s.Nodes[0] {
   337  		t.Error("Removed node does not match original")
   338  	}
   339  	printSel(t, doc.Selection)
   340  }
   341  
   342  func TestReplaceWith(t *testing.T) {
   343  	doc := Doc2Clone()
   344  
   345  	doc.Find("#nf6").ReplaceWith("#main")
   346  	assertLength(t, doc.Find("#foot #main:last-child").Nodes, 1)
   347  	printSel(t, doc.Selection)
   348  
   349  	doc.Find("#foot").ReplaceWith("#main")
   350  	assertLength(t, doc.Find("#foot").Nodes, 0)
   351  	assertLength(t, doc.Find("#main").Nodes, 1)
   352  
   353  	printSel(t, doc.Selection)
   354  }
   355  
   356  func TestReplaceWithHtml(t *testing.T) {
   357  	doc := Doc2Clone()
   358  	doc.Find("#main, #foot").ReplaceWithHtml("<div id=\"replace\"></div>")
   359  
   360  	assertLength(t, doc.Find("#replace").Nodes, 2)
   361  
   362  	printSel(t, doc.Selection)
   363  }
   364  
   365  func TestReplaceWithHtmlContext(t *testing.T) {
   366  	doc := loadString(t, `
   367  		<html>
   368  			<body>
   369  				<table>
   370  					<tr>
   371  						<th>Before1</th>
   372  					</tr>
   373  					<tr>
   374  						<th>Before2</th>
   375  					</tr>
   376  				</table>
   377  			</body>
   378  		</html>`)
   379  	doc.Find("table th").ReplaceWithHtml("<td class='c1'>Test</td><td class='c2'>Replace</td>")
   380  
   381  	assertLength(t, doc.Find("table th").Nodes, 0)
   382  	assertLength(t, doc.Find("table tr td").Nodes, 4)
   383  	assertClass(t, doc.Find("table tr td").First(), "c1")
   384  	printSel(t, doc.Selection)
   385  }
   386  
   387  func TestSetHtml(t *testing.T) {
   388  	doc := Doc2Clone()
   389  	q := doc.Find("#main, #foot")
   390  	q.SetHtml(`<div id="replace">test</div>`)
   391  
   392  	assertLength(t, doc.Find("#replace").Nodes, 2)
   393  	assertLength(t, doc.Find("#main, #foot").Nodes, 2)
   394  
   395  	if q.Text() != "testtest" {
   396  		t.Errorf("Expected text to be %v, found %v", "testtest", q.Text())
   397  	}
   398  
   399  	printSel(t, doc.Selection)
   400  }
   401  
   402  func TestSetHtmlNoMatch(t *testing.T) {
   403  	doc := Doc2Clone()
   404  	q := doc.Find("#notthere")
   405  	q.SetHtml(`<div id="replace">test</div>`)
   406  
   407  	assertLength(t, doc.Find("#replace").Nodes, 0)
   408  
   409  	printSel(t, doc.Selection)
   410  }
   411  
   412  func TestSetHtmlEmpty(t *testing.T) {
   413  	doc := Doc2Clone()
   414  	q := doc.Find("#main")
   415  	q.SetHtml(``)
   416  
   417  	assertLength(t, doc.Find("#main").Nodes, 1)
   418  	assertLength(t, doc.Find("#main").Children().Nodes, 0)
   419  	printSel(t, doc.Selection)
   420  }
   421  
   422  func TestSetHtmlContext(t *testing.T) {
   423  	doc := loadString(t, `
   424  		<html>
   425  			<body>
   426  				<table>
   427  					<tr>
   428  						<th>Before1</th>
   429  					</tr>
   430  					<tr>
   431  						<th>Before2</th>
   432  					</tr>
   433  				</table>
   434  			</body>
   435  		</html>`)
   436  	doc.Find("table tr").SetHtml("<td class='c1'>Test</td><td class='c2'>Again</td>")
   437  
   438  	assertLength(t, doc.Find("table th").Nodes, 0)
   439  	assertLength(t, doc.Find("table td").Nodes, 4)
   440  	assertLength(t, doc.Find("table tr").Nodes, 2)
   441  	printSel(t, doc.Selection)
   442  }
   443  
   444  func TestSetText(t *testing.T) {
   445  	doc := Doc2Clone()
   446  	q := doc.Find("#main, #foot")
   447  	repl := "<div id=\"replace\">test</div>"
   448  	q.SetText(repl)
   449  
   450  	assertLength(t, doc.Find("#replace").Nodes, 0)
   451  	assertLength(t, doc.Find("#main, #foot").Nodes, 2)
   452  
   453  	if q.Text() != (repl + repl) {
   454  		t.Errorf("Expected text to be %v, found %v", (repl + repl), q.Text())
   455  	}
   456  
   457  	h, err := q.Html()
   458  	if err != nil {
   459  		t.Errorf("Error: %v", err)
   460  	}
   461  	esc := "&lt;div id=&#34;replace&#34;&gt;test&lt;/div&gt;"
   462  	if h != esc {
   463  		t.Errorf("Expected html to be %v, found %v", esc, h)
   464  	}
   465  
   466  	printSel(t, doc.Selection)
   467  }
   468  
   469  func TestReplaceWithSelection(t *testing.T) {
   470  	doc := Doc2Clone()
   471  	sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5"))
   472  
   473  	assertSelectionIs(t, sel, "#nf6")
   474  	assertLength(t, doc.Find("#nf6").Nodes, 0)
   475  	assertLength(t, doc.Find("#nf5").Nodes, 1)
   476  
   477  	printSel(t, doc.Selection)
   478  }
   479  
   480  func TestUnwrap(t *testing.T) {
   481  	doc := Doc2Clone()
   482  
   483  	doc.Find("#nf5").Unwrap()
   484  	assertLength(t, doc.Find("#foot").Nodes, 0)
   485  	assertLength(t, doc.Find("body > #nf1").Nodes, 1)
   486  	assertLength(t, doc.Find("body > #nf5").Nodes, 1)
   487  
   488  	printSel(t, doc.Selection)
   489  
   490  	doc = Doc2Clone()
   491  
   492  	doc.Find("#nf5, #n1").Unwrap()
   493  	assertLength(t, doc.Find("#foot").Nodes, 0)
   494  	assertLength(t, doc.Find("#main").Nodes, 0)
   495  	assertLength(t, doc.Find("body > #n1").Nodes, 1)
   496  	assertLength(t, doc.Find("body > #nf5").Nodes, 1)
   497  
   498  	printSel(t, doc.Selection)
   499  }
   500  
   501  func TestUnwrapBody(t *testing.T) {
   502  	doc := Doc2Clone()
   503  
   504  	doc.Find("#main").Unwrap()
   505  	assertLength(t, doc.Find("body").Nodes, 1)
   506  	assertLength(t, doc.Find("body > #main").Nodes, 1)
   507  
   508  	printSel(t, doc.Selection)
   509  }
   510  
   511  func TestUnwrapHead(t *testing.T) {
   512  	doc := Doc2Clone()
   513  
   514  	doc.Find("title").Unwrap()
   515  	assertLength(t, doc.Find("head").Nodes, 0)
   516  	assertLength(t, doc.Find("head > title").Nodes, 0)
   517  	assertLength(t, doc.Find("title").Nodes, 1)
   518  
   519  	printSel(t, doc.Selection)
   520  }
   521  
   522  func TestUnwrapHtml(t *testing.T) {
   523  	doc := Doc2Clone()
   524  
   525  	doc.Find("head").Unwrap()
   526  	assertLength(t, doc.Find("html").Nodes, 0)
   527  	assertLength(t, doc.Find("html head").Nodes, 0)
   528  	assertLength(t, doc.Find("head").Nodes, 1)
   529  
   530  	printSel(t, doc.Selection)
   531  }
   532  
   533  func TestWrap(t *testing.T) {
   534  	doc := Doc2Clone()
   535  	doc.Find("#nf1").Wrap("#nf2")
   536  	nf1 := doc.Find("#foot #nf2 #nf1")
   537  	assertLength(t, nf1.Nodes, 1)
   538  
   539  	nf2 := doc.Find("#nf2")
   540  	assertLength(t, nf2.Nodes, 2)
   541  
   542  	printSel(t, doc.Selection)
   543  }
   544  
   545  func TestWrapEmpty(t *testing.T) {
   546  	doc := Doc2Clone()
   547  	doc.Find("#nf1").Wrap("#doesnt-exist")
   548  
   549  	origHtml, _ := Doc2().Html()
   550  	newHtml, _ := doc.Html()
   551  
   552  	if origHtml != newHtml {
   553  		t.Error("Expected the two documents to be identical.")
   554  	}
   555  
   556  	printSel(t, doc.Selection)
   557  }
   558  
   559  func TestWrapHtml(t *testing.T) {
   560  	doc := Doc2Clone()
   561  	doc.Find(".odd").WrapHtml(wrapHtml)
   562  	nf2 := doc.Find("#ins #nf2")
   563  	assertLength(t, nf2.Nodes, 1)
   564  	printSel(t, doc.Selection)
   565  }
   566  
   567  func TestWrapSelection(t *testing.T) {
   568  	doc := Doc2Clone()
   569  	doc.Find("#nf1").WrapSelection(doc.Find("#nf2"))
   570  	nf1 := doc.Find("#foot #nf2 #nf1")
   571  	assertLength(t, nf1.Nodes, 1)
   572  
   573  	nf2 := doc.Find("#nf2")
   574  	assertLength(t, nf2.Nodes, 2)
   575  
   576  	printSel(t, doc.Selection)
   577  }
   578  
   579  func TestWrapAll(t *testing.T) {
   580  	doc := Doc2Clone()
   581  	doc.Find(".odd").WrapAll("#nf1")
   582  	nf1 := doc.Find("#main #nf1")
   583  	assertLength(t, nf1.Nodes, 1)
   584  
   585  	sel := nf1.Find("#n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
   586  	assertLength(t, sel.Nodes, 1)
   587  
   588  	printSel(t, doc.Selection)
   589  }
   590  
   591  func TestWrapAllHtml(t *testing.T) {
   592  	doc := Doc2Clone()
   593  	doc.Find(".odd").WrapAllHtml(wrapHtml)
   594  	nf1 := doc.Find("#main div#ins div p em b #n2 ~ #n4 ~ #n6 ~ #nf2 ~ #nf4 ~ #nf6")
   595  	assertLength(t, nf1.Nodes, 1)
   596  	printSel(t, doc.Selection)
   597  }
   598  
   599  func TestWrapInnerNoContent(t *testing.T) {
   600  	doc := Doc2Clone()
   601  	doc.Find(".one").WrapInner(".two")
   602  
   603  	twos := doc.Find(".two")
   604  	assertLength(t, twos.Nodes, 4)
   605  	assertLength(t, doc.Find(".one .two").Nodes, 2)
   606  
   607  	printSel(t, doc.Selection)
   608  }
   609  
   610  func TestWrapInnerWithContent(t *testing.T) {
   611  	doc := Doc3Clone()
   612  	doc.Find(".one").WrapInner(".two")
   613  
   614  	twos := doc.Find(".two")
   615  	assertLength(t, twos.Nodes, 4)
   616  	assertLength(t, doc.Find(".one .two").Nodes, 2)
   617  
   618  	printSel(t, doc.Selection)
   619  }
   620  
   621  func TestWrapInnerNoWrapper(t *testing.T) {
   622  	doc := Doc2Clone()
   623  	doc.Find(".one").WrapInner(".not-exist")
   624  
   625  	twos := doc.Find(".two")
   626  	assertLength(t, twos.Nodes, 2)
   627  	assertLength(t, doc.Find(".one").Nodes, 2)
   628  	assertLength(t, doc.Find(".one .two").Nodes, 0)
   629  
   630  	printSel(t, doc.Selection)
   631  }
   632  
   633  func TestWrapInnerHtml(t *testing.T) {
   634  	doc := Doc2Clone()
   635  	doc.Find("#foot").WrapInnerHtml(wrapHtml)
   636  
   637  	foot := doc.Find("#foot div#ins div p em b #nf1 ~ #nf2 ~ #nf3")
   638  	assertLength(t, foot.Nodes, 1)
   639  
   640  	printSel(t, doc.Selection)
   641  }
   642  
   643  func TestParsingRespectsVaryingContext(t *testing.T) {
   644  	docA := loadString(t, `
   645  	<html>
   646  		<body>
   647  			<a class="x"></a>
   648  		</body>
   649  	</html>`)
   650  	docTable := loadString(t, `
   651  	<html>
   652  		<body>
   653  			<table class="x"></table>
   654  		</body>
   655  	</html>`)
   656  	docBoth := loadString(t, `
   657  	<html>
   658  		<body>
   659  			<table class="x"></table>
   660  			<a class="x"></a>
   661  		</body>
   662  	</html>`)
   663  
   664  	sA := docA.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")
   665  	sTable := docTable.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")
   666  	sBoth := docBoth.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")
   667  
   668  	printSel(t, docA.Selection)
   669  	printSel(t, docTable.Selection)
   670  	printSel(t, docBoth.Selection)
   671  
   672  	oA, _ := sA.Html()
   673  	oTable, _ := sTable.Html()
   674  
   675  	if oA == oTable {
   676  		t.Errorf("Expected inner html of <a> and <table> to not be equal, but got %s and %s", oA, oTable)
   677  	}
   678  
   679  	oBothTable, _ := sBoth.First().Html()
   680  	if oBothTable != oTable {
   681  		t.Errorf("Expected inner html of <table> and <table> in doc containing both tags to be equal, but got %s and %s",
   682  			oTable,
   683  			oBothTable)
   684  	}
   685  
   686  	oBothA, _ := sBoth.Last().Html()
   687  	if oBothA != oA {
   688  		t.Errorf("Expected inner html of <a> and <a> in doc containing both tags to be equal, but got %s and %s",
   689  			oA,
   690  			oBothA)
   691  	}
   692  }
   693  
   694  func TestHtmlWithNonElementNode(t *testing.T) {
   695  	const data = `
   696  <html>
   697    <head>
   698    </head>
   699    <body>
   700      <p>
   701        This is <span>some</span><b>text</b>.
   702      </p>
   703    </body>
   704  </html>
   705  `
   706  
   707  	cases := map[string]func(*Selection, string) *Selection{
   708  		"AfterHtml":       (*Selection).AfterHtml,
   709  		"AppendHtml":      (*Selection).AppendHtml,
   710  		"BeforeHtml":      (*Selection).BeforeHtml,
   711  		"PrependHtml":     (*Selection).PrependHtml,
   712  		"ReplaceWithHtml": (*Selection).ReplaceWithHtml,
   713  		"SetHtml":         (*Selection).SetHtml,
   714  	}
   715  	for nm, fn := range cases {
   716  		// this test is only to make sure that the HTML parsing/manipulation
   717  		// methods do not raise panics when executed over Selections that contain
   718  		// non-Element nodes.
   719  		t.Run(nm, func(t *testing.T) {
   720  			doc := loadString(t, data)
   721  			sel := doc.Find("p").Contents()
   722  			func() {
   723  				defer func() {
   724  					if err := recover(); err != nil {
   725  						t.Fatal(err)
   726  					}
   727  				}()
   728  				fn(sel, "<div></div>")
   729  			}()
   730  
   731  			// print the resulting document in verbose mode
   732  			h, err := OuterHtml(doc.Selection)
   733  			if err != nil {
   734  				log.Fatal(err)
   735  			}
   736  			t.Log(h)
   737  		})
   738  	}
   739  }
   740  

View as plain text