@@ -76,7 +76,7 @@ def __init__(self, tree, pseudo_element=None):
76
76
#: +-------------------------+----------------+--------------------------------+
77
77
#: | Invalid pseudo-class | ``li:marker`` | ``None`` |
78
78
#: +-------------------------+----------------+--------------------------------+
79
- #: | Functinal | ``a::foo(2)`` | ``FunctionalPseudoElement(…)`` |
79
+ #: | Functional | ``a::foo(2)`` | ``FunctionalPseudoElement(…)`` |
80
80
#: +-------------------------+----------------+--------------------------------+
81
81
#:
82
82
#: .. _Lists3: http://www.w3.org/TR/2011/WD-css3-lists-20110524/#marker-pseudoelement
@@ -92,6 +92,20 @@ def __repr__(self):
92
92
return '%s[%r%s]' % (
93
93
self .__class__ .__name__ , self .parsed_tree , pseudo_element )
94
94
95
+ def canonical (self ):
96
+ """Return a CSS representation for this selector (a string)
97
+ """
98
+ if isinstance (self .pseudo_element , FunctionalPseudoElement ):
99
+ pseudo_element = '::%s' % self .pseudo_element .canonical ()
100
+ elif self .pseudo_element :
101
+ pseudo_element = '::%s' % self .pseudo_element
102
+ else :
103
+ pseudo_element = ''
104
+ res = '%s%s' % (self .parsed_tree .canonical (), pseudo_element )
105
+ if len (res ) > 1 :
106
+ res = res .lstrip ('*' )
107
+ return res
108
+
95
109
def specificity (self ):
96
110
"""Return the specificity_ of this selector as a tuple of 3 integers.
97
111
@@ -116,6 +130,9 @@ def __repr__(self):
116
130
return '%s[%r.%s]' % (
117
131
self .__class__ .__name__ , self .selector , self .class_name )
118
132
133
+ def canonical (self ):
134
+ return '%s.%s' % (self .selector .canonical (), self .class_name )
135
+
119
136
def specificity (self ):
120
137
a , b , c = self .selector .specificity ()
121
138
b += 1
@@ -151,6 +168,10 @@ def __repr__(self):
151
168
def argument_types (self ):
152
169
return [token .type for token in self .arguments ]
153
170
171
+ def canonical (self ):
172
+ args = '' .join (token .css () for token in self .arguments )
173
+ return '%s(%s)' % (self .name , args )
174
+
154
175
def specificity (self ):
155
176
a , b , c = self .selector .specificity ()
156
177
b += 1
@@ -174,6 +195,10 @@ def __repr__(self):
174
195
def argument_types (self ):
175
196
return [token .type for token in self .arguments ]
176
197
198
+ def canonical (self ):
199
+ args = '' .join (token .css () for token in self .arguments )
200
+ return '%s:%s(%s)' % (self .selector .canonical (), self .name , args )
201
+
177
202
def specificity (self ):
178
203
a , b , c = self .selector .specificity ()
179
204
b += 1
@@ -192,6 +217,9 @@ def __repr__(self):
192
217
return '%s[%r:%s]' % (
193
218
self .__class__ .__name__ , self .selector , self .ident )
194
219
220
+ def canonical (self ):
221
+ return '%s:%s' % (self .selector .canonical (), self .ident )
222
+
195
223
def specificity (self ):
196
224
a , b , c = self .selector .specificity ()
197
225
b += 1
@@ -210,6 +238,12 @@ def __repr__(self):
210
238
return '%s[%r:not(%r)]' % (
211
239
self .__class__ .__name__ , self .selector , self .subselector )
212
240
241
+ def canonical (self ):
242
+ subsel = self .subselector .canonical ()
243
+ if len (subsel ) > 1 :
244
+ subsel = subsel .lstrip ('*' )
245
+ return '%s:not(%s)' % (self .selector .canonical (), subsel )
246
+
213
247
def specificity (self ):
214
248
a1 , b1 , c1 = self .selector .specificity ()
215
249
a2 , b2 , c2 = self .subselector .specificity ()
@@ -238,7 +272,20 @@ def __repr__(self):
238
272
else :
239
273
return '%s[%r[%s %s %r]]' % (
240
274
self .__class__ .__name__ , self .selector , attrib ,
241
- self .operator , self .value )
275
+ self .operator , self .value .value )
276
+
277
+ def canonical (self ):
278
+ if self .namespace :
279
+ attrib = '%s|%s' % (self .namespace , self .attrib )
280
+ else :
281
+ attrib = self .attrib
282
+
283
+ if self .operator == 'exists' :
284
+ op = attrib
285
+ else :
286
+ op = '%s%s%s' % (attrib , self .operator , self .value .css ())
287
+
288
+ return '%s[%s]' % (self .selector .canonical (), op )
242
289
243
290
def specificity (self ):
244
291
a , b , c = self .selector .specificity ()
@@ -258,10 +305,13 @@ def __init__(self, namespace=None, element=None):
258
305
self .element = element
259
306
260
307
def __repr__ (self ):
308
+ return '%s[%s]' % (self .__class__ .__name__ , self .canonical ())
309
+
310
+ def canonical (self ):
261
311
element = self .element or '*'
262
312
if self .namespace :
263
313
element = '%s|%s' % (self .namespace , element )
264
- return '%s[%s]' % ( self . __class__ . __name__ , element )
314
+ return element
265
315
266
316
def specificity (self ):
267
317
if self .element :
@@ -282,6 +332,9 @@ def __repr__(self):
282
332
return '%s[%r#%s]' % (
283
333
self .__class__ .__name__ , self .selector , self .id )
284
334
335
+ def canonical (self ):
336
+ return '%s#%s' % (self .selector .canonical (), self .id )
337
+
285
338
def specificity (self ):
286
339
a , b , c = self .selector .specificity ()
287
340
a += 1
@@ -303,6 +356,13 @@ def __repr__(self):
303
356
return '%s[%r %s %r]' % (
304
357
self .__class__ .__name__ , self .selector , comb , self .subselector )
305
358
359
+ def canonical (self ):
360
+ subsel = self .subselector .canonical ()
361
+ if len (subsel ) > 1 :
362
+ subsel = subsel .lstrip ('*' )
363
+ return '%s %s %s' % (
364
+ self .selector .canonical (), self .combinator , subsel )
365
+
306
366
def specificity (self ):
307
367
a1 , b1 , c1 = self .selector .specificity ()
308
368
a2 , b2 , c2 = self .subselector .specificity ()
@@ -546,7 +606,7 @@ def parse_attrib(selector, stream):
546
606
if next != ('DELIM' , ']' ):
547
607
raise SelectorSyntaxError (
548
608
"Expected ']', got %s" % (next ,))
549
- return Attrib (selector , namespace , attrib , op , value . value )
609
+ return Attrib (selector , namespace , attrib , op , value )
550
610
551
611
552
612
def parse_series (tokens ):
@@ -601,6 +661,12 @@ def is_delim(self, *values):
601
661
type = property (operator .itemgetter (0 ))
602
662
value = property (operator .itemgetter (1 ))
603
663
664
+ def css (self ):
665
+ if self .type == 'STRING' :
666
+ return repr (self .value )
667
+ else :
668
+ return self .value
669
+
604
670
605
671
class EOFToken (Token ):
606
672
def __new__ (cls , pos ):
0 commit comments