Bug 1434130 part 3 - Add SequenceWriter::item_str for writing str items. r?emilio
This will be used in the next patch for font-variant bitflag types.
MozReview-Commit-ID: 2IvcsnYBNqA
--- a/servo/components/style_traits/values.rs
+++ b/servo/components/style_traits/values.rs
@@ -168,35 +168,29 @@ where
pub fn new(inner: &'a mut CssWriter<'b, W>, separator: &'static str) -> Self {
if inner.prefix.is_none() {
// See comment in `item`.
inner.prefix = Some("");
}
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<T>` fields by
- /// just not printing anything on `None`.
#[inline]
- pub fn item<T>(&mut self, item: &T) -> fmt::Result
+ fn write_item<F>(&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() {
// If there is no prefix in the inner writer, a previous
// call to this method produced output, which means we need
// 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.
}
(None, Some(p)) => {
// Some previous call to `item` produced output,
// but this one did not, prefix should be the same as
// the one we set.
@@ -208,16 +202,39 @@ where
(Some(old), Some(new)) => {
// No previous call to `item` produced output, and this one
// either.
debug_assert_eq!(old, new);
}
}
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<T>` fields by
+ /// just not printing anything on `None`.
+ #[inline]
+ pub fn item<T>(&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.
pub struct Verbatim<'a, T>(pub &'a T)
where
T: ?Sized + 'a;
impl<'a, T> ToCss for Verbatim<'a, T>