diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs index b9bf61c192434..cfe1e73ddcbd5 100644 --- a/components/script/dom/cssstyledeclaration.rs +++ b/components/script/dom/cssstyledeclaration.rs @@ -21,7 +21,7 @@ use style::properties::{DeclarationSource, Importance, PropertyDeclarationBlock, use style::properties::{parse_one_declaration_into, parse_style_attribute, SourcePropertyDeclaration}; use style::selector_parser::PseudoElement; use style::shared_lock::Locked; -use style_traits::{ParsingMode, ToCss}; +use style_traits::{CssWriter, ParsingMode, ToCss}; // http://dev.w3.org/csswg/cssom/#the-cssstyledeclaration-interface #[dom_struct] @@ -225,7 +225,7 @@ impl CSSStyleDeclaration { let mut string = String::new(); self.owner.with_block(|pdb| { - pdb.property_value_to_css(&id, &mut string).unwrap(); + pdb.property_value_to_css(&id, &mut CssWriter::new(&mut string)).unwrap(); }); DOMString::from(string) @@ -392,7 +392,7 @@ impl CSSStyleDeclarationMethods for CSSStyleDeclaration { let mut string = String::new(); self.owner.mutate_associated_block(|pdb, changed| { - pdb.property_value_to_css(&id, &mut string).unwrap(); + pdb.property_value_to_css(&id, &mut CssWriter::new(&mut string)).unwrap(); *changed = pdb.remove_property(&id); }); diff --git a/components/script/dom/medialist.rs b/components/script/dom/medialist.rs index 6ae318da58234..449c3c19ea60e 100644 --- a/components/script/dom/medialist.rs +++ b/components/script/dom/medialist.rs @@ -93,12 +93,11 @@ impl MediaListMethods for MediaList { // https://drafts.csswg.org/cssom/#dom-medialist-item fn Item(&self, index: u32) -> Option { let guard = self.shared_lock().read(); - self.media_queries.read_with(&guard).media_queries - .get(index as usize).and_then(|query| { - let mut s = String::new(); - query.to_css(&mut s).unwrap(); - Some(DOMString::from_string(s)) - }) + self.media_queries + .read_with(&guard) + .media_queries + .get(index as usize) + .map(|query| query.to_css_string().into()) } // https://drafts.csswg.org/cssom/#dom-medialist-item diff --git a/components/script/dom/mediaquerylist.rs b/components/script/dom/mediaquerylist.rs index e89e7d4d744ad..4c6be0091b136 100644 --- a/components/script/dom/mediaquerylist.rs +++ b/components/script/dom/mediaquerylist.rs @@ -83,9 +83,7 @@ impl MediaQueryList { impl MediaQueryListMethods for MediaQueryList { // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-media fn Media(&self) -> DOMString { - let mut s = String::new(); - self.media_query_list.to_css(&mut s).unwrap(); - DOMString::from_string(s) + self.media_query_list.to_css_string().into() } // https://drafts.csswg.org/cssom-view/#dom-mediaquerylist-matches diff --git a/components/style/counter_style/mod.rs b/components/style/counter_style/mod.rs index f0fa18fda920a..72a1f266e6bec 100644 --- a/components/style/counter_style/mod.rs +++ b/components/style/counter_style/mod.rs @@ -17,9 +17,10 @@ use selectors::parser::SelectorParseErrorKind; use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard}; #[allow(unused_imports)] use std::ascii::AsciiExt; use std::borrow::Cow; -use std::fmt; +use std::fmt::{self, Write}; use std::ops::Range; -use style_traits::{Comma, OneOrMoreSeparated, ParseError, StyleParseErrorKind, ToCss}; +use style_traits::{Comma, CssWriter, OneOrMoreSeparated, ParseError}; +use style_traits::{StyleParseErrorKind, ToCss}; use values::CustomIdent; /// Parse a counter style name reference. @@ -228,8 +229,14 @@ macro_rules! counter_style_descriptors { } impl ToCssWithGuard for CounterStyleRuleData { - fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + _guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { dest.write_str("@counter-style ")?; self.name.to_css(dest)?; dest.write_str(" {\n")?; @@ -362,7 +369,10 @@ impl Parse for System { } impl ToCss for System { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { System::Cyclic => dest.write_str("cyclic"), System::Numeric => dest.write_str("numeric"), @@ -410,7 +420,10 @@ impl Parse for Symbol { } impl ToCss for Symbol { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { Symbol::String(ref s) => s.to_css(dest), Symbol::Ident(ref s) => serialize_identifier(s, dest), @@ -477,7 +490,10 @@ fn parse_bound<'i, 't>(input: &mut Parser<'i, 't>) -> Result, ParseE } impl ToCss for Ranges { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let mut iter = self.0.iter(); if let Some(first) = iter.next() { range_to_css(first, dest)?; @@ -492,14 +508,19 @@ impl ToCss for Ranges { } } -fn range_to_css(range: &Range>, dest: &mut W) -> fmt::Result -where W: fmt::Write { +fn range_to_css(range: &Range>, dest: &mut CssWriter) -> fmt::Result +where + W: Write, +{ bound_to_css(range.start, dest)?; dest.write_char(' ')?; bound_to_css(range.end, dest) } -fn bound_to_css(range: Option, dest: &mut W) -> fmt::Result where W: fmt::Write { +fn bound_to_css(range: Option, dest: &mut CssWriter) -> fmt::Result +where + W: Write, +{ if let Some(finite) = range { finite.to_css(dest) } else { @@ -556,7 +577,10 @@ impl Parse for Symbols { } impl ToCss for Symbols { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let mut iter = self.0.iter(); let first = iter.next().expect("expected at least one symbol"); first.to_css(dest)?; diff --git a/components/style/custom_properties.rs b/components/style/custom_properties.rs index 86aa135a0b8d6..0aa43109a1856 100644 --- a/components/style/custom_properties.rs +++ b/components/style/custom_properties.rs @@ -17,9 +17,9 @@ use smallvec::SmallVec; #[allow(unused_imports)] use std::ascii::AsciiExt; use std::borrow::{Borrow, Cow}; use std::cmp; -use std::fmt; +use std::fmt::{self, Write}; use std::hash::Hash; -use style_traits::{ToCss, StyleParseErrorKind, ParseError}; +use style_traits::{CssWriter, ToCss, StyleParseErrorKind, ParseError}; /// A custom property name is just an `Atom`. /// @@ -53,8 +53,9 @@ pub struct VariableValue { } impl ToCss for SpecifiedValue { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { dest.write_str(&self.css) } diff --git a/components/style/font_face.rs b/components/style/font_face.rs index 80c32a552e748..6915df2c8b87e 100644 --- a/components/style/font_face.rs +++ b/components/style/font_face.rs @@ -20,8 +20,9 @@ use parser::{ParserContext, ParserErrorContext, Parse}; use properties::longhands::font_language_override; use selectors::parser::SelectorParseErrorKind; use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; -use style_traits::{Comma, OneOrMoreSeparated, ParseError, StyleParseErrorKind, ToCss}; +use std::fmt::{self, Write}; +use style_traits::{Comma, CssWriter, OneOrMoreSeparated, ParseError}; +use style_traits::{StyleParseErrorKind, ToCss}; use values::computed::font::FamilyName; use values::specified::url::SpecifiedUrl; @@ -54,8 +55,9 @@ pub struct UrlSource { } impl ToCss for UrlSource { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { self.url.to_css(dest) } @@ -272,8 +274,14 @@ macro_rules! font_face_descriptors_common { impl ToCssWithGuard for FontFaceRuleData { // Serialization of FontFaceRule is not specced. - fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + _guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { dest.write_str("@font-face {\n")?; $( if let Some(ref value) = self.$ident { diff --git a/components/style/gecko/media_queries.rs b/components/style/gecko/media_queries.rs index bbecaff790557..69ad65ac70c9b 100644 --- a/components/style/gecko/media_queries.rs +++ b/components/style/gecko/media_queries.rs @@ -24,7 +24,7 @@ use std::fmt::{self, Write}; use std::sync::atomic::{AtomicBool, AtomicIsize, AtomicUsize, Ordering}; use str::starts_with_ignore_ascii_case; use string_cache::Atom; -use style_traits::{CSSPixel, DevicePixel}; +use style_traits::{CSSPixel, CssWriter, DevicePixel}; use style_traits::{ToCss, ParseError, StyleParseErrorKind}; use style_traits::viewport::ViewportConstraints; use stylesheets::Origin; @@ -236,7 +236,7 @@ pub struct Expression { } impl ToCss for Expression { - fn to_css(&self, dest: &mut W) -> fmt::Result + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write, { dest.write_str("(")?; @@ -408,7 +408,7 @@ impl MediaExpressionValue { } impl MediaExpressionValue { - fn to_css(&self, dest: &mut W, for_expr: &Expression) -> fmt::Result + fn to_css(&self, dest: &mut CssWriter, for_expr: &Expression) -> fmt::Result where W: fmt::Write, { match *self { diff --git a/components/style/gecko/rules.rs b/components/style/gecko/rules.rs index 64d30b4488369..6ebe2bf51ed34 100644 --- a/components/style/gecko/rules.rs +++ b/components/style/gecko/rules.rs @@ -17,7 +17,9 @@ use gecko_bindings::sugar::refptr::{RefPtr, UniqueRefPtr}; use nsstring::nsString; use properties::longhands::font_language_override; use shared_lock::{ToCssWithGuard, SharedRwLockReadGuard}; -use std::{fmt, str}; +use std::fmt::{self, Write}; +use std::str; +use style_traits::CssWriter; use values::computed::font::FamilyName; use values::generics::FontSettings; @@ -200,8 +202,14 @@ impl From for FontFaceRule { } impl ToCssWithGuard for FontFaceRule { - fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + _guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { let mut css_text = nsString::new(); unsafe { bindings::Gecko_CSSFontFaceRule_GetCssText(self.get(), &mut *css_text); @@ -237,8 +245,14 @@ impl From for CounterStyleRule { } impl ToCssWithGuard for CounterStyleRule { - fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + _guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { let mut css_text = nsString::new(); unsafe { bindings::Gecko_CSSCounterStyle_GetCssText(self.get(), &mut *css_text); diff --git a/components/style/gecko/selector_parser.rs b/components/style/gecko/selector_parser.rs index cb57d9b21fbcf..985bd453b4c5e 100644 --- a/components/style/gecko/selector_parser.rs +++ b/components/style/gecko/selector_parser.rs @@ -16,7 +16,7 @@ use selectors::parser::{self as selector_parser, Selector, Visit, SelectorParseE use selectors::visitor::SelectorVisitor; use std::fmt; use string_cache::{Atom, Namespace, WeakAtom, WeakNamespace}; -use style_traits::{ParseError, StyleParseErrorKind, ToCss as ToCss_}; +use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss as ToCss_}; pub use gecko::pseudo_element::{PseudoElement, EAGER_PSEUDOS, EAGER_PSEUDO_COUNT, PSEUDO_COUNT}; pub use gecko::snapshot::SnapshotMap; @@ -86,12 +86,12 @@ impl ToCss for NonTSPseudoClass { }, )* NonTSPseudoClass::MozLocaleDir(ref dir) => { dest.write_str(":-moz-locale-dir(")?; - dir.to_css(dest)?; + dir.to_css(&mut CssWriter::new(dest))?; return dest.write_char(')') }, NonTSPseudoClass::Dir(ref dir) => { dest.write_str(":dir(")?; - dir.to_css(dest)?; + dir.to_css(&mut CssWriter::new(dest))?; return dest.write_char(')') }, NonTSPseudoClass::MozAny(ref selectors) => { diff --git a/components/style/gecko/url.rs b/components/style/gecko/url.rs index 64eda812f45b7..b18d00a559c47 100644 --- a/components/style/gecko/url.rs +++ b/components/style/gecko/url.rs @@ -12,9 +12,9 @@ use gecko_bindings::sugar::refptr::RefPtr; use malloc_size_of::{MallocSizeOf, MallocSizeOfOps}; use parser::ParserContext; use servo_arc::{Arc, RawOffsetArc}; -use std::fmt; +use std::fmt::{self, Write}; use std::mem; -use style_traits::{ToCss, ParseError}; +use style_traits::{CssWriter, ToCss, ParseError}; /// A specified url() value for gecko. Gecko does not eagerly resolve SpecifiedUrls. #[derive(Clone, Debug, PartialEq)] @@ -136,7 +136,10 @@ impl SpecifiedUrl { } impl ToCss for SpecifiedUrl { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { dest.write_str("url(")?; self.serialization.to_css(dest)?; dest.write_str(")") diff --git a/components/style/lib.rs b/components/style/lib.rs index d4d3855a481ec..d789c1d3305da 100644 --- a/components/style/lib.rs +++ b/components/style/lib.rs @@ -134,8 +134,8 @@ pub mod traversal_flags; #[allow(non_camel_case_types)] pub mod values; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; #[cfg(feature = "gecko")] pub use gecko_string_cache as string_cache; #[cfg(feature = "gecko")] pub use gecko_string_cache::Atom; @@ -181,11 +181,13 @@ longhand_properties_idents!(reexport_computed_values); /// Serializes as CSS a comma-separated list of any `T` that supports being /// serialized as CSS. -pub fn serialize_comma_separated_list(dest: &mut W, - list: &[T]) - -> fmt::Result - where W: fmt::Write, - T: ToCss, +pub fn serialize_comma_separated_list( + dest: &mut CssWriter, + list: &[T], +) -> fmt::Result +where + W: Write, + T: ToCss, { if list.is_empty() { return Ok(()); diff --git a/components/style/macros.rs b/components/style/macros.rs index aa505498d3be7..ac795bdb09f66 100644 --- a/components/style/macros.rs +++ b/components/style/macros.rs @@ -91,8 +91,11 @@ macro_rules! define_numbered_css_keyword_enum { } } - impl ::style_traits::values::ToCss for $name { - fn to_css(&self, dest: &mut W) -> ::std::fmt::Result + impl ::style_traits::ToCss for $name { + fn to_css( + &self, + dest: &mut ::style_traits::CssWriter, + ) -> ::std::fmt::Result where W: ::std::fmt::Write, { diff --git a/components/style/media_queries.rs b/components/style/media_queries.rs index 42a72ba2d583e..8941b21a30a27 100644 --- a/components/style/media_queries.rs +++ b/components/style/media_queries.rs @@ -14,9 +14,9 @@ use error_reporting::{ContextualParseError, ParseErrorReporter}; use parser::{ParserContext, ParserErrorContext}; use selectors::parser::SelectorParseErrorKind; use serialize_comma_separated_list; -use std::fmt; +use std::fmt::{self, Write}; use str::string_as_ascii_lowercase; -use style_traits::{ToCss, ParseError, StyleParseErrorKind}; +use style_traits::{CssWriter, ToCss, ParseError, StyleParseErrorKind}; use values::CustomIdent; #[cfg(feature = "servo")] @@ -33,8 +33,9 @@ pub struct MediaList { } impl ToCss for MediaList { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { serialize_comma_separated_list(dest, &self.media_queries) } @@ -86,8 +87,9 @@ impl MediaQuery { } impl ToCss for MediaQuery { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { if let Some(qual) = self.qualifier { qual.to_css(dest)?; diff --git a/components/style/properties/declaration_block.rs b/components/style/properties/declaration_block.rs index c28ed5555f75c..403e3865d22ef 100644 --- a/components/style/properties/declaration_block.rs +++ b/components/style/properties/declaration_block.rs @@ -16,10 +16,10 @@ use properties::animated_properties::AnimationValue; use shared_lock::Locked; use smallbitvec::{self, SmallBitVec}; use smallvec::SmallVec; -use std::fmt; +use std::fmt::{self, Write}; use std::iter::{DoubleEndedIterator, Zip}; use std::slice::Iter; -use style_traits::{ToCss, ParseError, ParsingMode, StyleParseErrorKind}; +use style_traits::{CssWriter, ParseError, ParsingMode, StyleParseErrorKind, ToCss}; use stylesheets::{CssRuleType, Origin, UrlExtraData}; use super::*; use values::computed::Context; @@ -306,8 +306,13 @@ impl PropertyDeclarationBlock { /// Find the value of the given property in this block and serialize it /// /// - pub fn property_value_to_css(&self, property: &PropertyId, dest: &mut W) -> fmt::Result - where W: fmt::Write, + pub fn property_value_to_css( + &self, + property: &PropertyId, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, { // Step 1.1: done when parsing a string to PropertyId @@ -611,12 +616,12 @@ impl PropertyDeclarationBlock { pub fn single_value_to_css( &self, property: &PropertyId, - dest: &mut W, + dest: &mut CssWriter, computed_values: Option<&ComputedValues>, custom_properties_block: Option<&PropertyDeclarationBlock>, ) -> fmt::Result where - W: fmt::Write, + W: Write, { match property.as_shorthand() { Err(_longhand_or_custom) => { @@ -740,8 +745,9 @@ impl PropertyDeclarationBlock { impl ToCss for PropertyDeclarationBlock { // https://drafts.csswg.org/cssom/#serialize-a-css-declaration-block - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { let mut is_first_serialization = true; // trailing serializations should have a prepended space @@ -846,14 +852,14 @@ impl ToCss for PropertyDeclarationBlock { } #[cfg(feature = "gecko")] (_, Some(sys)) => { - sys.to_css(&mut v)?; + sys.to_css(&mut CssWriter::new(&mut v))?; AppendableValue::Css { css: &v, with_variables: false, } } (other, _) => { - append_declaration_value(&mut v, other)?; + append_declaration_value(&mut CssWriter::new(&mut v), other)?; // Substep 6 if v.is_empty() { @@ -952,10 +958,12 @@ pub enum AppendableValue<'a, I> } /// Potentially appends whitespace after the first (property: value;) pair. -fn handle_first_serialization(dest: &mut W, - is_first_serialization: &mut bool) - -> fmt::Result - where W: fmt::Write, +fn handle_first_serialization( + dest: &mut CssWriter, + is_first_serialization: &mut bool, +) -> fmt::Result +where + W: Write, { if !*is_first_serialization { dest.write_str(" ") @@ -966,11 +974,13 @@ fn handle_first_serialization(dest: &mut W, } /// Append a given kind of appendable value to a serialization. -pub fn append_declaration_value<'a, W, I>(dest: &mut W, - appendable_value: AppendableValue<'a, I>) - -> fmt::Result - where W: fmt::Write, - I: Iterator, +pub fn append_declaration_value<'a, W, I>( + dest: &mut CssWriter, + appendable_value: AppendableValue<'a, I>, +) -> fmt::Result +where + W: Write, + I: Iterator, { match appendable_value { AppendableValue::Css { css, .. } => { @@ -986,15 +996,17 @@ pub fn append_declaration_value<'a, W, I>(dest: &mut W, } /// Append a given property and value pair to a serialization. -pub fn append_serialization<'a, W, I, N>(dest: &mut W, - property_name: &N, - appendable_value: AppendableValue<'a, I>, - importance: Importance, - is_first_serialization: &mut bool) - -> fmt::Result - where W: fmt::Write, - I: Iterator, - N: ToCss, +pub fn append_serialization<'a, W, I, N>( + dest: &mut CssWriter, + property_name: &N, + appendable_value: AppendableValue<'a, I>, + importance: Importance, + is_first_serialization: &mut bool, +) -> fmt::Result +where + W: Write, + I: Iterator, + N: ToCss, { handle_first_serialization(dest, is_first_serialization)?; diff --git a/components/style/properties/helpers.mako.rs b/components/style/properties/helpers.mako.rs index becef7246570b..4d7c1ff19add7 100644 --- a/components/style/properties/helpers.mako.rs +++ b/components/style/properties/helpers.mako.rs @@ -82,8 +82,8 @@ need_animatable=need_animatable, **kwargs)"> #[allow(unused_imports)] use smallvec::SmallVec; - use std::fmt; - use style_traits::{Separator, ToCss}; + use std::fmt::{self, Write}; + use style_traits::{CssWriter, Separator, ToCss}; pub mod single_value { #[allow(unused_imports)] @@ -154,8 +154,9 @@ } impl ToCss for computed_value::T { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { let mut iter = self.0.iter(); if let Some(val) = iter.next() { @@ -180,8 +181,9 @@ pub struct SpecifiedValue(pub Vec); impl ToCss for SpecifiedValue { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { let mut iter = self.0.iter(); if let Some(val) = iter.next() { @@ -682,11 +684,11 @@ #[allow(unused_imports)] use selectors::parser::SelectorParseErrorKind; #[allow(unused_imports)] - use std::fmt; + use std::fmt::{self, Write}; #[allow(unused_imports)] use style_traits::{ParseError, StyleParseErrorKind}; #[allow(unused_imports)] - use style_traits::ToCss; + use style_traits::{CssWriter, ToCss}; pub struct Longhands { % for sub_property in shorthand.sub_properties: @@ -806,7 +808,10 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let rect = Rect::new( % for side in ["top", "right", "bottom", "left"]: &self.${to_rust_ident(sub_property_pattern % side)}, diff --git a/components/style/properties/helpers/animated_properties.mako.rs b/components/style/properties/helpers/animated_properties.mako.rs index a4581f1c31e90..6a76f0a84e177 100644 --- a/components/style/properties/helpers/animated_properties.mako.rs +++ b/components/style/properties/helpers/animated_properties.mako.rs @@ -27,9 +27,9 @@ use selectors::parser::SelectorParseErrorKind; use servo_arc::Arc; use smallvec::SmallVec; use std::cmp; -use std::fmt; +use std::fmt::{self, Write}; #[cfg(feature = "gecko")] use hash::FnvHashMap; -use style_traits::{ParseError, ToCss}; +use style_traits::{CssWriter, ParseError, ToCss}; use super::ComputedValues; use values::{CSSFloat, CustomIdent, Either}; use values::animated::{Animate, Procedure, ToAnimatedValue, ToAnimatedZero}; @@ -94,7 +94,10 @@ pub enum TransitionProperty { } impl ToCss for TransitionProperty { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { TransitionProperty::All => dest.write_str("all"), TransitionProperty::Shorthand(ref id) => dest.write_str(id.name()), diff --git a/components/style/properties/longhand/color.mako.rs b/components/style/properties/longhand/color.mako.rs index 459352fd59c49..3dfc685f082ab 100644 --- a/components/style/properties/longhand/color.mako.rs +++ b/components/style/properties/longhand/color.mako.rs @@ -66,8 +66,8 @@ pub mod system_colors { use cssparser::Parser; use gecko_bindings::bindings::Gecko_GetLookAndFeelSystemColor; use gecko_bindings::structs::root::mozilla::LookAndFeel_ColorID; - use std::fmt; - use style_traits::ToCss; + use std::fmt::{self, Write}; + use style_traits::{CssWriter, ToCss}; use values::computed::{Context, ToComputedValue}; pub type SystemColor = LookAndFeel_ColorID; @@ -77,7 +77,10 @@ pub mod system_colors { malloc_size_of_is_0!(SystemColor); impl ToCss for SystemColor { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let s = match *self { % for color in system_colors + extra_colors: LookAndFeel_ColorID::eColorID_${to_rust_ident(color)} => "${color}", diff --git a/components/style/properties/longhand/counters.mako.rs b/components/style/properties/longhand/counters.mako.rs index c65a5ba323965..d55e880dcc9ac 100644 --- a/components/style/properties/longhand/counters.mako.rs +++ b/components/style/properties/longhand/counters.mako.rs @@ -23,8 +23,8 @@ pub mod computed_value { use cssparser; - use std::fmt; - use style_traits::ToCss; + use std::fmt::{self, Write}; + use style_traits::{CssWriter, ToCss}; #[cfg(feature = "gecko")] use values::specified::url::SpecifiedUrl; @@ -62,7 +62,10 @@ } impl ToCss for ContentItem { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { ContentItem::String(ref s) => s.to_css(dest), ContentItem::Counter(ref s, ref counter_style) => { @@ -106,7 +109,10 @@ } impl ToCss for T { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { T::Normal => dest.write_str("normal"), T::None => dest.write_str("none"), @@ -232,8 +238,8 @@ <%helpers:longhand name="counter-increment" animation_value_type="discrete" spec="https://drafts.csswg.org/css-lists/#propdef-counter-increment"> - use std::fmt; - use style_traits::ToCss; + use std::fmt::{self, Write}; + use style_traits::{CssWriter, ToCss}; use values::CustomIdent; #[cfg_attr(feature = "gecko", derive(MallocSizeOf))] @@ -241,16 +247,17 @@ pub struct SpecifiedValue(pub Vec<(CustomIdent, specified::Integer)>); pub mod computed_value { - use std::fmt; - use style_traits::ToCss; + use std::fmt::{self, Write}; + use style_traits::{CssWriter, ToCss}; use values::CustomIdent; #[derive(Clone, Debug, MallocSizeOf, PartialEq)] pub struct T(pub Vec<(CustomIdent, i32)>); impl ToCss for T { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { if self.0.is_empty() { return dest.write_str("none") @@ -292,10 +299,10 @@ computed_value::T(Vec::new()) } - impl ToCss for SpecifiedValue { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { if self.0.is_empty() { return dest.write_str("none"); diff --git a/components/style/properties/longhand/inherited_text.mako.rs b/components/style/properties/longhand/inherited_text.mako.rs index a9ae76fe9a34c..c7ad330a274e6 100644 --- a/components/style/properties/longhand/inherited_text.mako.rs +++ b/components/style/properties/longhand/inherited_text.mako.rs @@ -193,8 +193,8 @@ ${helpers.predefined_type( animation_value_type="discrete" spec="https://drafts.csswg.org/css-text-decor/#propdef-text-emphasis-style"> use computed_values::writing_mode::T as WritingMode; - use std::fmt; - use style_traits::ToCss; + use std::fmt::{self, Write}; + use style_traits::{CssWriter, ToCss}; use unicode_segmentation::UnicodeSegmentation; @@ -229,7 +229,7 @@ ${helpers.predefined_type( } impl ToCss for KeywordValue { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if let Some(fill) = self.fill() { if fill { dest.write_str("filled")?; @@ -246,8 +246,12 @@ ${helpers.predefined_type( Ok(()) } } + impl ToCss for computed_value::KeywordValue { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { if self.fill { dest.write_str("filled")?; } else { diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 738a07bedd069..51d7077a1d9d6 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -16,8 +16,9 @@ use custom_properties::CustomPropertiesBuilder; use servo_arc::{Arc, UniqueArc}; use smallbitvec::SmallBitVec; use std::borrow::Cow; -use std::{fmt, mem, ops}; +use std::{mem, ops}; use std::cell::RefCell; +use std::fmt::{self, Write}; #[cfg(feature = "servo")] use cssparser::RGBA; use cssparser::{CowRcStr, Parser, TokenSerializationType, serialize_identifier}; @@ -38,7 +39,7 @@ use selector_parser::PseudoElement; use selectors::parser::SelectorParseErrorKind; #[cfg(feature = "servo")] use servo_config::prefs::PREFS; use shared_lock::StylesheetGuards; -use style_traits::{ParsingMode, ToCss, ParseError, StyleParseErrorKind}; +use style_traits::{CssWriter, ParseError, ParsingMode, StyleParseErrorKind, ToCss}; use stylesheets::{CssRuleType, Origin, UrlExtraData}; #[cfg(feature = "servo")] use values::Either; use values::generics::text::LineHeight; @@ -852,9 +853,14 @@ impl ShorthandId { /// /// Returns an error if writing to the stream fails, or if the declarations /// do not map to a shorthand. - pub fn longhands_to_css<'a, W, I>(&self, declarations: I, dest: &mut W) -> fmt::Result - where W: fmt::Write, - I: Iterator, + pub fn longhands_to_css<'a, W, I>( + &self, + declarations: I, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + I: Iterator, { match *self { ShorthandId::All => { @@ -1072,8 +1078,9 @@ impl UnparsedValue { } impl<'a, T: ToCss> ToCss for DeclaredValue<'a, T> { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { match *self { DeclaredValue::Value(ref inner) => inner.to_css(dest), @@ -1101,8 +1108,9 @@ pub enum PropertyDeclarationId<'a> { } impl<'a> ToCss for PropertyDeclarationId<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { match *self { PropertyDeclarationId::Longhand(id) => dest.write_str(id.name()), @@ -1147,7 +1155,6 @@ impl<'a> PropertyDeclarationId<'a> { match *self { PropertyDeclarationId::Longhand(id) => id.name().into(), PropertyDeclarationId::Custom(name) => { - use std::fmt::Write; let mut s = String::new(); write!(&mut s, "--{}", name).unwrap(); s.into() @@ -1174,13 +1181,14 @@ pub enum PropertyId { impl fmt::Debug for PropertyId { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - self.to_css(formatter) + self.to_css(&mut CssWriter::new(formatter)) } } impl ToCss for PropertyId { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { match *self { PropertyId::Longhand(id) => dest.write_str(id.name()), @@ -1328,7 +1336,6 @@ impl PropertyId { PropertyId::LonghandAlias(id, _) | PropertyId::Longhand(id) => id.name().into(), PropertyId::Custom(ref name) => { - use std::fmt::Write; let mut s = String::new(); write!(&mut s, "--{}", name).unwrap(); s.into() @@ -1461,15 +1468,17 @@ pub enum PropertyDeclaration { impl fmt::Debug for PropertyDeclaration { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.id().to_css(f)?; - f.write_str(": ")?; - self.to_css(f) + let mut dest = CssWriter::new(f); + self.id().to_css(&mut dest)?; + dest.write_str(": ")?; + self.to_css(&mut dest) } } impl ToCss for PropertyDeclaration { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { match *self { % for property in data.longhands: diff --git a/components/style/properties/shorthand/background.mako.rs b/components/style/properties/shorthand/background.mako.rs index 4fb5f2c10f43b..8fd2101ecec2b 100644 --- a/components/style/properties/shorthand/background.mako.rs +++ b/components/style/properties/shorthand/background.mako.rs @@ -131,7 +131,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let len = self.background_image.0.len(); // There should be at least one declared value if len == 0 { @@ -228,7 +228,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let len = self.background_position_x.0.len(); if len == 0 || len != self.background_position_y.0.len() { return Ok(()); diff --git a/components/style/properties/shorthand/border.mako.rs b/components/style/properties/shorthand/border.mako.rs index 3d175350a90ed..0209ebb23766c 100644 --- a/components/style/properties/shorthand/border.mako.rs +++ b/components/style/properties/shorthand/border.mako.rs @@ -34,7 +34,7 @@ ${helpers.four_sides_shorthand("border-style", "border-%s-style", } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { % for side in PHYSICAL_SIDES: let ${side} = &self.border_${side}_width; % endfor @@ -113,7 +113,7 @@ pub fn parse_border<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { super::serialize_directional_border( dest, self.border_${to_rust_ident(side)}_width, @@ -156,7 +156,7 @@ pub fn parse_border<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let all_equal = { % for side in PHYSICAL_SIDES: let border_${side}_width = self.border_${side}_width; @@ -215,7 +215,7 @@ pub fn parse_border<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let LonghandsToSerialize { border_top_left_radius: &BorderCornerRadius(ref tl), border_top_right_radius: &BorderCornerRadius(ref tr), @@ -315,7 +315,7 @@ pub fn parse_border<'i, 't>(context: &ParserContext, input: &mut Parser<'i, 't>) } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.border_image_source.to_css(dest)?; dest.write_str(" ")?; self.border_image_slice.to_css(dest)?; diff --git a/components/style/properties/shorthand/box.mako.rs b/components/style/properties/shorthand/box.mako.rs index ca8523173f8a8..137599078ad2c 100644 --- a/components/style/properties/shorthand/box.mako.rs +++ b/components/style/properties/shorthand/box.mako.rs @@ -48,7 +48,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if self.overflow_x == self.overflow_y { self.overflow_x.to_css(dest) } else { @@ -83,7 +83,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.overflow_clip_box_block.to_css(dest)?; if self.overflow_clip_box_block != self.overflow_clip_box_inline { @@ -203,7 +203,7 @@ macro_rules! try_parse_one { } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let property_len = self.transition_property.0.len(); // There are two cases that we can do shorthand serialization: @@ -327,7 +327,7 @@ macro_rules! try_parse_one { } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let len = self.animation_name.0.len(); // There should be at least one declared value if len == 0 { @@ -376,7 +376,7 @@ macro_rules! try_parse_one { impl<'a> ToCss for LonghandsToSerialize<'a> { // Serializes into the single keyword value if both scroll-snap-type-x and scroll-snap-type-y are same. // Otherwise into an empty string. This is done to match Gecko's behaviour. - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if self.scroll_snap_type_x == self.scroll_snap_type_y { self.scroll_snap_type_x.to_css(dest) } else { @@ -406,7 +406,7 @@ macro_rules! try_parse_one { impl<'a> ToCss for LonghandsToSerialize<'a> { // Serializes into the single keyword value if both overscroll-behavior-x and overscroll-behavior-y are same. // Otherwise into two values separated by a space. - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.overscroll_behavior_x.to_css(dest)?; if self.overscroll_behavior_y != self.overscroll_behavior_x { dest.write_str(" ")?; diff --git a/components/style/properties/shorthand/font.mako.rs b/components/style/properties/shorthand/font.mako.rs index b40505e3c8602..22bbe7d71ff42 100644 --- a/components/style/properties/shorthand/font.mako.rs +++ b/components/style/properties/shorthand/font.mako.rs @@ -151,9 +151,14 @@ } impl<'a> LonghandsToSerialize<'a> { - fn to_css_for(&self, - serialize_for: SerializeFor, - dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css_for( + &self, + serialize_for: SerializeFor, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { % if product == "gecko": match self.check_system() { CheckSystemResult::AllSystem(sys) => return sys.to_css(dest), @@ -226,7 +231,7 @@ } /// Serialize the shorthand value for canvas font attribute. - pub fn to_css_for_canvas(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + pub fn to_css_for_canvas(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.to_css_for(SerializeFor::Canvas, dest) } % endif @@ -234,7 +239,7 @@ // This may be a bit off, unsure, possibly needs changes impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.to_css_for(SerializeFor::Normal, dest) } } @@ -309,7 +314,7 @@ impl<'a> ToCss for LonghandsToSerialize<'a> { #[allow(unused_assignments)] - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let has_none_ligatures = % if product == "gecko": diff --git a/components/style/properties/shorthand/inherited_svg.mako.rs b/components/style/properties/shorthand/inherited_svg.mako.rs index 76f1c840f0e8f..1742ef28ed744 100644 --- a/components/style/properties/shorthand/inherited_svg.mako.rs +++ b/components/style/properties/shorthand/inherited_svg.mako.rs @@ -22,7 +22,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if self.marker_start == self.marker_mid && self.marker_mid == self.marker_end { self.marker_start.to_css(dest) } else { diff --git a/components/style/properties/shorthand/mask.mako.rs b/components/style/properties/shorthand/mask.mako.rs index 87feba23d1624..483ce9b50f870 100644 --- a/components/style/properties/shorthand/mask.mako.rs +++ b/components/style/properties/shorthand/mask.mako.rs @@ -121,7 +121,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { use properties::longhands::mask_origin::single_value::computed_value::T as Origin; use properties::longhands::mask_clip::single_value::computed_value::T as Clip; @@ -214,7 +214,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let len = self.mask_position_x.0.len(); if len == 0 || self.mask_position_y.0.len() != len { return Ok(()); diff --git a/components/style/properties/shorthand/outline.mako.rs b/components/style/properties/shorthand/outline.mako.rs index 0fe8a11118be1..95d6b0be28ae2 100644 --- a/components/style/properties/shorthand/outline.mako.rs +++ b/components/style/properties/shorthand/outline.mako.rs @@ -76,7 +76,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { use values::generics::border::BorderCornerRadius; let LonghandsToSerialize { diff --git a/components/style/properties/shorthand/position.mako.rs b/components/style/properties/shorthand/position.mako.rs index 1bddeeb8b335b..95a4c183e2057 100644 --- a/components/style/properties/shorthand/position.mako.rs +++ b/components/style/properties/shorthand/position.mako.rs @@ -119,7 +119,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if self.grid_row_gap == self.grid_column_gap { self.grid_row_gap.to_css(dest) } else { @@ -163,7 +163,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.grid_${kind}_start.to_css(dest)?; dest.write_str(" / ")?; self.grid_${kind}_end.to_css(dest) @@ -224,7 +224,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.grid_row_start.to_css(dest)?; let values = [&self.grid_column_start, &self.grid_row_end, &self.grid_column_end]; for value in &values { @@ -362,10 +362,14 @@ } /// Serialization for `` shorthand (also used by `grid` shorthand). - pub fn serialize_grid_template(template_rows: &GridTemplateComponent, - template_columns: &GridTemplateComponent, - template_areas: &Either, - dest: &mut W) -> fmt::Result where W: fmt::Write { + pub fn serialize_grid_template( + template_rows: &GridTemplateComponent, + template_columns: &GridTemplateComponent, + template_areas: &Either, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write { match *template_areas { Either::Second(_none) => { template_rows.to_css(dest)?; @@ -451,7 +455,7 @@ impl<'a> ToCss for LonghandsToSerialize<'a> { #[inline] - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { serialize_grid_template(self.grid_template_rows, self.grid_template_columns, self.grid_template_areas, dest) } @@ -542,7 +546,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if *self.grid_template_areas != Either::Second(None_) || (*self.grid_template_rows != GridTemplateComponent::None && *self.grid_template_columns != GridTemplateComponent::None) || @@ -635,7 +639,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.align_content.to_css(dest)?; if self.align_content != self.justify_content { dest.write_str(" ")?; @@ -670,7 +674,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if self.align_self == self.justify_self { self.align_self.to_css(dest) } else { @@ -713,7 +717,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if self.align_items.0 == self.justify_items.0 { self.align_items.to_css(dest) } else { diff --git a/components/style/properties/shorthand/serialize.mako.rs b/components/style/properties/shorthand/serialize.mako.rs index a86704a5488db..afcbe3c488bb9 100644 --- a/components/style/properties/shorthand/serialize.mako.rs +++ b/components/style/properties/shorthand/serialize.mako.rs @@ -2,15 +2,20 @@ * 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 style_traits::ToCss; +use style_traits::{CssWriter, ToCss}; use values::specified::{BorderStyle, Color}; -use std::fmt; +use std::fmt::{self, Write}; -fn serialize_directional_border(dest: &mut W, - width: &I, - style: &BorderStyle, - color: &Color) - -> fmt::Result where W: fmt::Write, I: ToCss { +fn serialize_directional_border( + dest: &mut CssWriter, + width: &I, + style: &BorderStyle, + color: &Color, +) -> fmt::Result +where + W: Write, + I: ToCss, +{ width.to_css(dest)?; dest.write_str(" ")?; style.to_css(dest)?; diff --git a/components/style/properties/shorthand/text.mako.rs b/components/style/properties/shorthand/text.mako.rs index 2c21d1971bf9b..c53f1b8b27e4c 100644 --- a/components/style/properties/shorthand/text.mako.rs +++ b/components/style/properties/shorthand/text.mako.rs @@ -62,7 +62,7 @@ } impl<'a> ToCss for LonghandsToSerialize<'a> { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.text_decoration_line.to_css(dest)?; % if product == "gecko": diff --git a/components/style/selector_parser.rs b/components/style/selector_parser.rs index 54978d284b721..e68aded1c9a7f 100644 --- a/components/style/selector_parser.rs +++ b/components/style/selector_parser.rs @@ -9,7 +9,7 @@ use cssparser::{Parser as CssParser, ParserInput}; use selectors::parser::SelectorList; use std::fmt::{self, Debug, Write}; -use style_traits::{ParseError, ToCss}; +use style_traits::{CssWriter, ParseError, ToCss}; use stylesheets::{Origin, Namespaces, UrlExtraData}; /// A convenient alias for the type that represents an attribute value used for @@ -201,7 +201,7 @@ impl Direction { } impl ToCss for Direction { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: Write { let dir_str = match *self { Direction::Rtl => "rtl", Direction::Ltr => "ltr", diff --git a/components/style/servo/media_queries.rs b/components/style/servo/media_queries.rs index ea71ee84ab0da..79bc288b775a1 100644 --- a/components/style/servo/media_queries.rs +++ b/components/style/servo/media_queries.rs @@ -12,9 +12,9 @@ use media_queries::MediaType; use parser::ParserContext; use properties::ComputedValues; use selectors::parser::SelectorParseErrorKind; -use std::fmt; +use std::fmt::{self, Write}; use std::sync::atomic::{AtomicBool, AtomicIsize, Ordering}; -use style_traits::{CSSPixel, DevicePixel, ToCss, ParseError}; +use style_traits::{CSSPixel, CssWriter, DevicePixel, ToCss, ParseError}; use style_traits::viewport::ViewportConstraints; use values::computed::{self, ToComputedValue}; use values::computed::font::FontSize; @@ -218,8 +218,9 @@ impl Expression { } impl ToCss for Expression { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { let (s, l) = match self.0 { ExpressionKind::Width(Range::Min(ref l)) => ("(min-width: ", l), diff --git a/components/style/servo/url.rs b/components/style/servo/url.rs index 6dc1d46d2aa04..25a4aa5297d45 100644 --- a/components/style/servo/url.rs +++ b/components/style/servo/url.rs @@ -6,12 +6,12 @@ use parser::ParserContext; use servo_url::ServoUrl; -use std::fmt; +use std::fmt::{self, Write}; // Note: We use std::sync::Arc rather than servo_arc::Arc here because the // nonzero optimization is important in keeping the size of SpecifiedUrl below // the threshold. use std::sync::Arc; -use style_traits::{ToCss, ParseError}; +use style_traits::{CssWriter, ParseError, ToCss}; use values::computed::{Context, ToComputedValue, ComputedUrl}; /// A specified url() value for servo. @@ -111,7 +111,10 @@ impl PartialEq for SpecifiedUrl { } impl ToCss for SpecifiedUrl { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let string = match self.original { Some(ref original) => &**original, None => match self.resolved { diff --git a/components/style/shared_lock.rs b/components/style/shared_lock.rs index 29b92b29d36c2..3e922b31f54bf 100644 --- a/components/style/shared_lock.rs +++ b/components/style/shared_lock.rs @@ -10,9 +10,10 @@ use atomic_refcell::{AtomicRefCell, AtomicRef, AtomicRefMut}; use parking_lot::RwLock; use servo_arc::Arc; use std::cell::UnsafeCell; -use std::fmt; +use std::fmt::{self, Write}; #[cfg(feature = "gecko")] use std::ptr; +use style_traits::CssWriter; use stylesheets::Origin; /// A shared read/write lock that can protect multiple objects. @@ -222,8 +223,13 @@ mod compile_time_assert { /// Like ToCss, but with a lock guard given by the caller. pub trait ToCssWithGuard { /// Serialize `self` in CSS syntax, writing to `dest`, using the given lock guard. - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write; + fn to_css( + &self, + guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write; /// Serialize `self` in CSS syntax using the given lock guard and return a string. /// @@ -231,7 +237,7 @@ pub trait ToCssWithGuard { #[inline] fn to_css_string(&self, guard: &SharedRwLockReadGuard) -> String { let mut s = String::new(); - self.to_css(guard, &mut s).unwrap(); + self.to_css(guard, &mut CssWriter::new(&mut s)).unwrap(); s } } diff --git a/components/style/stylesheets/document_rule.rs b/components/style/stylesheets/document_rule.rs index 61c8f2762ef8d..236dd48bb4347 100644 --- a/components/style/stylesheets/document_rule.rs +++ b/components/style/stylesheets/document_rule.rs @@ -13,8 +13,8 @@ use media_queries::Device; use parser::{Parse, ParserContext}; use servo_arc::Arc; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; -use style_traits::{ToCss, ParseError, StyleParseErrorKind}; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss}; use stylesheets::CssRules; use values::specified::url::SpecifiedUrl; @@ -40,8 +40,14 @@ impl DocumentRule { } impl ToCssWithGuard for DocumentRule { - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { dest.write_str("@-moz-document ")?; self.condition.to_css(dest)?; dest.write_str(" {")?; @@ -167,8 +173,10 @@ impl UrlMatchingFunction { } impl ToCss for UrlMatchingFunction { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { UrlMatchingFunction::Url(ref url) => { url.to_css(dest) @@ -219,8 +227,10 @@ impl DocumentCondition { } impl ToCss for DocumentCondition { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let mut iter = self.0.iter(); let first = iter.next() .expect("Empty DocumentCondition, should contain at least one URL matching function"); diff --git a/components/style/stylesheets/font_feature_values_rule.rs b/components/style/stylesheets/font_feature_values_rule.rs index a880d402b45c6..b07a84374240b 100644 --- a/components/style/stylesheets/font_feature_values_rule.rs +++ b/components/style/stylesheets/font_feature_values_rule.rs @@ -16,8 +16,8 @@ use gecko_bindings::bindings::Gecko_AppendFeatureValueHashEntry; use gecko_bindings::structs::{self, gfxFontFeatureValueSet, nsTArray}; use parser::{ParserContext, ParserErrorContext, Parse}; use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; -use style_traits::{ParseError, StyleParseErrorKind, ToCss}; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss}; use stylesheets::CssRuleType; use values::computed::font::FamilyName; @@ -36,7 +36,10 @@ pub struct FFVDeclaration { } impl ToCss for FFVDeclaration { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { serialize_identifier(&self.name.to_string(), dest)?; dest.write_str(": ")?; self.value.to_css(dest)?; @@ -100,7 +103,10 @@ impl Parse for PairValues { } impl ToCss for PairValues { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { self.0.to_css(dest)?; if let Some(second) = self.1 { dest.write_char(' ')?; @@ -152,7 +158,10 @@ impl Parse for VectorValues { } impl ToCss for VectorValues { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let mut iter = self.0.iter(); let first = iter.next(); if let Some(first) = first { @@ -279,7 +288,13 @@ macro_rules! font_feature_values_blocks { } /// Prints font family names. - pub fn font_family_to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + pub fn font_family_to_css( + &self, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { let mut iter = self.family_names.iter(); iter.next().unwrap().to_css(dest)?; for val in iter { @@ -290,7 +305,10 @@ macro_rules! font_feature_values_blocks { } /// Prints inside of `@font-feature-values` block. - pub fn value_to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + pub fn value_to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { $( if self.$ident.len() > 0 { dest.write_str(concat!("@", $name, " {\n"))?; @@ -341,8 +359,13 @@ macro_rules! font_feature_values_blocks { } impl ToCssWithGuard for FontFeatureValuesRule { - fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write + fn to_css( + &self, + _guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, { dest.write_str("@font-feature-values ")?; self.font_family_to_css(dest)?; diff --git a/components/style/stylesheets/import_rule.rs b/components/style/stylesheets/import_rule.rs index 8568012099a40..3d8f8d14f9f29 100644 --- a/components/style/stylesheets/import_rule.rs +++ b/components/style/stylesheets/import_rule.rs @@ -9,8 +9,8 @@ use cssparser::SourceLocation; use media_queries::MediaList; use shared_lock::{DeepCloneWithLock, DeepCloneParams, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use stylesheets::{StylesheetContents, StylesheetInDocument}; use values::specified::url::SpecifiedUrl; @@ -107,8 +107,13 @@ impl DeepCloneWithLock for ImportRule { } impl ToCssWithGuard for ImportRule { - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css( + &self, + guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, { dest.write_str("@import ")?; self.url.to_css(dest)?; diff --git a/components/style/stylesheets/keyframes_rule.rs b/components/style/stylesheets/keyframes_rule.rs index 60d7ee3f17522..98fd108235233 100644 --- a/components/style/stylesheets/keyframes_rule.rs +++ b/components/style/stylesheets/keyframes_rule.rs @@ -14,8 +14,8 @@ use properties::LonghandIdSet; use properties::longhands::transition_timing_function::single_value::SpecifiedValue as SpecifiedTimingFunction; use servo_arc::Arc; use shared_lock::{DeepCloneParams, DeepCloneWithLock, SharedRwLock, SharedRwLockReadGuard, Locked, ToCssWithGuard}; -use std::fmt; -use style_traits::{ParsingMode, ToCss, ParseError, StyleParseErrorKind}; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ParseError, ParsingMode, StyleParseErrorKind, ToCss}; use stylesheets::{CssRuleType, StylesheetContents}; use stylesheets::rule_parser::VendorPrefix; use values::{KeyframesName, serialize_percentage}; @@ -37,8 +37,9 @@ pub struct KeyframesRule { impl ToCssWithGuard for KeyframesRule { // Serialization of KeyframesRule is not specced. - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { dest.write_str("@keyframes ")?; self.name.to_css(dest)?; @@ -110,7 +111,10 @@ impl ::std::cmp::Ord for KeyframePercentage { impl ::std::cmp::Eq for KeyframePercentage { } impl ToCss for KeyframePercentage { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { serialize_percentage(self.0, dest) } } @@ -147,7 +151,10 @@ impl KeyframePercentage { pub struct KeyframeSelector(Vec); impl ToCss for KeyframeSelector { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let mut iter = self.0.iter(); iter.next().unwrap().to_css(dest)?; for percentage in iter { @@ -194,8 +201,14 @@ pub struct Keyframe { } impl ToCssWithGuard for Keyframe { - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { self.selector.to_css(dest)?; dest.write_str(" { ")?; self.block.read_with(guard).to_css(dest)?; diff --git a/components/style/stylesheets/media_rule.rs b/components/style/stylesheets/media_rule.rs index 4c468b44cf6c2..b4fbdf3f8b506 100644 --- a/components/style/stylesheets/media_rule.rs +++ b/components/style/stylesheets/media_rule.rs @@ -12,8 +12,8 @@ use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf}; use media_queries::MediaList; use servo_arc::Arc; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use stylesheets::CssRules; /// An [`@media`][media] urle. @@ -42,8 +42,14 @@ impl MediaRule { impl ToCssWithGuard for MediaRule { // Serialization of MediaRule is not specced. // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { dest.write_str("@media ")?; self.media_queries.read_with(guard).to_css(dest)?; self.rules.read_with(guard).to_css_block(guard, dest) diff --git a/components/style/stylesheets/mod.rs b/components/style/stylesheets/mod.rs index f419b9602ed9d..b9adf34e31b56 100644 --- a/components/style/stylesheets/mod.rs +++ b/components/style/stylesheets/mod.rs @@ -31,7 +31,7 @@ use parser::{ParserContext, ParserErrorContext}; use servo_arc::Arc; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; use std::fmt; -use style_traits::ParsingMode; +use style_traits::{CssWriter, ParsingMode}; pub use self::counter_style_rule::CounterStyleRule; pub use self::document_rule::DocumentRule; @@ -347,7 +347,7 @@ impl DeepCloneWithLock for CssRule { impl ToCssWithGuard for CssRule { // https://drafts.csswg.org/cssom/#serialize-a-css-rule - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result + fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { match *self { CssRule::Namespace(ref lock) => lock.read_with(guard).to_css(guard, dest), diff --git a/components/style/stylesheets/namespace_rule.rs b/components/style/stylesheets/namespace_rule.rs index 6cfba1d7234c5..8f3ed1c2ab5c6 100644 --- a/components/style/stylesheets/namespace_rule.rs +++ b/components/style/stylesheets/namespace_rule.rs @@ -7,7 +7,8 @@ use {Namespace, Prefix}; use cssparser::SourceLocation; use shared_lock::{SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; +use std::fmt::{self, Write}; +use style_traits::CssWriter; /// A `@namespace` rule. #[derive(Clone, Debug, PartialEq)] @@ -23,8 +24,14 @@ pub struct NamespaceRule { impl ToCssWithGuard for NamespaceRule { // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSNamespaceRule - fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + _guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { dest.write_str("@namespace ")?; if let Some(ref prefix) = self.prefix { dest.write_str(&*prefix.to_string())?; diff --git a/components/style/stylesheets/page_rule.rs b/components/style/stylesheets/page_rule.rs index ea8b93b1b8b36..57843af9caa74 100644 --- a/components/style/stylesheets/page_rule.rs +++ b/components/style/stylesheets/page_rule.rs @@ -12,8 +12,8 @@ use malloc_size_of::{MallocSizeOf, MallocSizeOfOps, MallocUnconditionalShallowSi use properties::PropertyDeclarationBlock; use servo_arc::Arc; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; /// A [`@page`][page] rule. /// @@ -44,8 +44,13 @@ impl PageRule { impl ToCssWithGuard for PageRule { /// Serialization of PageRule is not specced, adapted from steps for /// StyleRule. - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css( + &self, + guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, { dest.write_str("@page { ")?; let declaration_block = self.block.read_with(guard); diff --git a/components/style/stylesheets/rule_list.rs b/components/style/stylesheets/rule_list.rs index fbceb6c1a0b02..f25cbb17a235b 100644 --- a/components/style/stylesheets/rule_list.rs +++ b/components/style/stylesheets/rule_list.rs @@ -9,7 +9,8 @@ use malloc_size_of::{MallocShallowSizeOf, MallocSizeOfOps}; use servo_arc::{Arc, RawOffsetArc}; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked}; use shared_lock::{SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; +use std::fmt::{self, Write}; +use style_traits::CssWriter; use stylesheets::{CssRule, RulesMutateError}; use stylesheets::loader::StylesheetLoader; use stylesheets::rule_parser::State; @@ -95,8 +96,13 @@ impl CssRules { /// /// This should be speced into CSSOM spec at some point. See /// - pub fn to_css_block(&self, guard: &SharedRwLockReadGuard, dest: &mut W) - -> fmt::Result where W: fmt::Write + pub fn to_css_block( + &self, + guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, { dest.write_str(" {")?; for rule in self.0.iter() { diff --git a/components/style/stylesheets/style_rule.rs b/components/style/stylesheets/style_rule.rs index fbc391f6d2ba2..c8e5dc4df8a7d 100644 --- a/components/style/stylesheets/style_rule.rs +++ b/components/style/stylesheets/style_rule.rs @@ -12,8 +12,8 @@ use selector_parser::SelectorImpl; use selectors::SelectorList; use servo_arc::Arc; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; /// A style rule, with selectors and declarations. #[derive(Debug)] @@ -67,8 +67,9 @@ impl StyleRule { impl ToCssWithGuard for StyleRule { /// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { use cssparser::ToCss; diff --git a/components/style/stylesheets/supports_rule.rs b/components/style/stylesheets/supports_rule.rs index d9870641e431a..9ea0916f1348e 100644 --- a/components/style/stylesheets/supports_rule.rs +++ b/components/style/stylesheets/supports_rule.rs @@ -15,9 +15,9 @@ use servo_arc::Arc; use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; #[allow(unused_imports)] use std::ascii::AsciiExt; use std::ffi::{CStr, CString}; -use std::fmt; +use std::fmt::{self, Write}; use std::str; -use style_traits::{ToCss, ParseError}; +use style_traits::{CssWriter, ToCss, ParseError}; use stylesheets::{CssRuleType, CssRules}; /// An [`@supports`][supports] rule. @@ -46,8 +46,10 @@ impl SupportsRule { } impl ToCssWithGuard for SupportsRule { - fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { dest.write_str("@supports ")?; self.condition.to_css(dest)?; self.rules.read_with(guard).to_css_block(guard, dest) @@ -219,8 +221,9 @@ pub fn parse_condition_or_declaration<'i, 't>(input: &mut Parser<'i, 't>) } impl ToCss for SupportsCondition { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { match *self { SupportsCondition::Not(ref cond) => { @@ -276,7 +279,10 @@ impl ToCss for SupportsCondition { pub struct Declaration(pub String); impl ToCss for Declaration { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { dest.write_str(&self.0) } } diff --git a/components/style/stylesheets/viewport_rule.rs b/components/style/stylesheets/viewport_rule.rs index 6220d85874561..008ddb25b5de2 100644 --- a/components/style/stylesheets/viewport_rule.rs +++ b/components/style/stylesheets/viewport_rule.rs @@ -23,10 +23,10 @@ use shared_lock::{SharedRwLockReadGuard, StylesheetGuards, ToCssWithGuard}; #[allow(unused_imports)] use std::ascii::AsciiExt; use std::borrow::Cow; use std::cell::RefCell; -use std::fmt; +use std::fmt::{self, Write}; use std::iter::Enumerate; use std::str::Chars; -use style_traits::{PinchZoomFactor, ToCss, ParseError, StyleParseErrorKind}; +use style_traits::{CssWriter, ParseError, PinchZoomFactor, StyleParseErrorKind, ToCss}; use style_traits::viewport::{Orientation, UserZoom, ViewportConstraints, Zoom}; use stylesheets::{StylesheetInDocument, Origin}; use values::computed::{Context, ToComputedValue}; @@ -100,7 +100,10 @@ macro_rules! declare_viewport_descriptor_inner { } impl ToCss for ViewportDescriptor { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { $( ViewportDescriptor::$assigned_variant(ref val) => { @@ -148,8 +151,9 @@ pub enum ViewportLength { } impl ToCss for ViewportLength { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write, + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { match *self { ViewportLength::Specified(ref length) => length.to_css(dest), @@ -254,7 +258,10 @@ impl ViewportDescriptorDeclaration { } impl ToCss for ViewportDescriptorDeclaration { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { self.descriptor.to_css(dest)?; if self.important { dest.write_str(" !important")?; @@ -520,8 +527,14 @@ impl ViewportRule { impl ToCssWithGuard for ViewportRule { // Serialization of ViewportRule is not specced. - fn to_css(&self, _guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result - where W: fmt::Write { + fn to_css( + &self, + _guard: &SharedRwLockReadGuard, + dest: &mut CssWriter, + ) -> fmt::Result + where + W: Write, + { dest.write_str("@viewport { ")?; let mut iter = self.declarations.iter(); iter.next().unwrap().to_css(dest)?; diff --git a/components/style/values/computed/align.rs b/components/style/values/computed/align.rs index f3caade67a5be..85f8f70cb25d5 100644 --- a/components/style/values/computed/align.rs +++ b/components/style/values/computed/align.rs @@ -7,7 +7,7 @@ //! https://drafts.csswg.org/css-align/ use std::fmt; -use style_traits::ToCss; +use style_traits::{CssWriter, ToCss}; use values::computed::{Context, ToComputedValue}; use values::specified; @@ -26,7 +26,7 @@ pub struct JustifyItems { } impl ToCss for JustifyItems { - fn to_css(&self, dest: &mut W) -> fmt::Result + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write, { self.computed.to_css(dest) diff --git a/components/style/values/computed/background.rs b/components/style/values/computed/background.rs index e71f208c98414..7b7f7c4991bd1 100644 --- a/components/style/values/computed/background.rs +++ b/components/style/values/computed/background.rs @@ -6,8 +6,8 @@ use properties::animated_properties::RepeatableListAnimatable; use properties::longhands::background_size::computed_value::T as BackgroundSizeList; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use values::animated::{ToAnimatedValue, ToAnimatedZero}; use values::computed::{Context, ToComputedValue}; use values::computed::length::LengthOrPercentageOrAuto; @@ -96,9 +96,9 @@ impl BackgroundRepeat { } impl ToCss for BackgroundRepeat { - fn to_css(&self, dest: &mut W) -> fmt::Result + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where - W: fmt::Write, + W: Write, { match (self.0, self.1) { (RepeatKeyword::Repeat, RepeatKeyword::NoRepeat) => dest.write_str("repeat-x"), diff --git a/components/style/values/computed/basic_shape.rs b/components/style/values/computed/basic_shape.rs index 20dcffec9c6db..17c39741c81ca 100644 --- a/components/style/values/computed/basic_shape.rs +++ b/components/style/values/computed/basic_shape.rs @@ -7,8 +7,8 @@ //! //! [basic-shape]: https://drafts.csswg.org/css-shapes/#typedef-basic-shape -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use values::computed::{LengthOrPercentage, ComputedUrl, Image}; use values::generics::basic_shape::{BasicShape as GenericBasicShape}; use values::generics::basic_shape::{Circle as GenericCircle, ClippingShape as GenericClippingShape}; @@ -37,7 +37,10 @@ pub type Ellipse = GenericEllipse; impl ToCss for Circle { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { dest.write_str("circle(")?; self.radius.to_css(dest)?; dest.write_str(" at ")?; @@ -47,7 +50,10 @@ impl ToCss for Circle { } impl ToCss for Ellipse { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { dest.write_str("ellipse(")?; if (self.semiaxis_x, self.semiaxis_y) != Default::default() { self.semiaxis_x.to_css(dest)?; diff --git a/components/style/values/computed/color.rs b/components/style/values/computed/color.rs index 6327e73462636..314c4bed26602 100644 --- a/components/style/values/computed/color.rs +++ b/components/style/values/computed/color.rs @@ -6,7 +6,7 @@ use cssparser::{Color as CSSParserColor, RGBA}; use std::fmt; -use style_traits::ToCss; +use style_traits::{CssWriter, ToCss}; use values::animated::ToAnimatedValue; use values::animated::color::{Color as AnimatedColor, RGBA as AnimatedRGBA}; @@ -138,7 +138,7 @@ impl From for Color { } impl ToCss for Color { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { if self.is_numeric() { self.color.to_css(dest) } else if self.is_currentcolor() { diff --git a/components/style/values/computed/font.rs b/components/style/values/computed/font.rs index 407be16c203c1..20a41154ec9f5 100644 --- a/components/style/values/computed/font.rs +++ b/components/style/values/computed/font.rs @@ -19,7 +19,7 @@ use std::fmt::{self, Write}; use std::hash::{Hash, Hasher}; #[cfg(feature = "servo")] use std::slice; -use style_traits::{ToCss, ParseError}; +use style_traits::{CssWriter, ParseError, ToCss}; use values::CSSFloat; use values::animated::{ToAnimatedValue, ToAnimatedZero}; use values::computed::{Context, NonNegativeLength, ToComputedValue}; @@ -201,7 +201,7 @@ impl FontSize { } impl ToCss for FontSize { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { self.size.to_css(dest) } } @@ -257,7 +257,7 @@ impl MallocSizeOf for FontFamily { } impl ToCss for FontFamily { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { let mut iter = self.0.iter(); iter.next().unwrap().to_css(dest)?; for family in iter { @@ -279,7 +279,7 @@ pub struct FamilyName { } impl ToCss for FamilyName { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { match self.syntax { FamilyNameSyntax::Quoted => { dest.write_char('"')?; @@ -488,7 +488,7 @@ impl SingleFontFamily { } impl ToCss for SingleFontFamily { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { match *self { SingleFontFamily::FamilyName(ref name) => name.to_css(dest), @@ -731,7 +731,7 @@ impl FontLanguageOverride { } impl ToCss for FontLanguageOverride { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write { use std::str; if self.0 == 0 { diff --git a/components/style/values/computed/image.rs b/components/style/values/computed/image.rs index 8972774aa77f5..93fa627a41d0b 100644 --- a/components/style/values/computed/image.rs +++ b/components/style/values/computed/image.rs @@ -9,8 +9,8 @@ use cssparser::RGBA; use std::f32::consts::PI; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use values::{Either, None_}; use values::computed::{Angle, ComputedUrl, Context, Length, LengthOrPercentage, NumberOrPercentage, ToComputedValue}; #[cfg(feature = "gecko")] @@ -99,8 +99,13 @@ impl GenericLineDirection for LineDirection { } } - fn to_css(&self, dest: &mut W, compat_mode: CompatMode) -> fmt::Result - where W: fmt::Write + fn to_css( + &self, + dest: &mut CssWriter, + compat_mode: CompatMode, + ) -> fmt::Result + where + W: Write, { match *self { LineDirection::Angle(ref angle) => angle.to_css(dest), diff --git a/components/style/values/computed/inherited_box.rs b/components/style/values/computed/inherited_box.rs index 2fa4dd819a56b..f6fe6ad7bf960 100644 --- a/components/style/values/computed/inherited_box.rs +++ b/components/style/values/computed/inherited_box.rs @@ -4,8 +4,8 @@ //! Computed values for inherited box -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use values::specified::Angle; /// An angle rounded and normalized per https://drafts.csswg.org/css-images/#propdef-image-orientation @@ -31,7 +31,10 @@ impl Orientation { } impl ToCss for Orientation { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { // Should agree with Angle::to_css. match *self { Orientation::Angle0 => dest.write_str("0deg"), @@ -60,7 +63,10 @@ impl ImageOrientation { } impl ToCss for ImageOrientation { - fn to_css(&self, dest: &mut W) -> fmt::Result { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { match *self { ImageOrientation::FromImage => dest.write_str("from-image"), ImageOrientation::AngleWithFlipped(angle, flipped) => { diff --git a/components/style/values/computed/length.rs b/components/style/values/computed/length.rs index a0a92c713c2c4..49db7201a39ac 100644 --- a/components/style/values/computed/length.rs +++ b/components/style/values/computed/length.rs @@ -6,9 +6,9 @@ use app_units::Au; use ordered_float::NotNaN; -use std::fmt; +use std::fmt::{self, Write}; use std::ops::{Add, Neg}; -use style_traits::ToCss; +use style_traits::{CssWriter, ToCss}; use style_traits::values::specified::AllowedNumericType; use super::{Number, ToComputedValue, Context, Percentage}; use values::{Auto, CSSFloat, Either, ExtremumLength, None_, Normal, specified}; @@ -203,7 +203,10 @@ impl From for Option { } impl ToCss for CalcLengthOrPercentage { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { use num_traits::Zero; let (length, percentage) = match (self.length, self.percentage) { @@ -738,7 +741,10 @@ impl CSSPixelLength { impl ToCss for CSSPixelLength { #[inline] - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { self.0.to_css(dest)?; dest.write_str("px") } diff --git a/components/style/values/computed/mod.rs b/components/style/values/computed/mod.rs index 20bcefc655bb6..a559f4fbe8772 100644 --- a/components/style/values/computed/mod.rs +++ b/components/style/values/computed/mod.rs @@ -15,11 +15,12 @@ use properties::{ComputedValues, LonghandId, StyleBuilder}; use rule_cache::RuleCacheConditions; #[cfg(feature = "servo")] use servo_url::ServoUrl; -use std::{f32, fmt}; use std::cell::RefCell; +use std::f32; +use std::fmt::{self, Write}; #[cfg(feature = "servo")] use std::sync::Arc; -use style_traits::ToCss; +use style_traits::{CssWriter, ToCss}; use style_traits::cursor::CursorKind; use super::{CSSFloat, CSSInteger}; use super::generics::{GreaterThanOrEqualToOne, NonNegative}; @@ -531,7 +532,10 @@ pub struct ClipRect { } impl ToCss for ClipRect { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { dest.write_str("rect(")?; if let Some(top) = self.top { top.to_css(dest)?; @@ -627,7 +631,10 @@ impl ComputedUrl { #[cfg(feature = "servo")] impl ToCss for ComputedUrl { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let string = match *self { ComputedUrl::Valid(ref url) => url.as_str(), ComputedUrl::Invalid(ref invalid_string) => invalid_string, diff --git a/components/style/values/computed/percentage.rs b/components/style/values/computed/percentage.rs index 1bc28a13488ec..842019f03c8b1 100644 --- a/components/style/values/computed/percentage.rs +++ b/components/style/values/computed/percentage.rs @@ -5,7 +5,7 @@ //! Computed percentages. use std::fmt; -use style_traits::ToCss; +use style_traits::{CssWriter, ToCss}; use values::{CSSFloat, serialize_percentage}; /// A computed percentage. @@ -35,7 +35,7 @@ impl Percentage { } impl ToCss for Percentage { - fn to_css(&self, dest: &mut W) -> fmt::Result + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result where W: fmt::Write, { diff --git a/components/style/values/computed/pointing.rs b/components/style/values/computed/pointing.rs index 9b108dbcfc2c4..b14f22dec9c81 100644 --- a/components/style/values/computed/pointing.rs +++ b/components/style/values/computed/pointing.rs @@ -10,10 +10,10 @@ use cssparser::Parser; use parser::{Parse, ParserContext}; use selectors::parser::SelectorParseErrorKind; #[cfg(feature = "gecko")] -use std::fmt; -use style_traits::ParseError; +use std::fmt::{self, Write}; #[cfg(feature = "gecko")] -use style_traits::ToCss; +use style_traits::{CssWriter, ToCss}; +use style_traits::ParseError; use style_traits::cursor::CursorKind; /// The computed value for the `cursor` property. @@ -80,8 +80,9 @@ impl Parse for Cursor { #[cfg(feature = "gecko")] impl ToCss for Cursor { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { for url in &*self.images { url.to_css(dest)?; @@ -123,8 +124,9 @@ impl CursorImage { #[cfg(feature = "gecko")] impl ToCss for CursorImage { - fn to_css(&self, dest: &mut W) -> fmt::Result - where W: fmt::Write + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, { self.url.to_css(dest)?; if let Some((x, y)) = self.hotspot { diff --git a/components/style/values/computed/position.rs b/components/style/values/computed/position.rs index ed6a308151f8b..abd3522382d8b 100644 --- a/components/style/values/computed/position.rs +++ b/components/style/values/computed/position.rs @@ -7,8 +7,8 @@ //! //! [position]: https://drafts.csswg.org/css-backgrounds-3/#position -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use values::computed::{LengthOrPercentage, Percentage}; use values::generics::position::Position as GenericPosition; pub use values::specified::position::{GridAutoFlow, GridTemplateAreas}; @@ -40,7 +40,10 @@ impl Position { } impl ToCss for Position { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { self.horizontal.to_css(dest)?; dest.write_str(" ")?; self.vertical.to_css(dest) diff --git a/components/style/values/computed/text.rs b/components/style/values/computed/text.rs index 27c198a46a53e..123f735d5130c 100644 --- a/components/style/values/computed/text.rs +++ b/components/style/values/computed/text.rs @@ -6,8 +6,8 @@ #[cfg(feature = "servo")] use properties::StyleBuilder; -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use values::{CSSInteger, CSSFloat}; use values::animated::ToAnimatedZero; use values::computed::{NonNegativeLength, NonNegativeNumber}; @@ -66,7 +66,10 @@ impl TextOverflow { } impl ToCss for TextOverflow { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { if self.sides_are_logical { debug_assert!(self.first == TextOverflowSide::Clip); self.second.to_css(dest)?; @@ -80,7 +83,10 @@ impl ToCss for TextOverflow { } impl ToCss for TextDecorationLine { - fn to_css(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { + fn to_css(&self, dest: &mut CssWriter) -> fmt::Result + where + W: Write, + { let mut has_any = false; macro_rules! write_value { diff --git a/components/style/values/computed/time.rs b/components/style/values/computed/time.rs index a90a0935a8e83..cef128af4a8f7 100644 --- a/components/style/values/computed/time.rs +++ b/components/style/values/computed/time.rs @@ -4,8 +4,8 @@ //! Computed time values. -use std::fmt; -use style_traits::ToCss; +use std::fmt::{self, Write}; +use style_traits::{CssWriter, ToCss}; use values::CSSFloat; /// A computed `