mirror of
https://github.com/ericchiang/pup
synced 2024-11-24 00:48:36 +00:00
selector interface simplified
This commit is contained in:
parent
9f9ed735dc
commit
548f9480f4
12
main.go
12
main.go
@ -12,7 +12,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const VERSION string = "0.1.3"
|
||||
const VERSION string = "0.2.0"
|
||||
|
||||
var (
|
||||
// Flags
|
||||
@ -133,6 +133,8 @@ func main() {
|
||||
}
|
||||
selectors := make([]selector.Selector, len(cmds))
|
||||
for i, cmd := range cmds {
|
||||
// if this is the last element, check for a function like
|
||||
// text{} or attr{}
|
||||
if i+1 == len(cmds) {
|
||||
d, err := funcs.NewDisplayFunc(cmd)
|
||||
if err == nil {
|
||||
@ -147,14 +149,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
currNodes := []*html.Node{root}
|
||||
var selected []*html.Node
|
||||
for _, selector := range selectors {
|
||||
selected = []*html.Node{}
|
||||
for _, node := range currNodes {
|
||||
selected = append(selected,
|
||||
selector.FindAllChildren(node)...)
|
||||
}
|
||||
currNodes = selected
|
||||
currNodes = selector.Select(currNodes)
|
||||
}
|
||||
if displayer != nil {
|
||||
displayer.Display(currNodes)
|
||||
|
@ -14,12 +14,7 @@ type BasicSelector struct {
|
||||
}
|
||||
|
||||
type Selector interface {
|
||||
// Does this selector match a given node?
|
||||
Match(node *html.Node) bool
|
||||
// Find all nodes which match a selector. May return itself.
|
||||
FindAll(node *html.Node) []*html.Node
|
||||
// Find all child nodes which match a selector.
|
||||
FindAllChildren(node *html.Node) []*html.Node
|
||||
Select(nodes []*html.Node) []*html.Node
|
||||
}
|
||||
|
||||
type selectorField int
|
||||
@ -161,6 +156,14 @@ func NewSelector(s string) (Selector, error) {
|
||||
return selector, nil
|
||||
}
|
||||
|
||||
func (sel BasicSelector) Select(nodes []*html.Node) []*html.Node {
|
||||
selected := []*html.Node{}
|
||||
for _, node := range nodes {
|
||||
selected = append(selected, sel.FindAllChildren(node)...)
|
||||
}
|
||||
return selected
|
||||
}
|
||||
|
||||
// Find all nodes which match a selector.
|
||||
func (sel BasicSelector) FindAllChildren(node *html.Node) []*html.Node {
|
||||
selected := []*html.Node{}
|
||||
|
Loading…
Reference in New Issue
Block a user