servo: Merge #6700 - Update rust-selectors (from servo:selectors); r=Ms2ger

Update for https://github.com/servo/rust-selectors/pull/37

Source-Repo: https://github.com/servo/servo
Source-Revision: 5d857c5d0cac67337ea01895b7cf309359c89cce
This commit is contained in:
Simon Sapin 2015-07-23 10:55:37 -06:00
parent 335ba407a9
commit 7b6c4c937e
9 changed files with 146 additions and 141 deletions

View File

@ -17,7 +17,7 @@ use wrapper::{LayoutElement, LayoutNode};
use script::dom::characterdata::CharacterDataTypeId;
use script::dom::node::NodeTypeId;
use script::layout_interface::Animation;
use selectors::{Node, Element};
use selectors::{Element};
use selectors::bloom::BloomFilter;
use selectors::matching::{CommonStyleAffectingAttributeMode, CommonStyleAffectingAttributes};
use selectors::matching::{common_style_affecting_attributes, rare_style_affecting_attributes};
@ -222,18 +222,14 @@ impl StyleSharingCandidate {
/// Attempts to create a style sharing candidate from this node. Returns
/// the style sharing candidate or `None` if this node is ineligible for
/// style sharing.
fn new(node: &LayoutNode) -> Option<StyleSharingCandidate> {
let parent_node = match node.parent_node() {
fn new(element: &LayoutElement) -> Option<StyleSharingCandidate> {
let parent_element = match element.parent_element() {
None => return None,
Some(parent_node) => parent_node,
};
let element = match parent_node.as_element() {
Some(element) => element,
None => return None
Some(parent_element) => parent_element,
};
let style = unsafe {
match *node.borrow_layout_data_unchecked() {
match *element.as_node().borrow_layout_data_unchecked() {
None => return None,
Some(ref layout_data_ref) => {
match layout_data_ref.shared_data.style {
@ -244,7 +240,7 @@ impl StyleSharingCandidate {
}
};
let parent_style = unsafe {
match *parent_node.borrow_layout_data_unchecked() {
match *parent_element.as_node().borrow_layout_data_unchecked() {
None => return None,
Some(ref parent_layout_data_ref) => {
match parent_layout_data_ref.shared_data.style {
@ -357,8 +353,8 @@ impl StyleSharingCandidateCache {
self.cache.iter()
}
pub fn insert_if_possible(&mut self, node: &LayoutNode) {
match StyleSharingCandidate::new(node) {
pub fn insert_if_possible(&mut self, element: &LayoutElement) {
match StyleSharingCandidate::new(element) {
None => {}
Some(candidate) => self.cache.insert(candidate, ())
}
@ -395,7 +391,7 @@ pub trait MatchMethods {
fn match_node(&self,
stylist: &Stylist,
parent_bf: &Option<Box<BloomFilter>>,
parent_bf: Option<&BloomFilter>,
applicable_declarations: &mut ApplicableDeclarations,
shareable: &mut bool);
@ -540,7 +536,7 @@ impl<'ln> PrivateMatchMethods for LayoutNode<'ln> {
impl<'ln> MatchMethods for LayoutNode<'ln> {
fn match_node(&self,
stylist: &Stylist,
parent_bf: &Option<Box<BloomFilter>>,
parent_bf: Option<&BloomFilter>,
applicable_declarations: &mut ApplicableDeclarations,
shareable: &mut bool) {
let element = self.as_element().unwrap();

View File

@ -16,7 +16,6 @@ use wrapper::{layout_node_to_unsafe_layout_node, LayoutNode};
use wrapper::{ThreadSafeLayoutNode, UnsafeLayoutNode};
use selectors::bloom::BloomFilter;
use selectors::Node;
use util::opts;
use util::tid::tid;
@ -162,10 +161,7 @@ impl<'a> PreorderDomTraversal for RecalcStyleForNode<'a> {
let parent_opt = node.layout_parent_node(self.layout_context.shared);
// Get the style bloom filter.
let bf = take_task_local_bloom_filter(parent_opt, self.layout_context);
// Just needs to be wrapped in an option for `match_node`.
let some_bf = Some(bf);
let mut bf = take_task_local_bloom_filter(parent_opt, self.layout_context);
let nonincremental_layout = opts::get().nonincremental_layout;
if nonincremental_layout || node.is_dirty() {
@ -192,7 +188,7 @@ impl<'a> PreorderDomTraversal for RecalcStyleForNode<'a> {
// Perform the CSS selector matching.
let stylist = unsafe { &*self.layout_context.shared.stylist };
node.match_node(stylist,
&some_bf,
Some(&*bf),
&mut applicable_declarations,
&mut shareable);
} else if node.has_changed() {
@ -211,7 +207,9 @@ impl<'a> PreorderDomTraversal for RecalcStyleForNode<'a> {
// Add ourselves to the LRU cache.
if shareable {
style_sharing_candidate_cache.insert_if_possible(&node);
if let Some(element) = node.as_element() {
style_sharing_candidate_cache.insert_if_possible(&element);
}
}
}
StyleSharingResult::StyleWasShared(index, damage) => {
@ -221,8 +219,6 @@ impl<'a> PreorderDomTraversal for RecalcStyleForNode<'a> {
}
}
let mut bf = some_bf.unwrap();
let unsafe_layout_node = layout_node_to_unsafe_layout_node(&node);
// Before running the children, we need to insert our nodes into the bloom

View File

@ -67,7 +67,6 @@ use std::sync::mpsc::Sender;
use string_cache::{Atom, Namespace};
use style::computed_values::content::ContentItem;
use style::computed_values::{content, display, white_space};
use selectors::Node as SelectorsNode;
use selectors::matching::DeclarationBlock;
use selectors::parser::{NamespaceConstraint, AttrSelector};
use style::legacy::UnsignedIntegerAttribute;
@ -144,12 +143,6 @@ impl<'ln> LayoutNode<'ln> {
LayoutTreeIterator::new(self)
}
fn last_child(self) -> Option<LayoutNode<'ln>> {
unsafe {
self.get_jsmanaged().last_child_ref().map(|node| self.new_with_this_lifetime(&node))
}
}
/// Returns an iterator over this node's children.
pub fn children(self) -> LayoutNodeChildrenIterator<'ln> {
LayoutNodeChildrenIterator {
@ -209,10 +202,10 @@ impl<'ln> LayoutNode<'ln> {
pub fn debug_id(self) -> usize {
self.opaque().to_untrusted_node_address().0 as usize
}
}
impl<'ln> ::selectors::Node for LayoutNode<'ln> {
type Element = LayoutElement<'ln>;
pub fn as_element(&self) -> Option<LayoutElement<'ln>> {
as_element(self.node)
}
fn parent_node(&self) -> Option<LayoutNode<'ln>> {
unsafe {
@ -243,34 +236,6 @@ impl<'ln> ::selectors::Node for LayoutNode<'ln> {
self.node.next_sibling_ref().map(|node| self.new_with_this_lifetime(&node))
}
}
/// If this is an element, accesses the element data.
#[inline]
fn as_element(&self) -> Option<Self::Element> {
ElementCast::to_layout_js(&self.node).map(|element| {
LayoutElement {
element: element,
chain: self.chain,
}
})
}
fn is_document(&self) -> bool {
match self.type_id() {
NodeTypeId::Document(..) => true,
_ => false
}
}
fn is_element_or_non_empty_text(&self) -> bool {
if let Some(text) = TextCast::to_layout_js(&self.node) {
unsafe {
!CharacterDataCast::from_layout_js(&text).data_for_layout().is_empty()
}
} else {
ElementCast::to_layout_js(&self.node).is_some()
}
}
}
impl<'ln> LayoutNode<'ln> {
@ -387,18 +352,78 @@ impl<'le> LayoutElement<'le> {
&*self.element.style_attribute()
}
}
}
impl<'le> ::selectors::Element for LayoutElement<'le> {
type Node = LayoutNode<'le>;
#[inline]
fn as_node(&self) -> LayoutNode<'le> {
pub fn as_node(&self) -> LayoutNode<'le> {
LayoutNode {
node: NodeCast::from_layout_js(&self.element),
chain: PhantomData,
}
}
}
fn as_element<'le>(node: LayoutJS<Node>) -> Option<LayoutElement<'le>> {
ElementCast::to_layout_js(&node).map(|element| {
LayoutElement {
element: element,
chain: PhantomData,
}
})
}
impl<'le> ::selectors::Element for LayoutElement<'le> {
fn parent_element(&self) -> Option<LayoutElement<'le>> {
unsafe {
NodeCast::from_layout_js(&self.element).parent_node_ref().and_then(as_element)
}
}
fn first_child_element(&self) -> Option<LayoutElement<'le>> {
self.as_node().children().filter_map(|n| n.as_element()).next()
}
fn last_child_element(&self) -> Option<LayoutElement<'le>> {
self.as_node().rev_children().filter_map(|n| n.as_element()).next()
}
fn prev_sibling_element(&self) -> Option<LayoutElement<'le>> {
let mut node = self.as_node();
while let Some(sibling) = node.prev_sibling() {
if let Some(element) = sibling.as_element() {
return Some(element)
}
node = sibling;
}
None
}
fn next_sibling_element(&self) -> Option<LayoutElement<'le>> {
let mut node = self.as_node();
while let Some(sibling) = node.next_sibling() {
if let Some(element) = sibling.as_element() {
return Some(element)
}
node = sibling;
}
None
}
fn is_root(&self) -> bool {
match self.as_node().parent_node() {
None => false,
Some(node) => node.type_id() == NodeTypeId::Document,
}
}
fn is_empty(&self) -> bool {
self.as_node().children().all(|node| match node.type_id() {
NodeTypeId::Element(..) => false,
NodeTypeId::CharacterData(CharacterDataTypeId::Text) => unsafe {
CharacterDataCast::to_layout_js(&node.node).unwrap().data_for_layout().is_empty()
},
_ => true
})
}
#[inline]
fn get_local_name<'a>(&'a self) -> &'a Atom {

View File

@ -16,7 +16,9 @@ use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::Bindings::NamedNodeMapBinding::NamedNodeMapMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::CharacterDataCast;
use dom::bindings::codegen::InheritTypes::{ElementCast, ElementDerived, EventTargetCast};
use dom::bindings::codegen::InheritTypes::DocumentDerived;
use dom::bindings::codegen::InheritTypes::{HTMLBodyElementDerived, HTMLFontElementDerived};
use dom::bindings::codegen::InheritTypes::{HTMLIFrameElementDerived, HTMLInputElementCast};
use dom::bindings::codegen::InheritTypes::{HTMLInputElementDerived, HTMLTableElementCast};
@ -25,6 +27,7 @@ use dom::bindings::codegen::InheritTypes::{HTMLTableRowElementDerived, HTMLTextA
use dom::bindings::codegen::InheritTypes::{HTMLTableSectionElementDerived, NodeCast};
use dom::bindings::codegen::InheritTypes::HTMLAnchorElementCast;
use dom::bindings::codegen::InheritTypes::HTMLTableDataCellElementDerived;
use dom::bindings::codegen::InheritTypes::TextCast;
use dom::bindings::codegen::UnionTypes::NodeOrString;
use dom::bindings::error::{ErrorResult, Fallible};
use dom::bindings::error::Error::{InvalidCharacter, Syntax};
@ -34,6 +37,7 @@ use dom::bindings::js::{Root, RootedReference};
use dom::bindings::trace::RootedVec;
use dom::bindings::utils::{namespace_from_domstring, xml_name_type, validate_and_extract};
use dom::bindings::utils::XMLName::InvalidXMLName;
use dom::characterdata::CharacterDataHelpers;
use dom::create::create_element;
use dom::domrect::DOMRect;
use dom::domrectlist::DOMRectList;
@ -1449,7 +1453,7 @@ impl<'a> ElementMethods for &'a Element {
match parse_author_origin_selector_list_from_str(&selectors) {
Err(()) => Err(Syntax),
Ok(ref selectors) => {
Ok(matches(selectors, &self, &mut None))
Ok(matches(selectors, &Root::from_ref(self), None))
}
}
}
@ -1461,9 +1465,9 @@ impl<'a> ElementMethods for &'a Element {
Ok(ref selectors) => {
let root = NodeCast::from_ref(self);
for element in root.inclusive_ancestors() {
if let Some(element) = ElementCast::to_ref(element.r()) {
if matches(selectors, &element, &mut None) {
return Ok(Some(Root::from_ref(element)));
if let Some(element) = ElementCast::to_root(element) {
if matches(selectors, &element, None) {
return Ok(Some(element));
}
}
}
@ -1615,16 +1619,44 @@ impl<'a> VirtualMethods for &'a Element {
}
}
impl<'a> ::selectors::Element for &'a Element {
type Node = &'a Node;
impl<'a> ::selectors::Element for Root<Element> {
fn parent_element(&self) -> Option<Root<Element>> {
NodeCast::from_ref(&**self).GetParentElement()
}
fn as_node(&self) -> &'a Node {
NodeCast::from_ref(*self)
fn first_child_element(&self) -> Option<Root<Element>> {
self.node.child_elements().next()
}
fn last_child_element(&self) -> Option<Root<Element>> {
self.node.rev_children().filter_map(ElementCast::to_root).next()
}
fn prev_sibling_element(&self) -> Option<Root<Element>> {
self.node.preceding_siblings().filter_map(ElementCast::to_root).next()
}
fn next_sibling_element(&self) -> Option<Root<Element>> {
self.node.following_siblings().filter_map(ElementCast::to_root).next()
}
fn is_root(&self) -> bool {
match self.node.GetParentNode() {
None => false,
Some(node) => node.is_document(),
}
}
fn is_empty(&self) -> bool {
self.node.children().all(|node| !node.is_element() && match TextCast::to_ref(&*node) {
None => true,
Some(text) => CharacterDataCast::from_ref(text).data().is_empty()
})
}
fn is_link(&self) -> bool {
// FIXME: This is HTML only.
let node = NodeCast::from_ref(*self);
let node = NodeCast::from_ref(&**self);
match node.type_id() {
// https://html.spec.whatwg.org/multipage/#selector-link
NodeTypeId::Element(ElementTypeId::HTMLElement(HTMLElementTypeId::HTMLAnchorElement)) |
@ -1647,20 +1679,20 @@ impl<'a> ::selectors::Element for &'a Element {
}
fn get_local_name<'b>(&'b self) -> &'b Atom {
ElementHelpers::local_name(*self)
ElementHelpers::local_name(&**self)
}
fn get_namespace<'b>(&'b self) -> &'b Namespace {
self.namespace()
}
fn get_hover_state(&self) -> bool {
let node = NodeCast::from_ref(*self);
let node = NodeCast::from_ref(&**self);
node.get_hover_state()
}
fn get_focus_state(&self) -> bool {
// TODO: Also check whether the top-level browsing context has the system focus,
// and whether this element is a browsing context container.
// https://html.spec.whatwg.org/multipage/#selector-focus
let node = NodeCast::from_ref(*self);
let node = NodeCast::from_ref(&**self);
node.get_focus_state()
}
fn get_id(&self) -> Option<Atom> {
@ -1672,29 +1704,29 @@ impl<'a> ::selectors::Element for &'a Element {
})
}
fn get_disabled_state(&self) -> bool {
let node = NodeCast::from_ref(*self);
let node = NodeCast::from_ref(&**self);
node.get_disabled_state()
}
fn get_enabled_state(&self) -> bool {
let node = NodeCast::from_ref(*self);
let node = NodeCast::from_ref(&**self);
node.get_enabled_state()
}
fn get_checked_state(&self) -> bool {
let input_element: Option<&HTMLInputElement> = HTMLInputElementCast::to_ref(*self);
let input_element: Option<&HTMLInputElement> = HTMLInputElementCast::to_ref(&**self);
match input_element {
Some(input) => input.Checked(),
None => false,
}
}
fn get_indeterminate_state(&self) -> bool {
let input_element: Option<&HTMLInputElement> = HTMLInputElementCast::to_ref(*self);
let input_element: Option<&HTMLInputElement> = HTMLInputElementCast::to_ref(&**self);
match input_element {
Some(input) => input.get_indeterminate_state(),
None => false,
}
}
fn has_class(&self, name: &Atom) -> bool {
AttributeHandlers::has_class(*self, name)
AttributeHandlers::has_class(&**self, name)
}
fn each_class<F>(&self, mut callback: F)
where F: FnMut(&Atom)
@ -1708,7 +1740,7 @@ impl<'a> ::selectors::Element for &'a Element {
}
}
fn has_servo_nonzero_border(&self) -> bool {
let table_element: Option<&HTMLTableElement> = HTMLTableElementCast::to_ref(*self);
let table_element: Option<&HTMLTableElement> = HTMLTableElementCast::to_ref(&**self);
match table_element {
None => false,
Some(this) => {

View File

@ -413,13 +413,14 @@ impl<'a> Iterator for QuerySelectorIterator {
let selectors = &self.selectors;
// TODO(cgaebel): Is it worth it to build a bloom filter here
// (instead of passing `None`)? Probably.
self.iterator.find(|node| {
if let Some(element) = ElementCast::to_ref(node.r()) {
matches(selectors, &element, &mut None)
} else {
false
self.iterator.by_ref().filter_map(|node| {
if let Some(element) = ElementCast::to_root(node) {
if matches(selectors, &element, None) {
return Some(NodeCast::from_root(element))
}
}
})
None
}).next()
}
}
@ -891,7 +892,7 @@ impl<'a> NodeHelpers for &'a Node {
let root = self.ancestors().last();
let root = root.r().unwrap_or(self.clone());
Ok(root.traverse_preorder().filter_map(ElementCast::to_root).find(|element| {
matches(selectors, &element.r(), &mut None)
matches(selectors, element, None)
}))
}
}
@ -2579,51 +2580,6 @@ impl<'a> VirtualMethods for &'a Node {
}
}
impl<'a> ::selectors::Node for &'a Node {
type Element = &'a Element;
fn parent_node(&self) -> Option<&'a Node> {
(*self).parent_node.get()
.map(|node| node.root().get_unsound_ref_forever())
}
fn first_child(&self) -> Option<&'a Node> {
(*self).first_child.get()
.map(|node| node.root().get_unsound_ref_forever())
}
fn last_child(&self) -> Option<&'a Node> {
(*self).last_child.get()
.map(|node| node.root().get_unsound_ref_forever())
}
fn prev_sibling(&self) -> Option<&'a Node> {
(*self).prev_sibling.get()
.map(|node| node.root().get_unsound_ref_forever())
}
fn next_sibling(&self) -> Option<&'a Node> {
(*self).next_sibling.get()
.map(|node| node.root().get_unsound_ref_forever())
}
fn is_document(&self) -> bool {
DocumentDerived::is_document(*self)
}
fn as_element(&self) -> Option<Self::Element> {
ElementCast::to_ref(*self)
}
fn is_element_or_non_empty_text(&self) -> bool {
if self.is_text() {
self.GetTextContent().map_or(false, |s| !s.is_empty())
} else {
self.is_element()
}
}
}
pub trait DisabledStateHelpers {
fn check_ancestors_disabled_state_for_form_control(self);
fn check_parent_disabled_state_for_option(self);

View File

@ -1193,7 +1193,7 @@ dependencies = [
[[package]]
name = "selectors"
version = "0.1.0"
source = "git+https://github.com/servo/rust-selectors#a6cf1fba8f31960254aa62434ab8aeee13aff080"
source = "git+https://github.com/servo/rust-selectors#625734e1e8d70c012672b248adca302d0276fe08"
dependencies = [
"bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -193,7 +193,7 @@ impl Stylist {
pub fn push_applicable_declarations<E,V>(
&self,
element: &E,
parent_bf: &Option<Box<BloomFilter>>,
parent_bf: Option<&BloomFilter>,
style_attribute: Option<&PropertyDeclarationBlock>,
pseudo_element: Option<PseudoElement>,
applicable_declarations: &mut V)

View File

@ -1164,7 +1164,7 @@ dependencies = [
[[package]]
name = "selectors"
version = "0.1.0"
source = "git+https://github.com/servo/rust-selectors#a6cf1fba8f31960254aa62434ab8aeee13aff080"
source = "git+https://github.com/servo/rust-selectors#625734e1e8d70c012672b248adca302d0276fe08"
dependencies = [
"bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -1072,7 +1072,7 @@ dependencies = [
[[package]]
name = "selectors"
version = "0.1.0"
source = "git+https://github.com/servo/rust-selectors#a6cf1fba8f31960254aa62434ab8aeee13aff080"
source = "git+https://github.com/servo/rust-selectors#625734e1e8d70c012672b248adca302d0276fe08"
dependencies = [
"bitflags 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"cssparser 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)",