Skip to content

Commit

Permalink
add tests to confirm erroneous behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
mna committed Oct 7, 2020
1 parent d89086e commit 5d7b027
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 49 deletions.
2 changes: 1 addition & 1 deletion manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
109 changes: 62 additions & 47 deletions manipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,21 @@ func TestAfterHtml(t *testing.T) {
}

func TestAfterHtmlContext(t *testing.T) {
doc := loadString(`
doc := loadString(t, `
<html>
<body>
<table>
<tr>
<td>Before</td>
</tr
<td>Before1</td>
</tr>
<tr>
<td>Before2</td>
</tr>
</table>
</body>
</html>`,
t)
</html>`)
doc.Find("table tr td").AfterHtml("<td>Test</td>")
assertLength(t, doc.Find("table tr td").Nodes, 2)
assertLength(t, doc.Find("table tr td").Nodes, 4)
printSel(t, doc.Selection)
}

Expand Down Expand Up @@ -131,20 +133,22 @@ func TestAppendHtml(t *testing.T) {
}

func TestAppendHtmlContext(t *testing.T) {
doc := loadString(`
doc := loadString(t, `
<html>
<body>
<table>
<tr>
<td>Before</td>
</tr
<td>Before1</td>
</tr>
<tr>
<td>Before2</td>
</tr>
</table>
</body>
</html>`,
t)
</html>`)
doc.Find("table tr").AppendHtml("<td class='new-node'>new node</td>")

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)
}
Expand Down Expand Up @@ -188,19 +192,22 @@ func TestBeforeHtml(t *testing.T) {
}

func TestBeforeHtmlContext(t *testing.T) {
doc := loadString(`
doc := loadString(t, `
<html>
<body>
<table>
<tr>
<td>Before</td>
</tr
<td>Before1</td>
</tr>
<tr>
<td>Before2</td>
</tr>
</table>
</body>
</html>`,
t)
</html>`)
doc.Find("table tr td:first-child").BeforeHtml("<td class='new-node'>new node</td>")

assertLength(t, doc.Find("table td").Nodes, 4)
assertClass(t, doc.Find("table td").First(), "new-node")
printSel(t, doc.Selection)
}
Expand Down Expand Up @@ -273,21 +280,23 @@ func TestPrependHtml(t *testing.T) {
}

func TestPrependHtmlContext(t *testing.T) {
doc := loadString(`
doc := loadString(t, `
<html>
<body>
<table>
<tr>
<td>Before</td>
</tr
<td>Before1</td>
</tr>
<tr>
<td>Before2</td>
</tr>
</table>
</body>
</html>`,
t)
doc.Find("table tr").PrependHtml("<td class='new-node'>new node</td>")
</html>`)
doc.Find("table tr").PrependHtml("<td class='c1'>new node</td><td class='c2'>other new node</td>")

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)
}

Expand Down Expand Up @@ -352,21 +361,23 @@ func TestReplaceWithHtml(t *testing.T) {
}

func TestReplaceWithHtmlContext(t *testing.T) {
doc := loadString(`
doc := loadString(t, `
<html>
<body>
<table>
<tr>
<th>Before</th>
</tr
<th>Before1</th>
</tr>
<tr>
<th>Before2</th>
</tr>
</table>
</body>
</html>`,
t)
</html>`)
doc.Find("table th").ReplaceWithHtml("<td>Test</td><td>Replace</td>")

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)
}

Expand Down Expand Up @@ -406,21 +417,24 @@ func TestSetHtmlEmpty(t *testing.T) {
}

func TestSetHtmlContext(t *testing.T) {
doc := loadString(`
doc := loadString(t, `
<html>
<body>
<table>
<tr>
<th>Before</th>
</tr
<th>Before1</th>
</tr>
<tr>
<th>Before2</th>
</tr>
</table>
</body>
</html>`,
t)
doc.Find("table tr").SetHtml("<td class='new-node'>Test</td>")
</html>`)
doc.Find("table tr").SetHtml("<td class='c1'>Test</td><td class='c2'>Again</td>")

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)
}

Expand Down Expand Up @@ -624,33 +638,34 @@ func TestWrapInnerHtml(t *testing.T) {
}

func TestParsingRespectsVaryingContext(t *testing.T) {
docA := loadString(`
docA := loadString(t, `
<html>
<body>
<a class="x"></a>
</body>
</html>`,
t)
docTable := loadString(`
</html>`)
docTable := loadString(t, `
<html>
<body>
<table class="x"></table>
</body>
</html>`,
t)
docBoth := loadString(`
</html>`)
docBoth := loadString(t, `
<html>
<body>
<table class="x"></table>
<a class="x"></a>
</body>
</html>`,
t)
</html>`)

sA := docA.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")
sTable := docTable.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")
sBoth := docBoth.Find(".x").AppendHtml("<tr><td>Hello</td></tr>")

printSel(t, docA.Selection)
printSel(t, docTable.Selection)
printSel(t, docBoth.Selection)

oA, _ := sA.Html()
oTable, _ := sTable.Html()

Expand Down
2 changes: 1 addition & 1 deletion type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 5d7b027

Please sign in to comment.