From 70cbbd993f20f6a315c636a9ad05fb1a51f866d2 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sun, 12 May 2024 08:51:51 +0200 Subject: [PATCH 1/8] Add granular configuration of cssModules transforms --- napi/src/lib.rs | 10 +++++++++ src/css_modules.rs | 23 +++++++++++++++++++- src/printer.rs | 42 ++++++++++++++++++++----------------- src/properties/animation.rs | 4 +++- src/properties/grid.rs | 4 +++- src/rules/keyframes.rs | 7 +++++-- src/selector.rs | 6 +++--- src/values/ident.rs | 6 +++++- 8 files changed, 74 insertions(+), 28 deletions(-) diff --git a/napi/src/lib.rs b/napi/src/lib.rs index cc3ce78e..5e6b537c 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -605,6 +605,9 @@ enum CssModulesOption { struct CssModulesConfig { pattern: Option, dashed_idents: Option, + animation: Option, + grid: Option, + custom_idents: Option, } #[cfg(feature = "bundler")] @@ -713,6 +716,9 @@ fn compile<'i>( Default::default() }, dashed_idents: c.dashed_idents.unwrap_or_default(), + animation: c.animation.unwrap_or_default(), + grid: c.grid.unwrap_or_default(), + custom_idents: c.custom_idents.unwrap_or_default(), }), } } else { @@ -840,6 +846,10 @@ fn compile_bundle< Default::default() }, dashed_idents: c.dashed_idents.unwrap_or_default(), + animation: c.animation.unwrap_or(true), + grid: c.grid.unwrap_or(true), + keyframes: c.keyframes.unwrap_or(true), + custom_idents: c.custom_idents.unwrap_or(true), }), } } else { diff --git a/src/css_modules.rs b/src/css_modules.rs index b794bb43..cfe33d71 100644 --- a/src/css_modules.rs +++ b/src/css_modules.rs @@ -25,13 +25,34 @@ use std::hash::{Hash, Hasher}; use std::path::Path; /// Configuration for CSS modules. -#[derive(Default, Clone, Debug)] +#[derive(Clone, Debug)] pub struct Config<'i> { /// The name pattern to use when renaming class names and other identifiers. /// Default is `[hash]_[local]`. pub pattern: Pattern<'i>, /// Whether to rename dashed identifiers, e.g. custom properties. pub dashed_idents: bool, + /// Whether to scope animation names. + /// Default is `true`. + pub animation: bool, + /// Whether to scope grid names. + /// Default is `true`. + pub grid: bool, + /// Whether to scope custom identifiers + /// Default is `true`. + pub custom_idents: bool, +} + +impl<'i> Default for Config<'i> { + fn default() -> Self { + Config { + pattern: Default::default(), + dashed_idents: Default::default(), + animation: true, + grid: true, + custom_idents: true, + } + } } /// A CSS modules class name pattern. diff --git a/src/printer.rs b/src/printer.rs index 12fe09e3..ca29d5de 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -267,26 +267,30 @@ impl<'a, 'b, 'c, W: std::fmt::Write + Sized> Printer<'a, 'b, 'c, W> { /// Writes a CSS identifier to the underlying destination, escaping it /// as appropriate. If the `css_modules` option was enabled, then a hash /// is added, and the mapping is added to the CSS module. - pub fn write_ident(&mut self, ident: &str) -> Result<(), PrinterError> { - if let Some(css_module) = &mut self.css_module { - let dest = &mut self.dest; - let mut first = true; - css_module.config.pattern.write( - &css_module.hashes[self.loc.source_index as usize], - &css_module.sources[self.loc.source_index as usize], - ident, - |s| { - self.col += s.len() as u32; - if first { - first = false; - serialize_identifier(s, dest) - } else { - serialize_name(s, dest) - } - }, - )?; + pub fn write_ident(&mut self, ident: &str, handle_css_module: bool) -> Result<(), PrinterError> { + if handle_css_module { + if let Some(css_module) = &mut self.css_module { + let dest = &mut self.dest; + let mut first = true; + css_module.config.pattern.write( + &css_module.hashes[self.loc.source_index as usize], + &css_module.sources[self.loc.source_index as usize], + ident, + |s| { + self.col += s.len() as u32; + if first { + first = false; + serialize_identifier(s, dest) + } else { + serialize_name(s, dest) + } + }, + )?; - css_module.add_local(&ident, &ident, self.loc.source_index); + css_module.add_local(&ident, &ident, self.loc.source_index); + } else { + serialize_identifier(ident, self)?; + } } else { serialize_identifier(ident, self)?; } diff --git a/src/properties/animation.rs b/src/properties/animation.rs index c995c222..c784f6ee 100644 --- a/src/properties/animation.rs +++ b/src/properties/animation.rs @@ -67,7 +67,9 @@ impl<'i> ToCss for AnimationName<'i> { s.to_css(dest) } AnimationName::String(s) => { + let mut css_module_animation_enabled = false; if let Some(css_module) = &mut dest.css_module { + css_module_animation_enabled = css_module.config.animation; css_module.reference(&s, dest.loc.source_index) } @@ -78,7 +80,7 @@ impl<'i> ToCss for AnimationName<'i> { Ok(()) }, _ => { - dest.write_ident(s.as_ref()) + dest.write_ident(s.as_ref(), css_module_animation_enabled) } } } diff --git a/src/properties/grid.rs b/src/properties/grid.rs index fa0e5a64..41ee0d20 100644 --- a/src/properties/grid.rs +++ b/src/properties/grid.rs @@ -430,7 +430,9 @@ fn write_ident(name: &str, dest: &mut Printer) -> Result<(), PrinterError> where W: std::fmt::Write, { + let mut css_module_grid_enabled = false; if let Some(css_module) = &mut dest.css_module { + css_module_grid_enabled = css_module.config.grid; if let Some(last) = css_module.config.pattern.segments.last() { if !matches!(last, crate::css_modules::Segment::Local) { return Err(Error { @@ -444,7 +446,7 @@ where } } } - dest.write_ident(name)?; + dest.write_ident(name, css_module_grid_enabled)?; Ok(()) } diff --git a/src/rules/keyframes.rs b/src/rules/keyframes.rs index a9489f30..2f1580a0 100644 --- a/src/rules/keyframes.rs +++ b/src/rules/keyframes.rs @@ -92,9 +92,12 @@ impl<'i> ToCss for KeyframesName<'i> { where W: std::fmt::Write, { + let css_module_animation_enabled = + dest.css_module.as_mut().map_or(false, |css_module| css_module.config.animation); + match self { KeyframesName::Ident(ident) => { - dest.write_ident(ident.0.as_ref())?; + dest.write_ident(ident.0.as_ref(), css_module_animation_enabled)?; } KeyframesName::Custom(s) => { // CSS-wide keywords and `none` cannot remove quotes. @@ -103,7 +106,7 @@ impl<'i> ToCss for KeyframesName<'i> { serialize_string(&s, dest)?; }, _ => { - dest.write_ident(s.as_ref())?; + dest.write_ident(s.as_ref(), css_module_animation_enabled)?; } } } diff --git a/src/selector.rs b/src/selector.rs index 9cc04848..62550198 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -672,7 +672,7 @@ where if let Some(class) = class { dest.write_char('.')?; - dest.write_ident(class) + dest.write_ident(class, true) } else { dest.write_str($s) } @@ -1551,11 +1551,11 @@ where Component::Nesting => serialize_nesting(dest, context, false), Component::Class(ref class) => { dest.write_char('.')?; - dest.write_ident(&class.0) + dest.write_ident(&class.0, true) } Component::ID(ref id) => { dest.write_char('#')?; - dest.write_ident(&id.0) + dest.write_ident(&id.0, true) } Component::Host(selector) => { dest.write_str(":host")?; diff --git a/src/values/ident.rs b/src/values/ident.rs index 9d244965..8de02f19 100644 --- a/src/values/ident.rs +++ b/src/values/ident.rs @@ -49,7 +49,11 @@ impl<'i> ToCss for CustomIdent<'i> { where W: std::fmt::Write, { - dest.write_ident(&self.0) + let css_module_custom_idents_enabled = dest + .css_module + .as_mut() + .map_or(false, |css_module| css_module.config.custom_idents); + dest.write_ident(&self.0, css_module_custom_idents_enabled) } } From b0b3cfdb9a5b6e4215ab97d8a473c2634edd9c35 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sun, 12 May 2024 09:38:45 +0200 Subject: [PATCH 2/8] Remove leftover key --- napi/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/napi/src/lib.rs b/napi/src/lib.rs index 5e6b537c..e53bca93 100644 --- a/napi/src/lib.rs +++ b/napi/src/lib.rs @@ -848,7 +848,6 @@ fn compile_bundle< dashed_idents: c.dashed_idents.unwrap_or_default(), animation: c.animation.unwrap_or(true), grid: c.grid.unwrap_or(true), - keyframes: c.keyframes.unwrap_or(true), custom_idents: c.custom_idents.unwrap_or(true), }), } From 5b5d19835aea019819456779e0d2c19e65876ebf Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sun, 12 May 2024 16:03:30 +0200 Subject: [PATCH 3/8] Handle more cases --- src/printer.rs | 49 +++++++++++++++++++++++++----------------- src/properties/grid.rs | 29 ++++++++++++++----------- src/values/ident.rs | 30 ++++++++++++++++++-------- 3 files changed, 66 insertions(+), 42 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index ca29d5de..1fc363b1 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -289,38 +289,47 @@ impl<'a, 'b, 'c, W: std::fmt::Write + Sized> Printer<'a, 'b, 'c, W> { css_module.add_local(&ident, &ident, self.loc.source_index); } else { - serialize_identifier(ident, self)?; + serialize_identifier(ident, self)? } } else { - serialize_identifier(ident, self)?; + serialize_identifier(ident, self)? } Ok(()) } - pub(crate) fn write_dashed_ident(&mut self, ident: &str, is_declaration: bool) -> Result<(), PrinterError> { + pub(crate) fn write_dashed_ident( + &mut self, + ident: &str, + is_declaration: bool, + handle_css_module: bool, + ) -> Result<(), PrinterError> { self.write_str("--")?; - match &mut self.css_module { - Some(css_module) if css_module.config.dashed_idents => { - let dest = &mut self.dest; - css_module.config.pattern.write( - &css_module.hashes[self.loc.source_index as usize], - &css_module.sources[self.loc.source_index as usize], - &ident[2..], - |s| { - self.col += s.len() as u32; - serialize_name(s, dest) - }, - )?; + if handle_css_module { + match &mut self.css_module { + Some(css_module) if css_module.config.dashed_idents => { + let dest = &mut self.dest; + css_module.config.pattern.write( + &css_module.hashes[self.loc.source_index as usize], + &css_module.sources[self.loc.source_index as usize], + &ident[2..], + |s| { + self.col += s.len() as u32; + serialize_name(s, dest) + }, + )?; - if is_declaration { - css_module.add_dashed(ident, self.loc.source_index); + if is_declaration { + css_module.add_dashed(ident, self.loc.source_index); + } + } + _ => { + serialize_name(&ident[2..], self)?; } } - _ => { - serialize_name(&ident[2..], self)?; - } + } else { + serialize_name(&ident[2..], self)?; } Ok(()) diff --git a/src/properties/grid.rs b/src/properties/grid.rs index 41ee0d20..16942fb1 100644 --- a/src/properties/grid.rs +++ b/src/properties/grid.rs @@ -430,19 +430,22 @@ fn write_ident(name: &str, dest: &mut Printer) -> Result<(), PrinterError> where W: std::fmt::Write, { - let mut css_module_grid_enabled = false; - if let Some(css_module) = &mut dest.css_module { - css_module_grid_enabled = css_module.config.grid; - if let Some(last) = css_module.config.pattern.segments.last() { - if !matches!(last, crate::css_modules::Segment::Local) { - return Err(Error { - kind: PrinterErrorKind::InvalidCssModulesPatternInGrid, - loc: Some(ErrorLocation { - filename: dest.filename().into(), - line: dest.loc.line, - column: dest.loc.column, - }), - }); + let css_module_grid_enabled = dest.css_module.as_mut().map_or(false, |css_module| css_module.config.grid); + if css_module_grid_enabled { + if let Some(css_module) = &mut dest.css_module { + if css_module_grid_enabled { + if let Some(last) = css_module.config.pattern.segments.last() { + if !matches!(last, crate::css_modules::Segment::Local) { + return Err(Error { + kind: PrinterErrorKind::InvalidCssModulesPatternInGrid, + loc: Some(ErrorLocation { + filename: dest.filename().into(), + line: dest.loc.line, + column: dest.loc.column, + }), + }); + } + } } } } diff --git a/src/values/ident.rs b/src/values/ident.rs index 8de02f19..67b71c49 100644 --- a/src/values/ident.rs +++ b/src/values/ident.rs @@ -89,7 +89,12 @@ impl<'i> ToCss for DashedIdent<'i> { where W: std::fmt::Write, { - dest.write_dashed_ident(&self.0, true) + let css_module_custom_idents_enabled = dest + .css_module + .as_mut() + .map_or(false, |css_module| css_module.config.custom_idents); + + dest.write_dashed_ident(&self.0, true, css_module_custom_idents_enabled) } } @@ -156,18 +161,25 @@ impl<'i> ToCss for DashedIdentReference<'i> { where W: std::fmt::Write, { - match &mut dest.css_module { - Some(css_module) if css_module.config.dashed_idents => { - if let Some(name) = css_module.reference_dashed(&self.ident.0, &self.from, dest.loc.source_index) { - dest.write_str("--")?; - serialize_name(&name, dest)?; - return Ok(()); + let css_module_custom_idents_enabled = dest + .css_module + .as_mut() + .map_or(false, |css_module| css_module.config.custom_idents); + + if css_module_custom_idents_enabled { + match &mut dest.css_module { + Some(css_module) if css_module.config.dashed_idents => { + if let Some(name) = css_module.reference_dashed(&self.ident.0, &self.from, dest.loc.source_index) { + dest.write_str("--")?; + serialize_name(&name, dest)?; + return Ok(()); + } } + _ => {} } - _ => {} } - dest.write_dashed_ident(&self.ident.0, false) + dest.write_dashed_ident(&self.ident.0, false, css_module_custom_idents_enabled) } } From b19709ad7d37921e6735b2d7199e7f5539935176 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sun, 12 May 2024 16:03:45 +0200 Subject: [PATCH 4/8] Add test for grid: false --- src/lib.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index f13b7c48..bf5367aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23132,6 +23132,46 @@ mod tests { Default::default(), ); + #[cfg(feature = "grid")] + css_modules_test( + r#" + .grid { + grid-template-areas: "foo"; + } + + .foo { + grid-area: foo; + } + + .bar { + grid-column-start: foo-start; + } + "#, + indoc! {r#" + .EgL3uq_grid { + grid-template-areas: "foo"; + } + + .EgL3uq_foo { + grid-area: foo; + } + + .EgL3uq_bar { + grid-column-start: foo-start; + } + "#}, + map! { + "foo" => "EgL3uq_foo", + "grid" => "EgL3uq_grid", + "bar" => "EgL3uq_bar" + }, + HashMap::new(), + crate::css_modules::Config { + grid: false, + ..Default::default() + }, + ); + css_modules_test( r#" test { From d8c3e4c5d8b323ed9e31ba22d29609c6ac7fae96 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sun, 12 May 2024 16:49:10 +0200 Subject: [PATCH 5/8] Add test for animations --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 1 + src/lib.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index db6c59ae..4e0fd1df 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,6 +444,12 @@ dependencies = [ "matches", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "difflib" version = "0.4.0" @@ -785,6 +791,7 @@ dependencies = [ "paste", "pathdiff", "predicates 2.1.5", + "pretty_assertions", "rayon", "schemars", "serde", @@ -1196,6 +1203,16 @@ dependencies = [ "termtree", ] +[[package]] +name = "pretty_assertions" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af7cee1a6c8a5b9208b3cb1061f10c0cb689087b3d8ce85fb9d2dd7a29b6ba66" +dependencies = [ + "diff", + "yansi", +] + [[package]] name = "proc-macro-error" version = "1.0.4" @@ -1946,6 +1963,12 @@ dependencies = [ "tap", ] +[[package]] +name = "yansi" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09041cd90cf85f7f8b2df60c646f853b7f535ce68f85244eb6731cf89fa498ec" + [[package]] name = "zerocopy" version = "0.7.32" diff --git a/Cargo.toml b/Cargo.toml index 4e27dbf5..6ea8ad57 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,6 +85,7 @@ assert_cmd = "2.0" assert_fs = "1.0" predicates = "2.1" serde_json = "1" +pretty_assertions = "1.4.0" [[test]] name = "cli_integration_tests" diff --git a/src/lib.rs b/src/lib.rs index bf5367aa..140fa8dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,6 +64,7 @@ mod tests { use crate::vendor_prefix::VendorPrefix; use cssparser::SourceLocation; use indoc::indoc; + use pretty_assertions::assert_eq; use std::collections::HashMap; fn test(source: &str, expected: &str) { @@ -23050,6 +23051,53 @@ mod tests { Default::default(), ); + css_modules_test( + r#" + .foo { + color: red; + } + + #id { + animation: 2s test; + } + + @keyframes test { + from { color: red } + to { color: yellow } + } + "#, + indoc! {r#" + .EgL3uq_foo { + color: red; + } + + #EgL3uq_id { + animation: 2s test; + } + + @keyframes test { + from { + color: red; + } + + to { + color: #ff0; + } + } + "#}, + map! { + "foo" => "EgL3uq_foo", + "id" => "EgL3uq_id", + "test" => "EgL3uq_test" referenced: true + }, + HashMap::new(), + crate::css_modules::Config { + animation: false, + // custom_idents: false, + ..Default::default() + }, + ); + #[cfg(feature = "grid")] css_modules_test( r#" From 39b5a0316da0f004b0b73609f3876bb85751aa86 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sun, 12 May 2024 16:52:55 +0200 Subject: [PATCH 6/8] Add test for custom idents --- src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 140fa8dc..38f83f85 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23098,6 +23098,51 @@ mod tests { }, ); + css_modules_test( + r#" + @counter-style circles { + symbols: Ⓐ Ⓑ Ⓒ; + } + + ul { + list-style: circles; + } + + ol { + list-style-type: none; + } + + li { + list-style-type: disc; + } + "#, + indoc! {r#" + @counter-style circles { + symbols: Ⓐ Ⓑ Ⓒ; + } + + ul { + list-style: circles; + } + + ol { + list-style-type: none; + } + + li { + list-style-type: disc; + } + "#}, + map! { + "circles" => "EgL3uq_circles" referenced: true + }, + HashMap::new(), + crate::css_modules::Config { + custom_idents: false, + ..Default::default() + }, + ); + #[cfg(feature = "grid")] css_modules_test( r#" From e861a2db11c8caab7b506866666c078601b78937 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Tue, 14 May 2024 23:53:20 +0200 Subject: [PATCH 7/8] Add docs --- website/pages/css-modules.md | 51 +++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/website/pages/css-modules.md b/website/pages/css-modules.md index d66016c3..732f83cf 100644 --- a/website/pages/css-modules.md +++ b/website/pages/css-modules.md @@ -13,16 +13,16 @@ CSS modules treat the classes defined in each file as unique. Each class name or To enable CSS modules, provide the `cssModules` option when calling the Lightning CSS API. When using the CLI, enable the `--css-modules` flag. ```js -import { transform } from 'lightningcss'; +import {transform} from 'lightningcss'; -let { code, map, exports } = transform({ +let {code, map, exports} = transform({ // ... cssModules: true, code: Buffer.from(` .logo { background: skyblue; } - `) + `), }); ``` @@ -89,7 +89,7 @@ You can also reference class names defined in a different CSS file using the `fr ```css .logo { - composes: bg-indigo from "./colors.module.css"; + composes: bg-indigo from './colors.module.css'; } ``` @@ -150,10 +150,10 @@ compiles to: By default, class names, id selectors, and the names of `@keyframes`, `@counter-style`, and CSS grid lines and areas are scoped to the module they are defined in. Scoping for CSS variables and other [``](https://www.w3.org/TR/css-values-4/#dashed-idents) names can also be enabled using the `dashedIdents` option when calling the Lightning CSS API. When using the CLI, enable the `--css-modules-dashed-idents` flag. ```js -let { code, map, exports } = transform({ +let {code, map, exports} = transform({ // ... cssModules: { - dashedIdents: true + dashedIdents: true, }, }); ``` @@ -186,7 +186,7 @@ You can also reference variables defined in other files using the `from` keyword ```css .button { - background: var(--accent-color from "./vars.module.css"); + background: var(--accent-color from './vars.module.css'); } ``` @@ -207,19 +207,19 @@ By default, Lightning CSS prepends the hash of the filename to each class name a A pattern is a string with placeholders that will be filled in by Lightning CSS. This allows you to add custom prefixes or adjust the naming convention for scoped classes. ```js -let { code, map, exports } = transform({ +let {code, map, exports} = transform({ // ... cssModules: { - pattern: 'my-company-[name]-[hash]-[local]' - } + pattern: 'my-company-[name]-[hash]-[local]', + }, }); ``` The following placeholders are currently supported: -* `[name]` - The base name of the file, without the extension. -* `[hash]` - A hash of the full file path. -* `[local]` - The original class name or identifier. +- `[name]` - The base name of the file, without the extension. +- `[hash]` - A hash of the full file path. +- `[local]` - The original class name or identifier.
@@ -231,7 +231,7 @@ The following placeholders are currently supported: let { code, map, exports } = transform({ // ... cssModules: { - // ❌ [local] must be at the end so that + // ❌ [local] must be at the end so that // auto-generated grid line names work pattern: '[local]-[hash]' // ✅ do this instead @@ -242,7 +242,7 @@ let { code, map, exports } = transform({ ```css .grid { - grid-template-areas: "nav main"; + grid-template-areas: 'nav main'; } .nav { @@ -252,10 +252,25 @@ let { code, map, exports } = transform({
+## Turning off feature scoping + +Scoping of grid, animations, and custom identifiers can be turned off. By default all of these are scoped. + +```js +let {code, map, exports} = transform({ + // ... + cssModules: { + animation: true, + grid: true, + customIdents: true, + }, +}); +``` + ## Unsupported features Lightning CSS does not currently implement all CSS modules features available in other implementations. Some of these may be added in the future. -* Non-function syntax for the `:local` and `:global` pseudo classes. -* The `@value` rule – superseded by standard CSS variables. -* The `:import` and `:export` ICSS rules. +- Non-function syntax for the `:local` and `:global` pseudo classes. +- The `@value` rule – superseded by standard CSS variables. +- The `:import` and `:export` ICSS rules. From 250edc7cbc1241a98fcc757f2f9539299e28ca80 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Wed, 15 May 2024 00:27:55 +0200 Subject: [PATCH 8/8] Add to_css_with_options --- src/properties/animation.rs | 13 ++++++++----- src/values/ident.rs | 28 +++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/properties/animation.rs b/src/properties/animation.rs index c784f6ee..9da64159 100644 --- a/src/properties/animation.rs +++ b/src/properties/animation.rs @@ -58,19 +58,22 @@ impl<'i> ToCss for AnimationName<'i> { where W: std::fmt::Write, { + let css_module_animation_enabled = + dest.css_module.as_mut().map_or(false, |css_module| css_module.config.animation); + match self { AnimationName::None => dest.write_str("none"), AnimationName::Ident(s) => { if let Some(css_module) = &mut dest.css_module { css_module.reference(&s.0, dest.loc.source_index) } - s.to_css(dest) + s.to_css_with_options(dest, css_module_animation_enabled) } AnimationName::String(s) => { - let mut css_module_animation_enabled = false; - if let Some(css_module) = &mut dest.css_module { - css_module_animation_enabled = css_module.config.animation; - css_module.reference(&s, dest.loc.source_index) + if css_module_animation_enabled { + if let Some(css_module) = &mut dest.css_module { + css_module.reference(&s, dest.loc.source_index) + } } // CSS-wide keywords and `none` cannot remove quotes. diff --git a/src/values/ident.rs b/src/values/ident.rs index 67b71c49..a21b218f 100644 --- a/src/values/ident.rs +++ b/src/values/ident.rs @@ -49,11 +49,29 @@ impl<'i> ToCss for CustomIdent<'i> { where W: std::fmt::Write, { - let css_module_custom_idents_enabled = dest - .css_module - .as_mut() - .map_or(false, |css_module| css_module.config.custom_idents); - dest.write_ident(&self.0, css_module_custom_idents_enabled) + self.to_css_with_options(dest, true) + } +} + +impl<'i> CustomIdent<'i> { + /// Write the custom ident to CSS. + pub fn to_css_with_options( + &self, + dest: &mut Printer, + enabled_css_modules: bool, + ) -> Result<(), PrinterError> + where + W: std::fmt::Write, + { + if enabled_css_modules { + let css_module_custom_idents_enabled = dest + .css_module + .as_mut() + .map_or(false, |css_module| css_module.config.custom_idents); + dest.write_ident(&self.0, css_module_custom_idents_enabled) + } else { + dest.write_ident(&self.0, false) + } } }