1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 //! Generic types for CSS values related to <ratio>.
6 //! https://drafts.csswg.org/css-values/#ratios
7 
8 use std::fmt::{self, Write};
9 use style_traits::{CssWriter, ToCss};
10 
11 /// A generic value for the `<ratio>` value.
12 #[derive(
13     Clone,
14     Copy,
15     Debug,
16     MallocSizeOf,
17     PartialEq,
18     SpecifiedValueInfo,
19     ToComputedValue,
20     ToResolvedValue,
21     ToShmem,
22 )]
23 #[repr(C)]
24 pub struct Ratio<N>(pub N, pub N);
25 
26 impl<N> ToCss for Ratio<N>
27 where
28     N: ToCss,
29 {
to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result where W: Write,30     fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
31     where
32         W: Write,
33     {
34         self.0.to_css(dest)?;
35         // Even though 1 could be omitted, we don't per
36         // https://drafts.csswg.org/css-values-4/#ratio-value:
37         //
38         //     The second <number> is optional, defaulting to 1. However,
39         //     <ratio> is always serialized with both components.
40         //
41         // And for compat reasons, see bug 1669742.
42         //
43         // We serialize with spaces for consistency with all other
44         // slash-delimited things, see
45         // https://github.com/w3c/csswg-drafts/issues/4282
46         dest.write_str(" / ")?;
47         self.1.to_css(dest)?;
48         Ok(())
49     }
50 }
51