From 488c1291d483b0d5e0308f9499202577d3470482 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 3 Nov 2015 11:23:38 -0800 Subject: [PATCH 1/7] Hoist AttrValue into style. --- components/script/dom/attr.rs | 165 +---------------------- components/script/dom/bindings/trace.rs | 2 + components/style/attr.rs | 170 ++++++++++++++++++++++++ components/style/lib.rs | 1 + 4 files changed, 175 insertions(+), 163 deletions(-) create mode 100644 components/style/attr.rs diff --git a/components/script/dom/attr.rs b/components/script/dom/attr.rs index 315f26a3b7c..8a16124b389 100644 --- a/components/script/dom/attr.rs +++ b/components/script/dom/attr.rs @@ -2,7 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use cssparser::RGBA; use devtools_traits::AttrInfo; use dom::bindings::cell::DOMRefCell; use dom::bindings::codegen::Bindings::AttrBinding::{self, AttrMethods}; @@ -12,174 +11,14 @@ use dom::bindings::js::{JS, MutNullableHeap}; use dom::bindings::js::{LayoutJS, Root, RootedReference}; use dom::bindings::reflector::{Reflector, reflect_dom_object}; use dom::element::{AttributeMutation, Element}; -use dom::values::UNSIGNED_LONG_MAX; use dom::virtualmethods::vtable_for; use dom::window::Window; use std::borrow::ToOwned; use std::cell::Ref; use std::mem; -use std::ops::Deref; use string_cache::{Atom, Namespace}; -use style::values::specified::Length; -use util::str::{DOMString, LengthOrPercentageOrAuto, parse_unsigned_integer, parse_legacy_color, parse_length}; -use util::str::{split_html_space_chars, str_join}; - -#[derive(JSTraceable, PartialEq, Clone, HeapSizeOf)] -pub enum AttrValue { - String(DOMString), - TokenList(DOMString, Vec), - UInt(DOMString, u32), - Atom(Atom), - Length(DOMString, Option), - Color(DOMString, Option), - Dimension(DOMString, LengthOrPercentageOrAuto), -} - -impl AttrValue { - pub fn from_serialized_tokenlist(tokens: DOMString) -> AttrValue { - let atoms = - split_html_space_chars(&tokens) - .map(Atom::from_slice) - .fold(vec![], |mut acc, atom| { - if !acc.contains(&atom) { acc.push(atom) } - acc - }); - AttrValue::TokenList(tokens, atoms) - } - - pub fn from_atomic_tokens(atoms: Vec) -> AttrValue { - let tokens = DOMString(str_join(&atoms, "\x20")); - AttrValue::TokenList(tokens, atoms) - } - - // https://html.spec.whatwg.org/multipage/#reflecting-content-attributes-in-idl-attributes:idl-unsigned-long - pub fn from_u32(string: DOMString, default: u32) -> AttrValue { - let result = parse_unsigned_integer(string.chars()).unwrap_or(default); - let result = if result > UNSIGNED_LONG_MAX { - default - } else { - result - }; - AttrValue::UInt(string, result) - } - - // https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers-greater-than-zero - pub fn from_limited_u32(string: DOMString, default: u32) -> AttrValue { - let result = parse_unsigned_integer(string.chars()).unwrap_or(default); - let result = if result == 0 || result > UNSIGNED_LONG_MAX { - default - } else { - result - }; - AttrValue::UInt(string, result) - } - - pub fn from_atomic(string: DOMString) -> AttrValue { - let value = Atom::from_slice(&string); - AttrValue::Atom(value) - } - - pub fn from_legacy_color(string: DOMString) -> AttrValue { - let parsed = parse_legacy_color(&string).ok(); - AttrValue::Color(string, parsed) - } - - pub fn from_dimension(string: DOMString) -> AttrValue { - let parsed = parse_length(&string); - AttrValue::Dimension(string, parsed) - } - - /// Assumes the `AttrValue` is a `TokenList` and returns its tokens - /// - /// ## Panics - /// - /// Panics if the `AttrValue` is not a `TokenList` - pub fn as_tokens(&self) -> &[Atom] { - match *self { - AttrValue::TokenList(_, ref tokens) => tokens, - _ => panic!("Tokens not found"), - } - } - - /// Assumes the `AttrValue` is an `Atom` and returns its value - /// - /// ## Panics - /// - /// Panics if the `AttrValue` is not an `Atom` - pub fn as_atom(&self) -> &Atom { - match *self { - AttrValue::Atom(ref value) => value, - _ => panic!("Atom not found"), - } - } - - /// Assumes the `AttrValue` is a `Color` and returns its value - /// - /// ## Panics - /// - /// Panics if the `AttrValue` is not a `Color` - pub fn as_color(&self) -> Option<&RGBA> { - match *self { - AttrValue::Color(_, ref color) => color.as_ref(), - _ => panic!("Color not found"), - } - } - - /// Assumes the `AttrValue` is a `Length` and returns its value - /// - /// ## Panics - /// - /// Panics if the `AttrValue` is not a `Length` - pub fn as_length(&self) -> Option<&Length> { - match *self { - AttrValue::Length(_, ref length) => length.as_ref(), - _ => panic!("Length not found"), - } - } - - /// Assumes the `AttrValue` is a `Dimension` and returns its value - /// - /// ## Panics - /// - /// Panics if the `AttrValue` is not a `Dimension` - pub fn as_dimension(&self) -> &LengthOrPercentageOrAuto { - match *self { - AttrValue::Dimension(_, ref l) => l, - _ => panic!("Dimension not found"), - } - } - - /// Return the AttrValue as its integer representation, if any. - /// This corresponds to attribute values returned as `AttrValue::UInt(_)` - /// by `VirtualMethods::parse_plain_attribute()`. - /// - /// ## Panics - /// - /// Panics if the `AttrValue` is not a `UInt` - pub fn as_uint(&self) -> u32 { - if let AttrValue::UInt(_, value) = *self { - value - } else { - panic!("Uint not found"); - } - } -} - -impl Deref for AttrValue { - type Target = str; - - fn deref(&self) -> &str { - match *self { - AttrValue::String(ref value) | - AttrValue::TokenList(ref value, _) | - AttrValue::UInt(ref value, _) | - AttrValue::Length(ref value, _) | - AttrValue::Color(ref value, _) | - AttrValue::Dimension(ref value, _) => &value, - AttrValue::Atom(ref value) => &value, - } - } -} +pub use style::attr::AttrValue; +use util::str::DOMString; // https://dom.spec.whatwg.org/#interface-attr #[dom_struct] diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index 9b592e731f7..a86391e0178 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -82,6 +82,7 @@ use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::mpsc::{Receiver, Sender}; use string_cache::{Atom, Namespace, QualName}; +use style::attr::AttrValue; use style::properties::PropertyDeclarationBlock; use style::values::specified::Length; use url::Url; @@ -289,6 +290,7 @@ no_jsmanaged_fields!(Length); no_jsmanaged_fields!(ElementState); no_jsmanaged_fields!(DOMString); no_jsmanaged_fields!(Mime); +no_jsmanaged_fields!(AttrValue); impl JSTraceable for Box { #[inline] diff --git a/components/style/attr.rs b/components/style/attr.rs new file mode 100644 index 00000000000..3127be4bc45 --- /dev/null +++ b/components/style/attr.rs @@ -0,0 +1,170 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use cssparser::RGBA; +use std::ops::Deref; +use string_cache::{Atom, Namespace}; +use util::str::{DOMString, LengthOrPercentageOrAuto, parse_unsigned_integer, parse_legacy_color, parse_length}; +use util::str::{split_html_space_chars, str_join}; +use values::specified::{Length}; + +// Duplicated from script::dom::values. +const UNSIGNED_LONG_MAX: u32 = 2147483647; + +#[derive(PartialEq, Clone, HeapSizeOf)] +pub enum AttrValue { + String(DOMString), + TokenList(DOMString, Vec), + UInt(DOMString, u32), + Atom(Atom), + Length(DOMString, Option), + Color(DOMString, Option), + Dimension(DOMString, LengthOrPercentageOrAuto), +} + +impl AttrValue { + pub fn from_serialized_tokenlist(tokens: DOMString) -> AttrValue { + let atoms = + split_html_space_chars(&tokens) + .map(Atom::from_slice) + .fold(vec![], |mut acc, atom| { + if !acc.contains(&atom) { acc.push(atom) } + acc + }); + AttrValue::TokenList(tokens, atoms) + } + + pub fn from_atomic_tokens(atoms: Vec) -> AttrValue { + let tokens = DOMString(str_join(&atoms, "\x20")); + AttrValue::TokenList(tokens, atoms) + } + + // https://html.spec.whatwg.org/multipage/#reflecting-content-attributes-in-idl-attributes:idl-unsigned-long + pub fn from_u32(string: DOMString, default: u32) -> AttrValue { + let result = parse_unsigned_integer(string.chars()).unwrap_or(default); + let result = if result > UNSIGNED_LONG_MAX { + default + } else { + result + }; + AttrValue::UInt(string, result) + } + + // https://html.spec.whatwg.org/multipage/#limited-to-only-non-negative-numbers-greater-than-zero + pub fn from_limited_u32(string: DOMString, default: u32) -> AttrValue { + let result = parse_unsigned_integer(string.chars()).unwrap_or(default); + let result = if result == 0 || result > UNSIGNED_LONG_MAX { + default + } else { + result + }; + AttrValue::UInt(string, result) + } + + pub fn from_atomic(string: DOMString) -> AttrValue { + let value = Atom::from_slice(&string); + AttrValue::Atom(value) + } + + pub fn from_legacy_color(string: DOMString) -> AttrValue { + let parsed = parse_legacy_color(&string).ok(); + AttrValue::Color(string, parsed) + } + + pub fn from_dimension(string: DOMString) -> AttrValue { + let parsed = parse_length(&string); + AttrValue::Dimension(string, parsed) + } + + /// Assumes the `AttrValue` is a `TokenList` and returns its tokens + /// + /// ## Panics + /// + /// Panics if the `AttrValue` is not a `TokenList` + pub fn as_tokens(&self) -> &[Atom] { + match *self { + AttrValue::TokenList(_, ref tokens) => tokens, + _ => panic!("Tokens not found"), + } + } + + /// Assumes the `AttrValue` is an `Atom` and returns its value + /// + /// ## Panics + /// + /// Panics if the `AttrValue` is not an `Atom` + pub fn as_atom(&self) -> &Atom { + match *self { + AttrValue::Atom(ref value) => value, + _ => panic!("Atom not found"), + } + } + + /// Assumes the `AttrValue` is a `Color` and returns its value + /// + /// ## Panics + /// + /// Panics if the `AttrValue` is not a `Color` + pub fn as_color(&self) -> Option<&RGBA> { + match *self { + AttrValue::Color(_, ref color) => color.as_ref(), + _ => panic!("Color not found"), + } + } + + /// Assumes the `AttrValue` is a `Length` and returns its value + /// + /// ## Panics + /// + /// Panics if the `AttrValue` is not a `Length` + pub fn as_length(&self) -> Option<&Length> { + match *self { + AttrValue::Length(_, ref length) => length.as_ref(), + _ => panic!("Length not found"), + } + } + + /// Assumes the `AttrValue` is a `Dimension` and returns its value + /// + /// ## Panics + /// + /// Panics if the `AttrValue` is not a `Dimension` + pub fn as_dimension(&self) -> &LengthOrPercentageOrAuto { + match *self { + AttrValue::Dimension(_, ref l) => l, + _ => panic!("Dimension not found"), + } + } + + /// Return the AttrValue as its integer representation, if any. + /// This corresponds to attribute values returned as `AttrValue::UInt(_)` + /// by `VirtualMethods::parse_plain_attribute()`. + /// + /// ## Panics + /// + /// Panics if the `AttrValue` is not a `UInt` + pub fn as_uint(&self) -> u32 { + if let AttrValue::UInt(_, value) = *self { + value + } else { + panic!("Uint not found"); + } + } +} + +impl Deref for AttrValue { + type Target = str; + + fn deref(&self) -> &str { + match *self { + AttrValue::String(ref value) | + AttrValue::TokenList(ref value, _) | + AttrValue::UInt(ref value, _) | + AttrValue::Length(ref value, _) | + AttrValue::Color(ref value, _) | + AttrValue::Dimension(ref value, _) => &value, + AttrValue::Atom(ref value) => &value, + } + } +} diff --git a/components/style/lib.rs b/components/style/lib.rs index 997cd90b79c..a30274233d9 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -42,6 +42,7 @@ extern crate url; extern crate util; pub mod animation; +pub mod attr; mod custom_properties; pub mod font_face; pub mod legacy; From c9704992a9f378a682835fe86def542b9086ea1c Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 3 Nov 2015 12:16:53 -0800 Subject: [PATCH 2/7] Factor the name-related fields of Attr into a struct and move it to style. --- components/script/dom/attr.rs | 43 ++++++++++++------------- components/script/dom/bindings/trace.rs | 3 +- components/style/attr.rs | 8 +++++ 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/components/script/dom/attr.rs b/components/script/dom/attr.rs index 8a16124b389..c5cd7c3d4ec 100644 --- a/components/script/dom/attr.rs +++ b/components/script/dom/attr.rs @@ -17,18 +17,15 @@ use std::borrow::ToOwned; use std::cell::Ref; use std::mem; use string_cache::{Atom, Namespace}; -pub use style::attr::AttrValue; +pub use style::attr::{AttrIdentifier, AttrValue}; use util::str::DOMString; // https://dom.spec.whatwg.org/#interface-attr #[dom_struct] pub struct Attr { reflector_: Reflector, - local_name: Atom, + identifier: AttrIdentifier, value: DOMRefCell, - name: Atom, - namespace: Namespace, - prefix: Option, /// the element that owns this attribute. owner: MutNullableHeap>, @@ -39,11 +36,13 @@ impl Attr { prefix: Option, owner: Option<&Element>) -> Attr { Attr { reflector_: Reflector::new(), - local_name: local_name, + identifier: AttrIdentifier { + local_name: local_name, + name: name, + namespace: namespace, + prefix: prefix, + }, value: DOMRefCell::new(value), - name: name, - namespace: namespace, - prefix: prefix, owner: MutNullableHeap::new(owner), } } @@ -59,17 +58,17 @@ impl Attr { #[inline] pub fn name(&self) -> &Atom { - &self.name + &self.identifier.name } #[inline] pub fn namespace(&self) -> &Namespace { - &self.namespace + &self.identifier.namespace } #[inline] pub fn prefix(&self) -> &Option { - &self.prefix + &self.identifier.prefix } } @@ -89,7 +88,7 @@ impl AttrMethods for Attr { match self.owner() { None => *self.value.borrow_mut() = AttrValue::String(value), Some(owner) => { - let value = owner.parse_attribute(&self.namespace, self.local_name(), value); + let value = owner.parse_attribute(&self.identifier.namespace, self.local_name(), value); self.set_value(value, owner.r()); } } @@ -117,12 +116,12 @@ impl AttrMethods for Attr { // https://dom.spec.whatwg.org/#dom-attr-name fn Name(&self) -> DOMString { - DOMString((*self.name).to_owned()) + DOMString((*self.identifier.name).to_owned()) } // https://dom.spec.whatwg.org/#dom-attr-namespaceuri fn GetNamespaceURI(&self) -> Option { - let Namespace(ref atom) = self.namespace; + let Namespace(ref atom) = self.identifier.namespace; match &**atom { "" => None, url => Some(DOMString(url.to_owned())), @@ -150,7 +149,7 @@ impl Attr { pub fn set_value(&self, mut value: AttrValue, owner: &Element) { assert!(Some(owner) == self.owner().r()); mem::swap(&mut *self.value.borrow_mut(), &mut value); - if self.namespace == ns!("") { + if self.identifier.namespace == ns!("") { vtable_for(owner.upcast()).attribute_mutated( self, AttributeMutation::Set(Some(&value))); } @@ -161,21 +160,21 @@ impl Attr { } pub fn local_name(&self) -> &Atom { - &self.local_name + &self.identifier.local_name } /// Sets the owner element. Should be called after the attribute is added /// or removed from its older parent. pub fn set_owner(&self, owner: Option<&Element>) { - let ref ns = self.namespace; + let ref ns = self.identifier.namespace; match (self.owner().r(), owner) { (None, Some(new)) => { // Already in the list of attributes of new owner. - assert!(new.get_attribute(&ns, &self.local_name) == Some(Root::from_ref(self))) + assert!(new.get_attribute(&ns, &self.identifier.local_name) == Some(Root::from_ref(self))) } (Some(old), None) => { // Already gone from the list of attributes of old owner. - assert!(old.get_attribute(&ns, &self.local_name).is_none()) + assert!(old.get_attribute(&ns, &self.identifier.local_name).is_none()) } (old, new) => assert!(old == new) } @@ -187,7 +186,7 @@ impl Attr { } pub fn summarize(&self) -> AttrInfo { - let Namespace(ref ns) = self.namespace; + let Namespace(ref ns) = self.identifier.namespace; AttrInfo { namespace: (**ns).to_owned(), name: self.Name().0, @@ -239,7 +238,7 @@ impl AttrHelpersForLayout for LayoutJS { #[inline] unsafe fn local_name_atom_forever(&self) -> Atom { - (*self.unsafe_get()).local_name.clone() + (*self.unsafe_get()).identifier.local_name.clone() } #[inline] diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index a86391e0178..d3259f56453 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -82,7 +82,7 @@ use std::sync::Arc; use std::sync::atomic::AtomicBool; use std::sync::mpsc::{Receiver, Sender}; use string_cache::{Atom, Namespace, QualName}; -use style::attr::AttrValue; +use style::attr::{AttrIdentifier, AttrValue}; use style::properties::PropertyDeclarationBlock; use style::values::specified::Length; use url::Url; @@ -290,6 +290,7 @@ no_jsmanaged_fields!(Length); no_jsmanaged_fields!(ElementState); no_jsmanaged_fields!(DOMString); no_jsmanaged_fields!(Mime); +no_jsmanaged_fields!(AttrIdentifier); no_jsmanaged_fields!(AttrValue); impl JSTraceable for Box { diff --git a/components/style/attr.rs b/components/style/attr.rs index 3127be4bc45..e9fc50e0845 100644 --- a/components/style/attr.rs +++ b/components/style/attr.rs @@ -168,3 +168,11 @@ impl Deref for AttrValue { } } } + +#[derive(Clone, HeapSizeOf, Debug)] +pub struct AttrIdentifier { + pub local_name: Atom, + pub name: Atom, + pub namespace: Namespace, + pub prefix: Option, +} From 47744d95add56352ab5d0631d3dd5e054aad3849 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Mon, 9 Nov 2015 15:19:13 -0800 Subject: [PATCH 3/7] Make RESTYLE_SELF imply RESTYLE_DESCENDANTS. This is a temporary workaround for #8438. --- components/layout/wrapper.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components/layout/wrapper.rs b/components/layout/wrapper.rs index ae8462426e8..ad745d2d091 100644 --- a/components/layout/wrapper.rs +++ b/components/layout/wrapper.rs @@ -413,7 +413,7 @@ impl<'le> LayoutElement<'le> { } /// Properly marks nodes as dirty in response to restyle hints. - pub fn note_restyle_hint(&self, hint: RestyleHint) { + pub fn note_restyle_hint(&self, mut hint: RestyleHint) { // Bail early if there's no restyling to do. if hint.is_empty() { return; @@ -446,6 +446,11 @@ impl<'le> LayoutElement<'le> { // Process hints. if hint.contains(RESTYLE_SELF) { dirty_node(&node); + + // FIXME(bholley, #8438): We currently need to RESTYLE_DESCENDANTS in the + // RESTYLE_SELF case in order to make sure "inherit" style structs propagate + // properly. See the explanation in the github issue. + hint.insert(RESTYLE_DESCENDANTS); } if hint.contains(RESTYLE_DESCENDANTS) { unsafe { node.set_dirty_descendants(true); } From 7fa79366576751fa6d032091ea54698263783d5d Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Thu, 5 Nov 2015 09:58:06 -0800 Subject: [PATCH 4/7] Implement attribute restyle hints. Fixes #6942. --- components/layout/layout_task.rs | 6 +- components/layout/wrapper.rs | 19 +-- components/script/dom/attr.rs | 5 + components/script/dom/bindings/trace.rs | 2 + components/script/dom/document.rs | 27 +++- components/script/dom/element.rs | 42 +++-- components/script/dom/node.rs | 17 +- components/style/restyle_hints.rs | 196 ++++++++++++++++++------ components/style/selector_matching.rs | 24 +-- 9 files changed, 237 insertions(+), 101 deletions(-) diff --git a/components/layout/layout_task.rs b/components/layout/layout_task.rs index 514e3e71e66..d650a927ad3 100644 --- a/components/layout/layout_task.rs +++ b/components/layout/layout_task.rs @@ -1145,10 +1145,8 @@ impl LayoutTask { let modified_elements = document.drain_modified_elements(); if !needs_dirtying { - for &(el, old_state) in modified_elements.iter() { - let hint = rw_data.stylist.restyle_hint_for_state_change(&el, - el.get_state(), - old_state); + for (el, snapshot) in modified_elements { + let hint = rw_data.stylist.compute_restyle_hint(&el, &snapshot, el.get_state()); el.note_restyle_hint(hint); } } diff --git a/components/layout/wrapper.rs b/components/layout/wrapper.rs index ad745d2d091..75244159727 100644 --- a/components/layout/wrapper.rs +++ b/components/layout/wrapper.rs @@ -58,7 +58,6 @@ use selectors::states::*; use smallvec::VecLike; use std::borrow::ToOwned; use std::cell::{Ref, RefMut}; -use std::iter::FromIterator; use std::marker::PhantomData; use std::mem; use std::sync::Arc; @@ -69,7 +68,7 @@ use style::legacy::UnsignedIntegerAttribute; use style::node::TElementAttributes; use style::properties::ComputedValues; use style::properties::{PropertyDeclaration, PropertyDeclarationBlock}; -use style::restyle_hints::{RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint}; +use style::restyle_hints::{ElementSnapshot, RESTYLE_DESCENDANTS, RESTYLE_LATER_SIBLINGS, RESTYLE_SELF, RestyleHint}; use url::Url; use util::str::{is_whitespace, search_index}; @@ -375,15 +374,13 @@ impl<'le> LayoutDocument<'le> { self.as_node().children().find(LayoutNode::is_element) } - pub fn drain_modified_elements(&self) -> Vec<(LayoutElement, ElementState)> { - unsafe { - let elements = self.document.drain_modified_elements(); - Vec::from_iter(elements.iter().map(|&(el, state)| - (LayoutElement { - element: el, - chain: PhantomData, - }, state))) - } + pub fn drain_modified_elements(&self) -> Vec<(LayoutElement, ElementSnapshot)> { + let elements = unsafe { self.document.drain_modified_elements() }; + elements.into_iter().map(|(el, snapshot)| + (LayoutElement { + element: el, + chain: PhantomData, + }, snapshot)).collect() } } diff --git a/components/script/dom/attr.rs b/components/script/dom/attr.rs index c5cd7c3d4ec..02ef5e4451e 100644 --- a/components/script/dom/attr.rs +++ b/components/script/dom/attr.rs @@ -148,6 +148,7 @@ impl AttrMethods for Attr { impl Attr { pub fn set_value(&self, mut value: AttrValue, owner: &Element) { assert!(Some(owner) == self.owner().r()); + owner.will_mutate_attr(); mem::swap(&mut *self.value.borrow_mut(), &mut value); if self.identifier.namespace == ns!("") { vtable_for(owner.upcast()).attribute_mutated( @@ -155,6 +156,10 @@ impl Attr { } } + pub fn identifier(&self) -> &AttrIdentifier { + &self.identifier + } + pub fn value(&self) -> Ref { self.value.borrow() } diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index d3259f56453..cd8fab7e56f 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -84,6 +84,7 @@ use std::sync::mpsc::{Receiver, Sender}; use string_cache::{Atom, Namespace, QualName}; use style::attr::{AttrIdentifier, AttrValue}; use style::properties::PropertyDeclarationBlock; +use style::restyle_hints::ElementSnapshot; use style::values::specified::Length; use url::Url; use util::str::{DOMString, LengthOrPercentageOrAuto}; @@ -292,6 +293,7 @@ no_jsmanaged_fields!(DOMString); no_jsmanaged_fields!(Mime); no_jsmanaged_fields!(AttrIdentifier); no_jsmanaged_fields!(AttrValue); +no_jsmanaged_fields!(ElementSnapshot); impl JSTraceable for Box { #[inline] diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index c077a4f5769..262e5a8b4d5 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -88,7 +88,6 @@ use net_traits::{AsyncResponseTarget, PendingAsyncLoad}; use num::ToPrimitive; use script_task::{MainThreadScriptMsg, Runnable}; use script_traits::{MouseButton, TouchEventType, TouchId, UntrustedNodeAddress}; -use selectors::states::*; use std::ascii::AsciiExt; use std::borrow::ToOwned; use std::boxed::FnBox; @@ -102,6 +101,7 @@ use std::rc::Rc; use std::sync::Arc; use std::sync::mpsc::channel; use string_cache::{Atom, QualName}; +use style::restyle_hints::ElementSnapshot; use style::stylesheets::Stylesheet; use time; use url::Url; @@ -188,8 +188,9 @@ pub struct Document { /// This field is set to the document itself for inert documents. /// https://html.spec.whatwg.org/multipage/#appropriate-template-contents-owner-document appropriate_template_contents_owner_document: MutNullableHeap>, - /// For each element that has had a state change since the last restyle, track the original state. - modified_elements: DOMRefCell, ElementState>>, + /// For each element that has had a state or attribute change since the last restyle, + /// track the original condition of the element. + modified_elements: DOMRefCell, ElementSnapshot>>, /// http://w3c.github.io/touch-events/#dfn-active-touch-point active_touch_points: DOMRefCell>>, } @@ -1275,7 +1276,7 @@ pub enum DocumentSource { #[allow(unsafe_code)] pub trait LayoutDocumentHelpers { unsafe fn is_html_document_for_layout(&self) -> bool; - unsafe fn drain_modified_elements(&self) -> Vec<(LayoutJS, ElementState)>; + unsafe fn drain_modified_elements(&self) -> Vec<(LayoutJS, ElementSnapshot)>; } #[allow(unsafe_code)] @@ -1287,7 +1288,7 @@ impl LayoutDocumentHelpers for LayoutJS { #[inline] #[allow(unrooted_must_root)] - unsafe fn drain_modified_elements(&self) -> Vec<(LayoutJS, ElementState)> { + unsafe fn drain_modified_elements(&self) -> Vec<(LayoutJS, ElementSnapshot)> { let mut elements = (*self.unsafe_get()).modified_elements.borrow_mut_for_layout(); let drain = elements.drain(); let layout_drain = drain.map(|(k, v)| (k.to_layout(), v)); @@ -1457,7 +1458,21 @@ impl Document { pub fn element_state_will_change(&self, el: &Element) { let mut map = self.modified_elements.borrow_mut(); - map.entry(JS::from_ref(el)).or_insert(el.get_state()); + let snapshot = map.entry(JS::from_ref(el)).or_insert(ElementSnapshot::new()); + if snapshot.state.is_none() { + snapshot.state = Some(el.get_state()); + } + } + + pub fn element_attr_will_change(&self, el: &Element) { + let mut map = self.modified_elements.borrow_mut(); + let mut snapshot = map.entry(JS::from_ref(el)).or_insert(ElementSnapshot::new()); + if snapshot.attrs.is_none() { + let attrs = el.attrs().iter() + .map(|attr| (attr.identifier().clone(), attr.value().clone())) + .collect(); + snapshot.attrs = Some(attrs); + } } } diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index 829383a4a62..4d0cf6fea54 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -65,6 +65,7 @@ use html5ever::serialize::TraversalScope; use html5ever::serialize::TraversalScope::{ChildrenOnly, IncludeNode}; use html5ever::tree_builder::{LimitedQuirks, NoQuirks, Quirks}; use selectors::matching::{DeclarationBlock, matches}; +use selectors::matching::{common_style_affecting_attributes, rare_style_affecting_attributes}; use selectors::parser::{AttrSelector, NamespaceConstraint, parse_author_origin_selector_list_from_str}; use selectors::states::*; use smallvec::VecLike; @@ -851,6 +852,7 @@ impl Element { name: Atom, namespace: Namespace, prefix: Option) { + self.will_mutate_attr(); let window = window_from_node(self); let in_empty_ns = namespace == ns!(""); let attr = Attr::new(&window, local_name, value, name, namespace, prefix, Some(self)); @@ -963,6 +965,7 @@ impl Element { let idx = self.attrs.borrow().iter().position(|attr| find(&attr)); idx.map(|idx| { + self.will_mutate_attr(); let attr = Root::from_ref(&*(*self.attrs.borrow())[idx]); self.attrs.borrow_mut().remove(idx); attr.set_owner(None); @@ -1075,6 +1078,11 @@ impl Element { assert!(&**local_name == local_name.to_ascii_lowercase()); self.set_attribute(local_name, AttrValue::UInt(DOMString(value.to_string()), value)); } + + pub fn will_mutate_attr(&self) { + let node = self.upcast::(); + node.owner_doc().element_attr_will_change(self); + } } impl ElementMethods for Element { @@ -1459,6 +1467,10 @@ impl ElementMethods for Element { } } +pub fn fragment_affecting_attributes() -> [Atom; 3] { + [atom!("width"), atom!("height"), atom!("src")] +} + impl VirtualMethods for Element { fn super_type(&self) -> Option<&VirtualMethods> { Some(self.upcast::() as &VirtualMethods) @@ -1468,18 +1480,16 @@ impl VirtualMethods for Element { self.super_type().unwrap().attribute_mutated(attr, mutation); let node = self.upcast::(); let doc = node.owner_doc(); - let damage = match attr.local_name() { + match attr.local_name() { &atom!(style) => { // Modifying the `style` attribute might change style. *self.style_attribute.borrow_mut() = mutation.new_value(attr).map(|value| { parse_style_attribute(&value, &doc.base_url()) }); - NodeDamage::NodeStyleDamaged - }, - &atom!(class) => { - // Modifying a class can change style. - NodeDamage::NodeStyleDamaged + if node.is_in_doc() { + doc.content_changed(node, NodeDamage::NodeStyleDamaged); + } }, &atom!(id) => { *self.id_attribute.borrow_mut() = @@ -1510,16 +1520,22 @@ impl VirtualMethods for Element { } } } - NodeDamage::NodeStyleDamaged }, - _ => { - // Modifying any other attribute might change arbitrary things. - NodeDamage::OtherNodeDamage + _ if attr.namespace() == &ns!("") => { + if fragment_affecting_attributes().iter().any(|a| a == attr.local_name()) || + common_style_affecting_attributes().iter().any(|a| &a.atom == attr.local_name()) || + rare_style_affecting_attributes().iter().any(|a| a == attr.local_name()) + { + doc.content_changed(node, NodeDamage::OtherNodeDamage); + } }, + _ => {}, }; - if node.is_in_doc() { - doc.content_changed(node, damage); - } + + // Make sure we rev the version even if we didn't dirty the node. If we + // don't do this, various attribute-dependent htmlcollections (like those + // generated by getElementsByClassName) might become stale. + node.rev_version(); } fn parse_plain_attribute(&self, name: &Atom, value: DOMString) -> AttrValue { diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 49bb441e21e..3a8baa399d2 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -488,13 +488,7 @@ impl Node { self.dirty_impl(damage, true) } - pub fn dirty(&self, damage: NodeDamage) { - self.dirty_impl(damage, false) - } - - pub fn dirty_impl(&self, damage: NodeDamage, force_ancestors: bool) { - - // 0. Set version counter + pub fn rev_version(&self) { // The new version counter is 1 plus the max of the node's current version counter, // its descendants version, and the document's version. Normally, this will just be // the document's version, but we do have to deal with the case where the node has moved @@ -505,6 +499,15 @@ impl Node { ancestor.inclusive_descendants_version.set(version); } doc.inclusive_descendants_version.set(version); + } + + pub fn dirty(&self, damage: NodeDamage) { + self.dirty_impl(damage, false) + } + + pub fn dirty_impl(&self, damage: NodeDamage, force_ancestors: bool) { + // 0. Set version counter + self.rev_version(); // 1. Dirty self. match damage { diff --git a/components/style/restyle_hints.rs b/components/style/restyle_hints.rs index c180c13dcde..cc96f3f89dc 100644 --- a/components/style/restyle_hints.rs +++ b/components/style/restyle_hints.rs @@ -2,9 +2,10 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use attr::{AttrIdentifier, AttrValue}; use selectors::Element; use selectors::matching::matches_compound_selector; -use selectors::parser::{AttrSelector, Combinator, CompoundSelector, SimpleSelector}; +use selectors::parser::{AttrSelector, Combinator, CompoundSelector, NamespaceConstraint, SimpleSelector}; use selectors::states::*; use std::clone::Clone; use std::sync::Arc; @@ -12,12 +13,10 @@ use string_cache::{Atom, Namespace}; /// When the ElementState of an element (like IN_HOVER_STATE) changes, certain /// pseudo-classes (like :hover) may require us to restyle that element, its -/// siblings, and/or its descendants. Doing this conservatively is expensive, -/// and so we RestyleHints to short-circuit work we know is unnecessary. -/// -/// NB: We should extent restyle hints to check for attribute-dependent style -/// in addition to state-dependent style (Gecko does this). - +/// siblings, and/or its descendants. Similarly, when various attributes of an +/// element change, we may also need to restyle things with id, class, and attribute +/// selectors. Doing this conservatively is expensive, and so we use RestyleHints to +/// short-circuit work we know is unnecessary. bitflags! { flags RestyleHint: u8 { @@ -49,34 +48,68 @@ bitflags! { /// take the ElementWrapper approach for attribute-dependent style. So we do it the same both ways for /// now to reduce complexity, but it's worth measuring the performance impact (if any) of the /// mStateMask approach. -struct ElementWrapper where E: Element { + +#[derive(HeapSizeOf, Clone)] +pub struct ElementSnapshot { + pub state: Option, + pub attrs: Option>, +} + +impl ElementSnapshot { + pub fn new() -> ElementSnapshot { + EMPTY_SNAPSHOT.clone() + } + + // Gets an attribute matching |namespace| and |name|, if any. Panics if |attrs| is None. + pub fn get_attr(&self, namespace: &Namespace, name: &Atom) -> Option<&AttrValue> { + self.attrs.as_ref().unwrap().iter() + .find(|&&(ref ident, _)| ident.local_name == *name && ident.namespace == *namespace) + .map(|&(_, ref v)| v) + } + + // Gets an attribute matching |name| if any, ignoring namespace. Panics if |attrs| is None. + pub fn get_attr_ignore_ns(&self, name: &Atom) -> Option<&AttrValue> { + self.attrs.as_ref().unwrap().iter() + .find(|&&(ref ident, _)| ident.local_name == *name) + .map(|&(_, ref v)| v) + } +} + +static EMPTY_SNAPSHOT: ElementSnapshot = ElementSnapshot { state: None, attrs: None }; + +struct ElementWrapper<'a, E> where E: Element { element: E, - state_override: ElementState, + snapshot: &'a ElementSnapshot, } -impl<'a, E> ElementWrapper where E: Element { - pub fn new(el: E) -> ElementWrapper { - ElementWrapper { element: el, state_override: ElementState::empty() } +impl<'a, E> ElementWrapper<'a, E> where E: Element { + pub fn new(el: E) -> ElementWrapper<'a, E> { + ElementWrapper { element: el, snapshot: &EMPTY_SNAPSHOT } } - pub fn new_with_override(el: E, state: ElementState) -> ElementWrapper { - ElementWrapper { element: el, state_override: state } + pub fn new_with_snapshot(el: E, snapshot: &'a ElementSnapshot) -> ElementWrapper<'a, E> { + ElementWrapper { element: el, snapshot: snapshot } } } -macro_rules! overridden_state_accessors { +macro_rules! snapshot_state_accessors { ($( $(#[$Flag_attr: meta])* state $css: expr => $variant: ident / $method: ident / $flag: ident = $value: expr, - )+) => { $( fn $method(&self) -> bool { self.state_override.contains($flag) } )+ + )+) => { $( fn $method(&self) -> bool { + match self.snapshot.state { + Some(s) => s.contains($flag), + None => self.element.$method() + } + } )+ } } -impl Element for ElementWrapper where E: Element { +impl<'a, E> Element for ElementWrapper<'a, E> where E: Element { - // Implement the state accessors on Element to use our overridden state. - state_pseudo_classes!(overridden_state_accessors); + // Implement the state accessors on Element to use the snapshot state if it exists. + state_pseudo_classes!(snapshot_state_accessors); fn parent_element(&self) -> Option { self.element.parent_element().map(|el| ElementWrapper::new(el)) @@ -103,14 +136,31 @@ impl Element for ElementWrapper where E: Element { self.element.get_namespace() } fn get_id(&self) -> Option { - self.element.get_id() + match self.snapshot.attrs { + Some(_) => self.snapshot.get_attr(&ns!(""), &atom!("id")).map(|value| value.as_atom().clone()), + None => self.element.get_id(), + } } fn has_class(&self, name: &Atom) -> bool { - self.element.has_class(name) + match self.snapshot.attrs { + Some(_) => self.snapshot.get_attr(&ns!(""), &atom!("class")) + .map_or(false, |v| { v.as_tokens().iter().any(|atom| atom == name) }), + None => self.element.has_class(name), + } } fn match_attr(&self, attr: &AttrSelector, test: F) -> bool where F: Fn(&str) -> bool { - self.element.match_attr(attr, test) + match self.snapshot.attrs { + Some(_) => { + let html = self.is_html_element_in_html_document(); + let local_name = if html { &attr.lower_name } else { &attr.name }; + match attr.namespace { + NamespaceConstraint::Specific(ref ns) => self.snapshot.get_attr(ns, local_name), + NamespaceConstraint::Any => self.snapshot.get_attr_ignore_ns(local_name), + }.map_or(false, |v| test(v)) + }, + None => self.element.match_attr(attr, test) + } } fn is_empty(&self) -> bool { self.element.is_empty() @@ -127,8 +177,15 @@ impl Element for ElementWrapper where E: Element { fn is_unvisited_link(&self) -> bool { self.element.is_unvisited_link() } - fn each_class(&self, callback: F) where F: FnMut(&Atom) { - self.element.each_class(callback) + fn each_class(&self, mut callback: F) where F: FnMut(&Atom) { + match self.snapshot.attrs { + Some(_) => { + if let Some(v) = self.snapshot.get_attr(&ns!(""), &atom!("class")) { + for c in v.as_tokens() { callback(c) } + } + } + None => self.element.each_class(callback), + } } } @@ -147,6 +204,23 @@ macro_rules! gen_selector_to_state { } } +state_pseudo_classes!(gen_selector_to_state); + +fn is_attr_selector(sel: &SimpleSelector) -> bool { + match *sel { + SimpleSelector::ID(_) | + SimpleSelector::Class(_) | + SimpleSelector::AttrExists(_) | + SimpleSelector::AttrEqual(_, _, _) | + SimpleSelector::AttrIncludes(_, _) | + SimpleSelector::AttrDashMatch(_, _, _) | + SimpleSelector::AttrPrefixMatch(_, _) | + SimpleSelector::AttrSubstringMatch(_, _) | + SimpleSelector::AttrSuffixMatch(_, _) => true, + _ => false, + } +} + fn combinator_to_restyle_hint(combinator: Option) -> RestyleHint { match combinator { None => RESTYLE_SELF, @@ -159,49 +233,68 @@ fn combinator_to_restyle_hint(combinator: Option) -> RestyleHint { } } -state_pseudo_classes!(gen_selector_to_state); +#[derive(Debug)] +struct Sensitivities { + pub states: ElementState, + pub attrs: bool, +} + +impl Sensitivities { + fn is_empty(&self) -> bool { + self.states.is_empty() && !self.attrs + } + + fn new() -> Sensitivities { + Sensitivities { + states: ElementState::empty(), + attrs: false, + } + } +} // Mapping between (partial) CompoundSelectors (and the combinator to their right) -// and the states they depend on. +// and the states and attributes they depend on. // // In general, for all selectors in all applicable stylesheets of the form: // -// |s _ s:X _ s _ s:Y _ s| +// |a _ b _ c _ d _ e| // // Where: -// * Each |s| is an arbitrary simple selector. -// * Each |s| is an arbitrary combinator (or nothing). -// * X and Y are state-dependent pseudo-classes like :hover. +// * |b| and |d| are simple selectors that depend on state (like :hover) or +// attributes (like [attr...], .foo, or #foo). +// * |a|, |c|, and |e| are arbitrary simple selectors that do not depend on +// state or attributes. // -// We generate a StateDependency for both |s _ s:X _| and |s _ s:X _ s _ s:Y _|, even +// We generate a Dependency for both |a _ b:X _| and |a _ b:X _ c _ d:Y _|, even // though those selectors may not appear on their own in any stylesheet. This allows -// us to quickly scan through the operation points of pseudo-classes and determine the -// maximum effect their associated state changes may have on the style of elements in -// the document. +// us to quickly scan through the dependency sites of all style rules and determine the +// maximum effect that a given state or attribute change may have on the style of +// elements in the document. #[derive(Debug)] -struct StateDependency { +struct Dependency { selector: Arc, combinator: Option, - state: ElementState, + sensitivities: Sensitivities, } #[derive(Debug)] -pub struct StateDependencySet { - deps: Vec, +pub struct DependencySet { + deps: Vec, } -impl StateDependencySet { - pub fn new() -> StateDependencySet { - StateDependencySet { deps: Vec::new() } +impl DependencySet { + pub fn new() -> DependencySet { + DependencySet { deps: Vec::new() } } - pub fn compute_hint(&self, el: &E, current_state: ElementState, old_state: ElementState) + pub fn compute_hint(&self, el: &E, snapshot: &ElementSnapshot, current_state: ElementState) -> RestyleHint where E: Element, E: Clone { + let state_changes = snapshot.state.map_or(ElementState::empty(), |old_state| current_state ^ old_state); + let attrs_changed = snapshot.attrs.is_some(); let mut hint = RestyleHint::empty(); - let state_changes = current_state ^ old_state; for dep in &self.deps { - if state_changes.intersects(dep.state) { - let old_el: ElementWrapper = ElementWrapper::new_with_override(el.clone(), old_state); + if state_changes.intersects(dep.sensitivities.states) || (attrs_changed && dep.sensitivities.attrs) { + let old_el: ElementWrapper = ElementWrapper::new_with_snapshot(el.clone(), snapshot); let matched_then = matches_compound_selector(&*dep.selector, &old_el, None, &mut false); let matches_now = matches_compound_selector(&*dep.selector, el, None, &mut false); if matched_then != matches_now { @@ -219,15 +312,18 @@ impl StateDependencySet { let mut cur = selector; let mut combinator: Option = None; loop { - let mut deps = ElementState::empty(); + let mut sensitivities = Sensitivities::new(); for s in &cur.simple_selectors { - deps.insert(selector_to_state(s)); + sensitivities.states.insert(selector_to_state(s)); + if !sensitivities.attrs { + sensitivities.attrs = is_attr_selector(s); + } } - if !deps.is_empty() { - self.deps.push(StateDependency { + if !sensitivities.is_empty() { + self.deps.push(Dependency { selector: cur.clone(), combinator: combinator, - state: deps, + sensitivities: sensitivities, }); } diff --git a/components/style/selector_matching.rs b/components/style/selector_matching.rs index 85dadac97d1..5a4b1734917 100644 --- a/components/style/selector_matching.rs +++ b/components/style/selector_matching.rs @@ -6,7 +6,7 @@ use legacy::PresentationalHintSynthesis; use media_queries::{Device, MediaType}; use node::TElementAttributes; use properties::{PropertyDeclaration, PropertyDeclarationBlock}; -use restyle_hints::{RestyleHint, StateDependencySet}; +use restyle_hints::{ElementSnapshot, RestyleHint, DependencySet}; use selectors::Element; use selectors::bloom::BloomFilter; use selectors::matching::DeclarationBlock as GenericDeclarationBlock; @@ -95,8 +95,8 @@ pub struct Stylist { after_map: PerPseudoElementSelectorMap, rules_source_order: usize, - // Selector state dependencies used to compute restyle hints. - state_deps: StateDependencySet, + // Selector dependencies used to compute restyle hints. + state_deps: DependencySet, } impl Stylist { @@ -112,7 +112,7 @@ impl Stylist { before_map: PerPseudoElementSelectorMap::new(), after_map: PerPseudoElementSelectorMap::new(), rules_source_order: 0, - state_deps: StateDependencySet::new(), + state_deps: DependencySet::new(), }; // FIXME: Add iso-8859-9.css when the document’s encoding is ISO-8859-8. stylist @@ -204,12 +204,16 @@ impl Stylist { self.rules_source_order = rules_source_order; } - pub fn restyle_hint_for_state_change(&self, element: &E, - current_state: ElementState, - old_state: ElementState) - -> RestyleHint - where E: Element + Clone { - self.state_deps.compute_hint(element, current_state, old_state) + pub fn compute_restyle_hint(&self, element: &E, + snapshot: &ElementSnapshot, + // NB: We need to pass current_state as an argument because + // selectors::Element doesn't provide access to ElementState + // directly, and computing it from the ElementState would be + // more expensive than getting it directly from the caller. + current_state: ElementState) + -> RestyleHint + where E: Element + Clone { + self.state_deps.compute_hint(element, snapshot, current_state) } pub fn set_device(&mut self, mut device: Device, stylesheets: &[&Stylesheet]) { From b29369296c4ffb3e40326121923d6ac5f6396a45 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Tue, 10 Nov 2015 11:23:10 -0800 Subject: [PATCH 5/7] Add ::from_layout_js factory to LayoutNode, LayoutElement, and LayoutDocument. --- components/layout/wrapper.rs | 51 +++++++++++++++++------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/components/layout/wrapper.rs b/components/layout/wrapper.rs index 75244159727..57b0008c842 100644 --- a/components/layout/wrapper.rs +++ b/components/layout/wrapper.rs @@ -91,14 +91,17 @@ impl<'a> PartialEq for LayoutNode<'a> { } impl<'ln> LayoutNode<'ln> { - pub unsafe fn new(address: &TrustedNodeAddress) -> LayoutNode { - let node = LayoutJS::from_trusted_node_address(*address); + pub fn from_layout_js(n: LayoutJS) -> LayoutNode<'ln> { LayoutNode { - node: node, + node: n, chain: PhantomData, } } + pub unsafe fn new(address: &TrustedNodeAddress) -> LayoutNode { + LayoutNode::from_layout_js(LayoutJS::from_trusted_node_address(*address)) + } + /// Creates a new layout node with the same lifetime as this layout node. pub unsafe fn new_with_this_lifetime(&self, node: &LayoutJS) -> LayoutNode<'ln> { LayoutNode { @@ -215,12 +218,7 @@ impl<'ln> LayoutNode<'ln> { } pub fn as_document(&self) -> Option> { - self.node.downcast().map(|document| { - LayoutDocument { - document: document, - chain: PhantomData, - } - }) + self.node.downcast().map(|document| LayoutDocument::from_layout_js(document)) } fn parent_node(&self) -> Option> { @@ -363,24 +361,24 @@ pub struct LayoutDocument<'le> { } impl<'le> LayoutDocument<'le> { - pub fn as_node(&self) -> LayoutNode<'le> { - LayoutNode { - node: self.document.upcast(), + pub fn from_layout_js(doc: LayoutJS) -> LayoutDocument<'le> { + LayoutDocument { + document: doc, chain: PhantomData, } } + pub fn as_node(&self) -> LayoutNode<'le> { + LayoutNode::from_layout_js(self.document.upcast()) + } + pub fn root_node(&self) -> Option> { self.as_node().children().find(LayoutNode::is_element) } pub fn drain_modified_elements(&self) -> Vec<(LayoutElement, ElementSnapshot)> { let elements = unsafe { self.document.drain_modified_elements() }; - elements.into_iter().map(|(el, snapshot)| - (LayoutElement { - element: el, - chain: PhantomData, - }, snapshot)).collect() + elements.into_iter().map(|(el, snapshot)| (LayoutElement::from_layout_js(el), snapshot)).collect() } } @@ -392,6 +390,13 @@ pub struct LayoutElement<'le> { } impl<'le> LayoutElement<'le> { + pub fn from_layout_js(el: LayoutJS) -> LayoutElement<'le> { + LayoutElement { + element: el, + chain: PhantomData, + } + } + pub fn style_attribute(&self) -> &'le Option { unsafe { &*self.element.style_attribute() @@ -399,10 +404,7 @@ impl<'le> LayoutElement<'le> { } pub fn as_node(&self) -> LayoutNode<'le> { - LayoutNode { - node: self.element.upcast(), - chain: PhantomData, - } + LayoutNode::from_layout_js(self.element.upcast()) } pub fn get_state(&self) -> ElementState { @@ -466,12 +468,7 @@ impl<'le> LayoutElement<'le> { } fn as_element<'le>(node: LayoutJS) -> Option> { - node.downcast().map(|element| { - LayoutElement { - element: element, - chain: PhantomData, - } - }) + node.downcast().map(|element| LayoutElement::from_layout_js(element)) } macro_rules! state_getter { From a79f6e43a05be3401ace0ec816b8b2f6499246f4 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Mon, 9 Nov 2015 16:39:48 -0800 Subject: [PATCH 6/7] Tests. --- tests/wpt/mozilla/meta/MANIFEST.json | 24 ++++++++++ .../mozilla/tests/css/restyle_hints_attr.css | 44 +++++++++++++++++++ .../mozilla/tests/css/restyle_hints_attr.html | 35 +++++++++++++++ .../tests/css/restyle_hints_attr_ref.html | 16 +++++++ .../tests/css/restyle_hints_state.html | 3 +- 5 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 tests/wpt/mozilla/tests/css/restyle_hints_attr.css create mode 100644 tests/wpt/mozilla/tests/css/restyle_hints_attr.html create mode 100644 tests/wpt/mozilla/tests/css/restyle_hints_attr_ref.html diff --git a/tests/wpt/mozilla/meta/MANIFEST.json b/tests/wpt/mozilla/meta/MANIFEST.json index 340cbde980b..4dcc6df054e 100644 --- a/tests/wpt/mozilla/meta/MANIFEST.json +++ b/tests/wpt/mozilla/meta/MANIFEST.json @@ -3119,6 +3119,18 @@ "url": "/_mozilla/css/quotes_simple_a.html" } ], + "css/restyle_hints_attr.html": [ + { + "path": "css/restyle_hints_attr.html", + "references": [ + [ + "/_mozilla/css/restyle_hints_attr_ref.html", + "==" + ] + ], + "url": "/_mozilla/css/restyle_hints_attr.html" + } + ], "css/restyle_hints_state.html": [ { "path": "css/restyle_hints_state.html", @@ -8052,6 +8064,18 @@ "url": "/_mozilla/css/quotes_simple_a.html" } ], + "css/restyle_hints_attr.html": [ + { + "path": "css/restyle_hints_attr.html", + "references": [ + [ + "/_mozilla/css/restyle_hints_attr_ref.html", + "==" + ] + ], + "url": "/_mozilla/css/restyle_hints_attr.html" + } + ], "css/restyle_hints_state.html": [ { "path": "css/restyle_hints_state.html", diff --git a/tests/wpt/mozilla/tests/css/restyle_hints_attr.css b/tests/wpt/mozilla/tests/css/restyle_hints_attr.css new file mode 100644 index 00000000000..64d332c31f7 --- /dev/null +++ b/tests/wpt/mozilla/tests/css/restyle_hints_attr.css @@ -0,0 +1,44 @@ +div { + background-color: black; + padding: 10px; + width: 50px; + height: 50px; + color: gray; +} + +div[foo=kokosnuss] { + background: black; +} + +div[foo][bar] { + background: red; +} + +div[foo=kokosnuss][bar~=fett] { + background: green; +} + +div[bar] > div[baz] div { + color: cyan; +} + +div[foo][bar~=butter] div { + background: purple; + color: white; +} + +div[foo][bar~=butter] > div { + background: yellow; +} + +div[bar~=butter][foo=kokosnuss] + div { + background: pink; +} + +div[foo=kokosnuss] ~ div { + background: green; +} + +div + div[foo=keine][bar=ahnung] { + background: lavender; +} diff --git a/tests/wpt/mozilla/tests/css/restyle_hints_attr.html b/tests/wpt/mozilla/tests/css/restyle_hints_attr.html new file mode 100644 index 00000000000..d9c55498f8f --- /dev/null +++ b/tests/wpt/mozilla/tests/css/restyle_hints_attr.html @@ -0,0 +1,35 @@ + + + + + + +
+
+
some text
+
+
+
+
+
+ + + diff --git a/tests/wpt/mozilla/tests/css/restyle_hints_attr_ref.html b/tests/wpt/mozilla/tests/css/restyle_hints_attr_ref.html new file mode 100644 index 00000000000..2d87a0f4526 --- /dev/null +++ b/tests/wpt/mozilla/tests/css/restyle_hints_attr_ref.html @@ -0,0 +1,16 @@ + + + + + +
+
+
some text
+
+
+
+
+
+ + + diff --git a/tests/wpt/mozilla/tests/css/restyle_hints_state.html b/tests/wpt/mozilla/tests/css/restyle_hints_state.html index 52f82bc905d..480d1675bb5 100644 --- a/tests/wpt/mozilla/tests/css/restyle_hints_state.html +++ b/tests/wpt/mozilla/tests/css/restyle_hints_state.html @@ -30,8 +30,9 @@ * implement attribute-based restyle hints, we can stop dirtying the subtree * on attribute modifications, and these tests will start to be more useful. */ + window.dummy = 0; var $ = document.getElementById.bind(document); - function syncRestyle() { window.dummy != $("fs2").offsetTop; } + function syncRestyle() { window.dummy += $("fs2").offsetTop; } syncRestyle(); $('fs1').disabled = true; syncRestyle(); From 5d859568cf2f10b08ce9edc9454b2693b94b1078 Mon Sep 17 00:00:00 2001 From: Bobby Holley Date: Sun, 8 Nov 2015 18:34:04 -0800 Subject: [PATCH 7/7] Update test expectations. --- .../metadata-css/css21_dev/html4/background-root-101.htm.ini | 3 --- .../css21_dev/html4/dynamic-sibling-combinator-001.htm.ini | 4 ---- 2 files changed, 7 deletions(-) delete mode 100644 tests/wpt/metadata-css/css21_dev/html4/background-root-101.htm.ini delete mode 100644 tests/wpt/metadata-css/css21_dev/html4/dynamic-sibling-combinator-001.htm.ini diff --git a/tests/wpt/metadata-css/css21_dev/html4/background-root-101.htm.ini b/tests/wpt/metadata-css/css21_dev/html4/background-root-101.htm.ini deleted file mode 100644 index 004cd45bf51..00000000000 --- a/tests/wpt/metadata-css/css21_dev/html4/background-root-101.htm.ini +++ /dev/null @@ -1,3 +0,0 @@ -[background-root-101.htm] - type: reftest - expected: FAIL diff --git a/tests/wpt/metadata-css/css21_dev/html4/dynamic-sibling-combinator-001.htm.ini b/tests/wpt/metadata-css/css21_dev/html4/dynamic-sibling-combinator-001.htm.ini deleted file mode 100644 index 3f785687b72..00000000000 --- a/tests/wpt/metadata-css/css21_dev/html4/dynamic-sibling-combinator-001.htm.ini +++ /dev/null @@ -1,4 +0,0 @@ -[dynamic-sibling-combinator-001.htm] - type: reftest - bug: https://github.com/servo/servo/issues/7890 - expected: FAIL