lightningcss/rules/
unknown.rs

1//! An unknown at-rule.
2
3use super::Location;
4use crate::error::PrinterError;
5use crate::printer::Printer;
6use crate::properties::custom::TokenList;
7use crate::traits::ToCss;
8use crate::values::string::CowArcStr;
9#[cfg(feature = "visitor")]
10use crate::visitor::Visit;
11
12/// An unknown at-rule, stored as raw tokens.
13#[derive(Debug, PartialEq, Clone)]
14#[cfg_attr(feature = "visitor", derive(Visit))]
15#[cfg_attr(feature = "into_owned", derive(static_self::IntoOwned))]
16#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
17#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
18pub struct UnknownAtRule<'i> {
19  /// The name of the at-rule (without the @).
20  #[cfg_attr(feature = "serde", serde(borrow))]
21  #[cfg_attr(feature = "visitor", skip_visit)]
22  pub name: CowArcStr<'i>,
23  /// The prelude of the rule.
24  pub prelude: TokenList<'i>,
25  /// The contents of the block, if any.
26  pub block: Option<TokenList<'i>>,
27  /// The location of the rule in the source file.
28  #[cfg_attr(feature = "visitor", skip_visit)]
29  pub loc: Location,
30}
31
32impl<'i> ToCss for UnknownAtRule<'i> {
33  fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
34  where
35    W: std::fmt::Write,
36  {
37    #[cfg(feature = "sourcemap")]
38    dest.add_mapping(self.loc);
39    dest.write_char('@')?;
40    dest.write_str(&self.name)?;
41
42    if !self.prelude.0.is_empty() {
43      dest.write_char(' ')?;
44      self.prelude.to_css(dest, false)?;
45    }
46
47    if let Some(block) = &self.block {
48      dest.whitespace()?;
49      dest.write_char('{')?;
50      dest.indent();
51      dest.newline()?;
52      block.to_css(dest, false)?;
53      dest.dedent();
54      dest.newline()?;
55      dest.write_char('}')
56    } else {
57      dest.write_char(';')
58    }
59  }
60}