Debian Codegraph @

selector.go

raw symbols SCIP
1 package mainscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/
2
3 import (
4 "bytesscip-go gomod github.com/golang/go/src go1.13 bytes/"
5 "fmtscip-go gomod github.com/golang/go/src go1.13 fmt/"
6 "regexpscip-go gomod github.com/golang/go/src go1.13 regexp/"
7 "strconvscip-go gomod github.com/golang/go/src go1.13 strconv/"
8 "stringsscip-go gomod github.com/golang/go/src go1.13 strings/"
9 "text/scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/"
10
11 "golang.org/x/net/htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/"
12 )
13
14 type Selectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector# interface {
15 Matchscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector#Match.(node *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool
16 }
17
18 type SelectorFuncscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/SelectorFunc# func(nodes []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#
19
20 func Selectscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Select().(s Selectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector#) SelectorFuncscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/SelectorFunc# {
21 // have to define first to be able to do recursion
22 var selectChildren func(node *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#
23 selectChildren = func(node *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node# {
24 selected := []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#{}
25 for child := node.FirstChildscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#FirstChild.; child != nil; child = child.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
26 if s.Matchscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector#Match.(child) {
27 selected = append(selected, child)
28 } else {
29 selected = append(selected, selectChildren(child)...)
30 }
31 }
32 return selected
33 }
34 return func(nodes []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node# {
35 selected := []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#{}
36 for _, node := range nodes {
37 selected = append(selected, selectChildren(node)...)
38 }
39 return selected
40 }
41 }
42
43 // Defined for the '>' selector
44 func SelectNextSiblingscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/SelectNextSibling().(s Selectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector#) SelectorFuncscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/SelectorFunc# {
45 return func(nodes []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node# {
46 selected := []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#{}
47 for _, node := range nodes {
48 for ns := node.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling.; ns != nil; ns = ns.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
49 if ns.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
50 if s.Matchscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector#Match.(ns) {
51 selected = append(selected, ns)
52 }
53 break
54 }
55 }
56 }
57 return selected
58 }
59 }
60
61 // Defined for the '+' selector
62 func SelectFromChildrenscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/SelectFromChildren().(s Selectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector#) SelectorFuncscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/SelectorFunc# {
63 return func(nodes []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node# {
64 selected := []*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#{}
65 for _, node := range nodes {
66 for c := node.FirstChildscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#FirstChild.; c != nil; c = c.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
67 if s.Matchscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/Selector#Match.(c) {
68 selected = append(selected, c)
69 }
70 }
71 }
72 return selected
73 }
74 }
75
76 type PseudoClassscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PseudoClass# func(*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool
77
78 type CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector# struct {
79 Tagscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Tag. string
80 Attrsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Attrs. map[string]*regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#
81 Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. PseudoClassscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PseudoClass#
82 }
83
84 func (s CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#) Matchscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Match().(node *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
85 if node.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. != htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
86 return false
87 }
88 if s.Tagscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Tag. != "" {
89 if s.Tagscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Tag. != node.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom..Stringscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html/atom`/Atom#String().() {
90 return false
91 }
92 }
93 for attrKey, matcher := range s.Attrsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Attrs. {
94 matched := false
95 for _, attr := range node.Attrscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Attr. {
96 if attrKey == attr.Keyscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Attribute#Key. {
97 if !matcher.MatchStringscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#MatchString().(attr.Valscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Attribute#Val.) {
98 return false
99 }
100 matched = true
101 break
102 }
103 }
104 if !matched {
105 return false
106 }
107 }
108 if s.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. == nil {
109 return true
110 }
111 return s.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo.(node)
112 }
113
114 // Parse a selector
115 // e.g. `div#my-button.btn[href^="http"]`
116 func ParseSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseSelector().(cmd string) (selector CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#, err error) {
117 selector = CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#{
118 Tagscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Tag.: "",
119 Attrsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Attrs.: map[string]*regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.Regexpscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#{},
120 Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo.: nil,
121 }
122 var s scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#
123 s.Initscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Init().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.13 strings/NewReader().(cmd))
124 err = ParseTagMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseTagMatcher().(&selector, s)
125 return
126 }
127
128 // Parse the initial tag
129 // e.g. `div`
130 func ParseTagMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseTagMatcher().(selector *CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#, s scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#) error {
131 tag := bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.13 bytes/NewBuffer().([]byte{})
132 defer func() {
133 selector.Tagscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Tag. = tag.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()
134 }()
135 for {
136 c := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
137 switch c {
138 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
139 return nil
140 case '.':
141 return ParseClassMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseClassMatcher().(selector, s)
142 case '#':
143 return ParseIdMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseIdMatcher().(selector, s)
144 case '[':
145 return ParseAttrMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseAttrMatcher().(selector, s)
146 case ':':
147 return ParsePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParsePseudo().(selector, s)
148 default:
149 if _, err := tag.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(c); err != nil {
150 return err
151 }
152 }
153 }
154 }
155
156 // Parse a class matcher
157 // e.g. `.btn`
158 func ParseClassMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseClassMatcher().(selector *CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#, s scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#) error {
159 var class bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#
160 defer func() {
161 regexpStr := `(\A|\s)` + regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.QuoteMetascip-go gomod github.com/golang/go/src go1.13 regexp/QuoteMeta().(class.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()) + `(\s|\z)`
162 selector.Attrsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Attrs.["class"] = regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.13 regexp/MustCompile().(regexpStr)
163 }()
164 for {
165 c := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
166 switch c {
167 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
168 return nil
169 case '.':
170 return ParseClassMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseClassMatcher().(selector, s)
171 case '#':
172 return ParseIdMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseIdMatcher().(selector, s)
173 case '[':
174 return ParseAttrMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseAttrMatcher().(selector, s)
175 case ':':
176 return ParsePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParsePseudo().(selector, s)
177 default:
178 if _, err := class.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(c); err != nil {
179 return err
180 }
181 }
182 }
183 }
184
185 // Parse an id matcher
186 // e.g. `#my-picture`
187 func ParseIdMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseIdMatcher().(selector *CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#, s scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#) error {
188 var id bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#
189 defer func() {
190 regexpStr := `^` + regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.QuoteMetascip-go gomod github.com/golang/go/src go1.13 regexp/QuoteMeta().(id.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()) + `$`
191 selector.Attrsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Attrs.["id"] = regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.13 regexp/MustCompile().(regexpStr)
192 }()
193 for {
194 c := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
195 switch c {
196 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
197 return nil
198 case '.':
199 return ParseClassMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseClassMatcher().(selector, s)
200 case '#':
201 return ParseIdMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseIdMatcher().(selector, s)
202 case '[':
203 return ParseAttrMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseAttrMatcher().(selector, s)
204 case ':':
205 return ParsePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParsePseudo().(selector, s)
206 default:
207 if _, err := id.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(c); err != nil {
208 return err
209 }
210 }
211 }
212 }
213
214 // Parse an attribute matcher
215 // e.g. `[attr^="http"]`
216 func ParseAttrMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseAttrMatcher().(selector *CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#, s scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#) error {
217 var attrKey bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#
218 var attrVal bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#
219 hasMatchVal := false
220 matchType := '='
221 defer func() {
222 if hasMatchVal {
223 var regexpStr string
224 switch matchType {
225 case '=':
226 regexpStr = `^` + regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.QuoteMetascip-go gomod github.com/golang/go/src go1.13 regexp/QuoteMeta().(attrVal.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()) + `$`
227 case '*':
228 regexpStr = regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.QuoteMetascip-go gomod github.com/golang/go/src go1.13 regexp/QuoteMeta().(attrVal.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().())
229 case '$':
230 regexpStr = regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.QuoteMetascip-go gomod github.com/golang/go/src go1.13 regexp/QuoteMeta().(attrVal.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()) + `$`
231 case '^':
232 regexpStr = `^` + regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.QuoteMetascip-go gomod github.com/golang/go/src go1.13 regexp/QuoteMeta().(attrVal.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().())
233 case '~':
234 regexpStr = `(\A|\s)` + regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.QuoteMetascip-go gomod github.com/golang/go/src go1.13 regexp/QuoteMeta().(attrVal.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()) + `(\s|\z)`
235 }
236 selector.Attrsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Attrs.[attrKey.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()] = regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.13 regexp/MustCompile().(regexpStr)
237 } else {
238 selector.Attrsscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Attrs.[attrKey.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()] = regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.13 regexp/MustCompile().(`^.*$`)
239 }
240 }()
241 // After reaching ']' proceed
242 proceed := func() error {
243 switch s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().() {
244 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
245 return nil
246 case '.':
247 return ParseClassMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseClassMatcher().(selector, s)
248 case '#':
249 return ParseIdMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseIdMatcher().(selector, s)
250 case '[':
251 return ParseAttrMatcherscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseAttrMatcher().(selector, s)
252 case ':':
253 return ParsePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParsePseudo().(selector, s)
254 default:
255 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Expected selector indicator after ']'")
256 }
257 }
258 // Parse the attribute key matcher
259 for !hasMatchVal {
260 c := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
261 switch c {
262 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
263 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unmatched open brace '['")
264 case ']':
265 // No attribute value matcher, proceed!
266 return proceed()
267 case '$', '^', '~', '*':
268 matchType = c
269 hasMatchVal = true
270 if s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().() != '=' {
271 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("'%c' must be followed by a '='", matchType)
272 }
273 case '=':
274 matchType = c
275 hasMatchVal = true
276 default:
277 if _, err := attrKey.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(c); err != nil {
278 return err
279 }
280 }
281 }
282 // figure out if the value is quoted
283 c := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
284 inQuote := false
285 switch c {
286 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
287 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unmatched open brace '['")
288 case ']':
289 return proceed()
290 case '"':
291 inQuote = true
292 default:
293 if _, err := attrVal.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(c); err != nil {
294 return err
295 }
296 }
297 if inQuote {
298 for {
299 c := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
300 switch c {
301 case '\\':
302 // consume another character
303 if c = s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().(); c == scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF. {
304 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unmatched open brace '['")
305 }
306 case '"':
307 switch s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().() {
308 case ']':
309 return proceed()
310 default:
311 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Quote must end at ']'")
312 }
313 }
314 if _, err := attrVal.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(c); err != nil {
315 return err
316 }
317 }
318 } else {
319 for {
320 c := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
321 switch c {
322 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
323 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unmatched open brace '['")
324 case ']':
325 // No attribute value matcher, proceed!
326 return proceed()
327 }
328 if _, err := attrVal.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(c); err != nil {
329 return err
330 }
331 }
332 }
333 }
334
335 // Parse the selector after ':'
336 func ParsePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParsePseudo().(selector *CSSSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#, s scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#) error {
337 if selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. != nil {
338 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Combined multiple pseudo classes")
339 }
340 var b bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#
341 for s.Peekscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Peek().() != scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF. {
342 if _, err := b.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()); err != nil {
343 return err
344 }
345 }
346 cmd := b.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()
347 var err error
348 switch {
349 case cmd == "empty":
350 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. = func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
351 return n.FirstChildscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#FirstChild. == nil
352 }
353 case cmd == "first-child":
354 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. = firstChildPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/firstChildPseudo().
355 case cmd == "last-child":
356 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. = lastChildPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/lastChildPseudo().
357 case cmd == "only-child":
358 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. = func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
359 return firstChildPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/firstChildPseudo().(n) && lastChildPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/lastChildPseudo().(n)
360 }
361 case cmd == "first-of-type":
362 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. = firstOfTypePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/firstOfTypePseudo().
363 case cmd == "last-of-type":
364 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. = lastOfTypePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/lastOfTypePseudo().
365 case cmd == "only-of-type":
366 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo. = func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
367 return firstOfTypePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/firstOfTypePseudo().(n) && lastOfTypePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/lastOfTypePseudo().(n)
368 }
369 case stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(cmd, "contains("):
370 selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo., err = parseContainsPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseContainsPseudo().(cmd[len("contains("):])
371 if err != nil {
372 return err
373 }
374 case stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(cmd, "nth-child("),
375 stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(cmd, "nth-last-child("),
376 stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(cmd, "nth-last-of-type("),
377 stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(cmd, "nth-of-type("):
378 if selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo., err = parseNthPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseNthPseudo().(cmd); err != nil {
379 return err
380 }
381 case stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(cmd, "not("):
382 if selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo., err = parseNotPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseNotPseudo().(cmd[len("not("):]); err != nil {
383 return err
384 }
385 case stringsscip-go gomod github.com/golang/go/src go1.13 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.13 strings/HasPrefix().(cmd, "parent-of("):
386 if selector.Pseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Pseudo., err = parseParentOfPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseParentOfPseudo().(cmd[len("parent-of("):]); err != nil {
387 return err
388 }
389 default:
390 return fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("%s not a valid pseudo class", cmd)
391 }
392 return nil
393 }
394
395 // :first-of-child
396 func firstChildPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/firstChildPseudo().(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
397 for c := n.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling.; c != nil; c = c.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling. {
398 if c.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
399 return false
400 }
401 }
402 return true
403 }
404
405 // :last-of-child
406 func lastChildPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/lastChildPseudo().(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
407 for c := n.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling.; c != nil; c = c.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
408 if c.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
409 return false
410 }
411 }
412 return true
413 }
414
415 // :first-of-type
416 func firstOfTypePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/firstOfTypePseudo().(node *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
417 if node.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. != htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
418 return false
419 }
420 for n := node.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling.; n != nil; n = n.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling. {
421 if n.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. == node.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. {
422 return false
423 }
424 }
425 return true
426 }
427
428 // :last-of-type
429 func lastOfTypePseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/lastOfTypePseudo().(node *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
430 if node.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. != htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
431 return false
432 }
433 for n := node.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling.; n != nil; n = n.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
434 if n.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. == node.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. {
435 return false
436 }
437 }
438 return true
439 }
440
441 func parseNthPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseNthPseudo().(cmd string) (PseudoClassscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PseudoClass#, error) {
442 i := stringsscip-go gomod github.com/golang/go/src go1.13 strings/.IndexRunescip-go gomod github.com/golang/go/src go1.13 strings/IndexRune().(cmd, '(')
443 if i < 0 {
444 // really, we should never get here
445 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Fatal error, '%s' does not contain a '('", cmd)
446 }
447 pseudoName := cmd[:i]
448 // Figure out how the counting function works
449 var countNth func(*htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) int
450 switch pseudoName {
451 case "nth-child":
452 countNth = func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) int {
453 nth := 1
454 for sib := n.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling.; sib != nil; sib = sib.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling. {
455 if sib.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
456 nth++
457 }
458 }
459 return nth
460 }
461 case "nth-of-type":
462 countNth = func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) int {
463 nth := 1
464 for sib := n.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling.; sib != nil; sib = sib.PrevSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#PrevSibling. {
465 if sib.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. && sib.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. == n.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. {
466 nth++
467 }
468 }
469 return nth
470 }
471 case "nth-last-child":
472 countNth = func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) int {
473 nth := 1
474 for sib := n.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling.; sib != nil; sib = sib.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
475 if sib.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. {
476 nth++
477 }
478 }
479 return nth
480 }
481 case "nth-last-of-type":
482 countNth = func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) int {
483 nth := 1
484 for sib := n.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling.; sib != nil; sib = sib.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
485 if sib.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. && sib.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. == n.DataAtomscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#DataAtom. {
486 nth++
487 }
488 }
489 return nth
490 }
491 default:
492 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unrecognized pseudo '%s'", pseudoName)
493 }
494
495 nthString := cmd[i+1:]
496 i = stringsscip-go gomod github.com/golang/go/src go1.13 strings/.IndexRunescip-go gomod github.com/golang/go/src go1.13 strings/IndexRune().(nthString, ')')
497 if i < 0 {
498 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Unmatched '(' for pseudo class %s", pseudoName)
499 } else if i != len(nthString)-1 {
500 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("%s(n) must end selector", pseudoName)
501 }
502 number := nthString[:i]
503
504 // Check if the number is 'odd' or 'even'
505 oddOrEven := -1
506 switch number {
507 case "odd":
508 oddOrEven = 1
509 case "even":
510 oddOrEven = 0
511 }
512 if oddOrEven > -1 {
513 return func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
514 return n.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. && countNth(n)%2 == oddOrEven
515 }, nil
516 }
517 // Check against '3n+4' pattern
518 r := regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.13 regexp/MustCompile().(`([0-9]+)n[ ]?\+[ ]?([0-9])`)
519 subMatch := r.FindAllStringSubmatchscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#FindAllStringSubmatch().(number, -1)
520 if len(subMatch) == 1 && len(subMatch[0]) == 3 {
521 cycle, _ := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(subMatch[0][1])
522 offset, _ := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(subMatch[0][2])
523 return func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
524 return n.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. && countNth(n)%cycle == offset
525 }, nil
526 }
527 // check against 'n+2' pattern
528 r = regexpscip-go gomod github.com/golang/go/src go1.13 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.13 regexp/MustCompile().(`n[ ]?\+[ ]?([0-9])`)
529 subMatch = r.FindAllStringSubmatchscip-go gomod github.com/golang/go/src go1.13 regexp/Regexp#FindAllStringSubmatch().(number, -1)
530 if len(subMatch) == 1 && len(subMatch[0]) == 2 {
531 offset, _ := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(subMatch[0][1])
532 return func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
533 return n.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. && countNth(n) >= offset
534 }, nil
535 }
536 // the only other option is a numeric value
537 nth, err := strconvscip-go gomod github.com/golang/go/src go1.13 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.13 strconv/Atoi().(nthString[:i])
538 if err != nil {
539 return nil, err
540 } else if nth <= 0 {
541 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Argument to '%s' must be greater than 0", pseudoName)
542 }
543 return func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
544 return n.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. && countNth(n) == nth
545 }, nil
546 }
547
548 // Parse a :contains("") selector
549 // expects the input to be everything after the open parenthesis
550 // e.g. for `contains("Help")` the argument would be `"Help")`
551 func parseContainsPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseContainsPseudo().(cmd string) (PseudoClassscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PseudoClass#, error) {
552 var s scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.Scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#
553 s.Initscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Init().(stringsscip-go gomod github.com/golang/go/src go1.13 strings/.NewReaderscip-go gomod github.com/golang/go/src go1.13 strings/NewReader().(cmd))
554 switch s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().() {
555 case '"':
556 default:
557 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Malformed 'contains(\"\")' selector")
558 }
559 textToContain := bytesscip-go gomod github.com/golang/go/src go1.13 bytes/.NewBufferscip-go gomod github.com/golang/go/src go1.13 bytes/NewBuffer().([]byte{})
560 for {
561 r := s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
562 switch r {
563 case '"':
564 // ')' then EOF must follow '"'
565 if s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().() != ')' {
566 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Malformed 'contains(\"\")' selector")
567 }
568 if s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().() != scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF. {
569 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("'contains(\"\")' must end selector")
570 }
571 text := textToContain.Stringscip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#String().()
572 contains := func(node *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
573 for c := node.FirstChildscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#FirstChild.; c != nil; c = c.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
574 if c.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.TextNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/TextNode. {
575 if stringsscip-go gomod github.com/golang/go/src go1.13 strings/.Containsscip-go gomod github.com/golang/go/src go1.13 strings/Contains().(c.Datascip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Data., text) {
576 return true
577 }
578 }
579 }
580 return false
581 }
582 return contains, nil
583 case '\\':
584 s.Nextscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/Scanner#Next().()
585 case scannerscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/.EOFscip-go gomod github.com/golang/go/src go1.13 `text/scanner`/EOF.:
586 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("Malformed 'contains(\"\")' selector")
587 default:
588 if _, err := textToContain.WriteRunescip-go gomod github.com/golang/go/src go1.13 bytes/Buffer#WriteRune().(r); err != nil {
589 return nil, err
590 }
591 }
592 }
593 }
594
595 // Parse a :not(selector) selector
596 // expects the input to be everything after the open parenthesis
597 // e.g. for `not(div#id)` the argument would be `div#id)`
598 func parseNotPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseNotPseudo().(cmd string) (PseudoClassscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PseudoClass#, error) {
599 if len(cmd) < 2 {
600 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("malformed ':not' selector")
601 }
602 endQuote, cmd := cmd[len(cmd)-1], cmd[:len(cmd)-1]
603 selector, err := ParseSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseSelector().(cmd)
604 if err != nil {
605 return nil, err
606 }
607 if endQuote != ')' {
608 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("unmatched '('")
609 }
610 return func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
611 return !selector.Matchscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Match().(n)
612 }, nil
613 }
614
615 // Parse a :parent-of(selector) selector
616 // expects the input to be everything after the open parenthesis
617 // e.g. for `parent-of(div#id)` the argument would be `div#id)`
618 func parseParentOfPseudoscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/parseParentOfPseudo().(cmd string) (PseudoClassscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/PseudoClass#, error) {
619 if len(cmd) < 2 {
620 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("malformed ':parent-of' selector")
621 }
622 endQuote, cmd := cmd[len(cmd)-1], cmd[:len(cmd)-1]
623 selector, err := ParseSelectorscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/ParseSelector().(cmd)
624 if err != nil {
625 return nil, err
626 }
627 if endQuote != ')' {
628 return nil, fmtscip-go gomod github.com/golang/go/src go1.13 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.13 fmt/Errorf().("unmatched '('")
629 }
630 return func(n *htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.Nodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#) bool {
631 for c := n.FirstChildscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#FirstChild.; c != nil; c = c.NextSiblingscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#NextSibling. {
632 if c.Typescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/Node#Type. == htmlscip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/.ElementNodescip-go gomod golang.org/x/net 4d38db76854b `golang.org/x/net/html`/ElementNode. && selector.Matchscip-go gomod github.com/ericchiang/pup . `github.com/ericchiang/pup`/CSSSelector#Match().(c) {
633 return true
634 }
635 }
636 return false
637 }, nil
638 }