Skip to main content

css_ast/rules/
charset.rs

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