Skip to main content

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#[node]
9#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
11#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
12#[derive(csskit_derives::NodeWithMetadata)]
13pub struct StringFunction {
14	#[atom(CssAtomSet::String)]
15	pub name: T![Function],
16	pub params: StringFunctionParams,
17	#[semantic_eq(skip)]
18	pub close: T![')'],
19}
20
21#[node]
22#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
23#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
24#[derive(csskit_derives::NodeWithMetadata)]
25pub struct StringFunctionParams {
26	pub ident: T![Ident],
27	#[semantic_eq(skip)]
28	pub comma: Option<T![,]>,
29	pub keyword: Option<StringKeyword>,
30}
31
32#[node]
33#[derive(Parse, Peek, ToCursors, ToSpan, SemanticEq, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
34#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
35#[derive(csskit_derives::NodeWithMetadata)]
36pub enum StringKeyword {
37	#[atom(CssAtomSet::First)]
38	First(T![Ident]),
39	#[atom(CssAtomSet::Start)]
40	Start(T![Ident]),
41	#[atom(CssAtomSet::Last)]
42	Last(T![Ident]),
43	#[atom(CssAtomSet::FirstExcept)]
44	FirstExcept(T![Ident]),
45}
46
47#[cfg(test)]
48mod tests {
49	use super::*;
50	use crate::CssAtomSet;
51	use css_parse::{assert_parse, assert_parse_error};
52
53	#[test]
54	fn test_writes() {
55		assert_parse!(CssAtomSet::ATOMS, StringFunction, "string(foo)");
56		assert_parse!(CssAtomSet::ATOMS, StringFunction, "string(foo,first)");
57	}
58
59	#[test]
60	fn test_errors() {
61		assert_parse_error!(CssAtomSet::ATOMS, StringFunction, "string(foo bar)");
62	}
63}