rspack_lightningcss/rules/
layer.rs1use super::{CssRuleList, Location, MinifyContext};
4use crate::error::{MinifyError, ParserError, PrinterError};
5use crate::parser::DefaultAtRule;
6use crate::traits::{Parse, ToCss};
7use crate::values::string::CowArcStr;
8#[cfg(feature = "visitor")]
9use crate::visitor::Visit;
10use cssparser::*;
11use smallvec::SmallVec;
12
13#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(transparent))]
20#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
21pub struct LayerName<'i>(#[cfg_attr(feature = "serde", serde(borrow))] pub SmallVec<[CowArcStr<'i>; 1]>);
22
23macro_rules! expect_non_whitespace {
24 ($parser: ident, $($branches: tt)+) => {{
25 let start_location = $parser.current_source_location();
26 match *$parser.next_including_whitespace()? {
27 $($branches)+
28 ref token => {
29 return Err(start_location.new_basic_unexpected_token_error(token.clone()))
30 }
31 }
32 }}
33}
34
35impl<'i> Parse<'i> for LayerName<'i> {
36 fn parse<'t>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ParserError<'i>>> {
37 let mut parts = SmallVec::new();
38 let ident = input.expect_ident()?;
39 parts.push(ident.into());
40
41 loop {
42 let name = input.try_parse(|input| {
43 expect_non_whitespace! {input,
44 Token::Delim('.') => Ok(()),
45 }?;
46
47 expect_non_whitespace! {input,
48 Token::Ident(ref id) => Ok(id.into()),
49 }
50 });
51
52 match name {
53 Ok(name) => parts.push(name),
54 Err(_) => break,
55 }
56 }
57
58 Ok(LayerName(parts))
59 }
60}
61
62impl<'i> ToCss for LayerName<'i> {
63 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
64 let mut first = true;
65 for name in &self.0 {
66 if first {
67 first = false;
68 } else {
69 dest.write_char('.')?;
70 }
71
72 serialize_identifier(name, dest)?;
73 }
74
75 Ok(())
76 }
77}
78
79#[derive(Debug, Clone, PartialEq)]
83#[cfg_attr(feature = "visitor", derive(Visit))]
84#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
85#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
86#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
87pub struct LayerStatementRule<'i> {
88 #[cfg_attr(feature = "serde", serde(borrow))]
90 #[cfg_attr(feature = "visitor", skip_visit)]
91 pub names: Vec<LayerName<'i>>,
92 #[cfg_attr(feature = "visitor", skip_visit)]
94 pub loc: Location,
95}
96
97impl<'i> ToCss for LayerStatementRule<'i> {
98 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
99 dest.add_mapping(self.loc);
100 dest.write_str("@layer ")?;
101 self.names.to_css(dest)?;
102 dest.write_char(';')?;
103 Ok(())
104 }
105}
106
107#[derive(Debug, Clone, PartialEq)]
109#[cfg_attr(feature = "visitor", derive(Visit))]
110#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
111#[cfg_attr(feature = "jsonschema", derive(schemars::JsonSchema))]
112#[cfg_attr(feature = "into_owned", derive(rspack_lightningcss_static_self::IntoOwned))]
113pub struct LayerBlockRule<'i, R = DefaultAtRule> {
114 #[cfg_attr(feature = "serde", serde(borrow))]
116 #[cfg_attr(feature = "visitor", skip_visit)]
117 pub name: Option<LayerName<'i>>,
118 pub rules: CssRuleList<'i, R>,
120 #[cfg_attr(feature = "visitor", skip_visit)]
122 pub loc: Location,
123}
124
125impl<'i, T: Clone> LayerBlockRule<'i, T> {
126 pub(crate) fn minify(
127 &mut self,
128 context: &mut MinifyContext<'_, 'i>,
129 parent_is_unused: bool,
130 ) -> Result<bool, MinifyError> {
131 self.rules.minify(context, parent_is_unused)?;
132
133 Ok(self.rules.0.is_empty())
134 }
135}
136
137impl<'a, 'i, T: ToCss> ToCss for LayerBlockRule<'i, T> {
138 fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
139 dest.add_mapping(self.loc);
140 dest.write_str("@layer")?;
141 if let Some(name) = &self.name {
142 dest.write_char(' ')?;
143 name.to_css(dest)?;
144 }
145
146 dest.whitespace()?;
147 dest.write_char('{')?;
148 dest.indent();
149 dest.newline()?;
150 self.rules.to_css(dest)?;
151 dest.dedent();
152 dest.newline()?;
153 dest.write_char('}')?;
154 Ok(())
155 }
156}