add final *Matcher* overloads

This commit is contained in:
Martin Angers
2014-11-07 12:19:01 -05:00
parent ef025592df
commit fb2312a925
4 changed files with 64 additions and 19 deletions

View File

@@ -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 // Slice reduces the set of matched elements to a subset specified by a range
// of indices. // of indices.
func (s *Selection) Slice(start int, end int) *Selection { func (s *Selection) Slice(start, end int) *Selection {
if start < 0 { if start < 0 {
start += len(s.Nodes) start += len(s.Nodes)
} }

11
doc.go
View File

@@ -25,7 +25,7 @@
Package goquery implements features similar to jQuery, including the chainable Package goquery implements features similar to jQuery, including the chainable
syntax, to manipulate and query an HTML document. 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 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, 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 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), 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. 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 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 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...() - Append...()
- Before...() - Before...()
- Clone() - Clone()
- Empty - Empty()
- Remove...() - Remove...()
* property.go : methods that inspect and get the node's properties values. * property.go : methods that inspect and get the node's properties values.
- Attr(), RemoveAttr(), SetAttr() - Attr(), RemoveAttr(), SetAttr()
- AddClass(), HasClass(), RemoveClass(), RemoveClasses(), ToggleClass() - AddClass(), HasClass(), RemoveClass(), ToggleClass()
- Html() - Html()
- Length() - Length()
- Size(), which is an alias for 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...() - Prev...()
- Siblings...() - Siblings...()
* type.go : definition of the types exposed by GoQuery. * type.go : definition of the types exposed by goquery.
- Document - Document
- Selection - Selection
- Matcher
*/ */
package goquery package goquery

View File

@@ -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. // 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 { for _, n := range s.Nodes {
if attr := getAttributePtr(attrName, n); attr != nil { if attr := getAttributePtr(attrName, n); attr != nil {
attr.Val = val attr.Val = val

View File

@@ -226,18 +226,25 @@ func (s *Selection) ParentsFilteredUntil(filterSelector, untilSelector string) *
// ParentsFilteredUntilMatcher is like ParentsUntilMatcher, with the option to filter the // 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. // results based on a matcher. It returns a new Selection object containing the matched elements.
func (s *Selection) ParentsFilteredUntilMatcher(filterm, untilm Matcher) *Selection { func (s *Selection) ParentsFilteredUntilMatcher(filter, until Matcher) *Selection {
return filterAndPush(s, getParentsNodes(s.Nodes, untilm, nil), filterm) return filterAndPush(s, getParentsNodes(s.Nodes, until, nil), filter)
} }
// ParentsFilteredUntilSelection is like ParentsUntilSelection, with the // ParentsFilteredUntilSelection is like ParentsUntilSelection, with the
// option to filter the results based on a selector string. It returns a new // option to filter the results based on a selector string. It returns a new
// Selection object containing the matched elements. // Selection object containing the matched elements.
func (s *Selection) ParentsFilteredUntilSelection(filterSelector string, sel *Selection) *Selection { 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 { 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 // 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)) 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 // Siblings gets the siblings of each element in the Selection. It returns
// a new Selection object containing the matched elements. // a new Selection object containing the matched elements.
func (s *Selection) Siblings() *Selection { 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 // NextFilteredUntilMatcher is like NextUntilMatcher, with the option to filter
// the results based on a matcher. // the results based on a matcher.
// It returns a new Selection object containing the matched elements. // 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, return filterAndPush(s, getSiblingNodes(s.Nodes, siblingNextUntil,
untilm, nil), filterm) until, nil), filter)
} }
// NextFilteredUntilSelection is like NextUntilSelection, with the // NextFilteredUntilSelection is like NextUntilSelection, with the
// option to filter the results based on a selector string. It returns a new // option to filter the results based on a selector string. It returns a new
// Selection object containing the matched elements. // Selection object containing the matched elements.
func (s *Selection) NextFilteredUntilSelection(filterSelector string, sel *Selection) *Selection { 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 { 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 // NextFilteredUntilNodes is like NextUntilNodes, with the
@@ -449,6 +470,14 @@ func (s *Selection) NextFilteredUntilNodes(filterSelector string, nodes ...*html
nil, nodes), cascadia.MustCompile(filterSelector)) 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 // PrevFilteredUntil is like PrevUntil, with the option to filter
// the results based on a selector string. // the results based on a selector string.
// It returns a new Selection object containing the matched elements. // 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 // PrevFilteredUntilMatcher is like PrevUntilMatcher, with the option to filter
// the results based on a matcher. // the results based on a matcher.
// It returns a new Selection object containing the matched elements. // 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, return filterAndPush(s, getSiblingNodes(s.Nodes, siblingPrevUntil,
untilm, nil), filterm) until, nil), filter)
} }
// PrevFilteredUntilSelection is like PrevUntilSelection, with the // PrevFilteredUntilSelection is like PrevUntilSelection, with the
// option to filter the results based on a selector string. It returns a new // option to filter the results based on a selector string. It returns a new
// Selection object containing the matched elements. // Selection object containing the matched elements.
func (s *Selection) PrevFilteredUntilSelection(filterSelector string, sel *Selection) *Selection { 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 { 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 // PrevFilteredUntilNodes is like PrevUntilNodes, with the
@@ -483,6 +519,14 @@ func (s *Selection) PrevFilteredUntilNodes(filterSelector string, nodes ...*html
nil, nodes), cascadia.MustCompile(filterSelector)) 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 // Filter and push filters the nodes based on a matcher, and pushes the results
// on the stack, with the srcSel as previous selection. // on the stack, with the srcSel as previous selection.
func filterAndPush(srcSel *Selection, nodes []*html.Node, m Matcher) *Selection { func filterAndPush(srcSel *Selection, nodes []*html.Node, m Matcher) *Selection {