style/values/specified/
param.rs1use crate::custom_properties::VariableValue;
8use crate::parser::{Parse, ParserContext};
9use crate::values::fmt;
10use crate::values::CssWriter;
11use crate::{derives::*, values::DashedIdent};
12use cssparser::Parser;
13use std::fmt::Write;
14use style_traits::arc_slice::ArcSlice;
15use style_traits::owned_str::OwnedStr;
16use style_traits::{ParseError, ToCss};
17
18#[derive(
21 Clone,
22 Debug,
23 MallocSizeOf,
24 PartialEq,
25 SpecifiedValueInfo,
26 ToComputedValue,
27 ToResolvedValue,
28 ToShmem,
29)]
30#[repr(C, u8)]
31pub enum LinkParamValueOrNone {
32 None,
34 Specified(OwnedStr),
36}
37
38#[derive(
40 Clone,
41 Debug,
42 MallocSizeOf,
43 PartialEq,
44 SpecifiedValueInfo,
45 ToComputedValue,
46 ToResolvedValue,
47 ToShmem,
48)]
49#[repr(C)]
50pub struct LinkParam {
51 pub name: DashedIdent,
53 pub value: LinkParamValueOrNone,
55}
56
57#[derive(
61 Clone,
62 Debug,
63 Default,
64 MallocSizeOf,
65 PartialEq,
66 SpecifiedValueInfo,
67 ToComputedValue,
68 ToCss,
69 ToResolvedValue,
70 ToShmem,
71 ToTyped,
72)]
73#[repr(C)]
74#[css(comma)]
75#[typed(skip_derive_fields)]
76pub struct LinkParameters(
77 #[css(iterable, if_empty = "none")]
79 #[ignore_malloc_size_of = "Arc"]
80 pub ArcSlice<LinkParam>,
81);
82
83impl LinkParameters {
84 pub fn none() -> Self {
86 Self(ArcSlice::default())
87 }
88}
89
90impl Parse for LinkParameters {
91 fn parse<'i, 't>(
92 context: &ParserContext,
93 input: &mut Parser<'i, 't>,
94 ) -> Result<Self, ParseError<'i>> {
95 if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
96 return Ok(Self::none());
97 }
98
99 let params = input.parse_comma_separated(|input| {
100 input.expect_function_matching("param")?;
101 input.parse_nested_block(|input| {
102 let name = DashedIdent::parse(context, input)?;
103 let value = if input.try_parse(|i| i.expect_comma()).is_ok() {
106 let parsed = VariableValue::parse(input, None, &context.url_data)?;
107 LinkParamValueOrNone::Specified(OwnedStr::from(parsed.css))
108 } else {
109 LinkParamValueOrNone::None
110 };
111 Ok(LinkParam { name, value })
112 })
113 })?;
114
115 Ok(Self(crate::ArcSlice::from_iter(params.into_iter())))
116 }
117}
118
119impl ToCss for LinkParam {
120 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
121 where
122 W: Write,
123 {
124 dest.write_str("param(")?;
125 self.name.to_css(dest)?;
126 if let LinkParamValueOrNone::Specified(param) = &self.value {
127 dest.write_str(", ")?;
128 if !param.is_empty() {
129 dest.write_str(¶m)?;
131 }
132 }
133 dest.write_char(')')
134 }
135}