Auto merge of #12924 - servo:selectors-ser, r=nox

Update selectors to 0.10, with ToCss serialization.

<!-- Please describe your changes on the following line: -->
r? @emilio

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [x] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12924)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-08-19 13:01:29 -05:00 committed by GitHub
commit 07b217368f
22 changed files with 203 additions and 69 deletions

View file

@ -38,11 +38,11 @@ num-traits = "0.1.32"
ordered-float = "0.2.2"
rand = "0.3"
rustc-serialize = "0.3"
selectors = "0.9"
selectors = "0.10.1"
serde = {version = "0.8", optional = true}
serde_macros = {version = "0.8", optional = true}
smallvec = "0.1"
string_cache = {version = "0.2.23", features = ["heap_size"], optional = true}
string_cache = {version = "0.2.24", features = ["heap_size"], optional = true}
style_traits = {path = "../style_traits"}
time = "0.1"
url = "1.2"

View file

@ -2,10 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use cssparser::ToCss;
use element_state::ElementState;
use selector_impl::PseudoElementCascadeType;
use selector_impl::{attr_exists_selector_is_shareable, attr_equals_selector_is_shareable};
use selectors::parser::{ParserContext, SelectorImpl, AttrSelector};
use std::fmt;
use string_cache::{Atom, WeakAtom, Namespace, WeakNamespace};
use stylesheets::Stylesheet;
@ -93,6 +95,16 @@ impl PseudoElement {
}
}
impl ToCss for PseudoElement {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
// FIXME: why does the atom contain one colon? Pseudo-element has two
debug_assert!(self.0.as_slice().starts_with(&[b':' as u16]) &&
!self.0.as_slice().starts_with(&[b':' as u16, b':' as u16]));
try!(dest.write_char(':'));
write!(dest, "{}", self.0)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum NonTSPseudoClass {
AnyLink,
@ -109,6 +121,26 @@ pub enum NonTSPseudoClass {
ReadOnly,
}
impl ToCss for NonTSPseudoClass {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
use self::NonTSPseudoClass::*;
dest.write_str(match *self {
AnyLink => ":any-link",
Link => ":link",
Visited => ":visited",
Active => ":active",
Focus => ":focus",
Hover => ":hover",
Enabled => ":enabled",
Disabled => ":disabled",
Checked => ":checked",
Indeterminate => ":indeterminate",
ReadWrite => ":read-write",
ReadOnly => ":read-only",
})
}
}
impl NonTSPseudoClass {
pub fn state_flag(&self) -> ElementState {
use element_state::*;
@ -135,8 +167,9 @@ impl SelectorImpl for GeckoSelectorImpl {
type Identifier = Atom;
type ClassName = Atom;
type LocalName = Atom;
type Namespace = Namespace;
type BorrowedNamespace = WeakNamespace;
type NamespacePrefix = Atom;
type NamespaceUrl = Namespace;
type BorrowedNamespaceUrl = WeakNamespace;
type BorrowedLocalName = WeakAtom;
type PseudoElement = PseudoElement;

View file

@ -468,7 +468,7 @@ fn append_serialization<'a, W, I>(dest: &mut W,
return append_declaration_value(dest, appendable_value, is_important);
}
write!(dest, "{}:", property_name);
try!(write!(dest, "{}:", property_name));
// for normal parsed values, add a space between key: and value
match &appendable_value {

View file

@ -220,7 +220,7 @@ impl<'a, E> Element for ElementWrapper<'a, E>
self.element.get_local_name()
}
fn get_namespace(&self) -> &<Self::Impl as SelectorImpl>::BorrowedNamespace {
fn get_namespace(&self) -> &<Self::Impl as SelectorImpl>::BorrowedNamespaceUrl {
self.element.get_namespace()
}

View file

@ -3,6 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use attr::{AttrIdentifier, AttrValue};
use cssparser::ToCss;
use element_state::ElementState;
use error_reporting::StdoutErrorReporter;
use parser::ParserContextExtraData;
@ -11,6 +12,7 @@ use selector_impl::{ElementExt, PseudoElementCascadeType, TheSelectorImpl};
use selector_impl::{attr_exists_selector_is_shareable, attr_equals_selector_is_shareable};
use selectors::parser::{AttrSelector, ParserContext, SelectorImpl};
use selectors::{Element, MatchAttrGeneric};
use std::fmt;
use std::process;
use string_cache::{Atom, Namespace};
use stylesheets::{Stylesheet, Origin};
@ -28,6 +30,20 @@ pub enum PseudoElement {
DetailsContent,
}
impl ToCss for PseudoElement {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
use self::PseudoElement::*;
dest.write_str(match *self {
Before => "::before",
After => "::after",
Selection => "::selection",
DetailsSummary => "::-servo-details-summary",
DetailsContent => "::-servo-details-content",
})
}
}
impl PseudoElement {
#[inline]
pub fn is_before_or_after(&self) -> bool {
@ -70,6 +86,29 @@ pub enum NonTSPseudoClass {
Target,
}
impl ToCss for NonTSPseudoClass {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
use self::NonTSPseudoClass::*;
dest.write_str(match *self {
AnyLink => ":any-link",
Link => ":link",
Visited => ":visited",
Active => ":active",
Focus => ":focus",
Hover => ":hover",
Enabled => ":enabled",
Disabled => ":disabled",
Checked => ":checked",
Indeterminate => ":indeterminate",
ReadWrite => ":read-write",
ReadOnly => ":read-only",
PlaceholderShown => ":placeholder-shown",
Target => ":target",
ServoNonZeroBorder => ":-servo-nonzero-border",
})
}
}
impl NonTSPseudoClass {
pub fn state_flag(&self) -> ElementState {
use element_state::*;
@ -106,9 +145,10 @@ impl SelectorImpl for ServoSelectorImpl {
type Identifier = Atom;
type ClassName = Atom;
type LocalName = Atom;
type Namespace = Namespace;
type NamespacePrefix = Atom;
type NamespaceUrl = Namespace;
type BorrowedLocalName = Atom;
type BorrowedNamespace = Namespace;
type BorrowedNamespaceUrl = Namespace;
fn attr_exists_selector_is_shareable(attr_selector: &AttrSelector<Self>) -> bool {
attr_exists_selector_is_shareable(attr_selector)
@ -283,7 +323,7 @@ impl MatchAttrGeneric for ServoElementSnapshot {
let html = self.is_html_element_in_html_document;
let local_name = if html { &attr.lower_name } else { &attr.name };
match attr.namespace {
NamespaceConstraint::Specific(ref ns) => self.get_attr(ns, local_name),
NamespaceConstraint::Specific(ref ns) => self.get_attr(&ns.url, local_name),
NamespaceConstraint::Any => self.get_attr_ignore_ns(local_name),
}.map_or(false, |v| test(v))
}

View file

@ -58,7 +58,11 @@ pub struct Stylesheet {
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum CSSRule {
Charset(String),
Namespace(Option<String>, Namespace),
Namespace {
/// `None` for the default Namespace
prefix: Option<Atom>,
url: Namespace,
},
Style(StyleRule),
Media(MediaRule),
FontFace(FontFaceRule),
@ -143,13 +147,13 @@ impl Stylesheet {
while let Some(result) = iter.next() {
match result {
Ok(rule) => {
if let CSSRule::Namespace(ref prefix, ref namespace) = rule {
if let CSSRule::Namespace { ref prefix, ref url } = rule {
if let Some(prefix) = prefix.as_ref() {
iter.parser.context.selector_context.namespace_prefixes.insert(
prefix.clone(), namespace.clone());
prefix.clone(), url.clone());
} else {
iter.parser.context.selector_context.default_namespace =
Some(namespace.clone());
Some(url.clone());
}
}
@ -435,9 +439,12 @@ impl<'a> AtRuleParser for TopLevelRuleParser<'a> {
if self.state.get() <= State::Namespaces {
self.state.set(State::Namespaces);
let prefix = input.try(|input| input.expect_ident()).ok().map(|p| p.into_owned());
let prefix = input.try(|input| input.expect_ident()).ok().map(|p| p.into());
let url = Namespace(Atom::from(try!(input.expect_url_or_string())));
return Ok(AtRuleType::WithoutBlock(CSSRule::Namespace(prefix, url)))
return Ok(AtRuleType::WithoutBlock(CSSRule::Namespace {
prefix: prefix,
url: url,
}))
} else {
return Err(()) // "@namespace must be before any rule but @charset and @import"
}