css_ast/functions/
string_function.rs

1use super::prelude::*;
2
3/// <https://drafts.csswg.org/css-content-3/#string-function>
4///
5/// ```text,ignore
6/// string() = string( <custom-ident> , [ first | start | last | first-except ]? )
7/// ```
8#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
10#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
11#[derive(csskit_derives::NodeWithMetadata)]
12pub struct StringFunction {
13	#[atom(CssAtomSet::String)]
14	pub name: T![Function],
15	pub params: StringFunctionParams,
16	pub close: T![')'],
17}
18
19#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
21pub struct StringFunctionParams {
22	pub ident: T![Ident],
23	pub comma: Option<T![,]>,
24	pub keyword: Option<StringKeyword>,
25}
26
27#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
29pub enum StringKeyword {
30	#[atom(CssAtomSet::First)]
31	First(T![Ident]),
32	#[atom(CssAtomSet::Start)]
33	Start(T![Ident]),
34	#[atom(CssAtomSet::Last)]
35	Last(T![Ident]),
36	#[atom(CssAtomSet::FirstExcept)]
37	FirstExcept(T![Ident]),
38}
39
40#[cfg(test)]
41mod tests {
42	use super::*;
43	use crate::CssAtomSet;
44	use css_parse::{assert_parse, assert_parse_error};
45
46	#[test]
47	fn size_test() {
48		assert_eq!(std::mem::size_of::<StringFunction>(), 68);
49		assert_eq!(std::mem::size_of::<StringKeyword>(), 16);
50	}
51
52	#[test]
53	fn test_writes() {
54		assert_parse!(CssAtomSet::ATOMS, StringFunction, "string(foo)");
55		assert_parse!(CssAtomSet::ATOMS, StringFunction, "string(foo,first)");
56	}
57
58	#[test]
59	fn test_errors() {
60		assert_parse_error!(CssAtomSet::ATOMS, StringFunction, "string(foo bar)");
61	}
62}