-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcss.go
766 lines (697 loc) · 17.5 KB
/
css.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
// Package css implements CSS selector HTML search.
//
// Selectors compiled by this package search through golang.org/x/net/html nodes and should
// be used in conjunction with that package.
//
// data := `<p>
// <h2 id="foo">a header</h2>
// <h2 id="bar">another header</h2>
// </p>`
//
// sel, err := css.Parse("h2#foo")
// if err != nil {
// // handle error
// }
// node, err := html.Parse(strings.NewReader(data))
// if err != nil {
// // handle error
// }
// elements := sel.Select(node)
//
// This package aims to support Selectors Level 4 https://www.w3.org/TR/selectors-4/.
//
// The universal selector (*) is supported, along with:
//
// a // Type selector
// ns|a // Type selector with namespace
// .red // Class selector
// #demo // ID selector
// [attr] // Attribute selector
// [attr=value] // Attribute selector value
// [attr~=value] // Attribute selector element of list
// [attr|=value] // Attribute selector value or "{value}-" prefix
// [attr^=value] // Attribute selector prefix
// [attr$=value] // Attribute selector suffix
// [attr*=value] // Attribute selector contains value
// [attr=value i] // Attribute selector case insensitive modifier
// foo, bar // Selector list
// foo bar // Descendant combinator
// foo > bar // Child combinator
// foo ~ bar // General sibling combinator
// foo + bar // Adjacent sibling combinator
// :empty // Element with no children
// :first-child // First child of parent
// :first-of-type // First child of its type of parent
// :last-child // Last child of parent
// :last-of-type // Last child of its type of parent
// :only-child // Only child of parent
// :only-of-type // Only child of its type parent
// :root // Root element
// :nth-child(An+B) // Positional child matcher
// :nth-last-child(An+B) // Reverse positional child matcher
// :nth-last-of-type(An+B) // Reverse positional child matcher of type
// :nth-of-type(An+B) // Positional child matcher of type
package css
import (
"errors"
"fmt"
"strings"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// ParseError is returned indicating an lex, parse, or compilation error with
// the associated position in the string the error occurred.
type ParseError struct {
Pos int
Msg string
}
// Error returns a formatted version of the error.
func (p *ParseError) Error() string {
return fmt.Sprintf("css: %s at position %d", p.Msg, p.Pos)
}
func errorf(pos int, msg string, v ...interface{}) error {
return &ParseError{pos, fmt.Sprintf(msg, v...)}
}
// Selector is a compiled CSS selector.
type Selector struct {
s []*selector
}
// Select returns any matches from a parsed HTML document.
func (s *Selector) Select(n *html.Node) []*html.Node {
selected := []*html.Node{}
for _, sel := range s.s {
selected = append(selected, sel.find(n)...)
}
return selected
}
func findAll(n *html.Node, fn func(n *html.Node) bool) []*html.Node {
var m []*html.Node
if fn(n) {
m = append(m, n)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type != html.ElementNode {
continue
}
m = append(m, findAll(c, fn)...)
}
return m
}
// MustParse is like Parse but panics on errors.
func MustParse(s string) *Selector {
sel, err := Parse(s)
if err != nil {
panic(err)
}
return sel
}
// Parse compiles a complex selector list from a string. The parser supports
// Selectors Level 4.
//
// Multiple selectors are supported through comma separated values. For example
// "h1, h2".
//
// Parse reports the first error hit when compiling.
func Parse(s string) (*Selector, error) {
p := newParser(s)
list, err := p.parse()
if err != nil {
var perr *parseErr
if errors.As(err, &perr) {
return nil, &ParseError{perr.t.pos, perr.msg}
}
var lerr *lexErr
if errors.As(err, &lerr) {
return nil, &ParseError{lerr.last, lerr.msg}
}
return nil, err
}
sel := &Selector{}
c := compiler{maxErrs: 1}
for _, s := range list {
m := c.compile(&s)
if m == nil {
continue
}
sel.s = append(sel.s, m)
}
if err := c.err(); err != nil {
return nil, err
}
return sel, nil
}
type compiler struct {
sels []complexSelector
maxErrs int
errs []error
}
func (c *compiler) err() error {
if len(c.errs) == 0 {
return nil
}
return c.errs[0]
}
func (c *compiler) errorf(pos int, msg string, v ...interface{}) bool {
err := &ParseError{pos, fmt.Sprintf(msg, v...)}
c.errs = append(c.errs, err)
if len(c.errs) >= c.maxErrs {
return true
}
return false
}
type combinator interface {
find(n *html.Node) []*html.Node
}
type selector struct {
m *compoundSelectorMatcher
combinators []combinator
}
func (s selector) find(n *html.Node) []*html.Node {
nodes := findAll(n, s.m.match)
for _, c := range s.combinators {
var ns []*html.Node
for _, n := range nodes {
ns = append(ns, c.find(n)...)
}
nodes = ns
}
return nodes
}
type descendantCombinator struct {
m *compoundSelectorMatcher
}
func (c *descendantCombinator) find(n *html.Node) []*html.Node {
var nodes []*html.Node
for n := n.FirstChild; n != nil; n = n.NextSibling {
if n.Type != html.ElementNode {
continue
}
nodes = append(nodes, findAll(n, c.m.match)...)
}
return nodes
}
type childCombinator struct {
m *compoundSelectorMatcher
}
func (c *childCombinator) find(n *html.Node) []*html.Node {
var nodes []*html.Node
for n := n.FirstChild; n != nil; n = n.NextSibling {
if n.Type != html.ElementNode {
continue
}
if c.m.match(n) {
nodes = append(nodes, n)
}
}
return nodes
}
type adjacentCombinator struct {
m *compoundSelectorMatcher
}
func (c *adjacentCombinator) find(n *html.Node) []*html.Node {
var (
nodes []*html.Node
prev *html.Node
next *html.Node
)
for prev = n.PrevSibling; prev != nil; prev = prev.PrevSibling {
if prev.Type == html.ElementNode {
break
}
}
for next = n.NextSibling; next != nil; next = next.NextSibling {
if next.Type == html.ElementNode {
break
}
}
if prev != nil && c.m.match(prev) {
nodes = append(nodes, prev)
}
if next != nil && c.m.match(next) {
nodes = append(nodes, next)
}
return nodes
}
type siblingCombinator struct {
m *compoundSelectorMatcher
}
func (c *siblingCombinator) find(n *html.Node) []*html.Node {
var nodes []*html.Node
for n := n.PrevSibling; n != nil; n = n.PrevSibling {
if n.Type != html.ElementNode {
continue
}
if c.m.match(n) {
nodes = append(nodes, n)
}
}
for n := n.NextSibling; n != nil; n = n.NextSibling {
if n.Type != html.ElementNode {
continue
}
if c.m.match(n) {
nodes = append(nodes, n)
}
}
return nodes
}
func (c *compiler) compile(s *complexSelector) *selector {
m := &selector{
m: c.compoundSelector(&s.sel),
}
curr := s
for {
if curr.next == nil {
return m
}
sel := c.compoundSelector(&curr.next.sel)
comb := curr.combinator
curr = curr.next
var cm combinator
switch comb {
case "":
cm = &descendantCombinator{sel}
case ">":
cm = &childCombinator{sel}
case "+":
cm = &adjacentCombinator{sel}
case "~":
cm = &siblingCombinator{sel}
default:
c.errorf(curr.pos, "unexpected combinator: %s", comb)
continue
}
m.combinators = append(m.combinators, cm)
}
return m
}
type compoundSelectorMatcher struct {
m *typeSelectorMatcher
scm []subclassSelectorMatcher
}
func (c *compoundSelectorMatcher) match(n *html.Node) bool {
if c.m != nil {
if !c.m.match(n) {
return false
}
}
for _, m := range c.scm {
if !m.match(n) {
return false
}
}
return true
}
func (c *compiler) compoundSelector(s *compoundSelector) *compoundSelectorMatcher {
m := &compoundSelectorMatcher{}
if s.typeSelector != nil {
m.m = c.typeSelector(s.typeSelector)
}
for _, sc := range s.subClasses {
scm := c.subclassSelector(&sc)
if scm != nil {
m.scm = append(m.scm, *scm)
}
}
if len(s.pseudoSelectors) != 0 {
// It's not clear that it makes sense for us to support pseudo elements,
// since this is more about modifying added elements than selecting elements.
//
// https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-elements
if c.errorf(s.pos, "pseudo element selectors not supported") {
return nil
}
}
return m
}
type subclassSelectorMatcher struct {
idSelector string
classSelector string
attributeSelector *attributeSelectorMatcher
pseudoSelector func(*html.Node) bool
}
func (s *subclassSelectorMatcher) match(n *html.Node) bool {
if s.idSelector != "" {
for _, a := range n.Attr {
if a.Key == "id" && a.Val == s.idSelector {
return true
}
}
return false
}
if s.classSelector != "" {
for _, a := range n.Attr {
if a.Key == "class" {
for _, val := range strings.Fields(a.Val) {
if val == s.classSelector {
return true
}
}
}
}
return false
}
if s.attributeSelector != nil {
return s.attributeSelector.match(n)
}
if s.pseudoSelector != nil {
return s.pseudoSelector(n)
}
return false
}
func (c *compiler) subclassSelector(s *subclassSelector) *subclassSelectorMatcher {
m := &subclassSelectorMatcher{
idSelector: s.idSelector,
classSelector: s.classSelector,
}
if s.attributeSelector != nil {
m.attributeSelector = c.attributeSelector(s.attributeSelector)
}
if s.pseudoClassSelector != nil {
m.pseudoSelector = c.pseudoClassSelector(s.pseudoClassSelector)
}
return m
}
type pseudoClassSelectorMatcher struct {
matcher func(*html.Node) bool
}
func (c *compiler) pseudoClassSelector(s *pseudoClassSelector) func(*html.Node) bool {
// https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes
switch s.ident {
case "empty":
return emptyMatcher
case "first-child":
return firstChildMatcher
case "first-of-type":
return firstOfTypeMatcher
case "last-child":
return lastChildMatcher
case "last-of-type":
return lastOfTypeMatcher
case "only-child":
return onlyChildMatcher
case "only-of-type":
return onlyOfTypeMatcher
case "root":
return rootMatcher
case "":
default:
c.errorf(s.pos, "unsupported pseudo-class selector: %s", s.ident)
return nil
}
switch s.function {
case "nth-child(":
return c.nthChild(s)
case "nth-last-child(":
return c.nthLastChild(s)
case "nth-last-of-type(":
return c.nthLastOfType(s)
case "nth-of-type(":
return c.nthOfType(s)
default:
c.errorf(s.pos, "unsupported pseudo-class selector: %s", s.function)
return nil
}
return nil
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-child
func (c *compiler) nthChild(s *pseudoClassSelector) func(n *html.Node) bool {
nth := c.compileNth(s)
if nth == nil {
return nil
}
return func(n *html.Node) bool {
var i int64 = 1
for s := n.PrevSibling; s != nil; s = s.PrevSibling {
if s.Type == html.ElementNode {
i++
}
}
return nth.matches(i)
}
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-of-type
func (c *compiler) nthOfType(s *pseudoClassSelector) func(n *html.Node) bool {
nth := c.compileNth(s)
if nth == nil {
return nil
}
return func(n *html.Node) bool {
var i int64 = 1
for s := n.PrevSibling; s != nil; s = s.PrevSibling {
if s.Type == html.ElementNode && s.DataAtom == n.DataAtom {
i++
}
}
return nth.matches(i)
}
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-child
func (c *compiler) nthLastChild(s *pseudoClassSelector) func(n *html.Node) bool {
nth := c.compileNth(s)
if nth == nil {
return nil
}
return func(n *html.Node) bool {
var i int64 = 1
for s := n.NextSibling; s != nil; s = s.NextSibling {
if s.Type == html.ElementNode {
i++
}
}
return nth.matches(i)
}
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:nth-last-of-type
func (c *compiler) nthLastOfType(s *pseudoClassSelector) func(n *html.Node) bool {
nth := c.compileNth(s)
if nth == nil {
return nil
}
return func(n *html.Node) bool {
var i int64 = 1
for s := n.NextSibling; s != nil; s = s.NextSibling {
if s.Type == html.ElementNode && n.DataAtom == s.DataAtom {
i++
}
}
return nth.matches(i)
}
}
// nth holds a computed An+B value for :nth-child() and its associated selectors.
type nth struct {
a int64
b int64
}
func (nth nth) matches(val int64) bool {
// Is there a value for "n" given "An+B=val" where "n" is non-negative?
// An + B = val
// An = val - B
// n = (val - B) / A
if nth.a == 0 {
return val == nth.b
}
return (val-nth.b)%nth.a == 0 && (val-nth.b)/nth.a >= 0
}
func (c *compiler) compileNth(s *pseudoClassSelector) *nth {
p := newParserFromTokens(s.args)
a, err := p.aNPlusB()
if err != nil {
c.errorf(s.pos, "failed to parse <an+b> expression: %v", err)
return nil
}
if err := p.expectWhitespaceOrEOF(); err != nil {
c.errorf(s.pos, "failed to parse <an+b> expression: %v", err)
return nil
}
return a
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:empty
func emptyMatcher(n *html.Node) bool {
for c := n.FirstChild; c != nil; c = c.NextSibling {
if c.Type == html.ElementNode {
return false
}
}
return true
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child
func firstChildMatcher(n *html.Node) bool {
for s := n.PrevSibling; s != nil; s = s.PrevSibling {
if s.Type == html.ElementNode {
return false
}
}
return true
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:first-of-type
func firstOfTypeMatcher(n *html.Node) bool {
for s := n.PrevSibling; s != nil; s = s.PrevSibling {
if s.Type != html.ElementNode {
continue
}
if s.DataAtom == n.DataAtom {
return false
}
}
return true
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child
func lastChildMatcher(n *html.Node) bool {
for s := n.NextSibling; s != nil; s = s.NextSibling {
if s.Type == html.ElementNode {
return false
}
}
return true
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:last-of-type
func lastOfTypeMatcher(n *html.Node) bool {
for s := n.NextSibling; s != nil; s = s.NextSibling {
if s.Type != html.ElementNode {
continue
}
if s.DataAtom == n.DataAtom {
return false
}
}
return true
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:only-child
func onlyChildMatcher(n *html.Node) bool {
return firstChildMatcher(n) && lastChildMatcher(n)
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:only-of-type
func onlyOfTypeMatcher(n *html.Node) bool {
return firstOfTypeMatcher(n) && lastOfTypeMatcher(n)
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/:root
func rootMatcher(n *html.Node) bool {
return n.Parent == nil
}
type attributeSelectorMatcher struct {
ns namespaceMatcher
fn func(key, val string) bool
}
func (a *attributeSelectorMatcher) match(n *html.Node) bool {
for _, attr := range n.Attr {
if a.ns.match(attr.Namespace) && a.fn(attr.Key, attr.Val) {
return true
}
}
return false
}
func (c *compiler) attributeSelector(s *attributeSelector) *attributeSelectorMatcher {
m := &attributeSelectorMatcher{
ns: newNamespaceMatcher(s.wqName.hasPrefix, s.wqName.prefix),
}
key := s.wqName.value
val := s.val
if s.modifier {
key = strings.ToLower(key)
val = strings.ToLower(val)
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors
switch s.matcher {
case "=":
m.fn = func(k, v string) bool { return k == key && v == val }
case "~=":
m.fn = func(k, v string) bool {
if k != key {
return false
}
for _, f := range strings.Fields(v) {
if f == val {
return true
}
}
return false
}
case "|=":
// "Represents elements with an attribute name of attr whose value can be
// exactly value or can begin with value immediately followed by a hyphen,
// - (U+002D). It is often used for language subcode matches."
m.fn = func(k, v string) bool {
return k == key && (v == val || strings.HasPrefix(v, val+"-"))
}
case "^=":
m.fn = func(k, v string) bool {
return k == key && strings.HasPrefix(v, val)
}
case "$=":
m.fn = func(k, v string) bool {
return k == key && strings.HasSuffix(v, val)
}
case "*=":
m.fn = func(k, v string) bool {
return k == key && strings.Contains(v, val)
}
case "":
m.fn = func(k, v string) bool { return k == key }
default:
c.errorf(s.pos, "unsupported attribute matcher: %s", s.matcher)
return nil
}
if s.modifier {
fn := m.fn
m.fn = func(k, v string) bool {
k = strings.ToLower(k)
v = strings.ToLower(v)
return fn(k, v)
}
}
return m
}
// namespaceMatcher performs <ns-prefix> matching for elements and attributes.
type namespaceMatcher struct {
noNamespace bool
namespace string
}
func newNamespaceMatcher(hasPrefix bool, prefix string) namespaceMatcher {
if !hasPrefix {
return namespaceMatcher{}
}
if prefix == "" {
return namespaceMatcher{noNamespace: true}
}
if prefix == "*" {
return namespaceMatcher{}
}
return namespaceMatcher{namespace: prefix}
}
func (n *namespaceMatcher) match(ns string) bool {
if n.noNamespace {
return ns == ""
}
if n.namespace == "" {
return true
}
return n.namespace == ns
}
type typeSelectorMatcher struct {
allAtoms bool
atom atom.Atom
ns namespaceMatcher
}
func (t *typeSelectorMatcher) match(n *html.Node) (ok bool) {
if !(t.allAtoms || t.atom == n.DataAtom) {
return false
}
return t.ns.match(n.Namespace)
}
func (c *compiler) typeSelector(s *typeSelector) *typeSelectorMatcher {
m := &typeSelectorMatcher{}
if s.value == "*" {
m.allAtoms = true
} else {
a := atom.Lookup([]byte(s.value))
if a == 0 {
if c.errorf(s.pos, "unrecognized node name: %s", s.value) {
return nil
}
}
m.atom = a
}
m.ns = newNamespaceMatcher(s.hasPrefix, s.prefix)
return m
}