diff options
26 files changed, 136 insertions, 2896 deletions
diff --git a/components/layout/Cargo.toml b/components/layout/Cargo.toml index b24e2c2562c..5447ff64047 100644 --- a/components/layout/Cargo.toml +++ b/components/layout/Cargo.toml @@ -43,6 +43,9 @@ path = "../util" [dependencies.cssparser] git = "https://github.com/servo/rust-cssparser" +[dependencies.selectors] +git = "https://github.com/servo/rust-selectors" + [dependencies.geom] git = "https://github.com/servo/rust-geom" diff --git a/components/layout/css/matching.rs b/components/layout/css/matching.rs index 31c14804d6b..cc6147cce46 100644 --- a/components/layout/css/matching.rs +++ b/components/layout/css/matching.rs @@ -12,7 +12,7 @@ use util::{LayoutDataAccess, LayoutDataWrapper}; use wrapper::{LayoutElement, LayoutNode, TLayoutNode}; use script::dom::node::NodeTypeId; -use servo_util::bloom::BloomFilter; +use selectors::bloom::BloomFilter; use servo_util::cache::{LRUCache, SimpleHashCache}; use servo_util::smallvec::{SmallVec, SmallVec16}; use servo_util::arc_ptr_eq; @@ -21,12 +21,12 @@ use std::mem; use std::hash::{Hash, Hasher, Writer}; use std::slice::Iter; use string_cache::{Atom, Namespace}; -use style::selectors::PseudoElement; +use selectors::parser::PseudoElement; use style::selector_matching::{Stylist, DeclarationBlock}; use style::node::{TElement, TNode}; use style::properties::{ComputedValues, cascade}; -use style::selector_matching::{CommonStyleAffectingAttributeMode, CommonStyleAffectingAttributes}; -use style::selector_matching::{common_style_affecting_attributes, rare_style_affecting_attributes}; +use selectors::matching::{CommonStyleAffectingAttributeMode, CommonStyleAffectingAttributes}; +use selectors::matching::{common_style_affecting_attributes, rare_style_affecting_attributes}; use std::sync::Arc; pub struct ApplicableDeclarations { diff --git a/components/layout/lib.rs b/components/layout/lib.rs index 44f8f49c056..837945eff81 100644 --- a/components/layout/lib.rs +++ b/components/layout/lib.rs @@ -41,6 +41,7 @@ extern crate style; extern crate "plugins" as servo_plugins; extern crate net; extern crate msg; +extern crate selectors; #[macro_use] extern crate "util" as servo_util; diff --git a/components/layout/traversal.rs b/components/layout/traversal.rs index 3ec4aa1bb53..2211cf2fcce 100644 --- a/components/layout/traversal.rs +++ b/components/layout/traversal.rs @@ -18,7 +18,7 @@ use wrapper::{layout_node_to_unsafe_layout_node, LayoutNode}; use wrapper::{PostorderNodeMutTraversal, ThreadSafeLayoutNode, UnsafeLayoutNode}; use wrapper::{PreorderDomTraversal, PostorderDomTraversal}; -use servo_util::bloom::BloomFilter; +use selectors::bloom::BloomFilter; use servo_util::opts; use servo_util::tid::tid; use style::node::TNode; diff --git a/components/layout/wrapper.rs b/components/layout/wrapper.rs index 102dfb15f79..54f341d1d09 100644 --- a/components/layout/wrapper.rs +++ b/components/layout/wrapper.rs @@ -68,7 +68,7 @@ use std::mem; use std::sync::mpsc::Sender; use string_cache::{Atom, Namespace}; use style::computed_values::{content, display, white_space}; -use style::selectors::{NamespaceConstraint, AttrSelector}; +use selectors::parser::{NamespaceConstraint, AttrSelector}; use style::legacy::{LengthAttribute, SimpleColorAttribute, UnsignedIntegerAttribute, IntegerAttribute}; use style::node::{TElement, TElementAttributes, TNode}; use style::properties::PropertyDeclarationBlock; diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index f58356ff9f5..62a44c31109 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -42,6 +42,9 @@ path = "../canvas" [dependencies.cssparser] git = "https://github.com/servo/rust-cssparser" +[dependencies.selectors] +git = "https://github.com/servo/rust-selectors" + [dependencies.geom] git = "https://github.com/servo/rust-geom" diff --git a/components/script/dom/element.rs b/components/script/dom/element.rs index e54607f73a0..03e7247eb7a 100644 --- a/components/script/dom/element.rs +++ b/components/script/dom/element.rs @@ -53,9 +53,9 @@ use dom::nodelist::NodeList; use dom::virtualmethods::{VirtualMethods, vtable_for}; use devtools_traits::AttrInfo; use style::legacy::{SimpleColorAttribute, UnsignedIntegerAttribute, IntegerAttribute, LengthAttribute}; -use style::selector_matching::matches; +use selectors::matching::matches; use style::properties::{PropertyDeclarationBlock, PropertyDeclaration, parse_style_attribute}; -use style::selectors::parse_author_origin_selector_list_from_str; +use selectors::parser::parse_author_origin_selector_list_from_str; use style; use util::namespace; use util::str::{DOMString, LengthOrPercentageOrAuto}; diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index 253e987e454..18910764188 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -48,9 +48,9 @@ use devtools_traits::NodeInfo; use script_traits::UntrustedNodeAddress; use util::geometry::Au; use util::str::{DOMString, null_str_as_empty}; -use style::selectors::{Selector, AttrSelector, NamespaceConstraint}; -use style::selectors::parse_author_origin_selector_list_from_str; -use style::selector_matching::matches; +use selectors::parser::{Selector, AttrSelector, NamespaceConstraint}; +use selectors::parser::parse_author_origin_selector_list_from_str; +use selectors::matching::matches; use style::properties::ComputedValues; use style; diff --git a/components/script/lib.rs b/components/script/lib.rs index a35e3c2ee89..a88bc2f5fa9 100644 --- a/components/script/lib.rs +++ b/components/script/lib.rs @@ -41,6 +41,7 @@ extern crate "rustc-serialize" as rustc_serialize; extern crate time; extern crate canvas; extern crate script_traits; +extern crate selectors; #[no_link] #[plugin] #[macro_use] extern crate "plugins" as servo_plugins; extern crate util; diff --git a/components/servo/Cargo.lock b/components/servo/Cargo.lock index 00bedcf93f6..fea1d2740c5 100644 --- a/components/servo/Cargo.lock +++ b/components/servo/Cargo.lock @@ -517,6 +517,7 @@ dependencies = [ "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "script 0.0.1", "script_traits 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "style 0.0.1", @@ -720,6 +721,7 @@ dependencies = [ "plugins 0.0.1", "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "script_traits 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "style 0.0.1", @@ -743,6 +745,18 @@ dependencies = [ ] [[package]] +name = "selectors" +version = "0.1.0" +source = "git+https://github.com/servo/rust-selectors#1e5bbf16eee3adeec1e911c7eaa8f2be02cd4c67" +dependencies = [ + "bitflags 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)", + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", + "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", +] + +[[package]] name = "skia" version = "0.0.20130412" source = "git+https://github.com/servo/skia?branch=upstream-2014-06-16#76b626df0d6cfb32eb1ee5ba3c7b52aadd5a42e3" @@ -788,6 +802,7 @@ dependencies = [ "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "mod_path 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "text_writer 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -853,6 +868,7 @@ dependencies = [ "plugins 0.0.1", "rand 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "task_info 0.0.1", diff --git a/components/style/Cargo.toml b/components/style/Cargo.toml index 89e814ffd00..749d81645c8 100644 --- a/components/style/Cargo.toml +++ b/components/style/Cargo.toml @@ -21,6 +21,9 @@ git = "https://github.com/servo/rust-geom" [dependencies.cssparser] git = "https://github.com/servo/rust-cssparser" +[dependencies.selectors] +git = "https://github.com/servo/rust-selectors" + [dependencies.lazy_static] git = "https://github.com/Kimundi/lazy-static.rs" diff --git a/components/style/legacy.rs b/components/style/legacy.rs index bd4ef9a5b2a..c7d974d1094 100644 --- a/components/style/legacy.rs +++ b/components/style/legacy.rs @@ -5,17 +5,21 @@ //! Legacy presentational attributes defined in the HTML5 specification: `<td width>`, //! `<input size>`, and so forth. -use node::{TElement, TElementAttributes, TNode}; +use std::sync::Arc; + +use selectors::tree::{TElement, TNode}; +use selectors::matching::DeclarationBlock; +use node::TElementAttributes; use values::specified::CSSColor; use values::{CSSFloat, specified}; use properties::DeclaredValue::SpecifiedValue; use properties::PropertyDeclaration; use properties::longhands; -use selector_matching::{DeclarationBlock, Stylist}; +use selector_matching::Stylist; use cssparser::Color; +use selectors::smallvec::VecLike; use util::geometry::Au; -use util::smallvec::VecLike; use util::str::LengthOrPercentageOrAuto; /// Legacy presentational attributes that take a length as defined in HTML5 § 2.4.4.4. @@ -68,7 +72,7 @@ pub trait PresentationalHintSynthesis { where E: TElement<'a> + TElementAttributes, N: TNode<'a,E>, - V: VecLike<DeclarationBlock>; + V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>>; /// Synthesizes rules for the legacy `bgcolor` attribute. fn synthesize_presentational_hint_for_legacy_background_color_attribute<'a,E,V>( &self, @@ -80,7 +84,7 @@ pub trait PresentationalHintSynthesis { E: TElement<'a> + TElementAttributes, V: VecLike< - DeclarationBlock>; + DeclarationBlock<Vec<PropertyDeclaration>>>; /// Synthesizes rules for the legacy `border` attribute. fn synthesize_presentational_hint_for_legacy_border_attribute<'a,E,V>( &self, @@ -90,7 +94,7 @@ pub trait PresentationalHintSynthesis { where E: TElement<'a> + TElementAttributes, - V: VecLike<DeclarationBlock>; + V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>>; } impl PresentationalHintSynthesis for Stylist { @@ -102,7 +106,7 @@ impl PresentationalHintSynthesis for Stylist { where E: TElement<'a> + TElementAttributes, N: TNode<'a,E>, - V: VecLike<DeclarationBlock> { + V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>> { let element = node.as_element(); match element.get_local_name() { name if *name == atom!("td") => { @@ -110,13 +114,13 @@ impl PresentationalHintSynthesis for Stylist { LengthOrPercentageOrAuto::Auto => {} LengthOrPercentageOrAuto::Percentage(percentage) => { let width_value = specified::LengthOrPercentageOrAuto::Percentage(percentage); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::Width(SpecifiedValue(width_value)))); *shareable = false } LengthOrPercentageOrAuto::Length(length) => { let width_value = specified::LengthOrPercentageOrAuto::Length(specified::Length::Au(length)); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::Width(SpecifiedValue(width_value)))); *shareable = false } @@ -160,7 +164,7 @@ impl PresentationalHintSynthesis for Stylist { } _ => specified::Length::Au(Au::from_px(value as int)), }; - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::Width(SpecifiedValue( specified::LengthOrPercentageOrAuto::Length(value))))); *shareable = false @@ -177,7 +181,7 @@ impl PresentationalHintSynthesis for Stylist { // // https://html.spec.whatwg.org/multipage/rendering.html#textarea-effective-width let value = specified::Length::ServoCharacterWidth(value); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::Width(SpecifiedValue( specified::LengthOrPercentageOrAuto::Length(value))))); *shareable = false @@ -190,7 +194,7 @@ impl PresentationalHintSynthesis for Stylist { // // https://html.spec.whatwg.org/multipage/rendering.html#textarea-effective-height let value = specified::Length::Em(value as CSSFloat); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::Height(SpecifiedValue( longhands::height::SpecifiedValue( specified::LengthOrPercentageOrAuto::Length(value)))))); @@ -213,11 +217,11 @@ impl PresentationalHintSynthesis for Stylist { E: TElement<'a> + TElementAttributes, V: VecLike< - DeclarationBlock> { + DeclarationBlock<Vec<PropertyDeclaration>>> { match element.get_simple_color_attribute(SimpleColorAttribute::BgColor) { None => {} Some(color) => { - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::BackgroundColor(SpecifiedValue( CSSColor { parsed: Color::RGBA(color), authored: None })))); *shareable = false @@ -233,21 +237,21 @@ impl PresentationalHintSynthesis for Stylist { where E: TElement<'a> + TElementAttributes, - V: VecLike<DeclarationBlock> { + V: VecLike<DeclarationBlock<Vec<PropertyDeclaration>>> { match element.get_unsigned_integer_attribute(UnsignedIntegerAttribute::Border) { None => {} Some(length) => { let width_value = specified::Length::Au(Au::from_px(length as int)); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::BorderTopWidth(SpecifiedValue( longhands::border_top_width::SpecifiedValue(width_value))))); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::BorderLeftWidth(SpecifiedValue( longhands::border_left_width::SpecifiedValue(width_value))))); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::BorderBottomWidth(SpecifiedValue( longhands::border_bottom_width::SpecifiedValue(width_value))))); - matching_rules_list.vec_push(DeclarationBlock::from_declaration( + matching_rules_list.vec_push(from_declaration( PropertyDeclaration::BorderRightWidth(SpecifiedValue( longhands::border_right_width::SpecifiedValue(width_value))))); *shareable = false @@ -256,3 +260,10 @@ impl PresentationalHintSynthesis for Stylist { } } + +/// A convenience function to create a declaration block from a single declaration. This is +/// primarily used in `synthesize_rules_for_legacy_attributes`. +#[inline] +pub fn from_declaration(rule: PropertyDeclaration) -> DeclarationBlock<Vec<PropertyDeclaration>> { + DeclarationBlock::from_declarations(Arc::new(vec![rule])) +} diff --git a/components/style/lib.rs b/components/style/lib.rs index 9912133f44c..88fa9ea2701 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -7,7 +7,6 @@ #![feature(box_syntax)] #![feature(core)] #![feature(std_misc)] -#![feature(hash)] #![feature(collections)] #![feature(rustc_private)] @@ -30,6 +29,7 @@ extern crate matches; extern crate encoding; extern crate string_cache; +extern crate selectors; #[macro_use] extern crate lazy_static; @@ -41,7 +41,6 @@ extern crate util; pub mod stylesheets; pub mod parser; -pub mod selectors; pub mod selector_matching; #[macro_use] pub mod values; diff --git a/components/style/node.rs b/components/style/node.rs index df4a4864303..c3bd70a44bf 100644 --- a/components/style/node.rs +++ b/components/style/node.rs @@ -7,56 +7,9 @@ use cssparser::RGBA; use legacy::{IntegerAttribute, LengthAttribute, SimpleColorAttribute, UnsignedIntegerAttribute}; -use selectors::AttrSelector; use util::str::LengthOrPercentageOrAuto; -use string_cache::{Atom, Namespace}; -pub trait TNode<'a, E: TElement<'a>> : Clone + Copy { - fn parent_node(self) -> Option<Self>; - fn first_child(self) -> Option<Self>; - fn last_child(self) -> Option<Self>; - fn prev_sibling(self) -> Option<Self>; - fn next_sibling(self) -> Option<Self>; - fn is_document(self) -> bool; - fn is_element(self) -> bool; - fn as_element(self) -> E; - fn match_attr<F>(self, attr: &AttrSelector, test: F) -> bool where F: Fn(&str) -> bool; - fn is_html_element_in_html_document(self) -> bool; - - fn has_changed(self) -> bool; - unsafe fn set_changed(self, value: bool); - - fn is_dirty(self) -> bool; - unsafe fn set_dirty(self, value: bool); - - fn has_dirty_siblings(self) -> bool; - unsafe fn set_dirty_siblings(self, value: bool); - - fn has_dirty_descendants(self) -> bool; - unsafe fn set_dirty_descendants(self, value: bool); -} - -pub trait TElement<'a> : Copy { - fn get_attr(self, namespace: &Namespace, attr: &Atom) -> Option<&'a str>; - fn get_attrs(self, attr: &Atom) -> Vec<&'a str>; - fn get_link(self) -> Option<&'a str>; - fn get_local_name(self) -> &'a Atom; - fn get_namespace(self) -> &'a Namespace; - fn get_hover_state(self) -> bool; - fn get_id(self) -> Option<Atom>; - fn get_disabled_state(self) -> bool; - fn get_enabled_state(self) -> bool; - fn get_checked_state(self) -> bool; - fn get_indeterminate_state(self) -> bool; - fn has_class(self, name: &Atom) -> bool; - fn has_nonzero_border(self) -> bool; - - // Ordinarily I wouldn't use callbacks like this, but the alternative is - // really messy, since there is a `JSRef` and a `RefCell` involved. Maybe - // in the future when we have associated types and/or a more convenient - // JS GC story... --pcwalton - fn each_class<F>(self, callback: F) where F: FnMut(&Atom); -} +pub use selectors::tree::{TNode, TElement}; pub trait TElementAttributes : Copy { fn get_length_attribute(self, attribute: LengthAttribute) -> LengthOrPercentageOrAuto; diff --git a/components/style/parser.rs b/components/style/parser.rs index 5219c667dac..6fd821f3435 100644 --- a/components/style/parser.rs +++ b/components/style/parser.rs @@ -3,8 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use std::collections::HashMap; -use string_cache::Namespace; +use selectors::parser::ParserContext as SelectorParserContext; use cssparser::{Parser, SourcePosition}; use url::{Url, UrlParser}; use log; @@ -12,37 +11,24 @@ use log; use stylesheets::Origin; -pub struct NamespaceMap { - pub default: Option<Namespace>, - pub prefix_map: HashMap<String, Namespace>, -} - - pub struct ParserContext<'a> { - pub stylesheet_origin: Origin, pub base_url: &'a Url, - pub namespaces: NamespaceMap, + pub selector_context: SelectorParserContext, } impl<'a> ParserContext<'a> { pub fn new(stylesheet_origin: Origin, base_url: &'a Url) -> ParserContext<'a> { + let mut selector_context = SelectorParserContext::new(); + selector_context.in_user_agent_stylesheet = stylesheet_origin == Origin::UserAgent; ParserContext { - stylesheet_origin: stylesheet_origin, base_url: base_url, - namespaces: NamespaceMap { - default: None, - prefix_map: HashMap::new() - } + selector_context: selector_context, } } } impl<'a> ParserContext<'a> { - pub fn in_user_agent_stylesheet(&self) -> bool { - self.stylesheet_origin == Origin::UserAgent - } - pub fn parse_url(&self, input: &str) -> Url { UrlParser::new().base_url(self.base_url).parse(input) .unwrap_or_else(|_| Url::parse("about:invalid").unwrap()) diff --git a/components/style/properties.mako.rs b/components/style/properties.mako.rs index 3589e525e7b..f052520643a 100644 --- a/components/style/properties.mako.rs +++ b/components/style/properties.mako.rs @@ -21,7 +21,7 @@ use geom::SideOffsets2D; use values::specified::BorderStyle; use values::computed::{self, ToComputedValue}; -use selector_matching::DeclarationBlock; +use selectors::matching::DeclarationBlock; use parser::{ParserContext, log_css_error}; use stylesheets::Origin; use computed_values; @@ -3180,7 +3180,7 @@ fn initial_writing_mode_is_empty() { /// Fast path for the function below. Only computes new inherited styles. #[allow(unused_mut)] -fn cascade_with_cached_declarations(applicable_declarations: &[DeclarationBlock], +fn cascade_with_cached_declarations(applicable_declarations: &[DeclarationBlock<Vec<PropertyDeclaration>>], shareable: bool, parent_style: &ComputedValues, cached_style: &ComputedValues, @@ -3283,7 +3283,7 @@ fn cascade_with_cached_declarations(applicable_declarations: &[DeclarationBlock] /// this is ignored. /// /// Returns the computed values and a boolean indicating whether the result is cacheable. -pub fn cascade(applicable_declarations: &[DeclarationBlock], +pub fn cascade(applicable_declarations: &[DeclarationBlock<Vec<PropertyDeclaration>>], shareable: bool, parent_style: Option< &ComputedValues >, cached_style: Option< &ComputedValues >) diff --git a/components/style/selector_matching.rs b/components/style/selector_matching.rs index bf780ac6f0a..3bd03be1eae 100644 --- a/components/style/selector_matching.rs +++ b/components/style/selector_matching.rs @@ -2,258 +2,25 @@ * 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 std::ascii::AsciiExt; -use std::cmp::Ordering; -use std::collections::HashMap; -use std::sync::Arc; - use url::Url; -use util::bloom::BloomFilter; +use selectors::bloom::BloomFilter; +use selectors::matching::{SelectorMap, Rule}; +use selectors::matching::DeclarationBlock as GenericDeclarationBlock; +use selectors::parser::PseudoElement; +use selectors::smallvec::VecLike; +use selectors::tree::{TNode, TElement}; use util::resource_files::read_resource_file; -use util::smallvec::VecLike; -use util::sort; -use string_cache::Atom; use legacy::PresentationalHintSynthesis; use media_queries::Device; -use node::{TElement, TElementAttributes, TNode}; +use node::TElementAttributes; use properties::{PropertyDeclaration, PropertyDeclarationBlock}; -use selectors::{CaseSensitivity, Combinator, CompoundSelector, LocalName}; -use selectors::{PseudoElement, SimpleSelector, Selector}; use stylesheets::{Stylesheet, iter_stylesheet_media_rules, iter_stylesheet_style_rules, Origin}; -/// The definition of whitespace per CSS Selectors Level 3 § 4. -pub static SELECTOR_WHITESPACE: &'static [char] = &[' ', '\t', '\n', '\r', '\x0C']; - -/// Map node attributes to Rules whose last simple selector starts with them. -/// -/// e.g., -/// "p > img" would go into the set of Rules corresponding to the -/// element "img" -/// "a .foo .bar.baz" would go into the set of Rules corresponding to -/// the class "bar" -/// -/// Because we match Rules right-to-left (i.e., moving up the tree -/// from a node), we need to compare the last simple selector in the -/// Rule with the node. -/// -/// So, if a node has ID "id1" and classes "foo" and "bar", then all -/// the rules it matches will have their last simple selector starting -/// either with "#id1" or with ".foo" or with ".bar". -/// -/// Hence, the union of the rules keyed on each of node's classes, ID, -/// element name, etc. will contain the Rules that actually match that -/// node. -struct SelectorMap { - // TODO: Tune the initial capacity of the HashMap - id_hash: HashMap<Atom, Vec<Rule>>, - class_hash: HashMap<Atom, Vec<Rule>>, - local_name_hash: HashMap<Atom, Vec<Rule>>, - /// Same as local_name_hash, but keys are lower-cased. - /// For HTML elements in HTML documents. - lower_local_name_hash: HashMap<Atom, Vec<Rule>>, - // For Rules that don't have ID, class, or element selectors. - universal_rules: Vec<Rule>, - /// Whether this hash is empty. - empty: bool, -} - -impl SelectorMap { - fn new() -> SelectorMap { - SelectorMap { - id_hash: HashMap::new(), - class_hash: HashMap::new(), - local_name_hash: HashMap::new(), - lower_local_name_hash: HashMap::new(), - universal_rules: vec!(), - empty: true, - } - } - - /// Append to `rule_list` all Rules in `self` that match node. - /// - /// Extract matching rules as per node's ID, classes, tag name, etc.. - /// Sort the Rules at the end to maintain cascading order. - fn get_all_matching_rules<'a,E,N,V>(&self, - node: &N, - parent_bf: &Option<Box<BloomFilter>>, - matching_rules_list: &mut V, - shareable: &mut bool) - where E: TElement<'a> + TElementAttributes, - N: TNode<'a,E>, - V: VecLike<DeclarationBlock> { - if self.empty { - return - } - - // At the end, we're going to sort the rules that we added, so remember where we began. - let init_len = matching_rules_list.vec_len(); - let element = node.as_element(); - match element.get_id() { - Some(id) => { - SelectorMap::get_matching_rules_from_hash(node, - parent_bf, - &self.id_hash, - &id, - matching_rules_list, - shareable) - } - None => {} - } - - element.each_class(|class| { - SelectorMap::get_matching_rules_from_hash(node, - parent_bf, - &self.class_hash, - class, - matching_rules_list, - shareable); - }); +pub type DeclarationBlock = GenericDeclarationBlock<Vec<PropertyDeclaration>>; - let local_name_hash = if node.is_html_element_in_html_document() { - &self.lower_local_name_hash - } else { - &self.local_name_hash - }; - SelectorMap::get_matching_rules_from_hash(node, - parent_bf, - local_name_hash, - element.get_local_name(), - matching_rules_list, - shareable); - - SelectorMap::get_matching_rules(node, - parent_bf, - &self.universal_rules, - matching_rules_list, - shareable); - - // Sort only the rules we just added. - sort::quicksort_by(matching_rules_list.vec_slice_from_mut(init_len), compare); - - fn compare(a: &DeclarationBlock, b: &DeclarationBlock) -> Ordering { - (a.specificity, a.source_order).cmp(&(b.specificity, b.source_order)) - } - } - - fn get_matching_rules_from_hash<'a,E,N,V>(node: &N, - parent_bf: &Option<Box<BloomFilter>>, - hash: &HashMap<Atom, Vec<Rule>>, - key: &Atom, - matching_rules: &mut V, - shareable: &mut bool) - where E: TElement<'a> + TElementAttributes, - N: TNode<'a,E>, - V: VecLike<DeclarationBlock> { - match hash.get(key) { - Some(rules) => { - SelectorMap::get_matching_rules(node, - parent_bf, - rules, - matching_rules, - shareable) - } - None => {} - } - } - - /// Adds rules in `rules` that match `node` to the `matching_rules` list. - fn get_matching_rules<'a,E,N,V>(node: &N, - parent_bf: &Option<Box<BloomFilter>>, - rules: &[Rule], - matching_rules: &mut V, - shareable: &mut bool) - where E: TElement<'a> + TElementAttributes, - N: TNode<'a,E>, - V: VecLike<DeclarationBlock> { - for rule in rules.iter() { - if matches_compound_selector(&*rule.selector, node, parent_bf, shareable) { - matching_rules.vec_push(rule.declarations.clone()); - } - } - } - - /// Insert rule into the correct hash. - /// Order in which to try: id_hash, class_hash, local_name_hash, universal_rules. - fn insert(&mut self, rule: Rule) { - self.empty = false; - - match SelectorMap::get_id_name(&rule) { - Some(id_name) => { - find_push(&mut self.id_hash, id_name, rule); - return; - } - None => {} - } - match SelectorMap::get_class_name(&rule) { - Some(class_name) => { - find_push(&mut self.class_hash, class_name, rule); - return; - } - None => {} - } - - match SelectorMap::get_local_name(&rule) { - Some(LocalName { name, lower_name }) => { - find_push(&mut self.local_name_hash, name, rule.clone()); - find_push(&mut self.lower_local_name_hash, lower_name, rule); - return; - } - None => {} - } - - self.universal_rules.push(rule); - } - - /// Retrieve the first ID name in Rule, or None otherwise. - fn get_id_name(rule: &Rule) -> Option<Atom> { - let simple_selector_sequence = &rule.selector.simple_selectors; - for ss in simple_selector_sequence.iter() { - match *ss { - // TODO(pradeep): Implement case-sensitivity based on the document type and quirks - // mode. - SimpleSelector::ID(ref id) => return Some(id.clone()), - _ => {} - } - } - return None - } - - /// Retrieve the FIRST class name in Rule, or None otherwise. - fn get_class_name(rule: &Rule) -> Option<Atom> { - let simple_selector_sequence = &rule.selector.simple_selectors; - for ss in simple_selector_sequence.iter() { - match *ss { - // TODO(pradeep): Implement case-sensitivity based on the document type and quirks - // mode. - SimpleSelector::Class(ref class) => return Some(class.clone()), - _ => {} - } - } - return None - } - - /// Retrieve the name if it is a type selector, or None otherwise. - fn get_local_name(rule: &Rule) -> Option<LocalName> { - let simple_selector_sequence = &rule.selector.simple_selectors; - for ss in simple_selector_sequence.iter() { - match *ss { - SimpleSelector::LocalName(ref name) => { - return Some(name.clone()) - } - _ => {} - } - } - return None - } -} - -// The bloom filter for descendant CSS selectors will have a <1% false -// positive rate until it has this many selectors in it, then it will -// rapidly increase. -pub static RECOMMENDED_SELECTOR_BLOOM_FILTER_SIZE: uint = 4096; pub struct Stylist { // List of stylesheets (including all media rules) @@ -448,8 +215,8 @@ impl Stylist { // Step 3: Normal style attributes. style_attribute.map(|sa| { shareable = false; - applicable_declarations.vec_push(DeclarationBlock::from_declarations(sa.normal - .clone())) + applicable_declarations.vec_push( + GenericDeclarationBlock::from_declarations(sa.normal.clone())) }); // Step 4: Author-supplied `!important` rules. @@ -461,8 +228,8 @@ impl Stylist { // Step 5: `!important` style attributes. style_attribute.map(|sa| { shareable = false; - applicable_declarations.vec_push(DeclarationBlock::from_declarations(sa.important - .clone())) + applicable_declarations.vec_push( + GenericDeclarationBlock::from_declarations(sa.important.clone())) }); // Step 6: User and UA `!important` rules. @@ -480,8 +247,8 @@ impl Stylist { } struct PerOriginSelectorMap { - normal: SelectorMap, - important: SelectorMap, + normal: SelectorMap<Vec<PropertyDeclaration>>, + important: SelectorMap<Vec<PropertyDeclaration>>, } impl PerOriginSelectorMap { @@ -510,727 +277,3 @@ impl PerPseudoElementSelectorMap { } } } - -#[derive(Clone)] -struct Rule { - // This is an Arc because Rule will essentially be cloned for every node - // that it matches. Selector contains an owned vector (through - // CompoundSelector) and we want to avoid the allocation. - selector: Arc<CompoundSelector>, - declarations: DeclarationBlock, -} - -/// A property declaration together with its precedence among rules of equal specificity so that -/// we can sort them. -#[derive(Clone, Debug)] -pub struct DeclarationBlock { - pub declarations: Arc<Vec<PropertyDeclaration>>, - source_order: uint, - specificity: u32, -} - -impl DeclarationBlock { - #[inline] - pub fn from_declarations(declarations: Arc<Vec<PropertyDeclaration>>) -> DeclarationBlock { - DeclarationBlock { - declarations: declarations, - source_order: 0, - specificity: 0, - } - } - - /// A convenience function to create a declaration block from a single declaration. This is - /// primarily used in `synthesize_rules_for_legacy_attributes`. - #[inline] - pub fn from_declaration(rule: PropertyDeclaration) -> DeclarationBlock { - DeclarationBlock::from_declarations(Arc::new(vec![rule])) - } -} - -pub fn matches<'a,E,N>(selector_list: &Vec<Selector>, - element: &N, - parent_bf: &Option<Box<BloomFilter>>) - -> bool - where E: TElement<'a>, N: TNode<'a,E> { - selector_list.iter().any(|selector| { - selector.pseudo_element.is_none() && - matches_compound_selector(&*selector.compound_selectors, element, parent_bf, &mut false) - }) -} - -/// Determines whether the given element matches the given single or compound selector. -/// -/// NB: If you add support for any new kinds of selectors to this routine, be sure to set -/// `shareable` to false unless you are willing to update the style sharing logic. Otherwise things -/// will almost certainly break as nodes will start mistakenly sharing styles. (See the code in -/// `main/css/matching.rs`.) -fn matches_compound_selector<'a,E,N>(selector: &CompoundSelector, - element: &N, - parent_bf: &Option<Box<BloomFilter>>, - shareable: &mut bool) - -> bool - where E: TElement<'a>, N: TNode<'a,E> { - match matches_compound_selector_internal(selector, element, parent_bf, shareable) { - SelectorMatchingResult::Matched => true, - _ => false - } -} - -/// A result of selector matching, includes 3 failure types, -/// -/// NotMatchedAndRestartFromClosestLaterSibling -/// NotMatchedAndRestartFromClosestDescendant -/// NotMatchedGlobally -/// -/// When NotMatchedGlobally appears, stop selector matching completely since -/// the succeeding selectors never matches. -/// It is raised when -/// Child combinator cannot find the candidate element. -/// Descendant combinator cannot find the candidate element. -/// -/// When NotMatchedAndRestartFromClosestDescendant appears, the selector -/// matching does backtracking and restarts from the closest Descendant -/// combinator. -/// It is raised when -/// NextSibling combinator cannot find the candidate element. -/// LaterSibling combinator cannot find the candidate element. -/// Child combinator doesn't match on the found element. -/// -/// When NotMatchedAndRestartFromClosestLaterSibling appears, the selector -/// matching does backtracking and restarts from the closest LaterSibling -/// combinator. -/// It is raised when -/// NextSibling combinator doesn't match on the found element. -/// -/// For example, when the selector "d1 d2 a" is provided and we cannot *find* -/// an appropriate ancestor node for "d1", this selector matching raises -/// NotMatchedGlobally since even if "d2" is moved to more upper node, the -/// candidates for "d1" becomes less than before and d1 . -/// -/// The next example is siblings. When the selector "b1 + b2 ~ d1 a" is -/// provided and we cannot *find* an appropriate brother node for b1, -/// the selector matching raises NotMatchedAndRestartFromClosestDescendant. -/// The selectors ("b1 + b2 ~") doesn't match and matching restart from "d1". -/// -/// The additional example is child and sibling. When the selector -/// "b1 + c1 > b2 ~ d1 a" is provided and the selector "b1" doesn't match on -/// the element, this "b1" raises NotMatchedAndRestartFromClosestLaterSibling. -/// However since the selector "c1" raises -/// NotMatchedAndRestartFromClosestDescendant. So the selector -/// "b1 + c1 > b2 ~ " doesn't match and restart matching from "d1". -#[derive(PartialEq, Eq, Copy)] -enum SelectorMatchingResult { - Matched, - NotMatchedAndRestartFromClosestLaterSibling, - NotMatchedAndRestartFromClosestDescendant, - NotMatchedGlobally, -} - -/// Quickly figures out whether or not the compound selector is worth doing more -/// work on. If the simple selectors don't match, or there's a child selector -/// that does not appear in the bloom parent bloom filter, we can exit early. -fn can_fast_reject<'a,E,N>(mut selector: &CompoundSelector, - element: &N, - parent_bf: &Option<Box<BloomFilter>>, - shareable: &mut bool) - -> Option<SelectorMatchingResult> - where E: TElement<'a>, N: TNode<'a,E> { - if !selector.simple_selectors.iter().all(|simple_selector| { - matches_simple_selector(simple_selector, element, shareable) }) { - return Some(SelectorMatchingResult::NotMatchedAndRestartFromClosestLaterSibling); - } - - let bf: &BloomFilter = match *parent_bf { - None => return None, - Some(ref bf) => &**bf, - }; - - // See if the bloom filter can exclude any of the descendant selectors, and - // reject if we can. - loop { - match selector.next { - None => break, - Some((ref cs, Combinator::Descendant)) => selector = &**cs, - Some((ref cs, _)) => { - selector = &**cs; - continue; - } - }; - - for ss in selector.simple_selectors.iter() { - match *ss { - SimpleSelector::LocalName(LocalName { ref name, ref lower_name }) => { - if !bf.might_contain(name) - && !bf.might_contain(lower_name) { - return Some(SelectorMatchingResult::NotMatchedGlobally); - } - }, - SimpleSelector::Namespace(ref namespace) => { - if !bf.might_contain(namespace) { - return Some(SelectorMatchingResult::NotMatchedGlobally); - } - }, - SimpleSelector::ID(ref id) => { - if !bf.might_contain(id) { - return Some(SelectorMatchingResult::NotMatchedGlobally); - } - }, - SimpleSelector::Class(ref class) => { - if !bf.might_contain(class) { - return Some(SelectorMatchingResult::NotMatchedGlobally); - } - }, - _ => {}, - } - } - - } - - // Can't fast reject. - return None; -} - -fn matches_compound_selector_internal<'a,E,N>(selector: &CompoundSelector, - element: &N, - parent_bf: &Option<Box<BloomFilter>>, - shareable: &mut bool) - -> SelectorMatchingResult - where E: TElement<'a>, N: TNode<'a,E> { - match can_fast_reject(selector, element, parent_bf, shareable) { - None => {}, - Some(result) => return result, - }; - - match selector.next { - None => SelectorMatchingResult::Matched, - Some((ref next_selector, combinator)) => { - let (siblings, candidate_not_found) = match combinator { - Combinator::Child => (false, SelectorMatchingResult::NotMatchedGlobally), - Combinator::Descendant => (false, SelectorMatchingResult::NotMatchedGlobally), - Combinator::NextSibling => (true, SelectorMatchingResult::NotMatchedAndRestartFromClosestDescendant), - Combinator::LaterSibling => (true, SelectorMatchingResult::NotMatchedAndRestartFromClosestDescendant), - }; - let mut node = (*element).clone(); - loop { - let next_node = if siblings { - node.prev_sibling() - } else { - node.parent_node() - }; - match next_node { - None => return candidate_not_found, - Some(next_node) => node = next_node, - } - if node.is_element() { - let result = matches_compound_selector_internal(&**next_selector, - &node, - parent_bf, - shareable); - match (result, combinator) { - // Return the status immediately. - (SelectorMatchingResult::Matched, _) => return result, - (SelectorMatchingResult::NotMatchedGlobally, _) => return result, - - // Upgrade the failure status to - // NotMatchedAndRestartFromClosestDescendant. - (_, Combinator::Child) => return SelectorMatchingResult::NotMatchedAndRestartFromClosestDescendant, - - // Return the status directly. - (_, Combinator::NextSibling) => return result, - - // If the failure status is NotMatchedAndRestartFromClosestDescendant - // and combinator is Combinator::LaterSibling, give up this Combinator::LaterSibling matching - // and restart from the closest descendant combinator. - (SelectorMatchingResult::NotMatchedAndRestartFromClosestDescendant, Combinator::LaterSibling) => return result, - - // The Combinator::Descendant combinator and the status is - // NotMatchedAndRestartFromClosestLaterSibling or - // NotMatchedAndRestartFromClosestDescendant, - // or the Combinator::LaterSibling combinator and the status is - // NotMatchedAndRestartFromClosestDescendant - // can continue to matching on the next candidate element. - _ => {}, - } - } - } - } - } -} - -bitflags! { - flags CommonStyleAffectingAttributes: u8 { - const HIDDEN_ATTRIBUTE = 0x01, - const NO_WRAP_ATTRIBUTE = 0x02, - const ALIGN_LEFT_ATTRIBUTE = 0x04, - const ALIGN_CENTER_ATTRIBUTE = 0x08, - const ALIGN_RIGHT_ATTRIBUTE = 0x10, - } -} - -pub struct CommonStyleAffectingAttributeInfo { - pub atom: Atom, - pub mode: CommonStyleAffectingAttributeMode, -} - -#[derive(Copy)] -pub enum CommonStyleAffectingAttributeMode { - IsPresent(CommonStyleAffectingAttributes), - IsEqual(&'static str, CommonStyleAffectingAttributes), -} - -// NB: This must match the order in `layout::css::matching::CommonStyleAffectingAttributes`. -#[inline] -pub fn common_style_affecting_attributes() -> [CommonStyleAffectingAttributeInfo; 5] { - [ - CommonStyleAffectingAttributeInfo { - atom: atom!("hidden"), - mode: CommonStyleAffectingAttributeMode::IsPresent(HIDDEN_ATTRIBUTE), - }, - CommonStyleAffectingAttributeInfo { - atom: atom!("nowrap"), - mode: CommonStyleAffectingAttributeMode::IsPresent(NO_WRAP_ATTRIBUTE), - }, - CommonStyleAffectingAttributeInfo { - atom: atom!("align"), - mode: CommonStyleAffectingAttributeMode::IsEqual("left", ALIGN_LEFT_ATTRIBUTE), - }, - CommonStyleAffectingAttributeInfo { - atom: atom!("align"), - mode: CommonStyleAffectingAttributeMode::IsEqual("center", ALIGN_CENTER_ATTRIBUTE), - }, - CommonStyleAffectingAttributeInfo { - atom: atom!("align"), - mode: CommonStyleAffectingAttributeMode::IsEqual("right", ALIGN_RIGHT_ATTRIBUTE), - } - ] -} - -/// Attributes that, if present, disable style sharing. All legacy HTML attributes must be in -/// either this list or `common_style_affecting_attributes`. See the comment in -/// `synthesize_presentational_hints_for_legacy_attributes`. -pub fn rare_style_affecting_attributes() -> [Atom; 3] { - [ atom!("bgcolor"), atom!("border"), atom!("colspan") ] -} - -/// Determines whether the given element matches the given single selector. -/// -/// NB: If you add support for any new kinds of selectors to this routine, be sure to set -/// `shareable` to false unless you are willing to update the style sharing logic. Otherwise things -/// will almost certainly break as nodes will start mistakenly sharing styles. (See the code in -/// `main/css/matching.rs`.) -#[inline] -pub fn matches_simple_selector<'a,E,N>(selector: &SimpleSelector, - element: &N, - shareable: &mut bool) - -> bool - where E: TElement<'a>, N: TNode<'a,E> { - match *selector { - SimpleSelector::LocalName(LocalName { ref name, ref lower_name }) => { - let name = if element.is_html_element_in_html_document() { lower_name } else { name }; - let element = element.as_element(); - element.get_local_name() == name - } - - SimpleSelector::Namespace(ref namespace) => { - let element = element.as_element(); - element.get_namespace() == namespace - } - // TODO: case-sensitivity depends on the document type and quirks mode - SimpleSelector::ID(ref id) => { - *shareable = false; - let element = element.as_element(); - element.get_id().map_or(false, |attr| { - attr == *id - }) - } - SimpleSelector::Class(ref class) => { - let element = element.as_element(); - element.has_class(class) - } - - SimpleSelector::AttrExists(ref attr) => { - // NB(pcwalton): If you update this, remember to update the corresponding list in - // `can_share_style_with()` as well. - if common_style_affecting_attributes().iter().all(|common_attr_info| { - !(common_attr_info.atom == attr.name && match common_attr_info.mode { - CommonStyleAffectingAttributeMode::IsPresent(_) => true, - CommonStyleAffectingAttributeMode::IsEqual(..) => false, - }) - }) { - *shareable = false; - } - element.match_attr(attr, |_| true) - } - SimpleSelector::AttrEqual(ref attr, ref value, case_sensitivity) => { - if *value != "DIR" && - common_style_affecting_attributes().iter().all(|common_attr_info| { - !(common_attr_info.atom == attr.name && match common_attr_info.mode { - CommonStyleAffectingAttributeMode::IsEqual(target_value, _) => *value == target_value, - CommonStyleAffectingAttributeMode::IsPresent(_) => false, - }) - }) { - // FIXME(pcwalton): Remove once we start actually supporting RTL text. This is in - // here because the UA style otherwise disables all style sharing completely. - *shareable = false - } - element.match_attr(attr, |attr_value| { - match case_sensitivity { - CaseSensitivity::CaseSensitive => attr_value == *value, - CaseSensitivity::CaseInsensitive => attr_value.eq_ignore_ascii_case(value), - } - }) - } - SimpleSelector::AttrIncludes(ref attr, ref value) => { - *shareable = false; - element.match_attr(attr, |attr_value| { - attr_value.split(SELECTOR_WHITESPACE).any(|v| v == *value) - }) - } - SimpleSelector::AttrDashMatch(ref attr, ref value, ref dashing_value) => { - *shareable = false; - element.match_attr(attr, |attr_value| { - attr_value == *value || - attr_value.starts_with(dashing_value) - }) - } - SimpleSelector::AttrPrefixMatch(ref attr, ref value) => { - *shareable = false; - element.match_attr(attr, |attr_value| { - attr_value.starts_with(value) - }) - } - SimpleSelector::AttrSubstringMatch(ref attr, ref value) => { - *shareable = false; - element.match_attr(attr, |attr_value| { - attr_value.contains(value) - }) - } - SimpleSelector::AttrSuffixMatch(ref attr, ref value) => { - *shareable = false; - element.match_attr(attr, |attr_value| { - attr_value.ends_with(value) - }) - } - - SimpleSelector::AnyLink => { - *shareable = false; - let element = element.as_element(); - element.get_link().is_some() - } - SimpleSelector::Link => { - let elem = element.as_element(); - match elem.get_link() { - Some(url) => !url_is_visited(url), - None => false, - } - } - SimpleSelector::Visited => { - // NB(pcwalton): When we actually start supporting visited links, remember to update - // `can_share_style_with`. - let elem = element.as_element(); - match elem.get_link() { - Some(url) => url_is_visited(url), - None => false, - } - } - - SimpleSelector::Hover => { - *shareable = false; - let elem = element.as_element(); - elem.get_hover_state() - }, - // http://www.whatwg.org/html/#selector-disabled - SimpleSelector::Disabled => { - *shareable = false; - let elem = element.as_element(); - elem.get_disabled_state() - }, - // http://www.whatwg.org/html/#selector-enabled - SimpleSelector::Enabled => { - *shareable = false; - let elem = element.as_element(); - elem.get_enabled_state() - }, - // https://html.spec.whatwg.org/multipage/scripting.html#selector-checked - SimpleSelector::Checked => { - *shareable = false; - let elem = element.as_element(); - elem.get_checked_state() - } - // https://html.spec.whatwg.org/multipage/scripting.html#selector-indeterminate - SimpleSelector::Indeterminate => { - *shareable = false; - let elem = element.as_element(); - elem.get_indeterminate_state() - } - SimpleSelector::FirstChild => { - *shareable = false; - matches_first_child(element) - } - SimpleSelector::LastChild => { - *shareable = false; - matches_last_child(element) - } - SimpleSelector::OnlyChild => { - *shareable = false; - matches_first_child(element) && matches_last_child(element) - } - - SimpleSelector::Root => { - *shareable = false; - matches_root(element) - } - - SimpleSelector::NthChild(a, b) => { - *shareable = false; - matches_generic_nth_child(element, a, b, false, false) - } - SimpleSelector::NthLastChild(a, b) => { - *shareable = false; - matches_generic_nth_child(element, a, b, false, true) - } - SimpleSelector::NthOfType(a, b) => { - *shareable = false; - matches_generic_nth_child(element, a, b, true, false) - } - SimpleSelector::NthLastOfType(a, b) => { - *shareable = false; - matches_generic_nth_child(element, a, b, true, true) - } - - SimpleSelector::FirstOfType => { - *shareable = false; - matches_generic_nth_child(element, 0, 1, true, false) - } - SimpleSelector::LastOfType => { - *shareable = false; - matches_generic_nth_child(element, 0, 1, true, true) - } - SimpleSelector::OnlyOfType => { - *shareable = false; - matches_generic_nth_child(element, 0, 1, true, false) && - matches_generic_nth_child(element, 0, 1, true, true) - } - - SimpleSelector::ServoNonzeroBorder => { - *shareable = false; - let elem = element.as_element(); - elem.has_nonzero_border() - } - - SimpleSelector::Negation(ref negated) => { - *shareable = false; - !negated.iter().all(|s| matches_simple_selector(s, element, shareable)) - }, - } -} - -#[inline] -fn url_is_visited(_url: &str) -> bool { - // FIXME: implement this. - // This function will probably need to take a "session" - // or something containing browsing history as an additional parameter. - // NB(pcwalton): When you implement this, remember to update `can_share_style_with`! - false -} - -#[inline] -fn matches_generic_nth_child<'a,E,N>(element: &N, - a: i32, - b: i32, - is_of_type: bool, - is_from_end: bool) - -> bool - where E: TElement<'a>, N: TNode<'a,E> { - let mut node = element.clone(); - // fail if we can't find a parent or if the node is the root element - // of the document (Cf. Selectors Level 3) - match node.parent_node() { - Some(parent) => if parent.is_document() { - return false; - }, - None => return false - }; - - let mut index = 1; - loop { - if is_from_end { - match node.next_sibling() { - None => break, - Some(next_sibling) => node = next_sibling - } - } else { - match node.prev_sibling() { - None => break, - Some(prev_sibling) => node = prev_sibling - } - } - - if node.is_element() { - if is_of_type { - let element = element.as_element(); - let node = node.as_element(); - if element.get_local_name() == node.get_local_name() && - element.get_namespace() == node.get_namespace() { - index += 1; - } - } else { - index += 1; - } - } - } - - if a == 0 { - b == index - } else { - (index - b) / a >= 0 && - (index - b) % a == 0 - } -} - -#[inline] -fn matches_root<'a,E,N>(element: &N) -> bool where E: TElement<'a>, N: TNode<'a,E> { - match element.parent_node() { - Some(parent) => parent.is_document(), - None => false - } -} - -#[inline] -fn matches_first_child<'a,E,N>(element: &N) -> bool where E: TElement<'a>, N: TNode<'a,E> { - let mut node = element.clone(); - loop { - match node.prev_sibling() { - Some(prev_sibling) => { - node = prev_sibling; - if node.is_element() { - return false - } - }, - None => match node.parent_node() { - // Selectors level 3 says :first-child does not match the - // root of the document; Warning, level 4 says, for the time - // being, the contrary... - Some(parent) => return !parent.is_document(), - None => return false - } - } - } -} - -#[inline] -fn matches_last_child<'a,E,N>(element: &N) -> bool where E: TElement<'a>, N: TNode<'a,E> { - let mut node = element.clone(); - loop { - match node.next_sibling() { - Some(next_sibling) => { - node = next_sibling; - if node.is_element() { - return false - } - }, - None => match node.parent_node() { - // Selectors level 3 says :last-child does not match the - // root of the document; Warning, level 4 says, for the time - // being, the contrary... - Some(parent) => return !parent.is_document(), - None => return false - } - } - } -} - -fn find_push(map: &mut HashMap<Atom, Vec<Rule>>, key: Atom, value: Rule) { - match map.get_mut(&key) { - Some(vec) => { - vec.push(value); - return - } - None => {} - } - map.insert(key, vec![value]); -} - -#[cfg(test)] -mod tests { - use std::cmp::Ordering; - use std::sync::Arc; - use super::{DeclarationBlock, Rule, SelectorMap}; - use selectors::LocalName; - use string_cache::Atom; - use cssparser::Parser; - use parser::ParserContext; - use url::Url; - - /// Helper method to get some Rules from selector strings. - /// Each sublist of the result contains the Rules for one StyleRule. - fn get_mock_rules(css_selectors: &[&str]) -> Vec<Vec<Rule>> { - use selectors::parse_selector_list; - use stylesheets::Origin; - - css_selectors.iter().enumerate().map(|(i, selectors)| { - let url = Url::parse("about:blank").unwrap(); - let context = ParserContext::new(Origin::Author, &url); - parse_selector_list(&context, &mut Parser::new(*selectors)) - .unwrap().into_iter().map(|s| { - Rule { - selector: s.compound_selectors.clone(), - declarations: DeclarationBlock { - specificity: s.specificity, - declarations: Arc::new(vec!()), - source_order: i, - } - } - }).collect() - }).collect() - } - - #[test] - fn test_rule_ordering_same_specificity(){ - let rules_list = get_mock_rules(&["a.intro", "img.sidebar"]); - let a = &rules_list[0][0].declarations; - let b = &rules_list[1][0].declarations; - assert!((a.specificity, a.source_order).cmp(&(b.specificity, b.source_order)) == Ordering::Less, - "The rule that comes later should win."); - } - - #[test] - fn test_get_id_name(){ - let rules_list = get_mock_rules(&[".intro", "#top"]); - assert_eq!(SelectorMap::get_id_name(&rules_list[0][0]), None); - assert_eq!(SelectorMap::get_id_name(&rules_list[1][0]), Some(atom!("top"))); - } - - #[test] - fn test_get_class_name(){ - let rules_list = get_mock_rules(&[".intro.foo", "#top"]); - assert_eq!(SelectorMap::get_class_name(&rules_list[0][0]), Some(Atom::from_slice("intro"))); - assert_eq!(SelectorMap::get_class_name(&rules_list[1][0]), None); - } - - #[test] - fn test_get_local_name(){ - let rules_list = get_mock_rules(&["img.foo", "#top", "IMG", "ImG"]); - let check = |&:i: uint, names: Option<(&str, &str)>| { - assert!(SelectorMap::get_local_name(&rules_list[i][0]) - == names.map(|(name, lower_name)| LocalName { - name: Atom::from_slice(name), - lower_name: Atom::from_slice(lower_name) })) - }; - check(0, Some(("img", "img"))); - check(1, None); - check(2, Some(("IMG", "img"))); - check(3, Some(("ImG", "img"))); - } - - #[test] - fn test_insert(){ - let rules_list = get_mock_rules(&[".intro.foo", "#top"]); - let mut selector_map = SelectorMap::new(); - selector_map.insert(rules_list[1][0].clone()); - assert_eq!(1, selector_map.id_hash.get(&atom!("top")).unwrap()[0].declarations.source_order); - selector_map.insert(rules_list[0][0].clone()); - assert_eq!(0, selector_map.class_hash.get(&Atom::from_slice("intro")).unwrap()[0].declarations.source_order); - assert!(selector_map.class_hash.get(&Atom::from_slice("foo")).is_none()); - } -} diff --git a/components/style/selectors.rs b/components/style/selectors.rs deleted file mode 100644 index 93428c120de..00000000000 --- a/components/style/selectors.rs +++ /dev/null @@ -1,788 +0,0 @@ -/* 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 std::cmp; -use std::ascii::{AsciiExt, OwnedAsciiExt}; -use std::sync::Arc; -use std::string::CowString; - -use cssparser::{Token, Parser, parse_nth}; -use string_cache::{Atom, Namespace}; -use url::Url; - -use parser::ParserContext; -use stylesheets::Origin; - - -#[derive(PartialEq, Clone, Debug)] -pub struct Selector { - pub compound_selectors: Arc<CompoundSelector>, - pub pseudo_element: Option<PseudoElement>, - pub specificity: u32, -} - -#[derive(Eq, PartialEq, Clone, Hash, Copy, Debug)] -pub enum PseudoElement { - Before, - After, - // ... -} - - -#[derive(PartialEq, Clone, Debug)] -pub struct CompoundSelector { - pub simple_selectors: Vec<SimpleSelector>, - pub next: Option<(Box<CompoundSelector>, Combinator)>, // c.next is left of c -} - -#[derive(PartialEq, Clone, Copy, Debug)] -pub enum Combinator { - Child, // > - Descendant, // space - NextSibling, // + - LaterSibling, // ~ -} - -#[derive(Eq, PartialEq, Clone, Hash, Debug)] -pub enum SimpleSelector { - ID(Atom), - Class(Atom), - LocalName(LocalName), - Namespace(Namespace), - - // Attribute selectors - AttrExists(AttrSelector), // [foo] - AttrEqual(AttrSelector, String, CaseSensitivity), // [foo=bar] - AttrIncludes(AttrSelector, String), // [foo~=bar] - AttrDashMatch(AttrSelector, String, String), // [foo|=bar] Second string is the first + "-" - AttrPrefixMatch(AttrSelector, String), // [foo^=bar] - AttrSubstringMatch(AttrSelector, String), // [foo*=bar] - AttrSuffixMatch(AttrSelector, String), // [foo$=bar] - - // Pseudo-classes - Negation(Vec<SimpleSelector>), - AnyLink, - Link, - Visited, - Hover, - Disabled, - Enabled, - Checked, - Indeterminate, - FirstChild, LastChild, OnlyChild, - Root, - NthChild(i32, i32), - NthLastChild(i32, i32), - NthOfType(i32, i32), - NthLastOfType(i32, i32), - FirstOfType, - LastOfType, - OnlyOfType, - ServoNonzeroBorder, - // ... -} - - -#[derive(Eq, PartialEq, Clone, Hash, Copy, Debug)] -pub enum CaseSensitivity { - CaseSensitive, // Selectors spec says language-defined, but HTML says sensitive. - CaseInsensitive, -} - - -#[derive(Eq, PartialEq, Clone, Hash, Debug)] -pub struct LocalName { - pub name: Atom, - pub lower_name: Atom, -} - -#[derive(Eq, PartialEq, Clone, Hash, Debug)] -pub struct AttrSelector { - pub name: Atom, - pub lower_name: Atom, - pub namespace: NamespaceConstraint, -} - -#[derive(Eq, PartialEq, Clone, Hash, Debug)] -pub enum NamespaceConstraint { - Any, - Specific(Namespace), -} - - -fn compute_specificity(mut selector: &CompoundSelector, - pseudo_element: &Option<PseudoElement>) -> u32 { - struct Specificity { - id_selectors: u32, - class_like_selectors: u32, - element_selectors: u32, - } - let mut specificity = Specificity { - id_selectors: 0, - class_like_selectors: 0, - element_selectors: 0, - }; - if pseudo_element.is_some() { specificity.element_selectors += 1 } - - simple_selectors_specificity(&selector.simple_selectors, &mut specificity); - loop { - match selector.next { - None => break, - Some((ref next_selector, _)) => { - selector = &**next_selector; - simple_selectors_specificity(&selector.simple_selectors, &mut specificity) - } - } - } - - fn simple_selectors_specificity(simple_selectors: &[SimpleSelector], - specificity: &mut Specificity) { - for simple_selector in simple_selectors.iter() { - match simple_selector { - &SimpleSelector::LocalName(..) => - specificity.element_selectors += 1, - &SimpleSelector::ID(..) => - specificity.id_selectors += 1, - &SimpleSelector::Class(..) | - &SimpleSelector::AttrExists(..) | - &SimpleSelector::AttrEqual(..) | - &SimpleSelector::AttrIncludes(..) | - &SimpleSelector::AttrDashMatch(..) | - &SimpleSelector::AttrPrefixMatch(..) | - &SimpleSelector::AttrSubstringMatch(..) | - &SimpleSelector::AttrSuffixMatch(..) | - &SimpleSelector::AnyLink | &SimpleSelector::Link | - &SimpleSelector::Visited | &SimpleSelector::Hover | - &SimpleSelector::Disabled | &SimpleSelector::Enabled | - &SimpleSelector::FirstChild | &SimpleSelector::LastChild | - &SimpleSelector::OnlyChild | &SimpleSelector::Root | - &SimpleSelector::Checked | - &SimpleSelector::Indeterminate | - &SimpleSelector::NthChild(..) | - &SimpleSelector::NthLastChild(..) | - &SimpleSelector::NthOfType(..) | - &SimpleSelector::NthLastOfType(..) | - &SimpleSelector::FirstOfType | &SimpleSelector::LastOfType | - &SimpleSelector::OnlyOfType | - &SimpleSelector::ServoNonzeroBorder => - specificity.class_like_selectors += 1, - &SimpleSelector::Namespace(..) => (), - &SimpleSelector::Negation(ref negated) => - simple_selectors_specificity(negated, specificity), - } - } - } - - static MAX_10BIT: u32 = (1u32 << 10) - 1; - cmp::min(specificity.id_selectors, MAX_10BIT) << 20 - | cmp::min(specificity.class_like_selectors, MAX_10BIT) << 10 - | cmp::min(specificity.element_selectors, MAX_10BIT) -} - - - -pub fn parse_author_origin_selector_list_from_str(input: &str) -> Result<Vec<Selector>, ()> { - let url = Url::parse("about:blank").unwrap(); - let context = ParserContext::new(Origin::Author, &url); - parse_selector_list(&context, &mut Parser::new(input)) -} - -/// Parse a comma-separated list of Selectors. -/// aka Selector Group in http://www.w3.org/TR/css3-selectors/#grouping -/// -/// Return the Selectors or None if there is an invalid selector. -pub fn parse_selector_list(context: &ParserContext, input: &mut Parser) - -> Result<Vec<Selector>,()> { - input.parse_comma_separated(|input| parse_selector(context, input)) -} - - -/// Build up a Selector. -/// selector : simple_selector_sequence [ combinator simple_selector_sequence ]* ; -/// -/// `Err` means invalid selector. -fn parse_selector(context: &ParserContext, input: &mut Parser) -> Result<Selector,()> { - let (first, mut pseudo_element) = try!(parse_simple_selectors(context, input)); - let mut compound = CompoundSelector{ simple_selectors: first, next: None }; - - 'outer_loop: while pseudo_element.is_none() { - let combinator; - let mut any_whitespace = false; - loop { - let position = input.position(); - match input.next_including_whitespace() { - Err(()) => break 'outer_loop, - Ok(Token::WhiteSpace(_)) => any_whitespace = true, - Ok(Token::Delim('>')) => { - combinator = Combinator::Child; - break - } - Ok(Token::Delim('+')) => { - combinator = Combinator::NextSibling; - break - } - Ok(Token::Delim('~')) => { - combinator = Combinator::LaterSibling; - break - } - Ok(_) => { - input.reset(position); - if any_whitespace { - combinator = Combinator::Descendant; - break - } else { - break 'outer_loop - } - } - } - } - let (simple_selectors, pseudo) = try!(parse_simple_selectors(context, input)); - compound = CompoundSelector { - simple_selectors: simple_selectors, - next: Some((box compound, combinator)) - }; - pseudo_element = pseudo; - } - Ok(Selector { - specificity: compute_specificity(&compound, &pseudo_element), - compound_selectors: Arc::new(compound), - pseudo_element: pseudo_element, - }) -} - - -/// * `Err(())`: Invalid selector, abort -/// * `Ok(None)`: Not a type selector, could be something else. `input` was not consumed. -/// * `Ok(Some(vec))`: Length 0 (`*|*`), 1 (`*|E` or `ns|*`) or 2 (`|E` or `ns|E`) -fn parse_type_selector(context: &ParserContext, input: &mut Parser) - -> Result<Option<Vec<SimpleSelector>>, ()> { - match try!(parse_qualified_name(context, input, /* in_attr_selector = */ false)) { - None => Ok(None), - Some((namespace, local_name)) => { - let mut simple_selectors = vec!(); - match namespace { - NamespaceConstraint::Specific(ns) => { - simple_selectors.push(SimpleSelector::Namespace(ns)) - }, - NamespaceConstraint::Any => (), - } - match local_name { - Some(name) => { - simple_selectors.push(SimpleSelector::LocalName(LocalName { - name: Atom::from_slice(&name), - lower_name: Atom::from_slice(&name.into_owned().into_ascii_lowercase()) - })) - } - None => (), - } - Ok(Some(simple_selectors)) - } - } -} - - -#[derive(Debug)] -enum SimpleSelectorParseResult { - SimpleSelector(SimpleSelector), - PseudoElement(PseudoElement), -} - - -/// * `Err(())`: Invalid selector, abort -/// * `Ok(None)`: Not a simple selector, could be something else. `input` was not consumed. -/// * `Ok(Some((namespace, local_name)))`: `None` for the local name means a `*` universal selector -fn parse_qualified_name<'i, 't> - (context: &ParserContext, input: &mut Parser<'i, 't>, - in_attr_selector: bool) - -> Result<Option<(NamespaceConstraint, Option<CowString<'i>>)>, ()> { - let default_namespace = |local_name| { - let namespace = match context.namespaces.default { - Some(ref ns) => NamespaceConstraint::Specific(ns.clone()), - None => NamespaceConstraint::Any, - }; - Ok(Some((namespace, local_name))) - }; - - let explicit_namespace = |input: &mut Parser<'i, 't>, namespace| { - match input.next_including_whitespace() { - Ok(Token::Delim('*')) if !in_attr_selector => { - Ok(Some((namespace, None))) - }, - Ok(Token::Ident(local_name)) => { - Ok(Some((namespace, Some(local_name)))) - }, - _ => Err(()), - } - }; - - let position = input.position(); - match input.next_including_whitespace() { - Ok(Token::Ident(value)) => { - let position = input.position(); - match input.next_including_whitespace() { - Ok(Token::Delim('|')) => { - let result = context.namespaces.prefix_map.get(&*value); - let namespace = try!(result.ok_or(())); - explicit_namespace(input, NamespaceConstraint::Specific(namespace.clone())) - }, - _ => { - input.reset(position); - if in_attr_selector { - Ok(Some((NamespaceConstraint::Specific(ns!("")), Some(value)))) - } else { - default_namespace(Some(value)) - } - } - } - }, - Ok(Token::Delim('*')) => { - let position = input.position(); - match input.next_including_whitespace() { - Ok(Token::Delim('|')) => explicit_namespace(input, NamespaceConstraint::Any), - _ => { - input.reset(position); - if in_attr_selector { - Err(()) - } else { - default_namespace(None) - } - }, - } - }, - Ok(Token::Delim('|')) => explicit_namespace(input, NamespaceConstraint::Specific(ns!(""))), - _ => { - input.reset(position); - Ok(None) - } - } -} - - -fn parse_attribute_selector(context: &ParserContext, input: &mut Parser) - -> Result<SimpleSelector, ()> { - let attr = match try!(parse_qualified_name(context, input, /* in_attr_selector = */ true)) { - None => return Err(()), - Some((_, None)) => unreachable!(), - Some((namespace, Some(local_name))) => AttrSelector { - namespace: namespace, - lower_name: Atom::from_slice(&local_name.to_ascii_lowercase()), - name: Atom::from_slice(&local_name), - }, - }; - - fn parse_value(input: &mut Parser) -> Result<String, ()> { - Ok((try!(input.expect_ident_or_string())).into_owned()) - } - // TODO: deal with empty value or value containing whitespace (see spec) - match input.next() { - // [foo] - Err(()) => Ok(SimpleSelector::AttrExists(attr)), - - // [foo=bar] - Ok(Token::Delim('=')) => { - Ok(SimpleSelector::AttrEqual(attr, try!(parse_value(input)), - try!(parse_attribute_flags(input)))) - } - // [foo~=bar] - Ok(Token::IncludeMatch) => { - Ok(SimpleSelector::AttrIncludes(attr, try!(parse_value(input)))) - } - // [foo|=bar] - Ok(Token::DashMatch) => { - let value = try!(parse_value(input)); - let dashing_value = format!("{:?}-", value); - Ok(SimpleSelector::AttrDashMatch(attr, value, dashing_value)) - } - // [foo^=bar] - Ok(Token::PrefixMatch) => { - Ok(SimpleSelector::AttrPrefixMatch(attr, try!(parse_value(input)))) - } - // [foo*=bar] - Ok(Token::SubstringMatch) => { - Ok(SimpleSelector::AttrSubstringMatch(attr, try!(parse_value(input)))) - } - // [foo$=bar] - Ok(Token::SuffixMatch) => { - Ok(SimpleSelector::AttrSuffixMatch(attr, try!(parse_value(input)))) - } - _ => Err(()) - } -} - - -fn parse_attribute_flags(input: &mut Parser) -> Result<CaseSensitivity, ()> { - match input.next() { - Err(()) => Ok(CaseSensitivity::CaseSensitive), - Ok(Token::Ident(ref value)) if value.eq_ignore_ascii_case("i") => { - Ok(CaseSensitivity::CaseInsensitive) - } - _ => Err(()) - } -} - - -/// Level 3: Parse **one** simple_selector -fn parse_negation(context: &ParserContext, input: &mut Parser) -> Result<SimpleSelector,()> { - match try!(parse_type_selector(context, input)) { - Some(type_selector) => Ok(SimpleSelector::Negation(type_selector)), - None => { - match try!(parse_one_simple_selector(context, - input, - /* inside_negation = */ true)) { - Some(SimpleSelectorParseResult::SimpleSelector(simple_selector)) => { - Ok(SimpleSelector::Negation(vec![simple_selector])) - } - _ => Err(()) - } - }, - } -} - -/// simple_selector_sequence -/// : [ type_selector | universal ] [ HASH | class | attrib | pseudo | negation ]* -/// | [ HASH | class | attrib | pseudo | negation ]+ -/// -/// `Err(())` means invalid selector -fn parse_simple_selectors(context: &ParserContext, input: &mut Parser) - -> Result<(Vec<SimpleSelector>, Option<PseudoElement>),()> { - // Consume any leading whitespace. - loop { - let position = input.position(); - if !matches!(input.next_including_whitespace(), Ok(Token::WhiteSpace(_))) { - input.reset(position); - break - } - } - let mut empty = true; - let mut simple_selectors = match try!(parse_type_selector(context, input)) { - None => vec![], - Some(s) => { empty = false; s } - }; - - let mut pseudo_element = None; - loop { - match try!(parse_one_simple_selector(context, - input, - /* inside_negation = */ false)) { - None => break, - Some(SimpleSelectorParseResult::SimpleSelector(s)) => { - simple_selectors.push(s); - empty = false - } - Some(SimpleSelectorParseResult::PseudoElement(p)) => { - pseudo_element = Some(p); - empty = false; - break - } - } - } - if empty { - // An empty selector is invalid. - Err(()) - } else { - Ok((simple_selectors, pseudo_element)) - } -} - -fn parse_functional_pseudo_class(context: &ParserContext, - input: &mut Parser, - name: &str, - inside_negation: bool) - -> Result<SimpleSelector,()> { - match_ignore_ascii_case! { name, - "nth-child" => parse_nth_pseudo_class(input, SimpleSelector::NthChild), - "nth-of-type" => parse_nth_pseudo_class(input, SimpleSelector::NthOfType), - "nth-last-child" => parse_nth_pseudo_class(input, SimpleSelector::NthLastChild), - "nth-last-of-type" => parse_nth_pseudo_class(input, SimpleSelector::NthLastOfType), - "not" => { - if inside_negation { - Err(()) - } else { - parse_negation(context, input) - } - } - _ => Err(()) - } -} - - -fn parse_nth_pseudo_class<F>(input: &mut Parser, selector: F) -> Result<SimpleSelector, ()> -where F: FnOnce(i32, i32) -> SimpleSelector { - let (a, b) = try!(parse_nth(input)); - Ok(selector(a, b)) -} - - -/// Parse a simple selector other than a type selector. -/// -/// * `Err(())`: Invalid selector, abort -/// * `Ok(None)`: Not a simple selector, could be something else. `input` was not consumed. -/// * `Ok(Some(_))`: Parsed a simple selector or pseudo-element -fn parse_one_simple_selector(context: &ParserContext, - input: &mut Parser, - inside_negation: bool) - -> Result<Option<SimpleSelectorParseResult>,()> { - let start_position = input.position(); - match input.next_including_whitespace() { - Ok(Token::IDHash(id)) => { - let id = SimpleSelector::ID(Atom::from_slice(&id)); - Ok(Some(SimpleSelectorParseResult::SimpleSelector(id))) - } - Ok(Token::Delim('.')) => { - match input.next_including_whitespace() { - Ok(Token::Ident(class)) => { - let class = SimpleSelector::Class(Atom::from_slice(&class)); - Ok(Some(SimpleSelectorParseResult::SimpleSelector(class))) - } - _ => Err(()), - } - } - Ok(Token::SquareBracketBlock) => { - let attr = try!(input.parse_nested_block(|input| { - parse_attribute_selector(context, input) - })); - Ok(Some(SimpleSelectorParseResult::SimpleSelector(attr))) - } - Ok(Token::Colon) => { - match input.next_including_whitespace() { - Ok(Token::Ident(name)) => { - match parse_simple_pseudo_class(context, &name) { - Err(()) => { - let pseudo_element = match_ignore_ascii_case! { name, - // Supported CSS 2.1 pseudo-elements only. - // ** Do not add to this list! ** - "before" => PseudoElement::Before, - "after" => PseudoElement::After, - "first-line" => return Err(()), - "first-letter" => return Err(()) - _ => return Err(()) - }; - Ok(Some(SimpleSelectorParseResult::PseudoElement(pseudo_element))) - }, - Ok(result) => Ok(Some(SimpleSelectorParseResult::SimpleSelector(result))), - } - } - Ok(Token::Function(name)) => { - let pseudo = try!(input.parse_nested_block(|input| { - parse_functional_pseudo_class(context, input, &name, inside_negation) - })); - Ok(Some(SimpleSelectorParseResult::SimpleSelector(pseudo))) - } - Ok(Token::Colon) => { - match input.next() { - Ok(Token::Ident(name)) => { - let pseudo = try!(parse_pseudo_element(&name)); - Ok(Some(SimpleSelectorParseResult::PseudoElement(pseudo))) - } - _ => Err(()) - } - } - _ => Err(()) - } - } - _ => { - input.reset(start_position); - Ok(None) - } - } -} - -fn parse_simple_pseudo_class(context: &ParserContext, name: &str) -> Result<SimpleSelector,()> { - match_ignore_ascii_case! { name, - "any-link" => Ok(SimpleSelector::AnyLink), - "link" => Ok(SimpleSelector::Link), - "visited" => Ok(SimpleSelector::Visited), - "hover" => Ok(SimpleSelector::Hover), - "disabled" => Ok(SimpleSelector::Disabled), - "enabled" => Ok(SimpleSelector::Enabled), - "checked" => Ok(SimpleSelector::Checked), - "indeterminate" => Ok(SimpleSelector::Indeterminate), - "first-child" => Ok(SimpleSelector::FirstChild), - "last-child" => Ok(SimpleSelector::LastChild), - "only-child" => Ok(SimpleSelector::OnlyChild), - "root" => Ok(SimpleSelector::Root), - "first-of-type" => Ok(SimpleSelector::FirstOfType), - "last-of-type" => Ok(SimpleSelector::LastOfType), - "only-of-type" => Ok(SimpleSelector::OnlyOfType), - "-servo-nonzero-border" => { - if context.in_user_agent_stylesheet() { - Ok(SimpleSelector::ServoNonzeroBorder) - } else { - Err(()) - } - } - _ => Err(()) - } -} - -fn parse_pseudo_element(name: &str) -> Result<PseudoElement, ()> { - match_ignore_ascii_case! { name, - "before" => Ok(PseudoElement::Before), - "after" => Ok(PseudoElement::After) - _ => Err(()) - } -} - - -#[cfg(test)] -mod tests { - use std::sync::Arc; - use cssparser::Parser; - use stylesheets::Origin; - use string_cache::Atom; - use parser::ParserContext; - use url::Url; - use super::*; - - fn parse(input: &str) -> Result<Vec<Selector>, ()> { - parse_ns(input, &ParserContext::new(Origin::Author, &Url::parse("about:blank").unwrap())) - } - - fn parse_ns(input: &str, context: &ParserContext) -> Result<Vec<Selector>, ()> { - parse_selector_list(context, &mut Parser::new(input)) - } - - fn specificity(a: u32, b: u32, c: u32) -> u32 { - a << 20 | b << 10 | c - } - - #[test] - fn test_parsing() { - assert_eq!(parse(""), Err(())) ; - assert_eq!(parse("EeÉ"), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(SimpleSelector::LocalName(LocalName { - name: Atom::from_slice("EeÉ"), - lower_name: Atom::from_slice("eeÉ") })), - next: None, - }), - pseudo_element: None, - specificity: specificity(0, 0, 1), - }))); - assert_eq!(parse(".foo"), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(SimpleSelector::Class(Atom::from_slice("foo"))), - next: None, - }), - pseudo_element: None, - specificity: specificity(0, 1, 0), - }))); - assert_eq!(parse("#bar"), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(SimpleSelector::ID(Atom::from_slice("bar"))), - next: None, - }), - pseudo_element: None, - specificity: specificity(1, 0, 0), - }))); - assert_eq!(parse("e.foo#bar"), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(SimpleSelector::LocalName(LocalName { - name: Atom::from_slice("e"), - lower_name: Atom::from_slice("e") }), - SimpleSelector::Class(Atom::from_slice("foo")), - SimpleSelector::ID(Atom::from_slice("bar"))), - next: None, - }), - pseudo_element: None, - specificity: specificity(1, 1, 1), - }))); - assert_eq!(parse("e.foo #bar"), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(SimpleSelector::ID(Atom::from_slice("bar"))), - next: Some((box CompoundSelector { - simple_selectors: vec!(SimpleSelector::LocalName(LocalName { - name: Atom::from_slice("e"), - lower_name: Atom::from_slice("e") }), - SimpleSelector::Class(Atom::from_slice("foo"))), - next: None, - }, Combinator::Descendant)), - }), - pseudo_element: None, - specificity: specificity(1, 1, 1), - }))); - // Default namespace does not apply to attribute selectors - // https://github.com/mozilla/servo/pull/1652 - let url = Url::parse("about:blank").unwrap(); - let mut context = ParserContext::new(Origin::Author, &url); - assert_eq!(parse_ns("[Foo]", &context), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(SimpleSelector::AttrExists(AttrSelector { - name: Atom::from_slice("Foo"), - lower_name: Atom::from_slice("foo"), - namespace: NamespaceConstraint::Specific(ns!("")), - })), - next: None, - }), - pseudo_element: None, - specificity: specificity(0, 1, 0), - }))); - // Default namespace does not apply to attribute selectors - // https://github.com/mozilla/servo/pull/1652 - context.namespaces.default = Some(ns!(MathML)); - assert_eq!(parse_ns("[Foo]", &context), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(SimpleSelector::AttrExists(AttrSelector { - name: Atom::from_slice("Foo"), - lower_name: Atom::from_slice("foo"), - namespace: NamespaceConstraint::Specific(ns!("")), - })), - next: None, - }), - pseudo_element: None, - specificity: specificity(0, 1, 0), - }))); - // Default namespace does apply to type selectors - assert_eq!(parse_ns("e", &context), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!( - SimpleSelector::Namespace(ns!(MathML)), - SimpleSelector::LocalName(LocalName { - name: Atom::from_slice("e"), - lower_name: Atom::from_slice("e") }), - ), - next: None, - }), - pseudo_element: None, - specificity: specificity(0, 0, 1), - }))); - // https://github.com/mozilla/servo/issues/1723 - assert_eq!(parse("::before"), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(), - next: None, - }), - pseudo_element: Some(PseudoElement::Before), - specificity: specificity(0, 0, 1), - }))); - assert_eq!(parse("div :after"), Ok(vec!(Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec!(), - next: Some((box CompoundSelector { - simple_selectors: vec!(SimpleSelector::LocalName(LocalName { - name: atom!("div"), - lower_name: atom!("div") })), - next: None, - }, Combinator::Descendant)), - }), - pseudo_element: Some(PseudoElement::After), - specificity: specificity(0, 0, 2), - }))); - assert_eq!(parse("#d1 > .ok"), Ok(vec![Selector { - compound_selectors: Arc::new(CompoundSelector { - simple_selectors: vec![ - SimpleSelector::Class(Atom::from_slice("ok")), - ], - next: Some((box CompoundSelector { - simple_selectors: vec![ - SimpleSelector::ID(Atom::from_slice("d1")), - ], - next: None, - }, Combinator::Child)), - }), - pseudo_element: None, - specificity: (1 << 20) + (1 << 10) + (0 << 0), - }])) - } -} diff --git a/components/style/stylesheets.rs b/components/style/stylesheets.rs index 05bef7c87ba..c0efee55d8a 100644 --- a/components/style/stylesheets.rs +++ b/components/style/stylesheets.rs @@ -12,7 +12,7 @@ use encoding::EncodingRef; use cssparser::{Parser, decode_stylesheet_bytes, QualifiedRuleParser, AtRuleParser, RuleListParser, AtRuleType}; use string_cache::{Atom, Namespace}; -use selectors::{Selector, parse_selector_list}; +use selectors::parser::{Selector, parse_selector_list}; use parser::{ParserContext, log_css_error}; use properties::{PropertyDeclarationBlock, parse_property_declaration_list}; use media_queries::{self, Device, MediaQueryList, parse_media_query_list}; @@ -97,10 +97,11 @@ impl Stylesheet { Ok(rule) => { if let CSSRule::Namespace(ref prefix, ref namespace) = rule { if let Some(prefix) = prefix.as_ref() { - iter.parser.context.namespaces.prefix_map.insert( + iter.parser.context.selector_context.namespace_prefixes.insert( prefix.clone(), namespace.clone()); } else { - iter.parser.context.namespaces.default = Some(namespace.clone()); + iter.parser.context.selector_context.default_namespace = + Some(namespace.clone()); } } rules.push(rule); @@ -270,7 +271,7 @@ impl<'a, 'b> QualifiedRuleParser for NestedRuleParser<'a, 'b> { type QualifiedRule = CSSRule; fn parse_prelude(&self, input: &mut Parser) -> Result<Vec<Selector>, ()> { - parse_selector_list(self.context, input) + parse_selector_list(&self.context.selector_context, input) } fn parse_block(&self, prelude: Vec<Selector>, input: &mut Parser) -> Result<CSSRule, ()> { @@ -327,7 +328,7 @@ pub fn iter_font_face_rules<F>(stylesheet: &Stylesheet, device: &Device, fn test_parse_stylesheet() { use std::sync::Arc; use cssparser; - use selectors::*; + use selectors::parser::*; use string_cache::Atom; use properties::{PropertyDeclaration, DeclaredValue, longhands}; use std::borrow::ToOwned; diff --git a/components/util/Cargo.toml b/components/util/Cargo.toml index 1ce8a396942..53893d88614 100644 --- a/components/util/Cargo.toml +++ b/components/util/Cargo.toml @@ -21,6 +21,9 @@ path = "../plugins" [dependencies.cssparser] git = "https://github.com/servo/rust-cssparser" +[dependencies.selectors] +git = "https://github.com/servo/rust-selectors" + [dependencies.geom] git = "https://github.com/servo/rust-geom" diff --git a/components/util/bloom.rs b/components/util/bloom.rs deleted file mode 100644 index 6fd5d17e775..00000000000 --- a/components/util/bloom.rs +++ /dev/null @@ -1,338 +0,0 @@ -/* 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/. */ - -//! Simple counting bloom filters. - -use string_cache::{Atom, Namespace}; - -const KEY_SIZE: uint = 12; -const ARRAY_SIZE: uint = 1 << KEY_SIZE; -const KEY_MASK: u32 = (1 << KEY_SIZE) - 1; -const KEY_SHIFT: uint = 16; - -/// A counting Bloom filter with 8-bit counters. For now we assume -/// that having two hash functions is enough, but we may revisit that -/// decision later. -/// -/// The filter uses an array with 2**KeySize entries. -/// -/// Assuming a well-distributed hash function, a Bloom filter with -/// array size M containing N elements and -/// using k hash function has expected false positive rate exactly -/// -/// $ (1 - (1 - 1/M)^{kN})^k $ -/// -/// because each array slot has a -/// -/// $ (1 - 1/M)^{kN} $ -/// -/// chance of being 0, and the expected false positive rate is the -/// probability that all of the k hash functions will hit a nonzero -/// slot. -/// -/// For reasonable assumptions (M large, kN large, which should both -/// hold if we're worried about false positives) about M and kN this -/// becomes approximately -/// -/// $$ (1 - \exp(-kN/M))^k $$ -/// -/// For our special case of k == 2, that's $(1 - \exp(-2N/M))^2$, -/// or in other words -/// -/// $$ N/M = -0.5 * \ln(1 - \sqrt(r)) $$ -/// -/// where r is the false positive rate. This can be used to compute -/// the desired KeySize for a given load N and false positive rate r. -/// -/// If N/M is assumed small, then the false positive rate can -/// further be approximated as 4*N^2/M^2. So increasing KeySize by -/// 1, which doubles M, reduces the false positive rate by about a -/// factor of 4, and a false positive rate of 1% corresponds to -/// about M/N == 20. -/// -/// What this means in practice is that for a few hundred keys using a -/// KeySize of 12 gives false positive rates on the order of 0.25-4%. -/// -/// Similarly, using a KeySize of 10 would lead to a 4% false -/// positive rate for N == 100 and to quite bad false positive -/// rates for larger N. -pub struct BloomFilter { - counters: [u8; ARRAY_SIZE], -} - -impl Clone for BloomFilter { - #[inline] - fn clone(&self) -> BloomFilter { - BloomFilter { - counters: self.counters, - } - } -} - -impl BloomFilter { - /// Creates a new bloom filter. - #[inline] - pub fn new() -> BloomFilter { - BloomFilter { - counters: [0; ARRAY_SIZE], - } - } - - #[inline] - fn first_slot(&self, hash: u32) -> &u8 { - &self.counters[hash1(hash) as uint] - } - - #[inline] - fn first_mut_slot(&mut self, hash: u32) -> &mut u8 { - &mut self.counters[hash1(hash) as uint] - } - - #[inline] - fn second_slot(&self, hash: u32) -> &u8 { - &self.counters[hash2(hash) as uint] - } - - #[inline] - fn second_mut_slot(&mut self, hash: u32) -> &mut u8 { - &mut self.counters[hash2(hash) as uint] - } - - #[inline] - pub fn clear(&mut self) { - self.counters = [0; ARRAY_SIZE] - } - - #[inline] - fn insert_hash(&mut self, hash: u32) { - { - let slot1 = self.first_mut_slot(hash); - if !full(slot1) { - *slot1 += 1 - } - } - { - let slot2 = self.second_mut_slot(hash); - if !full(slot2) { - *slot2 += 1 - } - } - } - - /// Inserts an item into the bloom filter. - #[inline] - pub fn insert<T:BloomHash>(&mut self, elem: &T) { - self.insert_hash(elem.bloom_hash()) - - } - - #[inline] - fn remove_hash(&mut self, hash: u32) { - { - let slot1 = self.first_mut_slot(hash); - if !full(slot1) { - *slot1 -= 1 - } - } - { - let slot2 = self.second_mut_slot(hash); - if !full(slot2) { - *slot2 -= 1 - } - } - } - - /// Removes an item from the bloom filter. - #[inline] - pub fn remove<T:BloomHash>(&mut self, elem: &T) { - self.remove_hash(elem.bloom_hash()) - } - - #[inline] - fn might_contain_hash(&self, hash: u32) -> bool { - *self.first_slot(hash) != 0 && *self.second_slot(hash) != 0 - } - - /// Check whether the filter might contain an item. This can - /// sometimes return true even if the item is not in the filter, - /// but will never return false for items that are actually in the - /// filter. - #[inline] - pub fn might_contain<T:BloomHash>(&self, elem: &T) -> bool { - self.might_contain_hash(elem.bloom_hash()) - } -} - -pub trait BloomHash { - fn bloom_hash(&self) -> u32; -} - -impl BloomHash for int { - #[allow(exceeding_bitshifts)] - #[inline] - fn bloom_hash(&self) -> u32 { - ((*self >> 32) ^ *self) as u32 - } -} - -impl BloomHash for uint { - #[allow(exceeding_bitshifts)] - #[inline] - fn bloom_hash(&self) -> u32 { - ((*self >> 32) ^ *self) as u32 - } -} - -impl BloomHash for Atom { - #[inline] - fn bloom_hash(&self) -> u32 { - ((self.data >> 32) ^ self.data) as u32 - } -} - -impl BloomHash for Namespace { - #[inline] - fn bloom_hash(&self) -> u32 { - let Namespace(ref atom) = *self; - atom.bloom_hash() - } -} - -#[inline] -fn full(slot: &u8) -> bool { - *slot == 0xff -} - -#[inline] -fn hash1(hash: u32) -> u32 { - hash & KEY_MASK -} - -#[inline] -fn hash2(hash: u32) -> u32 { - (hash >> KEY_SHIFT) & KEY_MASK -} - -#[test] -fn create_and_insert_some_stuff() { - use std::iter::range; - - let mut bf = BloomFilter::new(); - - for i in range(0u, 1000) { - bf.insert(&i); - } - - for i in range(0u, 1000) { - assert!(bf.might_contain(&i)); - } - - let false_positives = - range(1001u, 2000).filter(|i| bf.might_contain(i)).count(); - - assert!(false_positives < 10); // 1%. - - for i in range(0u, 100) { - bf.remove(&i); - } - - for i in range(100u, 1000) { - assert!(bf.might_contain(&i)); - } - - let false_positives = range(0u, 100).filter(|i| bf.might_contain(i)).count(); - - assert!(false_positives < 2); // 2%. - - bf.clear(); - - for i in range(0u, 2000) { - assert!(!bf.might_contain(&i)); - } -} - -#[cfg(test)] -mod bench { - extern crate test; - - use std::hash::{hash, SipHasher}; - use std::iter; - use super::BloomFilter; - - #[bench] - fn create_insert_1000_remove_100_lookup_100(b: &mut test::Bencher) { - b.iter(|| { - let mut bf = BloomFilter::new(); - for i in iter::range(0u, 1000) { - bf.insert(&i); - } - for i in iter::range(0u, 100) { - bf.remove(&i); - } - for i in iter::range(100u, 200) { - test::black_box(bf.might_contain(&i)); - } - }); - } - - #[bench] - fn might_contain(b: &mut test::Bencher) { - let mut bf = BloomFilter::new(); - - for i in iter::range(0u, 1000) { - bf.insert(&i); - } - - let mut i = 0u; - - b.bench_n(1000, |b| { - b.iter(|| { - test::black_box(bf.might_contain(&i)); - i += 1; - }); - }); - } - - #[bench] - fn insert(b: &mut test::Bencher) { - let mut bf = BloomFilter::new(); - - b.bench_n(1000, |b| { - let mut i = 0u; - - b.iter(|| { - test::black_box(bf.insert(&i)); - i += 1; - }); - }); - } - - #[bench] - fn remove(b: &mut test::Bencher) { - let mut bf = BloomFilter::new(); - for i in range(0u, 1000) { - bf.insert(&i); - } - - b.bench_n(1000, |b| { - let mut i = 0u; - - b.iter(|| { - bf.remove(&i); - i += 1; - }); - }); - - test::black_box(bf.might_contain(&0u)); - } - - #[bench] - fn hash_a_uint(b: &mut test::Bencher) { - let mut i = 0u; - b.iter(|| { - test::black_box(hash::<uint, SipHasher>(&i)); - i += 1; - }) - } -} diff --git a/components/util/lib.rs b/components/util/lib.rs index b6c1f938e66..c8df5466a68 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -37,6 +37,7 @@ extern crate "rustc-serialize" as rustc_serialize; extern crate task_info; extern crate "time" as std_time; extern crate text_writer; +extern crate selectors; extern crate string_cache; extern crate unicode; extern crate url; @@ -45,9 +46,10 @@ extern crate url; extern crate string_cache_macros; extern crate lazy_static; +pub use selectors::smallvec; + use std::sync::Arc; -pub mod bloom; pub mod cache; pub mod cursor; pub mod debug_utils; @@ -62,8 +64,6 @@ pub mod opts; pub mod persistent_list; pub mod range; pub mod resource_files; -pub mod smallvec; -pub mod sort; pub mod str; pub mod task; pub mod tid; diff --git a/components/util/smallvec.rs b/components/util/smallvec.rs deleted file mode 100644 index 14d983376e8..00000000000 --- a/components/util/smallvec.rs +++ /dev/null @@ -1,585 +0,0 @@ -/* 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/. */ - -//! Small vectors in various sizes. These store a certain number of elements inline and fall back -//! to the heap for larger allocations. - -use std::mem::zeroed as i; -use std::cmp; -use std::fmt; -use std::intrinsics; -use std::iter::FromIterator; -use std::marker::ContravariantLifetime; -use std::mem; -use std::ptr; -use std::raw::Slice; -use alloc::heap; - -// Generic code for all small vectors - -pub trait VecLike<T> { - fn vec_len(&self) -> uint; - fn vec_push(&mut self, value: T); - - fn vec_slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T]; - - #[inline] - fn vec_slice_from_mut<'a>(&'a mut self, start: uint) -> &'a mut [T] { - let len = self.vec_len(); - self.vec_slice_mut(start, len) - } -} - -impl<T> VecLike<T> for Vec<T> { - #[inline] - fn vec_len(&self) -> uint { - self.len() - } - - #[inline] - fn vec_push(&mut self, value: T) { - self.push(value); - } - - #[inline] - fn vec_slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T] { - &mut self[start..end] - } -} - -pub trait SmallVecPrivate<T> { - unsafe fn set_len(&mut self, new_len: uint); - unsafe fn set_cap(&mut self, new_cap: uint); - fn data(&self, index: uint) -> *const T; - fn mut_data(&mut self, index: uint) -> *mut T; - unsafe fn ptr(&self) -> *const T; - unsafe fn mut_ptr(&mut self) -> *mut T; - unsafe fn set_ptr(&mut self, new_ptr: *mut T); -} - -pub trait SmallVec<T> : SmallVecPrivate<T> { - fn inline_size(&self) -> uint; - fn len(&self) -> uint; - fn is_empty(&self) -> bool; - fn cap(&self) -> uint; - - fn spilled(&self) -> bool { - self.cap() > self.inline_size() - } - - fn begin(&self) -> *const T { - unsafe { - if self.spilled() { - self.ptr() - } else { - self.data(0) - } - } - } - - fn end(&self) -> *const T { - unsafe { - self.begin().offset(self.len() as int) - } - } - - fn iter<'a>(&'a self) -> SmallVecIterator<'a,T> { - SmallVecIterator { - ptr: self.begin(), - end: self.end(), - lifetime: ContravariantLifetime::<'a>, - } - } - - fn mut_iter<'a>(&'a mut self) -> SmallVecMutIterator<'a,T> { - unsafe { - SmallVecMutIterator { - ptr: mem::transmute(self.begin()), - end: mem::transmute(self.end()), - lifetime: ContravariantLifetime::<'a>, - } - } - } - - /// NB: For efficiency reasons (avoiding making a second copy of the inline elements), this - /// actually clears out the original array instead of moving it. - fn into_iter<'a>(&'a mut self) -> SmallVecMoveIterator<'a,T> { - unsafe { - let iter = mem::transmute(self.iter()); - let ptr_opt = if self.spilled() { - Some(mem::transmute(self.ptr())) - } else { - None - }; - let cap = self.cap(); - let inline_size = self.inline_size(); - self.set_cap(inline_size); - self.set_len(0); - SmallVecMoveIterator { - allocation: ptr_opt, - cap: cap, - iter: iter, - lifetime: ContravariantLifetime::<'a>, - } - } - } - - fn push(&mut self, value: T) { - let cap = self.cap(); - if self.len() == cap { - self.grow(cmp::max(cap * 2, 1)) - } - unsafe { - let end: &mut T = mem::transmute(self.end()); - ptr::write(end, value); - let len = self.len(); - self.set_len(len + 1) - } - } - - fn push_all_move<V:SmallVec<T>>(&mut self, mut other: V) { - for value in other.into_iter() { - self.push(value) - } - } - - fn pop(&mut self) -> Option<T> { - if self.len() == 0 { - return None - } - - unsafe { - let mut value: T = mem::uninitialized(); - let last_index = self.len() - 1; - - if (last_index as int) < 0 { - panic!("overflow") - } - let end_ptr = self.begin().offset(last_index as int); - - mem::swap(&mut value, mem::transmute::<*const T,&mut T>(end_ptr)); - self.set_len(last_index); - Some(value) - } - } - - fn grow(&mut self, new_cap: uint) { - unsafe { - let new_alloc: *mut T = mem::transmute(heap::allocate(mem::size_of::<T>() * - new_cap, - mem::min_align_of::<T>())); - ptr::copy_nonoverlapping_memory(new_alloc, self.begin(), self.len()); - - if self.spilled() { - heap::deallocate(self.mut_ptr() as *mut u8, - mem::size_of::<T>() * self.cap(), - mem::min_align_of::<T>()) - } else { - let mut_begin: *mut T = mem::transmute(self.begin()); - intrinsics::set_memory(mut_begin, 0, self.len()) - } - - self.set_ptr(new_alloc); - self.set_cap(new_cap) - } - } - - fn get<'a>(&'a self, index: uint) -> &'a T { - if index >= self.len() { - self.fail_bounds_check(index) - } - unsafe { - mem::transmute(self.begin().offset(index as int)) - } - } - - fn get_mut<'a>(&'a mut self, index: uint) -> &'a mut T { - if index >= self.len() { - self.fail_bounds_check(index) - } - unsafe { - mem::transmute(self.begin().offset(index as int)) - } - } - - fn slice<'a>(&'a self, start: uint, end: uint) -> &'a [T] { - assert!(start <= end); - assert!(end <= self.len()); - unsafe { - mem::transmute(Slice { - data: self.begin().offset(start as int), - len: (end - start) - }) - } - } - - fn as_slice<'a>(&'a self) -> &'a [T] { - self.slice(0, self.len()) - } - - fn as_slice_mut<'a>(&'a mut self) -> &'a mut [T] { - let len = self.len(); - self.slice_mut(0, len) - } - - fn slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T] { - assert!(start <= end); - assert!(end <= self.len()); - unsafe { - mem::transmute(Slice { - data: self.begin().offset(start as int), - len: (end - start) - }) - } - } - - fn slice_from_mut<'a>(&'a mut self, start: uint) -> &'a mut [T] { - let len = self.len(); - self.slice_mut(start, len) - } - - fn fail_bounds_check(&self, index: uint) { - panic!("index {} beyond length ({})", index, self.len()) - } -} - -pub struct SmallVecIterator<'a,T> { - ptr: *const T, - end: *const T, - lifetime: ContravariantLifetime<'a> -} - -impl<'a,T> Iterator for SmallVecIterator<'a,T> { - type Item = &'a T; - - #[inline] - fn next(&mut self) -> Option<&'a T> { - unsafe { - if self.ptr == self.end { - return None - } - let old = self.ptr; - self.ptr = if mem::size_of::<T>() == 0 { - mem::transmute(self.ptr as uint + 1) - } else { - self.ptr.offset(1) - }; - Some(mem::transmute(old)) - } - } -} - -pub struct SmallVecMutIterator<'a,T> { - ptr: *mut T, - end: *mut T, - lifetime: ContravariantLifetime<'a>, -} - -impl<'a,T> Iterator for SmallVecMutIterator<'a,T> { - type Item = &'a mut T; - - #[inline] - fn next(&mut self) -> Option<&'a mut T> { - unsafe { - if self.ptr == self.end { - return None - } - let old = self.ptr; - self.ptr = if mem::size_of::<T>() == 0 { - mem::transmute(self.ptr as uint + 1) - } else { - self.ptr.offset(1) - }; - Some(mem::transmute(old)) - } - } -} - -pub struct SmallVecMoveIterator<'a,T> { - allocation: Option<*mut u8>, - cap: uint, - iter: SmallVecIterator<'a,T>, - lifetime: ContravariantLifetime<'a>, -} - -impl<'a, T: 'a> Iterator for SmallVecMoveIterator<'a,T> { - type Item = T; - - #[inline] - fn next(&mut self) -> Option<T> { - unsafe { - match self.iter.next() { - None => None, - Some(reference) => { - // Zero out the values as we go so they don't get double-freed. - let reference: &mut T = mem::transmute(reference); - Some(mem::replace(reference, mem::zeroed())) - } - } - } - } -} - -#[unsafe_destructor] -impl<'a, T: 'a> Drop for SmallVecMoveIterator<'a,T> { - fn drop(&mut self) { - // Destroy the remaining elements. - for _ in self.by_ref() {} - - match self.allocation { - None => {} - Some(allocation) => { - unsafe { - heap::deallocate(allocation as *mut u8, - mem::size_of::<T>() * self.cap, - mem::min_align_of::<T>()) - } - } - } - } -} - -// Concrete implementations - -macro_rules! def_small_vector( - ($name:ident, $size:expr) => ( - pub struct $name<T> { - len: uint, - cap: uint, - ptr: *const T, - data: [T; $size], - } - - impl<T> SmallVecPrivate<T> for $name<T> { - unsafe fn set_len(&mut self, new_len: uint) { - self.len = new_len - } - unsafe fn set_cap(&mut self, new_cap: uint) { - self.cap = new_cap - } - fn data(&self, index: uint) -> *const T { - let ptr: *const T = &self.data[index]; - ptr - } - fn mut_data(&mut self, index: uint) -> *mut T { - let ptr: *mut T = &mut self.data[index]; - ptr - } - unsafe fn ptr(&self) -> *const T { - self.ptr - } - unsafe fn mut_ptr(&mut self) -> *mut T { - mem::transmute(self.ptr) - } - unsafe fn set_ptr(&mut self, new_ptr: *mut T) { - self.ptr = mem::transmute(new_ptr) - } - } - - impl<T> SmallVec<T> for $name<T> { - fn inline_size(&self) -> uint { - $size - } - fn len(&self) -> uint { - self.len - } - fn is_empty(&self) -> bool { - self.len == 0 - } - fn cap(&self) -> uint { - self.cap - } - } - - impl<T> VecLike<T> for $name<T> { - #[inline] - fn vec_len(&self) -> uint { - self.len() - } - - #[inline] - fn vec_push(&mut self, value: T) { - self.push(value); - } - - #[inline] - fn vec_slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T] { - self.slice_mut(start, end) - } - } - - impl<T> FromIterator<T> for $name<T> { - fn from_iter<I: Iterator<Item=T>>(iter: I) -> $name<T> { - let mut v = $name::new(); - - let (lower_size_bound, _) = iter.size_hint(); - - if lower_size_bound > v.cap() { - v.grow(lower_size_bound); - } - - for elem in iter { - v.push(elem); - } - - v - } - } - - impl<T> $name<T> { - pub fn extend<I: Iterator<Item=T>>(&mut self, iter: I) { - let (lower_size_bound, _) = iter.size_hint(); - - let target_len = self.len() + lower_size_bound; - - if target_len > self.cap() { - self.grow(target_len); - } - - for elem in iter { - self.push(elem); - } - } - } - - impl<T: fmt::Debug> fmt::Debug for $name<T> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self.as_slice()) - } - } - - impl<T> $name<T> { - #[inline] - pub fn new() -> $name<T> { - unsafe { - $name { - len: 0, - cap: $size, - ptr: ptr::null(), - data: mem::zeroed(), - } - } - } - } - ) -); - -def_small_vector!(SmallVec1, 1); -def_small_vector!(SmallVec2, 2); -def_small_vector!(SmallVec4, 4); -def_small_vector!(SmallVec8, 8); -def_small_vector!(SmallVec16, 16); -def_small_vector!(SmallVec24, 24); -def_small_vector!(SmallVec32, 32); - -macro_rules! def_small_vector_drop_impl( - ($name:ident, $size:expr) => ( - #[unsafe_destructor] - impl<T> Drop for $name<T> { - fn drop(&mut self) { - if !self.spilled() { - return - } - - unsafe { - let ptr = self.mut_ptr(); - for i in range(0, self.len()) { - *ptr.offset(i as int) = mem::uninitialized(); - } - - heap::deallocate(self.mut_ptr() as *mut u8, - mem::size_of::<T>() * self.cap(), - mem::min_align_of::<T>()) - } - } - } - ) -); - -def_small_vector_drop_impl!(SmallVec1, 1); -def_small_vector_drop_impl!(SmallVec2, 2); -def_small_vector_drop_impl!(SmallVec4, 4); -def_small_vector_drop_impl!(SmallVec8, 8); -def_small_vector_drop_impl!(SmallVec16, 16); -def_small_vector_drop_impl!(SmallVec24, 24); -def_small_vector_drop_impl!(SmallVec32, 32); - -macro_rules! def_small_vector_clone_impl( - ($name:ident) => ( - impl<T: Clone> Clone for $name<T> { - fn clone(&self) -> $name<T> { - let mut new_vector = $name::new(); - for element in self.iter() { - new_vector.push((*element).clone()) - } - new_vector - } - } - ) -); - -def_small_vector_clone_impl!(SmallVec1); -def_small_vector_clone_impl!(SmallVec2); -def_small_vector_clone_impl!(SmallVec4); -def_small_vector_clone_impl!(SmallVec8); -def_small_vector_clone_impl!(SmallVec16); -def_small_vector_clone_impl!(SmallVec24); -def_small_vector_clone_impl!(SmallVec32); - -#[cfg(test)] -pub mod tests { - use smallvec::{SmallVec, SmallVec2, SmallVec16}; - use std::borrow::ToOwned; - - // We heap allocate all these strings so that double frees will show up under valgrind. - - #[test] - pub fn test_inline() { - let mut v = SmallVec16::new(); - v.push("hello".to_owned()); - v.push("there".to_owned()); - assert_eq!(v.as_slice(), vec![ - "hello".to_owned(), - "there".to_owned(), - ].as_slice()); - } - - #[test] - pub fn test_spill() { - let mut v = SmallVec2::new(); - v.push("hello".to_owned()); - v.push("there".to_owned()); - v.push("burma".to_owned()); - v.push("shave".to_owned()); - assert_eq!(v.as_slice(), vec![ - "hello".to_owned(), - "there".to_owned(), - "burma".to_owned(), - "shave".to_owned(), - ].as_slice()); - } - - #[test] - pub fn test_double_spill() { - let mut v = SmallVec2::new(); - v.push("hello".to_owned()); - v.push("there".to_owned()); - v.push("burma".to_owned()); - v.push("shave".to_owned()); - v.push("hello".to_owned()); - v.push("there".to_owned()); - v.push("burma".to_owned()); - v.push("shave".to_owned()); - assert_eq!(v.as_slice(), vec![ - "hello".to_owned(), - "there".to_owned(), - "burma".to_owned(), - "shave".to_owned(), - "hello".to_owned(), - "there".to_owned(), - "burma".to_owned(), - "shave".to_owned(), - ].as_slice()); - } -} diff --git a/components/util/sort.rs b/components/util/sort.rs deleted file mode 100644 index ce7ab4d86c9..00000000000 --- a/components/util/sort.rs +++ /dev/null @@ -1,104 +0,0 @@ -/* 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/. */ - -//! In-place sorting. - -use std::cmp::Ordering; - -fn quicksort_helper<T>(arr: &mut [T], left: int, right: int, compare: fn(&T, &T) -> Ordering) { - if right <= left { - return - } - - let mut i: int = left - 1; - let mut j: int = right; - let mut p: int = i; - let mut q: int = j; - unsafe { - let v: *mut T = &mut arr[right as uint]; - loop { - i += 1; - while compare(&arr[i as uint], &*v) == Ordering::Less { - i += 1 - } - j -= 1; - while compare(&*v, &arr[j as uint]) == Ordering::Less { - if j == left { - break - } - j -= 1; - } - if i >= j { - break - } - arr.swap(i as uint, j as uint); - if compare(&arr[i as uint], &*v) == Ordering::Equal { - p += 1; - arr.swap(p as uint, i as uint) - } - if compare(&*v, &arr[j as uint]) == Ordering::Equal { - q -= 1; - arr.swap(j as uint, q as uint) - } - } - } - - arr.swap(i as uint, right as uint); - j = i - 1; - i += 1; - let mut k: int = left; - while k < p { - arr.swap(k as uint, j as uint); - k += 1; - j -= 1; - assert!(k < arr.len() as int); - } - k = right - 1; - while k > q { - arr.swap(i as uint, k as uint); - k -= 1; - i += 1; - assert!(k != 0); - } - - quicksort_helper(arr, left, j, compare); - quicksort_helper(arr, i, right, compare); -} - -/// An in-place quicksort. -/// -/// The algorithm is from Sedgewick and Bentley, "Quicksort is Optimal": -/// http://www.cs.princeton.edu/~rs/talks/QuicksortIsOptimal.pdf -pub fn quicksort_by<T>(arr: &mut [T], compare: fn(&T, &T) -> Ordering) { - if arr.len() <= 1 { - return - } - - let len = arr.len(); - quicksort_helper(arr, 0, (len - 1) as int, compare); -} - -#[cfg(test)] -pub mod test { - use std::cmp::Ordering; - use std::rand; - use std::rand::Rng; - - use sort; - - #[test] - pub fn random() { - let mut rng = rand::thread_rng(); - for _ in range(0u32, 50000u32) { - let len: uint = rng.gen(); - let mut v: Vec<int> = rng.gen_iter::<int>().take((len % 32) + 1).collect(); - fn compare_ints(a: &int, b: &int) -> Ordering { a.cmp(b) } - sort::quicksort_by(v.as_mut_slice(), compare_ints); - for i in range(0, v.len() - 1) { - assert!(v[i] <= v[i + 1]) - } - } - } -} - diff --git a/ports/cef/Cargo.lock b/ports/cef/Cargo.lock index 55145072105..9eb4b0e5b22 100644 --- a/ports/cef/Cargo.lock +++ b/ports/cef/Cargo.lock @@ -525,6 +525,7 @@ dependencies = [ "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "script 0.0.1", "script_traits 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "style 0.0.1", @@ -728,6 +729,7 @@ dependencies = [ "plugins 0.0.1", "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "script_traits 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "style 0.0.1", @@ -751,6 +753,18 @@ dependencies = [ ] [[package]] +name = "selectors" +version = "0.1.0" +source = "git+https://github.com/servo/rust-selectors#1e5bbf16eee3adeec1e911c7eaa8f2be02cd4c67" +dependencies = [ + "bitflags 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)", + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", + "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", +] + +[[package]] name = "servo" version = "0.0.1" dependencies = [ @@ -815,6 +829,7 @@ dependencies = [ "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "mod_path 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "text_writer 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -880,6 +895,7 @@ dependencies = [ "plugins 0.0.1", "rand 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "task_info 0.0.1", diff --git a/ports/gonk/Cargo.lock b/ports/gonk/Cargo.lock index a2cd11d3770..11a67183351 100644 --- a/ports/gonk/Cargo.lock +++ b/ports/gonk/Cargo.lock @@ -437,6 +437,7 @@ dependencies = [ "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "script 0.0.1", "script_traits 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "style 0.0.1", @@ -640,6 +641,7 @@ dependencies = [ "plugins 0.0.1", "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", "script_traits 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "style 0.0.1", @@ -663,6 +665,18 @@ dependencies = [ ] [[package]] +name = "selectors" +version = "0.1.0" +source = "git+https://github.com/servo/rust-selectors#1e5bbf16eee3adeec1e911c7eaa8f2be02cd4c67" +dependencies = [ + "bitflags 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "cssparser 0.2.0 (git+https://github.com/servo/rust-cssparser)", + "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", + "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", +] + +[[package]] name = "servo" version = "0.0.1" dependencies = [ @@ -726,6 +740,7 @@ dependencies = [ "matches 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "mod_path 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "plugins 0.0.1", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "text_writer 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -783,6 +798,7 @@ dependencies = [ "plugins 0.0.1", "rand 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", + "selectors 0.1.0 (git+https://github.com/servo/rust-selectors)", "string_cache 0.0.0 (git+https://github.com/servo/string-cache)", "string_cache_macros 0.0.0 (git+https://github.com/servo/string-cache)", "task_info 0.0.1", |