From fb2312a925258bc69c0998103640733e65b75d1d Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Fri, 7 Nov 2014 12:19:01 -0500 Subject: [PATCH] add final *Matcher* overloads --- array.go | 2 +- doc.go | 11 +++++---- property.go | 2 +- traversal.go | 68 ++++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 64 insertions(+), 19 deletions(-) diff --git a/array.go b/array.go index 82119d5..d7af5ee 100644 --- a/array.go +++ b/array.go @@ -36,7 +36,7 @@ func (s *Selection) Eq(index int) *Selection { // Slice reduces the set of matched elements to a subset specified by a range // of indices. -func (s *Selection) Slice(start int, end int) *Selection { +func (s *Selection) Slice(start, end int) *Selection { if start < 0 { start += len(s.Nodes) } diff --git a/doc.go b/doc.go index 2a061d6..7e1224f 100644 --- a/doc.go +++ b/doc.go @@ -25,7 +25,7 @@ Package goquery implements features similar to jQuery, including the chainable syntax, to manipulate and query an HTML document. -GoQuery brings a syntax and a set of features similar to jQuery to the Go language. +goquery brings a syntax and a set of features similar to jQuery to the Go language. It is based on Go's net/html package and the CSS Selector library cascadia. Since the net/html parser returns tokens (nodes), and not a full-featured DOM object, jQuery's manipulation and modification functions have been left off (no point in @@ -36,7 +36,7 @@ the caller's responsibility to ensure that the source document provides UTF-8 en Supported functions are query-oriented features (`hasClass()`, `attr()` and the likes), as well as traversing functions that make sense given what we have to work with. -This makes GoQuery a great library for scraping web pages. +This makes goquery a great library for scraping web pages. Syntax-wise, it is as close as possible to jQuery, with the same function names when possible, and that warm and fuzzy chainable interface. jQuery being the @@ -82,12 +82,12 @@ The various methods are split into files based on the category of behavior: - Append...() - Before...() - Clone() - - Empty + - Empty() - Remove...() * property.go : methods that inspect and get the node's properties values. - Attr(), RemoveAttr(), SetAttr() - - AddClass(), HasClass(), RemoveClass(), RemoveClasses(), ToggleClass() + - AddClass(), HasClass(), RemoveClass(), ToggleClass() - Html() - Length() - Size(), which is an alias for Length() @@ -106,8 +106,9 @@ The various methods are split into files based on the category of behavior: - Prev...() - Siblings...() -* type.go : definition of the types exposed by GoQuery. +* type.go : definition of the types exposed by goquery. - Document - Selection + - Matcher */ package goquery diff --git a/property.go b/property.go index b52b58c..4205998 100644 --- a/property.go +++ b/property.go @@ -30,7 +30,7 @@ func (s *Selection) RemoveAttr(attrName string) *Selection { } // SetAttr sets the given attribute on each element in the set of matched elements. -func (s *Selection) SetAttr(attrName string, val string) *Selection { +func (s *Selection) SetAttr(attrName, val string) *Selection { for _, n := range s.Nodes { if attr := getAttributePtr(attrName, n); attr != nil { attr.Val = val diff --git a/traversal.go b/traversal.go index fbd05bc..16bfb1f 100644 --- a/traversal.go +++ b/traversal.go @@ -226,18 +226,25 @@ func (s *Selection) ParentsFilteredUntil(filterSelector, untilSelector string) * // ParentsFilteredUntilMatcher is like ParentsUntilMatcher, with the option to filter the // results based on a matcher. It returns a new Selection object containing the matched elements. -func (s *Selection) ParentsFilteredUntilMatcher(filterm, untilm Matcher) *Selection { - return filterAndPush(s, getParentsNodes(s.Nodes, untilm, nil), filterm) +func (s *Selection) ParentsFilteredUntilMatcher(filter, until Matcher) *Selection { + return filterAndPush(s, getParentsNodes(s.Nodes, until, nil), filter) } // ParentsFilteredUntilSelection is like ParentsUntilSelection, with the // option to filter the results based on a selector string. It returns a new // Selection object containing the matched elements. func (s *Selection) ParentsFilteredUntilSelection(filterSelector string, sel *Selection) *Selection { + return s.ParentsMatcherUntilSelection(cascadia.MustCompile(filterSelector), sel) +} + +// ParentsMatcherUntilSelection is like ParentsUntilSelection, with the +// option to filter the results based on a matcher. It returns a new +// Selection object containing the matched elements. +func (s *Selection) ParentsMatcherUntilSelection(filter Matcher, sel *Selection) *Selection { if sel == nil { - return s.ParentsFiltered(filterSelector) + return s.ParentsMatcher(filter) } - return s.ParentsFilteredUntilNodes(filterSelector, sel.Nodes...) + return s.ParentsMatcherUntilNodes(filter, sel.Nodes...) } // ParentsFilteredUntilNodes is like ParentsUntilNodes, with the @@ -247,6 +254,13 @@ func (s *Selection) ParentsFilteredUntilNodes(filterSelector string, nodes ...*h return filterAndPush(s, getParentsNodes(s.Nodes, nil, nodes), cascadia.MustCompile(filterSelector)) } +// ParentsMatcherUntilNodes is like ParentsUntilNodes, with the +// option to filter the results based on a matcher. It returns a new +// Selection object containing the matched elements. +func (s *Selection) ParentsMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection { + return filterAndPush(s, getParentsNodes(s.Nodes, nil, nodes), filter) +} + // Siblings gets the siblings of each element in the Selection. It returns // a new Selection object containing the matched elements. func (s *Selection) Siblings() *Selection { @@ -426,19 +440,26 @@ func (s *Selection) NextFilteredUntil(filterSelector, untilSelector string) *Sel // NextFilteredUntilMatcher is like NextUntilMatcher, with the option to filter // the results based on a matcher. // It returns a new Selection object containing the matched elements. -func (s *Selection) NextFilteredUntilMatcher(filterm, untilm Matcher) *Selection { +func (s *Selection) NextFilteredUntilMatcher(filter, until Matcher) *Selection { return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil, - untilm, nil), filterm) + until, nil), filter) } // NextFilteredUntilSelection is like NextUntilSelection, with the // option to filter the results based on a selector string. It returns a new // Selection object containing the matched elements. func (s *Selection) NextFilteredUntilSelection(filterSelector string, sel *Selection) *Selection { + return s.NextMatcherUntilSelection(cascadia.MustCompile(filterSelector), sel) +} + +// NextMatcherUntilSelection is like NextUntilSelection, with the +// option to filter the results based on a matcher. It returns a new +// Selection object containing the matched elements. +func (s *Selection) NextMatcherUntilSelection(filter Matcher, sel *Selection) *Selection { if sel == nil { - return s.NextFiltered(filterSelector) + return s.NextMatcher(filter) } - return s.NextFilteredUntilNodes(filterSelector, sel.Nodes...) + return s.NextMatcherUntilNodes(filter, sel.Nodes...) } // NextFilteredUntilNodes is like NextUntilNodes, with the @@ -449,6 +470,14 @@ func (s *Selection) NextFilteredUntilNodes(filterSelector string, nodes ...*html nil, nodes), cascadia.MustCompile(filterSelector)) } +// NextMatcherUntilNodes is like NextUntilNodes, with the +// option to filter the results based on a matcher. It returns a new +// Selection object containing the matched elements. +func (s *Selection) NextMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection { + return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil, + nil, nodes), filter) +} + // PrevFilteredUntil is like PrevUntil, with the option to filter // the results based on a selector string. // It returns a new Selection object containing the matched elements. @@ -460,19 +489,26 @@ func (s *Selection) PrevFilteredUntil(filterSelector, untilSelector string) *Sel // PrevFilteredUntilMatcher is like PrevUntilMatcher, with the option to filter // the results based on a matcher. // It returns a new Selection object containing the matched elements. -func (s *Selection) PrevFilteredUntilMatcher(filterm, untilm Matcher) *Selection { +func (s *Selection) PrevFilteredUntilMatcher(filter, until Matcher) *Selection { return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil, - untilm, nil), filterm) + until, nil), filter) } // PrevFilteredUntilSelection is like PrevUntilSelection, with the // option to filter the results based on a selector string. It returns a new // Selection object containing the matched elements. func (s *Selection) PrevFilteredUntilSelection(filterSelector string, sel *Selection) *Selection { + return s.PrevMatcherUntilSelection(cascadia.MustCompile(filterSelector), sel) +} + +// PrevMatcherUntilSelection is like PrevUntilSelection, with the +// option to filter the results based on a matcher. It returns a new +// Selection object containing the matched elements. +func (s *Selection) PrevMatcherUntilSelection(filter Matcher, sel *Selection) *Selection { if sel == nil { - return s.PrevFiltered(filterSelector) + return s.PrevMatcher(filter) } - return s.PrevFilteredUntilNodes(filterSelector, sel.Nodes...) + return s.PrevMatcherUntilNodes(filter, sel.Nodes...) } // PrevFilteredUntilNodes is like PrevUntilNodes, with the @@ -483,6 +519,14 @@ func (s *Selection) PrevFilteredUntilNodes(filterSelector string, nodes ...*html nil, nodes), cascadia.MustCompile(filterSelector)) } +// PrevMatcherUntilNodes is like PrevUntilNodes, with the +// option to filter the results based on a matcher. It returns a new +// Selection object containing the matched elements. +func (s *Selection) PrevMatcherUntilNodes(filter Matcher, nodes ...*html.Node) *Selection { + return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil, + nil, nodes), filter) +} + // Filter and push filters the nodes based on a matcher, and pushes the results // on the stack, with the srcSel as previous selection. func filterAndPush(srcSel *Selection, nodes []*html.Node, m Matcher) *Selection {