2323//! Meeting all these constraints without extra memmove traffic during parsing is non-trivial. This
2424//! module encapsulates those details and presents an easy-to-use API for the parser.
2525
26- use crate :: parser:: { Combinator , Component , RelativeSelector , Selector , SelectorImpl , ParseRelative } ;
26+ use crate :: parser:: {
27+ Combinator , Component , ParseRelative , RelativeSelector , Selector , SelectorImpl ,
28+ } ;
2729use crate :: sink:: Push ;
30+ use bitflags:: bitflags;
31+ use derive_more:: { Add , AddAssign } ;
2832use servo_arc:: { Arc , ThinArc } ;
2933use smallvec:: SmallVec ;
3034use std:: cmp;
3135use std:: slice;
32- use bitflags:: bitflags;
33- use derive_more:: { Add , AddAssign } ;
3436
3537#[ cfg( feature = "to_shmem" ) ]
3638use to_shmem_derive:: ToShmem ;
@@ -88,9 +90,15 @@ impl<Impl: SelectorImpl> SelectorBuilder<Impl> {
8890
8991 /// Consumes the builder, producing a Selector.
9092 #[ inline( always) ]
91- pub fn build ( & mut self , parse_relative : ParseRelative ) -> ThinArc < SpecificityAndFlags , Component < Impl > > {
93+ pub fn build (
94+ & mut self ,
95+ parse_relative : ParseRelative ,
96+ ) -> ThinArc < SpecificityAndFlags , Component < Impl > > {
9297 // Compute the specificity and flags.
93- let sf = specificity_and_flags ( self . components . iter ( ) , /* for_nesting_parent = */ false ) ;
98+ let sf = specificity_and_flags (
99+ self . components . iter ( ) ,
100+ /* for_nesting_parent = */ false ,
101+ ) ;
94102 self . build_with_specificity_and_flags ( sf, parse_relative)
95103 }
96104
@@ -103,17 +111,23 @@ impl<Impl: SelectorImpl> SelectorBuilder<Impl> {
103111 parse_relative : ParseRelative ,
104112 ) -> ThinArc < SpecificityAndFlags , Component < Impl > > {
105113 let implicit_addition = match parse_relative {
106- ParseRelative :: ForNesting if !spec. flags . intersects ( SelectorFlags :: HAS_PARENT ) => Some ( ( Component :: ParentSelector , SelectorFlags :: HAS_PARENT ) ) ,
107- ParseRelative :: ForScope if !spec. flags . intersects ( SelectorFlags :: HAS_SCOPE | SelectorFlags :: HAS_PARENT ) => Some ( ( Component :: ImplicitScope , SelectorFlags :: HAS_SCOPE ) ) ,
114+ ParseRelative :: ForNesting if !spec. flags . intersects ( SelectorFlags :: HAS_PARENT ) => {
115+ Some ( ( Component :: ParentSelector , SelectorFlags :: HAS_PARENT ) )
116+ } ,
117+ ParseRelative :: ForScope
118+ if !spec
119+ . flags
120+ . intersects ( SelectorFlags :: HAS_SCOPE | SelectorFlags :: HAS_PARENT ) =>
121+ {
122+ Some ( ( Component :: ImplicitScope , SelectorFlags :: HAS_SCOPE ) )
123+ } ,
108124 _ => None ,
109125 } ;
110126 let implicit_selector_and_combinator;
111127 let implicit_selector = if let Some ( ( component, flag) ) = implicit_addition {
112128 spec. flags . insert ( flag) ;
113- implicit_selector_and_combinator = [
114- Component :: Combinator ( Combinator :: Descendant ) ,
115- component,
116- ] ;
129+ implicit_selector_and_combinator =
130+ [ Component :: Combinator ( Combinator :: Descendant ) , component] ;
117131 & implicit_selector_and_combinator[ ..]
118132 } else {
119133 & [ ]
@@ -122,15 +136,23 @@ impl<Impl: SelectorImpl> SelectorBuilder<Impl> {
122136 // As an optimization, for a selector without combinators, we can just keep the order
123137 // as-is.
124138 if self . last_compound_start . is_none ( ) {
125- return Arc :: from_header_and_iter ( spec, ExactChain ( self . components . drain ( ..) , implicit_selector. iter ( ) . cloned ( ) ) ) ;
139+ return Arc :: from_header_and_iter (
140+ spec,
141+ ExactChain ( self . components . drain ( ..) , implicit_selector. iter ( ) . cloned ( ) ) ,
142+ ) ;
126143 }
127144
128145 self . reverse_last_compound ( ) ;
129- Arc :: from_header_and_iter ( spec, ExactChain ( self . components . drain ( ..) . rev ( ) , implicit_selector. iter ( ) . cloned ( ) ) )
146+ Arc :: from_header_and_iter (
147+ spec,
148+ ExactChain (
149+ self . components . drain ( ..) . rev ( ) ,
150+ implicit_selector. iter ( ) . cloned ( ) ,
151+ ) ,
152+ )
130153 }
131154}
132155
133-
134156impl < Impl : SelectorImpl > Default for SelectorBuilder < Impl > {
135157 #[ inline( always) ]
136158 fn default ( ) -> Self {
@@ -257,9 +279,9 @@ impl From<u32> for Specificity {
257279impl From < Specificity > for u32 {
258280 #[ inline]
259281 fn from ( specificity : Specificity ) -> u32 {
260- cmp:: min ( specificity. id_selectors , MAX_10BIT ) << 20 |
261- cmp:: min ( specificity. class_like_selectors , MAX_10BIT ) << 10 |
262- cmp:: min ( specificity. element_selectors , MAX_10BIT )
282+ cmp:: min ( specificity. id_selectors , MAX_10BIT ) << 20
283+ | cmp:: min ( specificity. class_like_selectors , MAX_10BIT ) << 10
284+ | cmp:: min ( specificity. element_selectors , MAX_10BIT )
263285 }
264286}
265287
@@ -304,9 +326,7 @@ where
304326 specificity. element_selectors += pseudo. specificity_count ( ) ;
305327 }
306328 } ,
307- Component :: LocalName ( ..) => {
308- specificity. element_selectors += 1
309- } ,
329+ Component :: LocalName ( ..) => specificity. element_selectors += 1 ,
310330 Component :: Slotted ( ref selector) => {
311331 flags. insert ( SelectorFlags :: HAS_SLOTTED ) ;
312332 if !for_nesting_parent {
@@ -333,14 +353,14 @@ where
333353 Component :: ID ( ..) => {
334354 specificity. id_selectors += 1 ;
335355 } ,
336- Component :: Class ( ..) |
337- Component :: AttributeInNoNamespace { .. } |
338- Component :: AttributeInNoNamespaceExists { .. } |
339- Component :: AttributeOther ( ..) |
340- Component :: Root |
341- Component :: Empty |
342- Component :: Nth ( ..) |
343- Component :: NonTSPseudoClass ( ..) => {
356+ Component :: Class ( ..)
357+ | Component :: AttributeInNoNamespace { .. }
358+ | Component :: AttributeInNoNamespaceExists { .. }
359+ | Component :: AttributeOther ( ..)
360+ | Component :: Root
361+ | Component :: Empty
362+ | Component :: Nth ( ..)
363+ | Component :: NonTSPseudoClass ( ..) => {
344364 specificity. class_like_selectors += 1 ;
345365 } ,
346366 Component :: Scope | Component :: ImplicitScope => {
@@ -357,7 +377,10 @@ where
357377 // specificity of a regular pseudo-class with that of its
358378 // selector argument S.
359379 specificity. class_like_selectors += 1 ;
360- let sf = selector_list_specificity_and_flags ( nth_of_data. selectors ( ) . iter ( ) , for_nesting_parent) ;
380+ let sf = selector_list_specificity_and_flags (
381+ nth_of_data. selectors ( ) . iter ( ) ,
382+ for_nesting_parent,
383+ ) ;
361384 * specificity += Specificity :: from ( sf. specificity ) ;
362385 flags. insert ( sf. flags ) ;
363386 } ,
@@ -366,27 +389,33 @@ where
366389 // The specificity of an :is(), :not(), or :has() pseudo-class
367390 // is replaced by the specificity of the most specific complex
368391 // selector in its selector list argument.
369- Component :: Where ( ref list) |
370- Component :: Negation ( ref list) |
371- Component :: Is ( ref list) => {
372- let sf = selector_list_specificity_and_flags ( list. slice ( ) . iter ( ) , /* nested = */ true ) ;
392+ Component :: Where ( ref list)
393+ | Component :: Negation ( ref list)
394+ | Component :: Is ( ref list) => {
395+ let sf = selector_list_specificity_and_flags (
396+ list. slice ( ) . iter ( ) ,
397+ /* nested = */ true ,
398+ ) ;
373399 if !matches ! ( * simple_selector, Component :: Where ( ..) ) {
374400 * specificity += Specificity :: from ( sf. specificity ) ;
375401 }
376402 flags. insert ( sf. flags ) ;
377403 } ,
378404 Component :: Has ( ref relative_selectors) => {
379- let sf = relative_selector_list_specificity_and_flags ( relative_selectors, for_nesting_parent) ;
405+ let sf = relative_selector_list_specificity_and_flags (
406+ relative_selectors,
407+ for_nesting_parent,
408+ ) ;
380409 * specificity += Specificity :: from ( sf. specificity ) ;
381410 flags. insert ( sf. flags ) ;
382411 } ,
383- Component :: ExplicitUniversalType |
384- Component :: ExplicitAnyNamespace |
385- Component :: ExplicitNoNamespace |
386- Component :: DefaultNamespace ( ..) |
387- Component :: Namespace ( ..) |
388- Component :: RelativeSelectorAnchor |
389- Component :: Invalid ( ..) => {
412+ Component :: ExplicitUniversalType
413+ | Component :: ExplicitAnyNamespace
414+ | Component :: ExplicitNoNamespace
415+ | Component :: DefaultNamespace ( ..)
416+ | Component :: Namespace ( ..)
417+ | Component :: RelativeSelectorAnchor
418+ | Component :: Invalid ( ..) => {
390419 // Does not affect specificity
391420 } ,
392421 }
@@ -417,7 +446,9 @@ pub(crate) fn selector_list_specificity_and_flags<'a, Impl: SelectorImpl>(
417446 let mut flags = SelectorFlags :: empty ( ) ;
418447 for selector in itr {
419448 let selector_flags = selector. flags ( ) ;
420- let selector_specificity = if for_nesting_parent && selector_flags. intersects ( SelectorFlags :: forbidden_for_nesting ( ) ) {
449+ let selector_specificity = if for_nesting_parent
450+ && selector_flags. intersects ( SelectorFlags :: forbidden_for_nesting ( ) )
451+ {
421452 // In this case we need to re-compute the specificity.
422453 specificity_and_flags ( selector. iter_raw_match_order ( ) , for_nesting_parent) . specificity
423454 } else {
0 commit comments