Skip to content

Return no siblings for nodes without a parent - #622

Open
nk-batman21 wants to merge 1 commit into
PuerkitoBio:masterfrom
nk-batman21:fix-siblings-without-parent
Open

nk-batman21 wants to merge 1 commit into
PuerkitoBio:masterfrom
nk-batman21:fix-siblings-without-parent

Conversation

@nk-batman21

Copy link
Copy Markdown

Siblings, SiblingsFiltered and SiblingsMatcher panic with a nil pointer dereference when the selection contains a node without a parent. This happens when some nodes of a selection are removed and the selection is used again, and when calling them on doc.Selection or on a Clone()d selection.

doc, _ := goquery.NewDocumentFromReader(strings.NewReader(`<ul><li>a</li><li>b</li><li>c</li></ul>`))
items := doc.Find("li")
items.Eq(0).Remove()
items.NextAll().Length() // 1
items.Siblings()         // panic: runtime error: invalid memory address or nil pointer dereference

getSiblingNodes passes n.Parent to getChildrenWithSiblingType, which reads parent.FirstChild in the siblings case. Next, Prev, NextAll and PrevAll already return nothing for these nodes, and jQuery's siblings() returns an empty set for an element without a parent. This change skips nodes without a parent, so items.Siblings() above returns b and c, and doc.Siblings() returns an empty selection.

Ran: go test ./... -v -cover (the new TestSiblingsWithoutParent panics without the change and passes with it)
Ran: go vet ./..., gofmt -l

Siblings, SiblingsFiltered and SiblingsMatcher panicked with a nil
pointer dereference when the selection contained a node without a
parent, such as the document root or a removed or cloned node. Return
no siblings for such nodes, as Next, Prev, NextAll and PrevAll already
do.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant