1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
5 //! Style resolution for a given element or pseudo-element.
7 use crate::applicable_declarations::ApplicableDeclarationList;
8 use crate::context::{CascadeInputs, ElementCascadeInputs, StyleContext};
9 use crate::data::{EagerPseudoStyles, ElementStyles};
10 use crate::dom::TElement;
11 use crate::matching::MatchMethods;
12 use crate::properties::longhands::display::computed_value::T as Display;
13 use crate::properties::ComputedValues;
14 use crate::rule_tree::StrongRuleNode;
15 use crate::selector_parser::{PseudoElement, SelectorImpl};
16 use crate::stylist::RuleInclusion;
17 use log::Level::Trace;
18 use selectors::matching::{ElementSelectorFlags, MatchingContext};
19 use selectors::matching::{MatchingMode, VisitedHandlingMode};
22 /// Whether pseudo-elements should be resolved or not.
23 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
24 pub enum PseudoElementResolution {
25 /// Only resolve pseudo-styles if possibly applicable.
27 /// Force pseudo-element resolution.
31 /// A struct that takes care of resolving the style of a given element.
32 pub struct StyleResolverForElement<'a, 'ctx, 'le, E>
36 E: TElement + MatchMethods + 'le,
39 context: &'a mut StyleContext<'ctx, E>,
40 rule_inclusion: RuleInclusion,
41 pseudo_resolution: PseudoElementResolution,
42 _marker: ::std::marker::PhantomData<&'le E>,
45 struct MatchingResults {
46 rule_node: StrongRuleNode,
49 /// A style returned from the resolver machinery.
50 pub struct ResolvedStyle(pub Arc<ComputedValues>);
52 /// The primary style of an element or an element-backed pseudo-element.
53 pub struct PrimaryStyle {
55 pub style: ResolvedStyle,
56 /// Whether the style was reused from another element via the rule node (see
57 /// `StyleSharingCache::lookup_by_rules`).
58 pub reused_via_rule_node: bool,
61 /// A set of style returned from the resolver machinery.
62 pub struct ResolvedElementStyles {
64 pub primary: PrimaryStyle,
66 pub pseudos: EagerPseudoStyles,
69 impl ResolvedElementStyles {
70 /// Convenience accessor for the primary style.
71 pub fn primary_style(&self) -> &Arc<ComputedValues> {
75 /// Convenience mutable accessor for the style.
76 pub fn primary_style_mut(&mut self) -> &mut Arc<ComputedValues> {
77 &mut self.primary.style.0
82 /// Convenience accessor for the style.
83 pub fn style(&self) -> &ComputedValues {
88 impl From<ResolvedElementStyles> for ElementStyles {
89 fn from(r: ResolvedElementStyles) -> ElementStyles {
91 primary: Some(r.primary.style.0),
97 fn with_default_parent_styles<E, F, R>(element: E, f: F) -> R
100 F: FnOnce(Option<&ComputedValues>, Option<&ComputedValues>) -> R,
102 let parent_el = element.inheritance_parent();
103 let parent_data = parent_el.as_ref().and_then(|e| e.borrow_data());
104 let parent_style = parent_data.as_ref().map(|d| d.styles.primary());
106 let mut layout_parent_el = parent_el.clone();
107 let layout_parent_data;
108 let mut layout_parent_style = parent_style;
109 if parent_style.map_or(false, |s| s.is_display_contents()) {
110 layout_parent_el = Some(layout_parent_el.unwrap().layout_parent());
111 layout_parent_data = layout_parent_el.as_ref().unwrap().borrow_data().unwrap();
112 layout_parent_style = Some(layout_parent_data.styles.primary());
116 parent_style.map(|x| &**x),
117 layout_parent_style.map(|s| &**s),
121 fn layout_parent_style_for_pseudo<'a>(
122 primary_style: &'a PrimaryStyle,
123 layout_parent_style: Option<&'a ComputedValues>,
124 ) -> Option<&'a ComputedValues> {
125 if primary_style.style().is_display_contents() {
128 Some(primary_style.style())
132 fn eager_pseudo_is_definitely_not_generated(
133 pseudo: &PseudoElement,
134 style: &ComputedValues,
136 use crate::computed_value_flags::ComputedValueFlags;
138 if !pseudo.is_before_or_after() {
144 .intersects(ComputedValueFlags::DISPLAY_DEPENDS_ON_INHERITED_STYLE) &&
145 style.get_box().clone_display() == Display::None
152 .intersects(ComputedValueFlags::CONTENT_DEPENDS_ON_INHERITED_STYLE) &&
153 style.ineffective_content_property()
161 impl<'a, 'ctx, 'le, E> StyleResolverForElement<'a, 'ctx, 'le, E>
165 E: TElement + MatchMethods + 'le,
167 /// Trivially construct a new StyleResolverForElement.
170 context: &'a mut StyleContext<'ctx, E>,
171 rule_inclusion: RuleInclusion,
172 pseudo_resolution: PseudoElementResolution,
179 _marker: ::std::marker::PhantomData,
183 /// Resolve just the style of a given element.
184 pub fn resolve_primary_style(
186 parent_style: Option<&ComputedValues>,
187 layout_parent_style: Option<&ComputedValues>,
189 let primary_results = self.match_primary(VisitedHandlingMode::AllLinksUnvisited);
191 let inside_link = parent_style.map_or(false, |s| s.visited_style().is_some());
193 let visited_rules = if self.context.shared.visited_styles_enabled &&
194 (inside_link || self.element.is_link())
196 let visited_matching_results =
197 self.match_primary(VisitedHandlingMode::RelevantLinkVisited);
198 Some(visited_matching_results.rule_node)
203 self.cascade_primary_style(
205 rules: Some(primary_results.rule_node),
213 fn cascade_primary_style(
215 inputs: CascadeInputs,
216 parent_style: Option<&ComputedValues>,
217 layout_parent_style: Option<&ComputedValues>,
219 // Before doing the cascade, check the sharing cache and see if we can
220 // reuse the style via rule node identity.
221 let may_reuse = !self.element.is_in_native_anonymous_subtree() &&
222 parent_style.is_some() &&
223 inputs.rules.is_some();
226 let cached = self.context.thread_local.sharing_cache.lookup_by_rules(
228 parent_style.unwrap(),
229 inputs.rules.as_ref().unwrap(),
230 inputs.visited_rules.as_ref(),
233 if let Some(mut primary_style) = cached {
234 self.context.thread_local.statistics.styles_reused += 1;
235 primary_style.reused_via_rule_node |= true;
236 return primary_style;
240 // No style to reuse. Cascade the style, starting with visited style
243 style: self.cascade_style_and_visited(
249 reused_via_rule_node: false,
253 /// Resolve the style of a given element, and all its eager pseudo-elements.
254 pub fn resolve_style(
256 parent_style: Option<&ComputedValues>,
257 layout_parent_style: Option<&ComputedValues>,
258 ) -> ResolvedElementStyles {
259 let primary_style = self.resolve_primary_style(parent_style, layout_parent_style);
261 let mut pseudo_styles = EagerPseudoStyles::default();
263 if !self.element.is_pseudo_element() {
264 let layout_parent_style_for_pseudo =
265 layout_parent_style_for_pseudo(&primary_style, layout_parent_style);
266 SelectorImpl::each_eagerly_cascaded_pseudo_element(|pseudo| {
267 let pseudo_style = self.resolve_pseudo_style(
270 layout_parent_style_for_pseudo,
273 if let Some(style) = pseudo_style {
274 if !matches!(self.pseudo_resolution, PseudoElementResolution::Force) &&
275 eager_pseudo_is_definitely_not_generated(&pseudo, &style.0)
279 pseudo_styles.set(&pseudo, style.0);
284 ResolvedElementStyles {
285 primary: primary_style,
286 pseudos: pseudo_styles,
290 /// Resolve an element's styles with the default inheritance parent/layout
292 pub fn resolve_style_with_default_parents(&mut self) -> ResolvedElementStyles {
293 with_default_parent_styles(self.element, |parent_style, layout_parent_style| {
294 self.resolve_style(parent_style, layout_parent_style)
298 /// Cascade a set of rules, using the default parent for inheritance.
299 pub fn cascade_style_and_visited_with_default_parents(
301 inputs: CascadeInputs,
303 with_default_parent_styles(self.element, |parent_style, layout_parent_style| {
304 self.cascade_style_and_visited(
313 /// Cascade a set of rules for pseudo element, using the default parent for inheritance.
314 pub fn cascade_style_and_visited_for_pseudo_with_default_parents(
316 inputs: CascadeInputs,
317 pseudo: &PseudoElement,
318 primary_style: &PrimaryStyle,
320 with_default_parent_styles(self.element, |_, layout_parent_style| {
321 let layout_parent_style_for_pseudo =
322 layout_parent_style_for_pseudo(primary_style, layout_parent_style);
324 self.cascade_style_and_visited(
326 Some(primary_style.style()),
327 layout_parent_style_for_pseudo,
333 fn cascade_style_and_visited(
335 inputs: CascadeInputs,
336 parent_style: Option<&ComputedValues>,
337 layout_parent_style: Option<&ComputedValues>,
338 pseudo: Option<&PseudoElement>,
340 debug_assert!(pseudo.map_or(true, |p| p.is_eager()));
342 let implemented_pseudo = self.element.implemented_pseudo_element();
343 let pseudo = pseudo.or(implemented_pseudo.as_ref());
345 let mut conditions = Default::default();
346 let values = self.context.shared.stylist.cascade_style_and_visited(
350 &self.context.shared.guards,
354 &self.context.thread_local.font_metrics_provider,
355 Some(&self.context.thread_local.rule_cache),
359 self.context.thread_local.rule_cache.insert_if_possible(
360 &self.context.shared.guards,
366 ResolvedStyle(values)
369 /// Cascade the element and pseudo-element styles with the default parents.
370 pub fn cascade_styles_with_default_parents(
372 inputs: ElementCascadeInputs,
373 ) -> ResolvedElementStyles {
374 with_default_parent_styles(self.element, move |parent_style, layout_parent_style| {
376 self.cascade_primary_style(inputs.primary, parent_style, layout_parent_style);
378 let mut pseudo_styles = EagerPseudoStyles::default();
379 if let Some(mut pseudo_array) = inputs.pseudos.into_array() {
380 let layout_parent_style_for_pseudo = if primary_style.style().is_display_contents()
384 Some(primary_style.style())
387 for (i, inputs) in pseudo_array.iter_mut().enumerate() {
388 if let Some(inputs) = inputs.take() {
389 let pseudo = PseudoElement::from_eager_index(i);
391 let style = self.cascade_style_and_visited(
393 Some(primary_style.style()),
394 layout_parent_style_for_pseudo,
398 if !matches!(self.pseudo_resolution, PseudoElementResolution::Force) &&
399 eager_pseudo_is_definitely_not_generated(&pseudo, &style.0)
404 pseudo_styles.set(&pseudo, style.0);
409 ResolvedElementStyles {
410 primary: primary_style,
411 pseudos: pseudo_styles,
416 fn resolve_pseudo_style(
418 pseudo: &PseudoElement,
419 originating_element_style: &PrimaryStyle,
420 layout_parent_style: Option<&ComputedValues>,
421 ) -> Option<ResolvedStyle> {
422 let rules = self.match_pseudo(
423 originating_element_style.style(),
425 VisitedHandlingMode::AllLinksUnvisited,
428 let mut visited_rules = None;
429 if originating_element_style.style().visited_style().is_some() {
430 visited_rules = self.match_pseudo(
431 originating_element_style.style(),
433 VisitedHandlingMode::RelevantLinkVisited,
437 Some(self.cascade_style_and_visited(
442 Some(originating_element_style.style()),
448 fn match_primary(&mut self, visited_handling: VisitedHandlingMode) -> MatchingResults {
450 "Match primary for {:?}, visited: {:?}",
451 self.element, visited_handling
453 let mut applicable_declarations = ApplicableDeclarationList::new();
455 let map = &mut self.context.thread_local.selector_flags;
456 let bloom_filter = self.context.thread_local.bloom_filter.filter();
457 let nth_index_cache = &mut self.context.thread_local.nth_index_cache;
458 let mut matching_context = MatchingContext::new_for_visited(
459 MatchingMode::Normal,
461 Some(nth_index_cache),
463 self.context.shared.quirks_mode(),
466 let stylist = &self.context.shared.stylist;
467 let implemented_pseudo = self.element.implemented_pseudo_element();
469 let resolving_element = self.element;
470 let mut set_selector_flags = |element: &E, flags: ElementSelectorFlags| {
471 resolving_element.apply_selector_flags(map, element, flags);
474 // Compute the primary rule node.
475 stylist.push_applicable_declarations(
477 implemented_pseudo.as_ref(),
478 self.element.style_attribute(),
479 self.element.smil_override(),
480 self.element.animation_declarations(self.context.shared),
482 &mut applicable_declarations,
483 &mut matching_context,
484 &mut set_selector_flags,
488 // FIXME(emilio): This is a hack for animations, and should go away.
489 self.element.unset_dirty_style_attribute();
491 let rule_node = stylist
493 .compute_rule_node(&mut applicable_declarations, &self.context.shared.guards);
495 if log_enabled!(Trace) {
496 trace!("Matched rules for {:?}:", self.element);
497 for rn in rule_node.self_and_ancestors() {
498 let source = rn.style_source();
499 if source.is_some() {
500 trace!(" > {:?}", source);
505 MatchingResults { rule_node }
510 originating_element_style: &ComputedValues,
511 pseudo_element: &PseudoElement,
512 visited_handling: VisitedHandlingMode,
513 ) -> Option<StrongRuleNode> {
515 "Match pseudo {:?} for {:?}, visited: {:?}",
516 self.element, pseudo_element, visited_handling
518 debug_assert!(pseudo_element.is_eager());
520 !self.element.is_pseudo_element(),
521 "Element pseudos can't have any other eager pseudo."
524 let mut applicable_declarations = ApplicableDeclarationList::new();
526 let stylist = &self.context.shared.stylist;
530 .may_generate_pseudo(pseudo_element, originating_element_style)
535 let bloom_filter = self.context.thread_local.bloom_filter.filter();
536 let nth_index_cache = &mut self.context.thread_local.nth_index_cache;
538 let mut matching_context = MatchingContext::new_for_visited(
539 MatchingMode::ForStatelessPseudoElement,
541 Some(nth_index_cache),
543 self.context.shared.quirks_mode(),
546 let map = &mut self.context.thread_local.selector_flags;
547 let resolving_element = self.element;
548 let mut set_selector_flags = |element: &E, flags: ElementSelectorFlags| {
549 resolving_element.apply_selector_flags(map, element, flags);
552 // NB: We handle animation rules for ::before and ::after when
554 stylist.push_applicable_declarations(
556 Some(pseudo_element),
559 /* animation_declarations = */ Default::default(),
561 &mut applicable_declarations,
562 &mut matching_context,
563 &mut set_selector_flags,
566 if applicable_declarations.is_empty() {
570 let rule_node = stylist
572 .compute_rule_node(&mut applicable_declarations, &self.context.shared.guards);