css_parse/syntax/
bang_important.rs

1use crate::{
2	Cursor, CursorSink, Diagnostic, Kind, Parse, Parser, Peek, Result, SemanticEq, Span, T, ToCursors, ToSpan,
3};
4
5/// Represents a two tokens, the first being [Kind::Delim] where the char is `!`, and the second being an `Ident` with
6/// the value `important`. [CSS defines this as]:
7///
8/// ```md
9/// <ws*>
10///     ╭──────────────────────────╮
11///  │├─╯─╭─ <whitespace-token> ─╮─╰─┤│
12///       ╰──────────────────────╯
13///
14/// <!important>
15///  │├─ "!" ─ <ws*> ─ <ident-token "important"> ─ <ws*> ─┤│
16/// ```
17///
18/// `<ws*>` is any number of `<whitespace-token>`s, defined as [Kind::Whitespace][Kind::Whitespace]. This is
19/// automatically skipped by default in the [Parser] anyway.
20///
21/// [1]: https://drafts.csswg.org/css-syntax-3/#!important-diagram
22///
23#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize), serde())]
25pub struct BangImportant {
26	pub bang: T![!],
27	pub important: T![Ident],
28}
29
30impl<'a> Peek<'a> for BangImportant {
31	fn peek<Iter>(p: &Parser<'a, Iter>, c: Cursor) -> bool
32	where
33		Iter: Iterator<Item = Cursor> + Clone,
34	{
35		if c == Kind::Delim && c == '!' {
36			let c = p.peek_n(2);
37			c == Kind::Ident && p.to_source_cursor(c).eq_ignore_ascii_case("important")
38		} else {
39			false
40		}
41	}
42}
43
44impl<'a> Parse<'a> for BangImportant {
45	fn parse<Iter>(p: &mut Parser<'a, Iter>) -> Result<Self>
46	where
47		Iter: Iterator<Item = Cursor> + Clone,
48	{
49		let bang = p.parse::<T![!]>()?;
50		let important = p.parse::<T![Ident]>()?;
51		if !p.to_source_cursor(important.into()).eq_ignore_ascii_case("important") {
52			Err(Diagnostic::new(important.into(), Diagnostic::unexpected_ident))?
53		}
54		Ok(Self { bang, important })
55	}
56}
57
58impl ToCursors for BangImportant {
59	fn to_cursors(&self, s: &mut impl CursorSink) {
60		s.append(self.bang.into());
61		s.append(self.important.into());
62	}
63}
64
65impl ToSpan for BangImportant {
66	fn to_span(&self) -> Span {
67		self.bang.to_span() + self.important.to_span()
68	}
69}
70
71impl SemanticEq for BangImportant {
72	fn semantic_eq(&self, _: &Self) -> bool {
73		// The presence of !important is semantic in of itself, so this is just always true
74		true
75	}
76}