@@ -161,16 +161,18 @@ def __init__(self, name, arguments):
161
161
self .arguments = arguments
162
162
163
163
def __repr__ (self ):
164
- return '%s[::%s(%r)]' % (
165
- self .__class__ .__name__ , self .name ,
166
- [token .value for token in self .arguments ])
164
+ return "%s[::%s(%r)]" % (
165
+ self .__class__ .__name__ ,
166
+ self .name ,
167
+ [token .value for token in self .arguments [0 ]],
168
+ )
167
169
168
170
def argument_types (self ):
169
171
return [token .type for token in self .arguments ]
170
172
171
173
def canonical (self ):
172
- args = '' .join (token .css () for token in self .arguments )
173
- return ' %s(%s)' % (self .name , args )
174
+ args = "" .join (token .css () for token in self .arguments [ 0 ] )
175
+ return " %s(%s)" % (self .name , args )
174
176
175
177
def specificity (self ):
176
178
a , b , c = self .selector .specificity ()
@@ -182,12 +184,27 @@ class Function(object):
182
184
"""
183
185
Represents selector:name(expr)
184
186
"""
185
- def __init__ (self , selector , name , arguments ):
187
+
188
+ def __init__ (self , selector , name , arguments , of_type = None ):
186
189
self .selector = selector
187
190
self .name = ascii_lower (name )
188
191
self .arguments = arguments
189
192
193
+ # for css4 :nth-child(An+B of Subselector)
194
+ try :
195
+ self .of_type = of_type [0 ]
196
+ except (IndexError , TypeError ):
197
+ self .of_type = None
198
+
190
199
def __repr__ (self ):
200
+ if self .of_type :
201
+ return "%s[%r:%s(%r of %s)]" % (
202
+ self .__class__ .__name__ ,
203
+ self .selector ,
204
+ self .name ,
205
+ [token .value for token in self .arguments ],
206
+ self .of_type .__repr__ (),
207
+ )
191
208
return '%s[%r:%s(%r)]' % (
192
209
self .__class__ .__name__ , self .selector , self .name ,
193
210
[token .value for token in self .arguments ])
@@ -539,7 +556,8 @@ def parse_simple_selector(stream, inside_negation=False):
539
556
raise SelectorSyntaxError ("Expected ')', got %s" % (next ,))
540
557
result = Negation (result , argument )
541
558
else :
542
- result = Function (result , ident , parse_arguments (stream ))
559
+ arguments , of_type = parse_arguments (stream )
560
+ result = Function (result , ident , arguments , of_type )
543
561
else :
544
562
raise SelectorSyntaxError (
545
563
"Expected selector, got %s" % (peek ,))
@@ -554,16 +572,33 @@ def parse_arguments(stream):
554
572
while 1 :
555
573
stream .skip_whitespace ()
556
574
next = stream .next ()
557
- if next .type in ('IDENT' , 'STRING' , 'NUMBER' ) or next in [
558
- ('DELIM' , '+' ), ('DELIM' , '-' )]:
575
+ if next == ("IDENT" , "of" ):
576
+ stream .skip_whitespace ()
577
+ of_type = parse_of_type (stream )
578
+ return arguments , of_type
579
+ elif next .type in ("IDENT" , "STRING" , "NUMBER" ) or next in [
580
+ ("DELIM" , "+" ),
581
+ ("DELIM" , "-" ),
582
+ ]:
559
583
arguments .append (next )
560
584
elif next == ('DELIM' , ')' ):
561
- return arguments
585
+ return arguments , None
562
586
else :
563
587
raise SelectorSyntaxError (
564
588
"Expected an argument, got %s" % (next ,))
565
589
566
590
591
+ def parse_of_type (stream ):
592
+ subselector = ""
593
+ while 1 :
594
+ next = stream .next ()
595
+ if next == ("DELIM" , ")" ):
596
+ break
597
+ subselector += next .value
598
+ result = parse (subselector )
599
+ return result
600
+
601
+
567
602
def parse_attrib (selector , stream ):
568
603
stream .skip_whitespace ()
569
604
attrib = stream .next_ident_or_star ()
@@ -620,6 +655,7 @@ def parse_series(tokens):
620
655
for token in tokens :
621
656
if token .type == 'STRING' :
622
657
raise ValueError ('String tokens not allowed in series.' )
658
+
623
659
s = '' .join (token .value for token in tokens ).strip ()
624
660
if s == 'odd' :
625
661
return 2 , 1
@@ -630,7 +666,7 @@ def parse_series(tokens):
630
666
if 'n' not in s :
631
667
# Just b
632
668
return 0 , int (s )
633
- a , b = s .split ('n' , 1 )
669
+ a , b = s .split ("n" , 1 )
634
670
if not a :
635
671
a = 1
636
672
elif a == '-' or a == '+' :
@@ -641,6 +677,7 @@ def parse_series(tokens):
641
677
b = 0
642
678
else :
643
679
b = int (b )
680
+
644
681
return a , b
645
682
646
683
0 commit comments