diff --git a/manipulation.go b/manipulation.go index 07237a9..2c31770 100644 --- a/manipulation.go +++ b/manipulation.go @@ -41,9 +41,10 @@ func (s *Selection) AfterSelection(sel *Selection) *Selection { // This follows the same rules as Selection.Append. func (s *Selection) AfterHtml(htmlStr string) *Selection { return s.eachNodeHtml(htmlStr, true, func(node *html.Node, nodes []*html.Node) { + nextSibling := node.NextSibling for _, n := range nodes { if node.Parent != nil { - node.Parent.InsertBefore(n, node.NextSibling) + node.Parent.InsertBefore(n, nextSibling) } } }) diff --git a/manipulation_test.go b/manipulation_test.go index 1d37054..74dda7c 100644 --- a/manipulation_test.go +++ b/manipulation_test.go @@ -70,8 +70,9 @@ func TestAfterHtmlContext(t *testing.T) { `) - doc.Find("table tr td").AfterHtml("Test") - assertLength(t, doc.Find("table tr td").Nodes, 4) + doc.Find("table tr td").AfterHtml("TestAgain") + assertLength(t, doc.Find("table tr td").Nodes, 6) + assertClass(t, doc.Find("table tr td").Last(), "c2") printSel(t, doc.Selection) } @@ -146,10 +147,10 @@ func TestAppendHtmlContext(t *testing.T) { `) - doc.Find("table tr").AppendHtml("new node") + doc.Find("table tr").AppendHtml("new1new2") - assertLength(t, doc.Find("table td").Nodes, 4) - assertClass(t, doc.Find("table td").Last(), "new-node") + assertLength(t, doc.Find("table td").Nodes, 6) + assertClass(t, doc.Find("table td").Last(), "c2") printSel(t, doc.Selection) } @@ -205,10 +206,10 @@ func TestBeforeHtmlContext(t *testing.T) { `) - doc.Find("table tr td:first-child").BeforeHtml("new node") + doc.Find("table tr td:first-child").BeforeHtml("new1new2") - assertLength(t, doc.Find("table td").Nodes, 4) - assertClass(t, doc.Find("table td").First(), "new-node") + assertLength(t, doc.Find("table td").Nodes, 6) + assertClass(t, doc.Find("table td").First(), "c1") printSel(t, doc.Selection) } @@ -374,10 +375,11 @@ func TestReplaceWithHtmlContext(t *testing.T) { `) - doc.Find("table th").ReplaceWithHtml("TestReplace") + doc.Find("table th").ReplaceWithHtml("TestReplace") assertLength(t, doc.Find("table th").Nodes, 0) assertLength(t, doc.Find("table tr td").Nodes, 4) + assertClass(t, doc.Find("table tr td").First(), "c1") printSel(t, doc.Selection) }