Skip to content

Commit

Permalink
style: Implement shadow part forwarding (minus invalidation).
Browse files Browse the repository at this point in the history
Some of the stuff, in particular inside GeckoBindings stuff should be
refactored to be less ugly and duplicate a bit less code, but the rest of the
code should be landable as is.

Some invalidation changes are already needed because we weren't matching with
the right shadow host during invalidation (which made existing ::part() tests
fail).

Pending invalidation work:

 * Making exportparts work right on the snapshots.
 * Invalidating parts from descendant hosts.

They're not very hard but I need to think how to best implement it:

 * Maybe get rid of ShadowRoot::mParts and just walk DOM descendants in the
   Shadow DOM.

 * Maybe implement a ElementHasExportPartsAttr much like HasPartAttr and use
   that to keep the list of elements.

 * Maybe invalidate :host and ::part() together in here[1]

 * Maybe something else.

Opinions?

[1]: https://searchfox.org/mozilla-central/rev/131338e5017bc0283d86fb73844407b9a2155c98/servo/components/style/invalidation/element/invalidator.rs#561

Differential Revision: https://phabricator.services.mozilla.com/D53730
  • Loading branch information
emilio committed Nov 30, 2019
1 parent 576883d commit e3009a4
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 47 deletions.
37 changes: 36 additions & 1 deletion components/selectors/matching.rs
Expand Up @@ -8,6 +8,7 @@ use crate::nth_index_cache::NthIndexCacheInner;
use crate::parser::{AncestorHashes, Combinator, Component, LocalName};
use crate::parser::{NonTSPseudoClass, Selector, SelectorImpl, SelectorIter, SelectorList};
use crate::tree::Element;
use smallvec::SmallVec;
use std::borrow::Borrow;
use std::iter;

Expand Down Expand Up @@ -667,7 +668,41 @@ where

match *selector {
Component::Combinator(_) => unreachable!(),
Component::Part(ref parts) => parts.iter().all(|part| element.is_part(part)),
Component::Part(ref parts) => {
let mut hosts = SmallVec::<[E; 4]>::new();

let mut host = match element.containing_shadow_host() {
Some(h) => h,
None => return false,
};

loop {
let outer_host = host.containing_shadow_host();
if outer_host.as_ref().map(|h| h.opaque()) == context.shared.current_host {
break;
}
let outer_host = match outer_host {
Some(h) => h,
None => return false,
};
// TODO(emilio): if worth it, we could early return if
// host doesn't have the exportparts attribute.
hosts.push(host);
host = outer_host;
}

// Translate the part into the right scope.
parts.iter().all(|part| {
let mut part = part.clone();
for host in hosts.iter().rev() {
part = match host.imported_part(&part) {
Some(p) => p,
None => return false,
};
}
element.is_part(&part)
})
},
Component::Slotted(ref selector) => {
// <slots> are never flattened tree slottables.
!element.is_html_slot_element() &&
Expand Down
14 changes: 14 additions & 0 deletions components/selectors/tree.rs
Expand Up @@ -117,6 +117,20 @@ pub trait Element: Sized + Clone + Debug {
case_sensitivity: CaseSensitivity,
) -> bool;

/// Returns the mapping from the `exportparts` attribute in the regular
/// direction, that is, inner-tree -> outer-tree.
fn exported_part(
&self,
name: &<Self::Impl as SelectorImpl>::PartName,
) -> Option<<Self::Impl as SelectorImpl>::PartName>;

/// Returns the mapping from the `exportparts` attribute in the reverse
/// direction, that is, in an outer-tree -> inner-tree direction.
fn imported_part(
&self,
name: &<Self::Impl as SelectorImpl>::PartName,
) -> Option<<Self::Impl as SelectorImpl>::PartName>;

fn is_part(&self, name: &<Self::Impl as SelectorImpl>::PartName) -> bool;

/// Returns whether this element matches `:empty`.
Expand Down
6 changes: 5 additions & 1 deletion components/style/dom_apis.rs
Expand Up @@ -172,7 +172,11 @@ where
};

for selector in self.selector_list.0.iter() {
target_vector.push(Invalidation::new(selector, 0))
target_vector.push(Invalidation::new(
selector,
self.matching_context.current_host.clone(),
0,
))
}

false
Expand Down
22 changes: 22 additions & 0 deletions components/style/gecko/wrapper.rs
Expand Up @@ -2216,6 +2216,28 @@ impl<'le> ::selectors::Element for GeckoElement<'le> {
snapshot_helpers::has_class_or_part(name, CaseSensitivity::CaseSensitive, attr)
}

#[inline]
fn imported_part(&self, name: &Atom) -> Option<Atom> {
let imported = unsafe {
bindings::Gecko_Element_ImportedPart(self.0, name.as_ptr())
};
if imported.is_null() {
return None;
}
Some(unsafe { Atom::from_raw(imported) })
}

#[inline]
fn exported_part(&self, name: &Atom) -> Option<Atom> {
let exported = unsafe {
bindings::Gecko_Element_ExportedPart(self.0, name.as_ptr())
};
if exported.is_null() {
return None;
}
Some(unsafe { Atom::from_raw(exported) })
}

#[inline(always)]
fn has_class(&self, name: &Atom, case_sensitivity: CaseSensitivity) -> bool {
let attr = match self.get_class_attr() {
Expand Down
8 changes: 7 additions & 1 deletion components/style/invalidation/element/document_state.rs
Expand Up @@ -79,7 +79,13 @@ where
continue;
}

self_invalidations.push(Invalidation::new(&dependency.selector, 0));
// We pass `None` as a scope, as document state selectors aren't
// affected by the current scope.
self_invalidations.push(Invalidation::new(
&dependency.selector,
/* scope = */ None,
0,
));
}
}

Expand Down
10 changes: 10 additions & 0 deletions components/style/invalidation/element/element_wrapper.rs
Expand Up @@ -365,6 +365,16 @@ where
}
}

fn exported_part(&self, name: &Atom) -> Option<Atom> {
// FIXME(emilio): Implement for proper invalidation.
self.element.exported_part(name)
}

fn imported_part(&self, name: &Atom) -> Option<Atom> {
// FIXME(emilio): Implement for proper invalidation.
self.element.imported_part(name)
}

fn has_class(&self, name: &Atom, case_sensitivity: CaseSensitivity) -> bool {
match self.snapshot() {
Some(snapshot) if snapshot.has_attrs() => snapshot.has_class(name, case_sensitivity),
Expand Down
31 changes: 24 additions & 7 deletions components/style/invalidation/element/invalidator.rs
Expand Up @@ -8,6 +8,7 @@
use crate::context::StackLimitChecker;
use crate::dom::{TElement, TNode, TShadowRoot};
use crate::selector_parser::SelectorImpl;
use selectors::OpaqueElement;
use selectors::matching::matches_compound_selector_from;
use selectors::matching::{CompoundSelectorMatchingResult, MatchingContext};
use selectors::parser::{Combinator, Component, Selector};
Expand Down Expand Up @@ -127,6 +128,8 @@ enum InvalidationKind {
#[derive(Clone)]
pub struct Invalidation<'a> {
selector: &'a Selector<SelectorImpl>,
/// The right shadow host from where the rule came from, if any.
scope: Option<OpaqueElement>,
/// The offset of the selector pointing to a compound selector.
///
/// This order is a "parse order" offset, that is, zero is the leftmost part
Expand All @@ -143,9 +146,14 @@ pub struct Invalidation<'a> {

impl<'a> Invalidation<'a> {
/// Create a new invalidation for a given selector and offset.
pub fn new(selector: &'a Selector<SelectorImpl>, offset: usize) -> Self {
pub fn new(
selector: &'a Selector<SelectorImpl>,
scope: Option<OpaqueElement>,
offset: usize,
) -> Self {
Self {
selector,
scope,
offset,
matched_by_any_previous: false,
}
Expand Down Expand Up @@ -483,6 +491,9 @@ where

let mut any = false;
let mut sibling_invalidations = InvalidationVector::new();

// FIXME(emilio): We also need to invalidate parts in descendant shadow
// hosts that have exportparts attributes.
for element in shadow.parts() {
any |= self.invalidate_child(
*element,
Expand Down Expand Up @@ -721,12 +732,17 @@ where
self.element, invalidation, invalidation_kind
);

let matching_result = matches_compound_selector_from(
&invalidation.selector,
invalidation.offset,
self.processor.matching_context(),
&self.element,
);
let matching_result = {
let mut context = self.processor.matching_context();
context.current_host = invalidation.scope;

matches_compound_selector_from(
&invalidation.selector,
invalidation.offset,
context,
&self.element,
)
};

let mut invalidated_self = false;
let mut matched = false;
Expand Down Expand Up @@ -809,6 +825,7 @@ where

let next_invalidation = Invalidation {
selector: invalidation.selector,
scope: invalidation.scope,
offset: next_combinator_offset + 1,
matched_by_any_previous: false,
};
Expand Down
Expand Up @@ -457,6 +457,7 @@ where

let invalidation = Invalidation::new(
&dependency.selector,
self.matching_context.current_host.clone(),
dependency.selector.len() - dependency.selector_offset + 1,
);

Expand Down
98 changes: 61 additions & 37 deletions components/style/rule_collector.rs
Expand Up @@ -4,6 +4,7 @@

//! Collects a series of applicable rules for a given element.

use crate::Atom;
use crate::applicable_declarations::{ApplicableDeclarationBlock, ApplicableDeclarationList};
use crate::dom::{TElement, TNode, TShadowRoot};
use crate::properties::{AnimationRules, PropertyDeclarationBlock};
Expand Down Expand Up @@ -328,52 +329,75 @@ where
return;
}

let shadow = match self.rule_hash_target.containing_shadow() {
let mut inner_shadow = match self.rule_hash_target.containing_shadow() {
Some(s) => s,
None => return,
};

let host = shadow.host();
let containing_shadow = host.containing_shadow();
let part_rules = match containing_shadow {
Some(shadow) => shadow
.style_data()
.and_then(|data| data.part_rules(self.pseudo_element)),
None => self
.stylist
.cascade_data()
.borrow_for_origin(Origin::Author)
.part_rules(self.pseudo_element),
};
let mut shadow_cascade_order = ShadowCascadeOrder::for_innermost_containing_tree();

let mut parts = SmallVec::<[Atom; 3]>::new();
self.rule_hash_target.each_part(|p| parts.push(p.clone()));

// TODO(emilio): Cascade order will need to increment for each tree when
// we implement forwarding.
let shadow_cascade_order = ShadowCascadeOrder::for_innermost_containing_tree();
if let Some(part_rules) = part_rules {
let containing_host = containing_shadow.map(|s| s.host());
let element = self.element;
let rule_hash_target = self.rule_hash_target;
let rules = &mut self.rules;
let flags_setter = &mut self.flags_setter;
let cascade_level = CascadeLevel::AuthorNormal {
shadow_cascade_order,
loop {
if parts.is_empty() {
return;
}

let outer_shadow = inner_shadow.host().containing_shadow();
let part_rules = match outer_shadow {
Some(shadow) => shadow
.style_data()
.and_then(|data| data.part_rules(self.pseudo_element)),
None => self
.stylist
.cascade_data()
.borrow_for_origin(Origin::Author)
.part_rules(self.pseudo_element),
};
let start = rules.len();
self.context.with_shadow_host(containing_host, |context| {
rule_hash_target.each_part(|p| {
if let Some(part_rules) = part_rules.get(p) {
SelectorMap::get_matching_rules(
element,
&part_rules,
rules,
context,
flags_setter,
cascade_level,
);

if let Some(part_rules) = part_rules {
let containing_host = outer_shadow.map(|s| s.host());
let element = self.element;
let rules = &mut self.rules;
let flags_setter = &mut self.flags_setter;
let cascade_level = CascadeLevel::AuthorNormal {
shadow_cascade_order,
};
let start = rules.len();
self.context.with_shadow_host(containing_host, |context| {
for p in &parts {
if let Some(part_rules) = part_rules.get(p) {
SelectorMap::get_matching_rules(
element,
&part_rules,
rules,
context,
flags_setter,
cascade_level,
);
}
}
});
sort_rules_from(rules, start);
shadow_cascade_order.inc();
}

let inner_shadow_host = inner_shadow.host();

inner_shadow = match outer_shadow {
Some(s) => s,
None => break, // Nowhere to export to.
};

parts.retain(|part| {
let exported_part = match inner_shadow_host.exported_part(part) {
Some(part) => part,
None => return false,
};
std::mem::replace(part, exported_part);
true
});
sort_rules_from(rules, start);
}
}

Expand Down

0 comments on commit e3009a4

Please sign in to comment.