css_ast/rules/
charset.rs

1use super::prelude::*;
2
3// https://drafts.csswg.org/css-syntax-3/#charset-rule
4#[derive(ToSpan, ToCursors, SemanticEq, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
6#[cfg_attr(feature = "visitable", derive(csskit_derives::Visitable), visit(self))]
7#[cfg_attr(feature = "css_feature_data", derive(::csskit_derives::ToCSSFeature), css_feature("css.at-rules.charset"))]
8pub struct CharsetRule {
9	at_keyword: T![AtKeyword],
10	space: T![' '],
11	string: T![String],
12	semicolon: Option<T![;]>,
13}
14
15// CharsetRule is a special rule which means it cannot use standard AtRule parsing... comments below
16// https://drafts.csswg.org/css-syntax-3/#determine-the-fallback-encoding
17impl<'a> Parse<'a> for CharsetRule {
18	fn parse<I>(p: &mut Parser<'a, I>) -> ParserResult<Self>
19	where
20		I: Iterator<Item = Cursor> + Clone,
21	{
22		let at_keyword = p.parse::<T![AtKeyword]>()?;
23		let c: Cursor = at_keyword.into();
24		// CharsetRule MUST be all lowercase, alt cases such as CHARSET or charSet aren't
25		// valid here, compares to other at-rules which are case insensitive.
26		if !p.equals_atom(c, &CssAtomSet::Charset) {
27			Err(Diagnostic::new(c, Diagnostic::unexpected))?;
28		}
29		// Charsets MUST have a space between the at keyword and the string. This
30		// isn't necessary in other at rules where an at keyword can align with other
31		// delims (e.g. `(`) or unambinguous tokens like strings.
32		let space = p.parse::<T![' ']>()?;
33		let string = p.parse::<T![String]>()?;
34		// TODO: check quote style as it should be "
35		let semicolon = p.parse::<T![;]>().ok();
36		Ok(Self { at_keyword, space, string, semicolon })
37	}
38}
39
40impl NodeWithMetadata<CssMetadata> for CharsetRule {
41	fn metadata(&self) -> CssMetadata {
42		CssMetadata { used_at_rules: AtRuleId::Charset, ..Default::default() }
43	}
44}
45
46#[cfg(test)]
47mod tests {
48	use super::*;
49	use crate::CssAtomSet;
50	use css_parse::assert_parse;
51
52	#[test]
53	fn size_test() {
54		assert_eq!(std::mem::size_of::<CharsetRule>(), 52);
55	}
56
57	#[test]
58	fn test_writes() {
59		assert_parse!(CssAtomSet::ATOMS, CharsetRule, "@charset \"utf-8\";");
60		assert_parse!(CssAtomSet::ATOMS, CharsetRule, "@charset \"UTF-8\";");
61	}
62}