From 548f9480f4560cd67877db43c9ff64313b8eafaf Mon Sep 17 00:00:00 2001 From: ericchiang Date: Wed, 17 Sep 2014 20:32:49 -0400 Subject: [PATCH] selector interface simplified --- main.go | 12 ++++-------- selector/selector.go | 15 +++++++++------ 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index dad16ae..6ac695b 100644 --- a/main.go +++ b/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) diff --git a/selector/selector.go b/selector/selector.go index 0a1aa89..48003ed 100644 --- a/selector/selector.go +++ b/selector/selector.go @@ -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{}