servo: Merge #17215 - Derive more ToCss impls (from servo:derive-all-the-things); r=emilio

Source-Repo: https://github.com/servo/servo
Source-Revision: 1555f0fc413415d5c8f7c5a5f3fec2eecfce640e

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : 60a2001016256af7bd79d02c3b48df1d1b6e45cb
This commit is contained in:
Anthony Ramine 2017-06-09 05:00:45 -07:00
Родитель 54a89a2e9b
Коммит e497b77c77
15 изменённых файлов: 54 добавлений и 96 удалений

Просмотреть файл

@ -2853,8 +2853,8 @@ impl ServoComputedValuesCursorUtility for ServoComputedValues {
fn get_cursor(&self, default_cursor: Cursor) -> Option<Cursor> { fn get_cursor(&self, default_cursor: Cursor) -> Option<Cursor> {
match (self.get_pointing().pointer_events, self.get_pointing().cursor) { match (self.get_pointing().pointer_events, self.get_pointing().cursor) {
(pointer_events::T::none, _) => None, (pointer_events::T::none, _) => None,
(pointer_events::T::auto, cursor::Keyword::AutoCursor) => Some(default_cursor), (pointer_events::T::auto, cursor::Keyword::Auto) => Some(default_cursor),
(pointer_events::T::auto, cursor::Keyword::SpecifiedCursor(cursor)) => Some(cursor), (pointer_events::T::auto, cursor::Keyword::Cursor(cursor)) => Some(cursor),
} }
} }
} }

Просмотреть файл

@ -7,8 +7,8 @@
//! [counter-style]: https://drafts.csswg.org/css-counter-styles/ //! [counter-style]: https://drafts.csswg.org/css-counter-styles/
use Atom; use Atom;
use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser, Token}; use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser};
use cssparser::{serialize_string, serialize_identifier}; use cssparser::{Parser, Token, serialize_identifier};
#[cfg(feature = "gecko")] use gecko::rules::CounterStyleDescriptors; #[cfg(feature = "gecko")] use gecko::rules::CounterStyleDescriptors;
#[cfg(feature = "gecko")] use gecko_bindings::structs::nsCSSCounterDesc; #[cfg(feature = "gecko")] use gecko_bindings::structs::nsCSSCounterDesc;
use parser::{ParserContext, log_css_error, Parse}; use parser::{ParserContext, log_css_error, Parse};
@ -361,7 +361,7 @@ impl Parse for Symbol {
impl ToCss for Symbol { impl ToCss for Symbol {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
Symbol::String(ref s) => serialize_string(s, dest), Symbol::String(ref s) => s.to_css(dest),
Symbol::Ident(ref s) => serialize_identifier(s, dest), Symbol::Ident(ref s) => serialize_identifier(s, dest),
} }
} }

Просмотреть файл

@ -4241,8 +4241,8 @@ clip-path
use style_traits::cursor::Cursor; use style_traits::cursor::Cursor;
self.gecko.mCursor = match v.keyword { self.gecko.mCursor = match v.keyword {
Keyword::AutoCursor => structs::NS_STYLE_CURSOR_AUTO, Keyword::Auto => structs::NS_STYLE_CURSOR_AUTO,
Keyword::SpecifiedCursor(cursor) => match cursor { Keyword::Cursor(cursor) => match cursor {
Cursor::None => structs::NS_STYLE_CURSOR_NONE, Cursor::None => structs::NS_STYLE_CURSOR_NONE,
Cursor::Default => structs::NS_STYLE_CURSOR_DEFAULT, Cursor::Default => structs::NS_STYLE_CURSOR_DEFAULT,
Cursor::Pointer => structs::NS_STYLE_CURSOR_POINTER, Cursor::Pointer => structs::NS_STYLE_CURSOR_POINTER,

Просмотреть файл

@ -70,9 +70,7 @@
impl ToCss for ContentItem { impl ToCss for ContentItem {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
ContentItem::String(ref s) => { ContentItem::String(ref s) => s.to_css(dest),
cssparser::serialize_string(&**s, dest)
}
ContentItem::Counter(ref s, ref counter_style) => { ContentItem::Counter(ref s, ref counter_style) => {
try!(dest.write_str("counter(")); try!(dest.write_str("counter("));
try!(cssparser::serialize_identifier(&**s, dest)); try!(cssparser::serialize_identifier(&**s, dest));
@ -84,9 +82,9 @@
try!(dest.write_str("counters(")); try!(dest.write_str("counters("));
try!(cssparser::serialize_identifier(&**s, dest)); try!(cssparser::serialize_identifier(&**s, dest));
try!(dest.write_str(", ")); try!(dest.write_str(", "));
try!(cssparser::serialize_string(&**separator, dest)); separator.to_css(dest)?;
try!(dest.write_str(", ")); try!(dest.write_str(", "));
try!(counter_style.to_css(dest)); counter_style.to_css(dest)?;
dest.write_str(")") dest.write_str(")")
} }
ContentItem::OpenQuote => dest.write_str("open-quote"), ContentItem::OpenQuote => dest.write_str("open-quote"),

Просмотреть файл

@ -1894,11 +1894,9 @@ https://drafts.csswg.org/css-fonts-4/#low-level-font-variation-settings-control-
impl ToCss for SpecifiedValue { impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
use cssparser;
match *self { match *self {
SpecifiedValue::Normal => dest.write_str("normal"), SpecifiedValue::Normal => dest.write_str("normal"),
SpecifiedValue::Override(ref lang) => SpecifiedValue::Override(ref lang) => lang.to_css(dest),
cssparser::serialize_string(lang, dest),
SpecifiedValue::System(_) => Ok(()) SpecifiedValue::System(_) => Ok(())
} }
} }
@ -1921,7 +1919,6 @@ https://drafts.csswg.org/css-fonts-4/#low-level-font-variation-settings-control-
use std::{fmt, str}; use std::{fmt, str};
use style_traits::ToCss; use style_traits::ToCss;
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
use cssparser;
impl ToCss for T { impl ToCss for T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
@ -1936,7 +1933,7 @@ https://drafts.csswg.org/css-fonts-4/#low-level-font-variation-settings-control-
} else { } else {
unsafe { str::from_utf8_unchecked(&buf) } unsafe { str::from_utf8_unchecked(&buf) }
}; };
cssparser::serialize_string(slice.trim_right(), dest) slice.trim_right().to_css(dest)
} }
} }

Просмотреть файл

@ -427,8 +427,8 @@ ${helpers.predefined_type("word-spacing",
no_viewport_percentage!(SpecifiedValue); no_viewport_percentage!(SpecifiedValue);
pub mod computed_value { pub mod computed_value {
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToCss)]
pub enum T { pub enum T {
Keyword(KeywordValue), Keyword(KeywordValue),
None, None,
@ -443,33 +443,14 @@ ${helpers.predefined_type("word-spacing",
} }
} }
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, PartialEq, ToCss)]
pub enum SpecifiedValue { pub enum SpecifiedValue {
Keyword(KeywordValue), Keyword(KeywordValue),
None, None,
String(String), String(String),
} }
impl ToCss for computed_value::T {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
computed_value::T::Keyword(ref keyword) => keyword.to_css(dest),
computed_value::T::None => dest.write_str("none"),
computed_value::T::String(ref string) => write!(dest, "\"{}\"", string),
}
}
}
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
SpecifiedValue::Keyword(ref keyword) => keyword.to_css(dest),
SpecifiedValue::None => dest.write_str("none"),
SpecifiedValue::String(ref string) => write!(dest, "\"{}\"", string),
}
}
}
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum KeywordValue { pub enum KeywordValue {

Просмотреть файл

@ -33,9 +33,6 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu
% else: % else:
<%helpers:longhand name="list-style-type" animation_value_type="none" boxed="True" <%helpers:longhand name="list-style-type" animation_value_type="none" boxed="True"
spec="https://drafts.csswg.org/css-lists/#propdef-list-style-type"> spec="https://drafts.csswg.org/css-lists/#propdef-list-style-type">
use cssparser;
use std::fmt;
use style_traits::ToCss;
use values::CustomIdent; use values::CustomIdent;
use values::computed::ComputedValueAsSpecified; use values::computed::ComputedValueAsSpecified;
use values::generics::CounterStyleOrNone; use values::generics::CounterStyleOrNone;
@ -46,7 +43,7 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu
use values::generics::CounterStyleOrNone; use values::generics::CounterStyleOrNone;
/// <counter-style> | <string> | none /// <counter-style> | <string> | none
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, Eq, PartialEq, ToCss)]
pub enum T { pub enum T {
CounterStyle(CounterStyleOrNone), CounterStyle(CounterStyleOrNone),
String(String), String(String),
@ -56,15 +53,6 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu
impl ComputedValueAsSpecified for SpecifiedValue {} impl ComputedValueAsSpecified for SpecifiedValue {}
no_viewport_percentage!(SpecifiedValue); no_viewport_percentage!(SpecifiedValue);
impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
SpecifiedValue::CounterStyle(ref s) => s.to_css(dest),
SpecifiedValue::String(ref s) => cssparser::serialize_string(s, dest)
}
}
}
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
impl SpecifiedValue { impl SpecifiedValue {
/// Convert from gecko keyword to list-style-type. /// Convert from gecko keyword to list-style-type.

Просмотреть файл

@ -17,17 +17,19 @@
no_viewport_percentage!(SpecifiedValue); no_viewport_percentage!(SpecifiedValue);
pub mod computed_value { pub mod computed_value {
#[cfg(feature = "gecko")]
use std::fmt; use std::fmt;
use style_traits::cursor::Cursor; #[cfg(feature = "gecko")]
use style_traits::ToCss; use style_traits::ToCss;
use style_traits::cursor::Cursor;
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
use values::specified::url::SpecifiedUrl; use values::specified::url::SpecifiedUrl;
#[derive(Clone, PartialEq, Copy, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Copy, Debug, PartialEq, ToCss)]
pub enum Keyword { pub enum Keyword {
AutoCursor, Auto,
SpecifiedCursor(Cursor), Cursor(Cursor),
} }
#[cfg(not(feature = "gecko"))] #[cfg(not(feature = "gecko"))]
@ -47,15 +49,6 @@
pub keyword: Keyword, pub keyword: Keyword,
} }
impl ToCss for Keyword {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
Keyword::AutoCursor => dest.write_str("auto"),
Keyword::SpecifiedCursor(c) => c.to_css(dest),
}
}
}
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
impl ToCss for Image { impl ToCss for Image {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
@ -85,7 +78,7 @@
#[cfg(not(feature = "gecko"))] #[cfg(not(feature = "gecko"))]
#[inline] #[inline]
pub fn get_initial_value() -> computed_value::T { pub fn get_initial_value() -> computed_value::T {
computed_value::Keyword::AutoCursor computed_value::Keyword::Auto
} }
#[cfg(feature = "gecko")] #[cfg(feature = "gecko")]
@ -93,7 +86,7 @@
pub fn get_initial_value() -> computed_value::T { pub fn get_initial_value() -> computed_value::T {
computed_value::T { computed_value::T {
images: vec![], images: vec![],
keyword: computed_value::Keyword::AutoCursor keyword: computed_value::Keyword::Auto
} }
} }
@ -103,9 +96,9 @@
use style_traits::cursor::Cursor; use style_traits::cursor::Cursor;
let ident = try!(input.expect_ident()); let ident = try!(input.expect_ident());
if ident.eq_ignore_ascii_case("auto") { if ident.eq_ignore_ascii_case("auto") {
Ok(computed_value::Keyword::AutoCursor) Ok(computed_value::Keyword::Auto)
} else { } else {
Cursor::from_css_keyword(&ident).map(computed_value::Keyword::SpecifiedCursor) Cursor::from_css_keyword(&ident).map(computed_value::Keyword::Cursor)
} }
} }
} }

Просмотреть файл

@ -354,7 +354,6 @@ ${helpers.predefined_type("object-position",
animation_value_type="none" animation_value_type="none"
disable_when_testing="True" disable_when_testing="True"
boxed="True"> boxed="True">
use cssparser::serialize_string;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::ops::Range; use std::ops::Range;
@ -486,7 +485,7 @@ ${helpers.predefined_type("object-position",
if i != 0 { if i != 0 {
dest.write_str(" ")?; dest.write_str(" ")?;
} }
serialize_string(string, dest)?; string.to_css(dest)?;
} }
Ok(()) Ok(())
} }

Просмотреть файл

@ -16,12 +16,11 @@
spec="https://drafts.csswg.org/css-ui/#propdef-text-overflow"> spec="https://drafts.csswg.org/css-ui/#propdef-text-overflow">
use std::fmt; use std::fmt;
use style_traits::ToCss; use style_traits::ToCss;
use cssparser;
no_viewport_percentage!(SpecifiedValue); no_viewport_percentage!(SpecifiedValue);
#[derive(PartialEq, Eq, Clone, Debug)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))] #[cfg_attr(feature = "servo", derive(HeapSizeOf))]
#[derive(Clone, Debug, Eq, PartialEq, ToCss)]
pub enum Side { pub enum Side {
Clip, Clip,
Ellipsis, Ellipsis,
@ -127,18 +126,6 @@
} }
} }
impl ToCss for Side {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self {
Side::Clip => dest.write_str("clip"),
Side::Ellipsis => dest.write_str("ellipsis"),
Side::String(ref s) => {
cssparser::serialize_string(s, dest)
}
}
}
}
impl ToCss for SpecifiedValue { impl ToCss for SpecifiedValue {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
try!(self.first.to_css(dest)); try!(self.first.to_css(dest));

Просмотреть файл

@ -250,7 +250,6 @@
spec="https://drafts.csswg.org/css-grid/#propdef-grid-template" spec="https://drafts.csswg.org/css-grid/#propdef-grid-template"
disable_when_testing="True" disable_when_testing="True"
products="gecko"> products="gecko">
use cssparser::serialize_string;
use parser::Parse; use parser::Parse;
use properties::longhands::grid_template_rows; use properties::longhands::grid_template_rows;
use properties::longhands::grid_template_areas::TemplateAreas; use properties::longhands::grid_template_areas::TemplateAreas;
@ -370,7 +369,7 @@
concat_serialize_idents("[", "] ", names, " ", dest)?; concat_serialize_idents("[", "] ", names, " ", dest)?;
} }
serialize_string(string, dest)?; string.to_css(dest)?;
dest.write_str(" ")?; dest.write_str(" ")?;
size.to_css(dest)?; size.to_css(dest)?;
} }

Просмотреть файл

@ -6,7 +6,7 @@
//! initially in CSS Conditional Rules Module Level 3, @document has been postponed to the level 4. //! initially in CSS Conditional Rules Module Level 3, @document has been postponed to the level 4.
//! We implement the prefixed `@-moz-document`. //! We implement the prefixed `@-moz-document`.
use cssparser::{Parser, Token, SourceLocation, serialize_string}; use cssparser::{Parser, Token, SourceLocation};
use media_queries::Device; use media_queries::Device;
use parser::{Parse, ParserContext}; use parser::{Parse, ParserContext};
use shared_lock::{DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; use shared_lock::{DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
@ -160,17 +160,17 @@ impl ToCss for UrlMatchingFunction {
}, },
UrlMatchingFunction::UrlPrefix(ref url_prefix) => { UrlMatchingFunction::UrlPrefix(ref url_prefix) => {
dest.write_str("url-prefix(")?; dest.write_str("url-prefix(")?;
serialize_string(url_prefix, dest)?; url_prefix.to_css(dest)?;
dest.write_str(")") dest.write_str(")")
}, },
UrlMatchingFunction::Domain(ref domain) => { UrlMatchingFunction::Domain(ref domain) => {
dest.write_str("domain(")?; dest.write_str("domain(")?;
serialize_string(domain, dest)?; domain.to_css(dest)?;
dest.write_str(")") dest.write_str(")")
}, },
UrlMatchingFunction::RegExp(ref regex) => { UrlMatchingFunction::RegExp(ref regex) => {
dest.write_str("regexp(")?; dest.write_str("regexp(")?;
serialize_string(regex, dest)?; regex.to_css(dest)?;
dest.write_str(")") dest.write_str(")")
}, },
} }

Просмотреть файл

@ -143,12 +143,11 @@ impl<T> OneOrMoreCommaSeparated for FontSettingTag<T> {}
impl<T: ToCss> ToCss for FontSettingTag<T> { impl<T: ToCss> ToCss for FontSettingTag<T> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
use byteorder::{BigEndian, ByteOrder}; use byteorder::{BigEndian, ByteOrder};
use cssparser::serialize_string;
use std::str; use std::str;
let mut raw = [0u8; 4]; let mut raw = [0u8; 4];
BigEndian::write_u32(&mut raw, self.tag); BigEndian::write_u32(&mut raw, self.tag);
serialize_string(str::from_utf8(&raw).unwrap_or_default(), dest)?; str::from_utf8(&raw).unwrap_or_default().to_css(dest)?;
self.value.to_css(dest) self.value.to_css(dest)
} }

Просмотреть файл

@ -9,7 +9,7 @@
#![deny(missing_docs)] #![deny(missing_docs)]
use Atom; use Atom;
pub use cssparser::{RGBA, Token, Parser, serialize_identifier, serialize_string}; pub use cssparser::{RGBA, Token, Parser, serialize_identifier};
use parser::{Parse, ParserContext}; use parser::{Parse, ParserContext};
use std::ascii::AsciiExt; use std::ascii::AsciiExt;
use std::borrow::Cow; use std::borrow::Cow;
@ -166,7 +166,7 @@ impl ToCss for KeyframesName {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
match *self { match *self {
KeyframesName::Ident(ref ident) => ident.to_css(dest), KeyframesName::Ident(ref ident) => ident.to_css(dest),
KeyframesName::QuotedString(ref atom) => serialize_string(&atom.to_string(), dest), KeyframesName::QuotedString(ref atom) => atom.to_string().to_css(dest),
} }
} }
} }

Просмотреть файл

@ -5,11 +5,14 @@
//! Helper types and traits for the handling of CSS values. //! Helper types and traits for the handling of CSS values.
use app_units::Au; use app_units::Au;
use cssparser::UnicodeRange; use cssparser::{UnicodeRange, serialize_string};
use std::fmt; use std::fmt;
/// Serialises a value according to its CSS representation. /// Serialises a value according to its CSS representation.
/// ///
/// This trait is implemented for `str` and its friends, serialising the string
/// contents as a CSS quoted string.
///
/// This trait is derivable with `#[derive(ToCss)]`, with the following behaviour: /// This trait is derivable with `#[derive(ToCss)]`, with the following behaviour:
/// * unit variants get serialised as the `snake-case` representation /// * unit variants get serialised as the `snake-case` representation
/// of their name; /// of their name;
@ -38,6 +41,20 @@ impl<'a, T> ToCss for &'a T where T: ToCss + ?Sized {
} }
} }
impl ToCss for str {
#[inline]
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
serialize_string(self, dest)
}
}
impl ToCss for String {
#[inline]
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {
serialize_string(self, dest)
}
}
/// Marker trait to automatically implement ToCss for Vec<T>. /// Marker trait to automatically implement ToCss for Vec<T>.
pub trait OneOrMoreCommaSeparated {} pub trait OneOrMoreCommaSeparated {}
@ -55,7 +72,7 @@ impl<T> ToCss for Vec<T> where T: ToCss + OneOrMoreCommaSeparated {
} }
} }
impl<T: ToCss> ToCss for Box<T> { impl<T> ToCss for Box<T> where T: ?Sized + ToCss {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where W: fmt::Write, where W: fmt::Write,
{ {