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(
20 Clone,
21 Debug,
22 MallocSizeOf,
23 PartialEq,
24 SpecifiedValueInfo,
25 ToComputedValue,
26 ToResolvedValue,
27 ToShmem,
28)]
29#[repr(transparent)]
30pub struct LinkParamValue(pub OwnedStr);
31
32#[derive(
34 Clone,
35 Debug,
36 MallocSizeOf,
37 PartialEq,
38 SpecifiedValueInfo,
39 ToComputedValue,
40 ToResolvedValue,
41 ToShmem,
42)]
43#[repr(C)]
44pub struct LinkParam {
45 pub name: DashedIdent,
47 pub value: LinkParamValue,
49}
50
51#[derive(
55 Clone,
56 Debug,
57 Default,
58 MallocSizeOf,
59 PartialEq,
60 SpecifiedValueInfo,
61 ToComputedValue,
62 ToCss,
63 ToResolvedValue,
64 ToShmem,
65 ToTyped,
66)]
67#[repr(C)]
68#[css(comma)]
69#[typed(skip_derive_fields)]
70pub struct LinkParameters(
71 #[css(iterable, if_empty = "none")]
73 #[ignore_malloc_size_of = "Arc"]
74 pub ArcSlice<LinkParam>,
75);
76
77impl LinkParameters {
78 pub fn none() -> Self {
80 Self(ArcSlice::default())
81 }
82}
83
84impl Parse for LinkParameters {
85 fn parse(context: &ParserContext, input: &mut Parser) -> Result<Self, ParseError> {
86 if input.try_parse(|i| i.expect_ident_matching("none")).is_ok() {
87 return Ok(Self::none());
88 }
89
90 let params = input.parse_comma_separated(|input| {
91 input.expect_function_matching("param")?;
92 input.parse_nested_block(|input| {
93 let name = DashedIdent::parse(context, input)?;
94 input.expect_comma()?;
95 let parsed = VariableValue::parse(input, None, context.url_data)?;
98 let value = LinkParamValue(OwnedStr::from(parsed.css));
99 Ok(LinkParam { name, value })
100 })
101 })?;
102
103 Ok(Self(crate::ArcSlice::from_iter(params.into_iter())))
104 }
105}
106
107impl ToCss for LinkParam {
108 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
109 where
110 W: Write,
111 {
112 dest.write_str("param(")?;
113 self.name.to_css(dest)?;
114 dest.write_str(", ")?;
115 if !self.value.0.is_empty() {
116 dest.write_str(&self.value.0)?;
118 }
119 dest.write_char(')')
120 }
121}