diff --git a/manipulation.go b/manipulation.go index 407a963..2df68aa 100644 --- a/manipulation.go +++ b/manipulation.go @@ -372,8 +372,8 @@ func (s *Selection) WrapSelection(sel *Selection) *Selection { // It returns the original set of elements. func (s *Selection) WrapHtml(htmlStr string) *Selection { nodesMap := make(map[html.NodeType][]*html.Node) - var parent *html.Node for _, context := range s.Nodes { + var parent *html.Node if context.Parent != nil { parent = context.Parent } else { diff --git a/manipulation_test.go b/manipulation_test.go index 3f7535a..a08d3bd 100644 --- a/manipulation_test.go +++ b/manipulation_test.go @@ -57,19 +57,21 @@ func TestAfterHtml(t *testing.T) { } func TestAfterHtmlContext(t *testing.T) { - doc := loadString(` + doc := loadString(t, ` - - Before1 + + + +
Before
Before2
- `, - t) + `) doc.Find("table tr td").AfterHtml("Test") - assertLength(t, doc.Find("table tr td").Nodes, 2) + assertLength(t, doc.Find("table tr td").Nodes, 4) printSel(t, doc.Selection) } @@ -131,20 +133,22 @@ func TestAppendHtml(t *testing.T) { } func TestAppendHtmlContext(t *testing.T) { - doc := loadString(` + doc := loadString(t, ` - - Before1 + + + +
Before
Before2
- `, - t) + `) doc.Find("table tr").AppendHtml("new node") - assertLength(t, doc.Find("table td").Nodes, 2) + assertLength(t, doc.Find("table td").Nodes, 4) assertClass(t, doc.Find("table td").Last(), "new-node") printSel(t, doc.Selection) } @@ -188,19 +192,22 @@ func TestBeforeHtml(t *testing.T) { } func TestBeforeHtmlContext(t *testing.T) { - doc := loadString(` + doc := loadString(t, ` - - Before1 + + + +
Before
Before2
- `, - t) + `) doc.Find("table tr td:first-child").BeforeHtml("new node") + assertLength(t, doc.Find("table td").Nodes, 4) assertClass(t, doc.Find("table td").First(), "new-node") printSel(t, doc.Selection) } @@ -273,21 +280,23 @@ func TestPrependHtml(t *testing.T) { } func TestPrependHtmlContext(t *testing.T) { - doc := loadString(` + doc := loadString(t, ` - - Before1 + + + +
Before
Before2
- `, - t) - doc.Find("table tr").PrependHtml("new node") + `) + doc.Find("table tr").PrependHtml("new nodeother new node") - assertLength(t, doc.Find("table td").Nodes, 2) - assertClass(t, doc.Find("table tr td").First(), "new-node") + assertLength(t, doc.Find("table td").Nodes, 6) + assertClass(t, doc.Find("table tr td").First(), "c1") printSel(t, doc.Selection) } @@ -352,21 +361,23 @@ func TestReplaceWithHtml(t *testing.T) { } func TestReplaceWithHtmlContext(t *testing.T) { - doc := loadString(` + doc := loadString(t, ` - - Before1 + + + +
Before
Before2
- `, - t) + `) doc.Find("table th").ReplaceWithHtml("TestReplace") assertLength(t, doc.Find("table th").Nodes, 0) - assertLength(t, doc.Find("table tr td").Nodes, 2) + assertLength(t, doc.Find("table tr td").Nodes, 4) printSel(t, doc.Selection) } @@ -406,21 +417,24 @@ func TestSetHtmlEmpty(t *testing.T) { } func TestSetHtmlContext(t *testing.T) { - doc := loadString(` + doc := loadString(t, ` - - Before1 + + + +
Before
Before2
- `, - t) - doc.Find("table tr").SetHtml("Test") + `) + doc.Find("table tr").SetHtml("TestAgain") assertLength(t, doc.Find("table th").Nodes, 0) - assertLength(t, doc.Find("table td.new-node").Nodes, 1) + assertLength(t, doc.Find("table td").Nodes, 4) + assertLength(t, doc.Find("table t2").Nodes, 2) printSel(t, doc.Selection) } @@ -624,33 +638,34 @@ func TestWrapInnerHtml(t *testing.T) { } func TestParsingRespectsVaryingContext(t *testing.T) { - docA := loadString(` + docA := loadString(t, ` - `, - t) - docTable := loadString(` + `) + docTable := loadString(t, `
- `, - t) - docBoth := loadString(` + `) + docBoth := loadString(t, `
- `, - t) + `) sA := docA.Find(".x").AppendHtml("Hello") sTable := docTable.Find(".x").AppendHtml("Hello") sBoth := docBoth.Find(".x").AppendHtml("Hello") + printSel(t, docA.Selection) + printSel(t, docTable.Selection) + printSel(t, docBoth.Selection) + oA, _ := sA.Html() oTable, _ := sTable.Html() diff --git a/type_test.go b/type_test.go index b57cbad..798d4ea 100644 --- a/type_test.go +++ b/type_test.go @@ -121,7 +121,7 @@ func loadDoc(page string) *Document { return NewDocumentFromNode(node) } -func loadString(doc string, t *testing.T) *Document { +func loadString(t *testing.T, doc string) *Document { d, err := NewDocumentFromReader(strings.NewReader(doc)) if err != nil { t.Error("Failed to parse test document")