mirror of
https://github.com/servo/servo.git
synced 2025-07-04 05:53:39 +01:00
Auto merge of #18827 - heycam:invalidate-less, r=emilio
style: Support more selectors in the style sheet invalidator. In addition to being able to invalidate just based on local name, this also adds support for invalidating based on selectors with no ancestor combinator in them (resulting in a RESTYLE_SELF for those elements that match). Reviewed in https://bugzilla.mozilla.org/show_bug.cgi?id=1407522 by Emilio. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/18827) <!-- Reviewable:end -->
This commit is contained in:
commit
2f3bc0de49
1 changed files with 114 additions and 54 deletions
|
@ -8,45 +8,65 @@
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
|
|
||||||
use Atom;
|
use Atom;
|
||||||
|
use LocalName as SelectorLocalName;
|
||||||
use dom::{TElement, TNode};
|
use dom::{TElement, TNode};
|
||||||
use fnv::FnvHashSet;
|
use fnv::FnvHashSet;
|
||||||
use invalidation::element::restyle_hints::RestyleHint;
|
use invalidation::element::restyle_hints::{RESTYLE_SELF, RestyleHint};
|
||||||
use media_queries::Device;
|
use media_queries::Device;
|
||||||
use selector_parser::SelectorImpl;
|
use selector_parser::SelectorImpl;
|
||||||
use selectors::attr::CaseSensitivity;
|
use selectors::attr::CaseSensitivity;
|
||||||
use selectors::parser::{Component, Selector};
|
use selectors::parser::{Component, LocalName, Selector};
|
||||||
use shared_lock::SharedRwLockReadGuard;
|
use shared_lock::SharedRwLockReadGuard;
|
||||||
use stylesheets::{CssRule, StylesheetInDocument};
|
use stylesheets::{CssRule, StylesheetInDocument};
|
||||||
|
|
||||||
/// An invalidation scope represents a kind of subtree that may need to be
|
/// A style sheet invalidation represents a kind of element or subtree that may
|
||||||
/// restyled.
|
/// need to be restyled. Whether it represents a whole subtree or just a single
|
||||||
|
/// element is determined by whether the invalidation is stored in the
|
||||||
|
/// StylesheetInvalidationSet's invalid_scopes or invalid_elements table.
|
||||||
#[derive(Debug, Eq, Hash, PartialEq)]
|
#[derive(Debug, Eq, Hash, PartialEq)]
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
enum InvalidationScope {
|
enum Invalidation {
|
||||||
/// All the descendants of an element with a given id.
|
/// An element with a given id.
|
||||||
ID(Atom),
|
ID(Atom),
|
||||||
/// All the descendants of an element with a given class name.
|
/// An element with a given class name.
|
||||||
Class(Atom),
|
Class(Atom),
|
||||||
|
/// An element with a given local name.
|
||||||
|
LocalName { name: SelectorLocalName, lower_name: SelectorLocalName },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InvalidationScope {
|
impl Invalidation {
|
||||||
fn is_id(&self) -> bool {
|
fn is_id(&self) -> bool {
|
||||||
matches!(*self, InvalidationScope::ID(..))
|
matches!(*self, Invalidation::ID(..))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_id_or_class(&self) -> bool {
|
||||||
|
matches!(*self, Invalidation::ID(..) | Invalidation::Class(..))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn matches<E>(&self, element: E) -> bool
|
fn matches<E>(&self, element: E) -> bool
|
||||||
where E: TElement,
|
where E: TElement,
|
||||||
{
|
{
|
||||||
match *self {
|
match *self {
|
||||||
InvalidationScope::Class(ref class) => {
|
Invalidation::Class(ref class) => {
|
||||||
|
// FIXME This should look at the quirks mode of the document to
|
||||||
|
// determine case sensitivity.
|
||||||
element.has_class(class, CaseSensitivity::CaseSensitive)
|
element.has_class(class, CaseSensitivity::CaseSensitive)
|
||||||
}
|
}
|
||||||
InvalidationScope::ID(ref id) => {
|
Invalidation::ID(ref id) => {
|
||||||
match element.get_id() {
|
match element.get_id() {
|
||||||
|
// FIXME This should look at the quirks mode of the document
|
||||||
|
// to determine case sensitivity.
|
||||||
Some(element_id) => element_id == *id,
|
Some(element_id) => element_id == *id,
|
||||||
None => false,
|
None => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Invalidation::LocalName { ref name, ref lower_name } => {
|
||||||
|
// This could look at the quirks mode of the document, instead
|
||||||
|
// of testing against both names, but it's probably not worth
|
||||||
|
// it.
|
||||||
|
let local_name = element.get_local_name();
|
||||||
|
*local_name == **name || *local_name == **lower_name
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,9 +77,11 @@ impl InvalidationScope {
|
||||||
/// changes too (or even selector changes?).
|
/// changes too (or even selector changes?).
|
||||||
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
|
||||||
pub struct StylesheetInvalidationSet {
|
pub struct StylesheetInvalidationSet {
|
||||||
/// The style scopes we know we have to restyle so far.
|
/// The subtrees we know we have to restyle so far.
|
||||||
invalid_scopes: FnvHashSet<InvalidationScope>,
|
invalid_scopes: FnvHashSet<Invalidation>,
|
||||||
/// Whether the whole document should be invalid.
|
/// The elements we know we have to restyle so far.
|
||||||
|
invalid_elements: FnvHashSet<Invalidation>,
|
||||||
|
/// Whether the whole document should be restyled.
|
||||||
fully_invalid: bool,
|
fully_invalid: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,6 +90,7 @@ impl StylesheetInvalidationSet {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
invalid_scopes: FnvHashSet::default(),
|
invalid_scopes: FnvHashSet::default(),
|
||||||
|
invalid_elements: FnvHashSet::default(),
|
||||||
fully_invalid: false,
|
fully_invalid: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,6 +99,7 @@ impl StylesheetInvalidationSet {
|
||||||
pub fn invalidate_fully(&mut self) {
|
pub fn invalidate_fully(&mut self) {
|
||||||
debug!("StylesheetInvalidationSet::invalidate_fully");
|
debug!("StylesheetInvalidationSet::invalidate_fully");
|
||||||
self.invalid_scopes.clear();
|
self.invalid_scopes.clear();
|
||||||
|
self.invalid_elements.clear();
|
||||||
self.fully_invalid = true;
|
self.fully_invalid = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,11 +131,13 @@ impl StylesheetInvalidationSet {
|
||||||
self.collect_invalidations_for_rule(rule, guard);
|
self.collect_invalidations_for_rule(rule, guard);
|
||||||
if self.fully_invalid {
|
if self.fully_invalid {
|
||||||
self.invalid_scopes.clear();
|
self.invalid_scopes.clear();
|
||||||
|
self.invalid_elements.clear();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!(" > resulting invalidations: {:?}", self.invalid_scopes);
|
debug!(" > resulting subtree invalidations: {:?}", self.invalid_scopes);
|
||||||
|
debug!(" > resulting self invalidations: {:?}", self.invalid_elements);
|
||||||
debug!(" > fully_invalid: {}", self.fully_invalid);
|
debug!(" > fully_invalid: {}", self.fully_invalid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,6 +159,7 @@ impl StylesheetInvalidationSet {
|
||||||
/// Clears the invalidation set without processing.
|
/// Clears the invalidation set without processing.
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.invalid_scopes.clear();
|
self.invalid_scopes.clear();
|
||||||
|
self.invalid_elements.clear();
|
||||||
self.fully_invalid = false;
|
self.fully_invalid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,7 +180,7 @@ impl StylesheetInvalidationSet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.invalid_scopes.is_empty() {
|
if self.invalid_scopes.is_empty() && self.invalid_elements.is_empty() {
|
||||||
debug!("process_invalidations: empty invalidation set");
|
debug!("process_invalidations: empty invalidation set");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -161,9 +188,9 @@ impl StylesheetInvalidationSet {
|
||||||
self.process_invalidations_in_subtree(element)
|
self.process_invalidations_in_subtree(element)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Process style invalidations in a given subtree, that is, look for all
|
/// Process style invalidations in a given subtree. This traverses the
|
||||||
/// the relevant scopes in the subtree, and mark as dirty only the relevant
|
/// subtree looking for elements that match the invalidations in
|
||||||
/// ones.
|
/// invalid_scopes and invalid_elements.
|
||||||
///
|
///
|
||||||
/// Returns whether it invalidated at least one element's style.
|
/// Returns whether it invalidated at least one element's style.
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
|
@ -185,15 +212,28 @@ impl StylesheetInvalidationSet {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for scope in &self.invalid_scopes {
|
for invalidation in &self.invalid_scopes {
|
||||||
if scope.matches(element) {
|
if invalidation.matches(element) {
|
||||||
debug!("process_invalidations_in_subtree: {:?} matched {:?}",
|
debug!("process_invalidations_in_subtree: {:?} matched subtree {:?}",
|
||||||
element, scope);
|
element, invalidation);
|
||||||
data.hint.insert(RestyleHint::restyle_subtree());
|
data.hint.insert(RestyleHint::restyle_subtree());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut self_invalid = false;
|
||||||
|
|
||||||
|
if !data.hint.contains(RESTYLE_SELF) {
|
||||||
|
for invalidation in &self.invalid_elements {
|
||||||
|
if invalidation.matches(element) {
|
||||||
|
debug!("process_invalidations_in_subtree: {:?} matched self {:?}",
|
||||||
|
element, invalidation);
|
||||||
|
data.hint.insert(RESTYLE_SELF);
|
||||||
|
self_invalid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut any_children_invalid = false;
|
let mut any_children_invalid = false;
|
||||||
|
|
||||||
|
@ -212,22 +252,30 @@ impl StylesheetInvalidationSet {
|
||||||
unsafe { element.set_dirty_descendants() }
|
unsafe { element.set_dirty_descendants() }
|
||||||
}
|
}
|
||||||
|
|
||||||
return any_children_invalid
|
return self_invalid || any_children_invalid
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scan_component(
|
fn scan_component(
|
||||||
component: &Component<SelectorImpl>,
|
component: &Component<SelectorImpl>,
|
||||||
scope: &mut Option<InvalidationScope>)
|
invalidation: &mut Option<Invalidation>)
|
||||||
{
|
{
|
||||||
match *component {
|
match *component {
|
||||||
|
Component::LocalName(LocalName { ref name, ref lower_name }) => {
|
||||||
|
if invalidation.as_ref().map_or(true, |s| !s.is_id_or_class()) {
|
||||||
|
*invalidation = Some(Invalidation::LocalName {
|
||||||
|
name: name.clone(),
|
||||||
|
lower_name: lower_name.clone(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Component::Class(ref class) => {
|
Component::Class(ref class) => {
|
||||||
if scope.as_ref().map_or(true, |s| !s.is_id()) {
|
if invalidation.as_ref().map_or(true, |s| !s.is_id()) {
|
||||||
*scope = Some(InvalidationScope::Class(class.clone()));
|
*invalidation = Some(Invalidation::Class(class.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Component::ID(ref id) => {
|
Component::ID(ref id) => {
|
||||||
if scope.is_none() {
|
if invalidation.is_none() {
|
||||||
*scope = Some(InvalidationScope::ID(id.clone()));
|
*invalidation = Some(Invalidation::ID(id.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
@ -236,47 +284,59 @@ impl StylesheetInvalidationSet {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Collect a style scopes for a given selector.
|
/// Collect invalidations for a given selector.
|
||||||
///
|
///
|
||||||
/// We look at the outermost class or id selector to the left of an ancestor
|
/// We look at the outermost local name, class, or ID selector to the left
|
||||||
/// combinator, in order to restyle only a given subtree.
|
/// of an ancestor combinator, in order to restyle only a given subtree.
|
||||||
///
|
///
|
||||||
/// We prefer id scopes to class scopes, and outermost scopes to innermost
|
/// If the selector has no ancestor combinator, then we do the same for
|
||||||
/// scopes (to reduce the amount of traversal we need to do).
|
/// the only sequence it has, but record it as an element invalidation
|
||||||
fn collect_scopes(&mut self, selector: &Selector<SelectorImpl>) {
|
/// instead of a subtree invalidation.
|
||||||
debug!("StylesheetInvalidationSet::collect_scopes({:?})", selector);
|
///
|
||||||
|
/// We prefer IDs to classs, and classes to local names, on the basis
|
||||||
|
/// that the former should be more specific than the latter. We also
|
||||||
|
/// prefer to generate subtree invalidations for the outermost part
|
||||||
|
/// of the selector, to reduce the amount of traversal we need to do
|
||||||
|
/// when flushing invalidations.
|
||||||
|
fn collect_invalidations(&mut self, selector: &Selector<SelectorImpl>) {
|
||||||
|
debug!("StylesheetInvalidationSet::collect_invalidations({:?})", selector);
|
||||||
|
|
||||||
let mut scope: Option<InvalidationScope> = None;
|
let mut element_invalidation: Option<Invalidation> = None;
|
||||||
|
let mut subtree_invalidation: Option<Invalidation> = None;
|
||||||
|
|
||||||
|
let mut scan_for_element_invalidation = true;
|
||||||
|
let mut scan_for_subtree_invalidation = false;
|
||||||
|
|
||||||
let mut scan = true;
|
|
||||||
let mut iter = selector.iter();
|
let mut iter = selector.iter();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
for component in &mut iter {
|
for component in &mut iter {
|
||||||
if scan {
|
if scan_for_element_invalidation {
|
||||||
Self::scan_component(component, &mut scope);
|
Self::scan_component(component, &mut element_invalidation);
|
||||||
|
} else if scan_for_subtree_invalidation {
|
||||||
|
Self::scan_component(component, &mut subtree_invalidation);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
match iter.next_sequence() {
|
match iter.next_sequence() {
|
||||||
None => break,
|
None => break,
|
||||||
Some(combinator) => {
|
Some(combinator) => {
|
||||||
scan = combinator.is_ancestor();
|
scan_for_subtree_invalidation = combinator.is_ancestor();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
scan_for_element_invalidation = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
match scope {
|
if let Some(s) = subtree_invalidation {
|
||||||
Some(s) => {
|
debug!(" > Found subtree invalidation: {:?}", s);
|
||||||
debug!(" > Found scope: {:?}", s);
|
self.invalid_scopes.insert(s);
|
||||||
self.invalid_scopes.insert(s);
|
} else if let Some(s) = element_invalidation {
|
||||||
}
|
debug!(" > Found element invalidation: {:?}", s);
|
||||||
None => {
|
self.invalid_elements.insert(s);
|
||||||
debug!(" > Scope not found");
|
} else {
|
||||||
|
// The selector was of a form that we can't handle. Any element
|
||||||
// If we didn't find a scope, any element could match this, so
|
// could match it, so let's just bail out.
|
||||||
// let's just bail out.
|
debug!(" > Can't handle selector, marking fully invalid");
|
||||||
self.fully_invalid = true;
|
self.fully_invalid = true;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,7 +354,7 @@ impl StylesheetInvalidationSet {
|
||||||
Style(ref lock) => {
|
Style(ref lock) => {
|
||||||
let style_rule = lock.read_with(guard);
|
let style_rule = lock.read_with(guard);
|
||||||
for selector in &style_rule.selectors.0 {
|
for selector in &style_rule.selectors.0 {
|
||||||
self.collect_scopes(selector);
|
self.collect_invalidations(selector);
|
||||||
if self.fully_invalid {
|
if self.fully_invalid {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue