From eb554044dbd984ed77c4f7bc089e90118ad55857 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Sun, 29 Apr 2018 09:03:31 +1000 Subject: [PATCH] style: Add SequenceWriter::item_str for writing str items. This will be used in the next patch for font-variant bitflag types. Bug: 1434130 Reviewed-by: emilio MozReview-Commit-ID: 2IvcsnYBNqA --- components/style_traits/values.rs | 35 +++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/components/style_traits/values.rs b/components/style_traits/values.rs index d6bd05b7b40..481eb1aff6d 100644 --- a/components/style_traits/values.rs +++ b/components/style_traits/values.rs @@ -173,16 +173,10 @@ where Self { inner, separator } } - /// Serialises a CSS value, writing any separator as necessary. - /// - /// The separator is never written before any `item` produces any output, - /// and is written in subsequent calls only if the `item` produces some - /// output on its own again. This lets us handle `Option` fields by - /// just not printing anything on `None`. #[inline] - pub fn item(&mut self, item: &T) -> fmt::Result + fn write_item(&mut self, f: F) -> fmt::Result where - T: ToCss, + F: FnOnce(&mut CssWriter<'b, W>) -> fmt::Result { let old_prefix = self.inner.prefix; if old_prefix.is_none() { @@ -191,7 +185,7 @@ where // to write the separator next time we produce output again. self.inner.prefix = Some(self.separator); } - item.to_css(&mut self.inner)?; + f(self.inner)?; match (old_prefix, self.inner.prefix) { (_, None) => { // This call produced output and cleaned up after itself. @@ -213,6 +207,29 @@ where } Ok(()) } + + /// Serialises a CSS value, writing any separator as necessary. + /// + /// The separator is never written before any `item` produces any output, + /// and is written in subsequent calls only if the `item` produces some + /// output on its own again. This lets us handle `Option` fields by + /// just not printing anything on `None`. + #[inline] + pub fn item(&mut self, item: &T) -> fmt::Result + where + T: ToCss, + { + self.write_item(|inner| item.to_css(inner)) + } + + /// Writes a string as-is (i.e. not escaped or wrapped in quotes) + /// with any separator as necessary. + /// + /// See SequenceWriter::item. + #[inline] + pub fn raw_item(&mut self, item: &str) -> fmt::Result { + self.write_item(|inner| inner.write_str(item)) + } } /// A wrapper type that implements `ToCss` by printing its inner field.