diff --git a/components/style/counter_style/mod.rs b/components/style/counter_style/mod.rs index a018f9b8286..55d1010259c 100644 --- a/components/style/counter_style/mod.rs +++ b/components/style/counter_style/mod.rs @@ -7,8 +7,8 @@ //! [counter-style]: https://drafts.csswg.org/css-counter-styles/ use Atom; -use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser, Parser, Token}; -use cssparser::{serialize_string, serialize_identifier}; +use cssparser::{AtRuleParser, DeclarationListParser, DeclarationParser}; +use cssparser::{Parser, Token, serialize_identifier}; #[cfg(feature = "gecko")] use gecko::rules::CounterStyleDescriptors; #[cfg(feature = "gecko")] use gecko_bindings::structs::nsCSSCounterDesc; use parser::{ParserContext, log_css_error, Parse}; @@ -361,7 +361,7 @@ impl Parse for Symbol { impl ToCss for Symbol { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { 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), } } diff --git a/components/style/properties/longhand/counters.mako.rs b/components/style/properties/longhand/counters.mako.rs index e71d80c42af..55d054e8a27 100644 --- a/components/style/properties/longhand/counters.mako.rs +++ b/components/style/properties/longhand/counters.mako.rs @@ -70,9 +70,7 @@ impl ToCss for ContentItem { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { match *self { - ContentItem::String(ref s) => { - cssparser::serialize_string(&**s, dest) - } + ContentItem::String(ref s) => s.to_css(dest), ContentItem::Counter(ref s, ref counter_style) => { try!(dest.write_str("counter(")); try!(cssparser::serialize_identifier(&**s, dest)); @@ -84,9 +82,9 @@ try!(dest.write_str("counters(")); try!(cssparser::serialize_identifier(&**s, dest)); try!(dest.write_str(", ")); - try!(cssparser::serialize_string(&**separator, dest)); + separator.to_css(dest)?; try!(dest.write_str(", ")); - try!(counter_style.to_css(dest)); + counter_style.to_css(dest)?; dest.write_str(")") } ContentItem::OpenQuote => dest.write_str("open-quote"), diff --git a/components/style/properties/longhand/font.mako.rs b/components/style/properties/longhand/font.mako.rs index 825577b1e60..127a1856e3e 100644 --- a/components/style/properties/longhand/font.mako.rs +++ b/components/style/properties/longhand/font.mako.rs @@ -1894,11 +1894,9 @@ https://drafts.csswg.org/css-fonts-4/#low-level-font-variation-settings-control- impl ToCss for SpecifiedValue { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { - use cssparser; match *self { SpecifiedValue::Normal => dest.write_str("normal"), - SpecifiedValue::Override(ref lang) => - cssparser::serialize_string(lang, dest), + SpecifiedValue::Override(ref lang) => lang.to_css(dest), 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 style_traits::ToCss; use byteorder::{BigEndian, ByteOrder}; - use cssparser; impl ToCss for T { fn to_css(&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 { unsafe { str::from_utf8_unchecked(&buf) } }; - cssparser::serialize_string(slice.trim_right(), dest) + slice.trim_right().to_css(dest) } } diff --git a/components/style/properties/longhand/inherited_text.mako.rs b/components/style/properties/longhand/inherited_text.mako.rs index 649caa5cece..5b1b4490a58 100644 --- a/components/style/properties/longhand/inherited_text.mako.rs +++ b/components/style/properties/longhand/inherited_text.mako.rs @@ -427,8 +427,8 @@ ${helpers.predefined_type("word-spacing", no_viewport_percentage!(SpecifiedValue); pub mod computed_value { - #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + #[derive(Clone, Debug, PartialEq, ToCss)] pub enum T { Keyword(KeywordValue), None, @@ -443,33 +443,14 @@ ${helpers.predefined_type("word-spacing", } } - #[derive(Debug, Clone, PartialEq)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + #[derive(Clone, Debug, PartialEq, ToCss)] pub enum SpecifiedValue { Keyword(KeywordValue), None, String(String), } - impl ToCss for computed_value::T { - fn to_css(&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(&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)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] pub enum KeywordValue { diff --git a/components/style/properties/longhand/list.mako.rs b/components/style/properties/longhand/list.mako.rs index 34f51b97b9d..78347620036 100644 --- a/components/style/properties/longhand/list.mako.rs +++ b/components/style/properties/longhand/list.mako.rs @@ -33,9 +33,6 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu % else: <%helpers:longhand name="list-style-type" animation_value_type="none" boxed="True" 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::computed::ComputedValueAsSpecified; use values::generics::CounterStyleOrNone; @@ -46,7 +43,7 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu use values::generics::CounterStyleOrNone; /// | | none - #[derive(Debug, Clone, PartialEq, Eq)] + #[derive(Debug, Clone, Eq, PartialEq, ToCss)] pub enum T { CounterStyle(CounterStyleOrNone), String(String), @@ -56,15 +53,6 @@ ${helpers.single_keyword("list-style-position", "outside inside", animation_valu impl ComputedValueAsSpecified for SpecifiedValue {} no_viewport_percentage!(SpecifiedValue); - impl ToCss for SpecifiedValue { - fn to_css(&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")] impl SpecifiedValue { /// Convert from gecko keyword to list-style-type. diff --git a/components/style/properties/longhand/position.mako.rs b/components/style/properties/longhand/position.mako.rs index ebf4265cd0d..748f8a4ae81 100644 --- a/components/style/properties/longhand/position.mako.rs +++ b/components/style/properties/longhand/position.mako.rs @@ -354,7 +354,6 @@ ${helpers.predefined_type("object-position", animation_value_type="none" disable_when_testing="True" boxed="True"> - use cssparser::serialize_string; use std::collections::HashMap; use std::fmt; use std::ops::Range; @@ -486,7 +485,7 @@ ${helpers.predefined_type("object-position", if i != 0 { dest.write_str(" ")?; } - serialize_string(string, dest)?; + string.to_css(dest)?; } Ok(()) } diff --git a/components/style/properties/longhand/text.mako.rs b/components/style/properties/longhand/text.mako.rs index 08113b228f2..6ebf1e9d277 100644 --- a/components/style/properties/longhand/text.mako.rs +++ b/components/style/properties/longhand/text.mako.rs @@ -16,12 +16,11 @@ spec="https://drafts.csswg.org/css-ui/#propdef-text-overflow"> use std::fmt; use style_traits::ToCss; - use cssparser; no_viewport_percentage!(SpecifiedValue); - #[derive(PartialEq, Eq, Clone, Debug)] #[cfg_attr(feature = "servo", derive(HeapSizeOf))] + #[derive(Clone, Debug, Eq, PartialEq, ToCss)] pub enum Side { Clip, Ellipsis, @@ -127,18 +126,6 @@ } } - impl ToCss for Side { - fn to_css(&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 { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { try!(self.first.to_css(dest)); diff --git a/components/style/properties/shorthand/position.mako.rs b/components/style/properties/shorthand/position.mako.rs index 65586ab1ec2..6627edd92bf 100644 --- a/components/style/properties/shorthand/position.mako.rs +++ b/components/style/properties/shorthand/position.mako.rs @@ -250,7 +250,6 @@ spec="https://drafts.csswg.org/css-grid/#propdef-grid-template" disable_when_testing="True" products="gecko"> - use cssparser::serialize_string; use parser::Parse; use properties::longhands::grid_template_rows; use properties::longhands::grid_template_areas::TemplateAreas; @@ -370,7 +369,7 @@ concat_serialize_idents("[", "] ", names, " ", dest)?; } - serialize_string(string, dest)?; + string.to_css(dest)?; dest.write_str(" ")?; size.to_css(dest)?; } diff --git a/components/style/stylesheets/document_rule.rs b/components/style/stylesheets/document_rule.rs index 0c79b0bfd8e..f921eeb7631 100644 --- a/components/style/stylesheets/document_rule.rs +++ b/components/style/stylesheets/document_rule.rs @@ -6,7 +6,7 @@ //! initially in CSS Conditional Rules Module Level 3, @document has been postponed to the level 4. //! We implement the prefixed `@-moz-document`. -use cssparser::{Parser, Token, SourceLocation, serialize_string}; +use cssparser::{Parser, Token, SourceLocation}; use media_queries::Device; use parser::{Parse, ParserContext}; use shared_lock::{DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; @@ -160,17 +160,17 @@ impl ToCss for UrlMatchingFunction { }, UrlMatchingFunction::UrlPrefix(ref url_prefix) => { dest.write_str("url-prefix(")?; - serialize_string(url_prefix, dest)?; + url_prefix.to_css(dest)?; dest.write_str(")") }, UrlMatchingFunction::Domain(ref domain) => { dest.write_str("domain(")?; - serialize_string(domain, dest)?; + domain.to_css(dest)?; dest.write_str(")") }, UrlMatchingFunction::RegExp(ref regex) => { dest.write_str("regexp(")?; - serialize_string(regex, dest)?; + regex.to_css(dest)?; dest.write_str(")") }, } diff --git a/components/style/values/generics/mod.rs b/components/style/values/generics/mod.rs index 40d3f4e46df..da0ba4a4301 100644 --- a/components/style/values/generics/mod.rs +++ b/components/style/values/generics/mod.rs @@ -143,12 +143,11 @@ impl OneOrMoreCommaSeparated for FontSettingTag {} impl ToCss for FontSettingTag { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { use byteorder::{BigEndian, ByteOrder}; - use cssparser::serialize_string; use std::str; let mut raw = [0u8; 4]; 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) } diff --git a/components/style/values/mod.rs b/components/style/values/mod.rs index 506584c1e76..32b7df063c9 100644 --- a/components/style/values/mod.rs +++ b/components/style/values/mod.rs @@ -9,7 +9,7 @@ #![deny(missing_docs)] 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 std::ascii::AsciiExt; use std::borrow::Cow; @@ -166,7 +166,7 @@ impl ToCss for KeyframesName { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { match *self { 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), } } } diff --git a/components/style_traits/values.rs b/components/style_traits/values.rs index 7c73f7bd4cf..406df824e37 100644 --- a/components/style_traits/values.rs +++ b/components/style_traits/values.rs @@ -5,11 +5,14 @@ //! Helper types and traits for the handling of CSS values. use app_units::Au; -use cssparser::UnicodeRange; +use cssparser::{UnicodeRange, serialize_string}; use std::fmt; /// 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: /// * unit variants get serialised as the `snake-case` representation /// 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(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + serialize_string(self, dest) + } +} + +impl ToCss for String { + #[inline] + fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + serialize_string(self, dest) + } +} + /// Marker trait to automatically implement ToCss for Vec. pub trait OneOrMoreCommaSeparated {} @@ -55,7 +72,7 @@ impl ToCss for Vec where T: ToCss + OneOrMoreCommaSeparated { } } -impl ToCss for Box { +impl ToCss for Box where T: ?Sized + ToCss { fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write, {