forked from ericchiang/pup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
runselectors.go
60 lines (55 loc) · 1.35 KB
/
runselectors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"fmt"
"io"
"golang.org/x/net/html"
)
func runSelectors(w io.Writer, cmds []string, root *html.Node) error {
// Parse the selectors
selectorFuncs := []SelectorFunc{}
funcGenerator := Select
var cmd string
for len(cmds) > 0 {
cmd, cmds = cmds[0], cmds[1:]
if len(cmds) == 0 {
if err := ParseDisplayer(cmd); err == nil {
continue
}
}
switch cmd {
case "*": // select all
continue
case ">":
funcGenerator = SelectFromChildren
case "+":
funcGenerator = SelectNextSibling
case ",": // nil will signify a comma
selectorFuncs = append(selectorFuncs, nil)
default:
selector, err := ParseSelector(cmd)
if err != nil {
return fmt.Errorf("selector parsing error: %w", err)
}
selectorFuncs = append(selectorFuncs, funcGenerator(selector))
funcGenerator = Select
}
}
selectedNodes := []*html.Node{}
currNodes := []*html.Node{root}
for _, selectorFunc := range selectorFuncs {
if selectorFunc == nil { // hit a comma
selectedNodes = append(selectedNodes, currNodes...)
currNodes = []*html.Node{root}
} else {
currNodes = selectorFunc(currNodes)
}
}
selectedNodes = append(selectedNodes, currNodes...)
if pupInvertSelect {
RemoveInverseMatches(root, selectedNodes)
pupDisplayer.Display(w, []*html.Node{root})
} else {
pupDisplayer.Display(w, selectedNodes)
}
return nil
}