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