From 4a687a672f0f3b30ea3a60775067215da81d8081 Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Thu, 8 Oct 2020 09:02:13 -0400 Subject: [PATCH] add test cases to check html insertion order --- manipulation.go | 3 ++- manipulation_test.go | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) 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) }