Skip to main content

rspack_lightningcss/rules/
nesting.rs

1//! The `@nest` rule.
2
3use smallvec::SmallVec;
4
5use super::style::StyleRule;
6use super::Location;
7use super::MinifyContext;
8use crate::context::DeclarationContext;
9use crate::declaration::DeclarationBlock;
10use crate::error::{MinifyError, PrinterError};
11use crate::parser::DefaultAtRule;
12use crate::targets::should_compile;
13use crate::traits::ToCss;
14#[cfg(feature = "visitor")]
15use crate::visitor::Visit;
16
17/// A [@nest](https://www.w3.org/TR/css-nesting-1/#at-nest) rule.
18#[derive(Debug, PartialEq, Clone)]
19#[cfg_attr(feature = "visitor", derive(Visit))]
20#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
21#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
22#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
23pub struct NestingRule<'i, R = DefaultAtRule> {
24  /// The style rule that defines the selector and declarations for the `@nest` rule.
25  #[cfg_attr(feature = "serde", serde(borrow))]
26  pub style: StyleRule<'i, R>,
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, T: Clone> NestingRule<'i, T> {
33  pub(crate) fn minify(
34    &mut self,
35    context: &mut MinifyContext<'_, 'i>,
36    parent_is_unused: bool,
37  ) -> Result<bool, MinifyError> {
38    self.style.minify(context, parent_is_unused)
39  }
40}
41
42impl<'a, 'i, T: ToCss> ToCss for NestingRule<'i, T> {
43  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
44    dest.add_mapping(self.loc);
45    if dest.state().context.is_none() {
46      dest.write_str("@nest ")?;
47    }
48    self.style.to_css(dest)
49  }
50}
51
52/// A [nested declarations](https://drafts.csswg.org/css-nesting/#nested-declarations-rule) rule.
53#[derive(Debug, PartialEq, Clone)]
54#[cfg_attr(feature = "visitor", derive(Visit))]
55#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
56#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
57#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
58pub struct NestedDeclarationsRule<'i> {
59  /// The style rule that defines the selector and declarations for the `@nest` rule.
60  #[cfg_attr(feature = "serde", serde(borrow))]
61  pub declarations: DeclarationBlock<'i>,
62  /// The location of the rule in the source file.
63  #[cfg_attr(feature = "visitor", skip_visit)]
64  pub loc: Location,
65}
66
67impl<'i> NestedDeclarationsRule<'i> {
68  pub(crate) fn minify(&mut self, context: &mut MinifyContext<'_, 'i>, parent_is_unused: bool) -> bool {
69    if parent_is_unused {
70      return true;
71    }
72
73    context.handler_context.context = DeclarationContext::StyleRule;
74    self
75      .declarations
76      .minify(context.handler, context.important_handler, &mut context.handler_context);
77    context.handler_context.context = DeclarationContext::None;
78    return false;
79  }
80}
81
82impl<'i> ToCss for NestedDeclarationsRule<'i> {
83  fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
84    dest.add_mapping(self.loc);
85
86    if should_compile!(dest.state().targets.current, Nesting) {
87      let context = crate::rules::detach_style_context(dest.state().context);
88      if let Some(context) = context {
89        let has_printable_declarations = self.declarations.has_printable_declarations();
90        if has_printable_declarations {
91          let parent = dest.state().context;
92          dest.state_mut().context = None;
93          dest.state_mut().context = context.parent.map(crate::rules::style_context_ptr);
94          let res = context.selectors.to_css(dest);
95          dest.state_mut().context = parent;
96          res?;
97          dest.whitespace()?;
98          dest.write_char('{')?;
99          dest.indent();
100          dest.newline()?;
101        }
102
103        self
104          .declarations
105          .to_css_declarations(dest, false, &context.selectors, self.loc.source_index)?;
106
107        if has_printable_declarations {
108          dest.dedent();
109          dest.newline()?;
110          dest.write_char('}')?;
111        }
112        return Ok(());
113      }
114    }
115
116    self.declarations.to_css_declarations(
117      dest,
118      false,
119      &rspack_lightningcss_selectors::SelectorList(SmallVec::new()),
120      0,
121    )
122  }
123}