diff --git a/expand.go b/expand.go index f0c6c86..2f8c2db 100644 --- a/expand.go +++ b/expand.go @@ -38,9 +38,33 @@ func (s *Selection) AddNodes(nodes ...*html.Node) *Selection { return pushStack(s, appendWithoutDuplicates(s.Nodes, nodes, nil)) } +// Deprecated: This function has been deprecated and is now an alias for AddBack(). // AndSelf adds the previous set of elements on the stack to the current set. // It returns a new Selection object containing the current Selection combined // with the previous one. func (s *Selection) AndSelf() *Selection { + return s.AddBack() +} + +// AddBack adds the previous set of elements on the stack to the current set. +// It returns a new Selection object containing the current Selection combined +// with the previous one. +func (s *Selection) AddBack() *Selection { return s.AddSelection(s.prevSel) } + +// AddBackFiltered reduces the previous set of elements on the stack to those that +// match the selector string, and adds them to the current set. +// It returns a new Selection object containing the current Selection combined +// with the filtered previous one +func (s *Selection) AddBackFiltered(selector string) *Selection { + return s.AddSelection(s.prevSel.Filter(selector)) +} + +// AddBackMatcher reduces the previous set of elements on the stack to those that match +// the mateher, and adds them to the curernt set. +// It returns a new Selection object containing the current Selection combined +// with the filtered previous one +func (s *Selection) AddBackMatcher(m Matcher) *Selection { + return s.AddSelection(s.prevSel.FilterMatcher(m)) +} diff --git a/expand_test.go b/expand_test.go index 4557025..c034dc6 100644 --- a/expand_test.go +++ b/expand_test.go @@ -94,3 +94,25 @@ func TestAndSelfRollback(t *testing.T) { sel2 := sel.Find("a").AndSelf().End().End() assertEqual(t, sel, sel2) } + +func TestAddBack(t *testing.T) { + sel := Doc().Find(".span12").Last().AddBack() + assertLength(t, sel.Nodes, 2) +} + +func TestAddBackRollback(t *testing.T) { + sel := Doc().Find(".pvk-content") + sel2 := sel.Find("a").AddBack().End().End() + assertEqual(t, sel, sel2) +} + +func TestAddBackFiltered(t *testing.T) { + sel := Doc().Find(".span12, .footer").Find("h1").AddBackFiltered(".footer") + assertLength(t, sel.Nodes, 2) +} + +func TestAddBackFilteredRollback(t *testing.T) { + sel := Doc().Find(".span12, .footer") + sel2 := sel.Find("h1").AddBackFiltered(".footer").End().End() + assertEqual(t, sel, sel2) +}