mirror of
https://github.com/servo/servo.git
synced 2025-08-04 13:10:20 +01:00
style: Store values for :nth- selectors in dedicated struct NthSelectorData
To accomplish this, all :nth- Components were replaced with type Nth, which uses NthSelectorData. Using NthSelectorData will make it easier to add selector lists for :nth selectors later on, but this change by itself shouldn't change any behavior. Differential Revision: https://phabricator.services.mozilla.com/D165831
This commit is contained in:
parent
b373d9c1c4
commit
281ae0748f
5 changed files with 49 additions and 49 deletions
|
@ -717,10 +717,7 @@ where
|
||||||
Component::Root |
|
Component::Root |
|
||||||
Component::Empty |
|
Component::Empty |
|
||||||
Component::Scope |
|
Component::Scope |
|
||||||
Component::NthChild(..) |
|
Component::Nth(..) |
|
||||||
Component::NthLastChild(..) |
|
|
||||||
Component::NthOfType(..) |
|
|
||||||
Component::NthLastOfType(..) |
|
|
||||||
Component::FirstOfType |
|
Component::FirstOfType |
|
||||||
Component::LastOfType |
|
Component::LastOfType |
|
||||||
Component::OnlyOfType |
|
Component::OnlyOfType |
|
||||||
|
|
|
@ -322,10 +322,7 @@ where
|
||||||
Component::Root |
|
Component::Root |
|
||||||
Component::Empty |
|
Component::Empty |
|
||||||
Component::Scope |
|
Component::Scope |
|
||||||
Component::NthChild(..) |
|
Component::Nth(..) |
|
||||||
Component::NthLastChild(..) |
|
|
||||||
Component::NthOfType(..) |
|
|
||||||
Component::NthLastOfType(..) |
|
|
||||||
Component::FirstOfType |
|
Component::FirstOfType |
|
||||||
Component::LastOfType |
|
Component::LastOfType |
|
||||||
Component::OnlyOfType |
|
Component::OnlyOfType |
|
||||||
|
|
|
@ -8,7 +8,7 @@ use crate::attr::{
|
||||||
};
|
};
|
||||||
use crate::bloom::{BloomFilter, BLOOM_HASH_MASK};
|
use crate::bloom::{BloomFilter, BLOOM_HASH_MASK};
|
||||||
use crate::nth_index_cache::NthIndexCacheInner;
|
use crate::nth_index_cache::NthIndexCacheInner;
|
||||||
use crate::parser::{AncestorHashes, Combinator, Component, LocalName};
|
use crate::parser::{AncestorHashes, Combinator, Component, LocalName, NthType};
|
||||||
use crate::parser::{NonTSPseudoClass, Selector, SelectorImpl, SelectorIter, SelectorList};
|
use crate::parser::{NonTSPseudoClass, Selector, SelectorImpl, SelectorIter, SelectorList};
|
||||||
use crate::tree::Element;
|
use crate::tree::Element;
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
|
@ -391,10 +391,7 @@ fn hover_and_active_quirk_applies<Impl: SelectorImpl>(
|
||||||
Component::LastChild |
|
Component::LastChild |
|
||||||
Component::OnlyChild |
|
Component::OnlyChild |
|
||||||
Component::Empty |
|
Component::Empty |
|
||||||
Component::NthChild(_, _) |
|
Component::Nth(_) |
|
||||||
Component::NthLastChild(_, _) |
|
|
||||||
Component::NthOfType(_, _) |
|
|
||||||
Component::NthLastOfType(_, _) |
|
|
||||||
Component::FirstOfType |
|
Component::FirstOfType |
|
||||||
Component::LastOfType |
|
Component::LastOfType |
|
||||||
Component::OnlyOfType => false,
|
Component::OnlyOfType => false,
|
||||||
|
@ -815,18 +812,14 @@ where
|
||||||
Some(ref scope_element) => element.opaque() == *scope_element,
|
Some(ref scope_element) => element.opaque() == *scope_element,
|
||||||
None => element.is_root(),
|
None => element.is_root(),
|
||||||
},
|
},
|
||||||
Component::NthChild(a, b) => {
|
Component::Nth(nth_data) => matches_generic_nth_child(
|
||||||
matches_generic_nth_child(element, context.shared, a, b, false, false)
|
element,
|
||||||
},
|
context.shared,
|
||||||
Component::NthLastChild(a, b) => {
|
nth_data.a,
|
||||||
matches_generic_nth_child(element, context.shared, a, b, false, true)
|
nth_data.b,
|
||||||
},
|
nth_data.ty == NthType::OfType || nth_data.ty == NthType::LastOfType,
|
||||||
Component::NthOfType(a, b) => {
|
nth_data.ty == NthType::LastChild || nth_data.ty == NthType::LastOfType,
|
||||||
matches_generic_nth_child(element, context.shared, a, b, true, false)
|
),
|
||||||
},
|
|
||||||
Component::NthLastOfType(a, b) => {
|
|
||||||
matches_generic_nth_child(element, context.shared, a, b, true, true)
|
|
||||||
},
|
|
||||||
Component::FirstOfType => {
|
Component::FirstOfType => {
|
||||||
matches_generic_nth_child(element, context.shared, 0, 1, true, false)
|
matches_generic_nth_child(element, context.shared, 0, 1, true, false)
|
||||||
},
|
},
|
||||||
|
|
|
@ -1036,6 +1036,27 @@ impl Combinator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An enum for the different types of :nth- pseudoclasses
|
||||||
|
#[derive(Copy, Clone, Eq, PartialEq, ToShmem)]
|
||||||
|
#[shmem(no_bounds)]
|
||||||
|
pub enum NthType {
|
||||||
|
Child,
|
||||||
|
LastChild,
|
||||||
|
OfType,
|
||||||
|
LastOfType,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The properties that comprise an :nth- pseudoclass as of Selectors 3 (e.g.,
|
||||||
|
/// nth-child(An+B)).
|
||||||
|
/// https://www.w3.org/TR/selectors-3/#nth-child-pseudo
|
||||||
|
#[derive(Copy, Clone, Eq, PartialEq, ToShmem)]
|
||||||
|
#[shmem(no_bounds)]
|
||||||
|
pub struct NthSelectorData {
|
||||||
|
pub ty: NthType,
|
||||||
|
pub a: i32,
|
||||||
|
pub b: i32,
|
||||||
|
}
|
||||||
|
|
||||||
/// A CSS simple selector or combinator. We store both in the same enum for
|
/// A CSS simple selector or combinator. We store both in the same enum for
|
||||||
/// optimal packing and cache performance, see [1].
|
/// optimal packing and cache performance, see [1].
|
||||||
///
|
///
|
||||||
|
@ -1084,10 +1105,7 @@ pub enum Component<Impl: SelectorImpl> {
|
||||||
Root,
|
Root,
|
||||||
Empty,
|
Empty,
|
||||||
Scope,
|
Scope,
|
||||||
NthChild(i32, i32),
|
Nth(NthSelectorData),
|
||||||
NthLastChild(i32, i32),
|
|
||||||
NthOfType(i32, i32),
|
|
||||||
NthLastOfType(i32, i32),
|
|
||||||
FirstOfType,
|
FirstOfType,
|
||||||
LastOfType,
|
LastOfType,
|
||||||
OnlyOfType,
|
OnlyOfType,
|
||||||
|
@ -1598,15 +1616,14 @@ impl<Impl: SelectorImpl> ToCss for Component<Impl> {
|
||||||
FirstOfType => dest.write_str(":first-of-type"),
|
FirstOfType => dest.write_str(":first-of-type"),
|
||||||
LastOfType => dest.write_str(":last-of-type"),
|
LastOfType => dest.write_str(":last-of-type"),
|
||||||
OnlyOfType => dest.write_str(":only-of-type"),
|
OnlyOfType => dest.write_str(":only-of-type"),
|
||||||
NthChild(a, b) | NthLastChild(a, b) | NthOfType(a, b) | NthLastOfType(a, b) => {
|
Nth(nth_data) => {
|
||||||
match *self {
|
match nth_data.ty {
|
||||||
NthChild(_, _) => dest.write_str(":nth-child(")?,
|
NthType::Child => dest.write_str(":nth-child(")?,
|
||||||
NthLastChild(_, _) => dest.write_str(":nth-last-child(")?,
|
NthType::LastChild => dest.write_str(":nth-last-child(")?,
|
||||||
NthOfType(_, _) => dest.write_str(":nth-of-type(")?,
|
NthType::OfType => dest.write_str(":nth-of-type(")?,
|
||||||
NthLastOfType(_, _) => dest.write_str(":nth-last-of-type(")?,
|
NthType::LastOfType => dest.write_str(":nth-last-of-type(")?,
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
}
|
||||||
write_affine(dest, a, b)?;
|
write_affine(dest, nth_data.a, nth_data.b)?;
|
||||||
dest.write_char(')')
|
dest.write_char(')')
|
||||||
},
|
},
|
||||||
Is(ref list) | Where(ref list) | Negation(ref list) | Has(ref list) => {
|
Is(ref list) | Where(ref list) | Negation(ref list) | Has(ref list) => {
|
||||||
|
@ -2312,10 +2329,10 @@ where
|
||||||
Impl: SelectorImpl,
|
Impl: SelectorImpl,
|
||||||
{
|
{
|
||||||
match_ignore_ascii_case! { &name,
|
match_ignore_ascii_case! { &name,
|
||||||
"nth-child" => return parse_nth_pseudo_class(parser, input, state, Component::NthChild),
|
"nth-child" => return parse_nth_pseudo_class(parser, input, state, NthType::Child),
|
||||||
"nth-of-type" => return parse_nth_pseudo_class(parser, input, state, Component::NthOfType),
|
"nth-of-type" => return parse_nth_pseudo_class(parser, input, state, NthType::OfType),
|
||||||
"nth-last-child" => return parse_nth_pseudo_class(parser, input, state, Component::NthLastChild),
|
"nth-last-child" => return parse_nth_pseudo_class(parser, input, state, NthType::LastChild),
|
||||||
"nth-last-of-type" => return parse_nth_pseudo_class(parser, input, state, Component::NthLastOfType),
|
"nth-last-of-type" => return parse_nth_pseudo_class(parser, input, state, NthType::LastOfType),
|
||||||
"is" if parser.parse_is_and_where() => return parse_is_where_has(parser, input, state, Component::Is),
|
"is" if parser.parse_is_and_where() => return parse_is_where_has(parser, input, state, Component::Is),
|
||||||
"where" if parser.parse_is_and_where() => return parse_is_where_has(parser, input, state, Component::Where),
|
"where" if parser.parse_is_and_where() => return parse_is_where_has(parser, input, state, Component::Where),
|
||||||
"has" if parser.parse_has() => return parse_is_where_has(parser, input, state, Component::Has),
|
"has" if parser.parse_has() => return parse_is_where_has(parser, input, state, Component::Has),
|
||||||
|
@ -2342,22 +2359,21 @@ where
|
||||||
P::parse_non_ts_functional_pseudo_class(parser, name, input).map(Component::NonTSPseudoClass)
|
P::parse_non_ts_functional_pseudo_class(parser, name, input).map(Component::NonTSPseudoClass)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_nth_pseudo_class<'i, 't, P, Impl, F>(
|
fn parse_nth_pseudo_class<'i, 't, P, Impl>(
|
||||||
_: &P,
|
_: &P,
|
||||||
input: &mut CssParser<'i, 't>,
|
input: &mut CssParser<'i, 't>,
|
||||||
state: SelectorParsingState,
|
state: SelectorParsingState,
|
||||||
selector: F,
|
ty: NthType,
|
||||||
) -> Result<Component<Impl>, ParseError<'i, P::Error>>
|
) -> Result<Component<Impl>, ParseError<'i, P::Error>>
|
||||||
where
|
where
|
||||||
P: Parser<'i, Impl = Impl>,
|
P: Parser<'i, Impl = Impl>,
|
||||||
Impl: SelectorImpl,
|
Impl: SelectorImpl,
|
||||||
F: FnOnce(i32, i32) -> Component<Impl>,
|
|
||||||
{
|
{
|
||||||
if !state.allows_tree_structural_pseudo_classes() {
|
if !state.allows_tree_structural_pseudo_classes() {
|
||||||
return Err(input.new_custom_error(SelectorParseErrorKind::InvalidState));
|
return Err(input.new_custom_error(SelectorParseErrorKind::InvalidState));
|
||||||
}
|
}
|
||||||
let (a, b) = parse_nth(input)?;
|
let (a, b) = parse_nth(input)?;
|
||||||
Ok(selector(a, b))
|
Ok(Component::Nth(NthSelectorData { ty, a, b }))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether the name corresponds to a CSS2 pseudo-element that
|
/// Returns whether the name corresponds to a CSS2 pseudo-element that
|
||||||
|
|
|
@ -1921,10 +1921,7 @@ fn component_needs_revalidation(
|
||||||
Component::FirstChild |
|
Component::FirstChild |
|
||||||
Component::LastChild |
|
Component::LastChild |
|
||||||
Component::OnlyChild |
|
Component::OnlyChild |
|
||||||
Component::NthChild(..) |
|
Component::Nth(..) |
|
||||||
Component::NthLastChild(..) |
|
|
||||||
Component::NthOfType(..) |
|
|
||||||
Component::NthLastOfType(..) |
|
|
||||||
Component::FirstOfType |
|
Component::FirstOfType |
|
||||||
Component::LastOfType |
|
Component::LastOfType |
|
||||||
Component::OnlyOfType => true,
|
Component::OnlyOfType => true,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue