Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,10 @@ func (s *Selection) WrapAllHtml(htmlStr string) *Selection {
var context *html.Node
var nodes []*html.Node
if len(s.Nodes) > 0 {
context = s.Nodes[0]
if context.Parent != nil {
// The wrapper takes the place of the first node, so parse it in the
// context of that node's parent, as WrapHtml does.
context = s.Nodes[0].Parent
if context != nil && context.Type == html.ElementNode {
nodes = parseHtmlWithContext(htmlStr, context, s.parseOpts())
} else {
nodes = parseHtml(htmlStr, s.parseOpts())
Expand Down
16 changes: 16 additions & 0 deletions manipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,22 @@ func TestWrapAllHtml(t *testing.T) {
printSel(t, doc.Selection)
}

func TestWrapAllHtmlRawTextElement(t *testing.T) {
// The wrapper must be parsed in the parent's context. Parsed inside a
// textarea it would become text, and the textarea would be lost.
doc := loadString(t, `<html><body><textarea>hi</textarea></body></html>`)
doc.Find("textarea").WrapAllHtml(`<div class="wrap"></div>`)
assertLength(t, doc.Find("body > div.wrap > textarea").Nodes, 1)
printSel(t, doc.Selection)
}

func TestWrapAllHtmlTextNode(t *testing.T) {
doc := loadString(t, `<html><body><p>a <b>b</b></p></body></html>`)
doc.Find("p").Contents().WrapAllHtml(`<span class="wrap"></span>`)
assertLength(t, doc.Find("p > span.wrap > b").Nodes, 1)
printSel(t, doc.Selection)
}

func TestWrapInnerNoContent(t *testing.T) {
doc := Doc2Clone()
doc.Find(".one").WrapInner(".two")
Expand Down