Skip to main content

rspack_lightningcss/rules/
document.rs

1//! The `@-moz-document` rule.
2
3use super::Location;
4use super::{CssRuleList, MinifyContext};
5use crate::error::{MinifyError, PrinterError};
6use crate::parser::DefaultAtRule;
7use crate::traits::ToCss;
8#[cfg(feature = "visitor")]
9use crate::visitor::Visit;
10
11/// A [@-moz-document](https://www.w3.org/TR/2012/WD-css3-conditional-20120911/#at-document) rule.
12///
13/// Note that only the `url-prefix()` function with no arguments is supported, and only the `-moz` prefix
14/// is allowed since Firefox was the only browser that ever implemented this rule.
15#[derive(Debug, PartialEq, Clone)]
16#[cfg_attr(feature = "visitor", derive(Visit))]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
19#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
20pub struct MozDocumentRule<'i, R = DefaultAtRule> {
21  /// Nested rules within the `@-moz-document` rule.
22  #[cfg_attr(feature = "serde", serde(borrow))]
23  pub rules: CssRuleList<'i, R>,
24  /// The location of the rule in the source file.
25  #[cfg_attr(feature = "visitor", skip_visit)]
26  pub loc: Location,
27}
28
29impl<'i, T: Clone> MozDocumentRule<'i, T> {
30  pub(crate) fn minify(&mut self, context: &mut MinifyContext<'_, 'i>) -> Result<(), MinifyError> {
31    self.rules.minify(context, false)
32  }
33}
34
35impl<'i, T: ToCss> ToCss for MozDocumentRule<'i, T> {
36  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
37    #[cfg(feature = "custom_sourcemap")]
38    dest.add_mapping(self.loc);
39    dest.write_str("@-moz-document url-prefix()")?;
40    dest.whitespace()?;
41    dest.write_char('{')?;
42    dest.indent();
43    dest.newline()?;
44    self.rules.to_css(dest)?;
45    dest.dedent();
46    dest.newline()?;
47    dest.write_char('}')?;
48    Ok(())
49  }
50}