Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions derive/src/to_css.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ pub fn derive_to_css(input: TokenStream) -> TokenStream {

let output = quote! {
impl #impl_generics ToCss for #ident #ty_generics #where_clause {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
#imp
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/custom_at_rule.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::{collections::HashMap, convert::Infallible, fmt::Write};
use std::{collections::HashMap, convert::Infallible};

use cssparser::*;
use rspack_lightningcss::{
declaration::DeclarationBlock,
error::PrinterError,
printer::Printer,
printer::PrinterTrait,
properties::custom::{Token, TokenOrValue},
rules::{style::StyleRule, CssRule, CssRuleList, Location},
selector::{Component, Selector},
Expand Down Expand Up @@ -285,7 +285,7 @@ impl<'i, V: Visitor<'i, AtRule>> Visit<'i, AtRule, V> for AtRule {
}

impl ToCss for AtRule {
fn to_css<W: std::fmt::Write>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> {
fn to_css<PrinterT: PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
match self {
AtRule::Tailwind(rule) => {
let _ = rule.loc; // TODO: source maps
Expand Down
2 changes: 1 addition & 1 deletion examples/wpt_adapter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Test-only JSONL bridge for the WPT harness. No production parser behavior is changed.
use cssparser::{Parser, ParserInput};
use lightningcss::{
use rspack_lightningcss::{
properties::{
custom::{CustomPropertyName, TokenList, TokenOrValue, UnresolvedColor},
Property, PropertyId,
Expand Down
12 changes: 5 additions & 7 deletions napi/src/at_rule_parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{collections::HashMap, fmt::Write};
use std::collections::HashMap;

use cssparser::*;
use rspack_lightningcss::{
declaration::DeclarationBlock,
error::ParserError,
printer::PrinterTrait,
rules::{CssRuleList, Location},
stylesheet::ParserOptions,
traits::{AtRuleParser, ToCss},
Expand Down Expand Up @@ -161,13 +162,10 @@ impl<'i> AtRuleParser<'i> for CustomAtRuleParser {
}

impl<'i> ToCss for AtRule<'i> {
fn to_css<W>(
fn to_css<PrinterT: PrinterTrait>(
&self,
dest: &mut rspack_lightningcss::printer::Printer<W>,
) -> Result<(), rspack_lightningcss::error::PrinterError>
where
W: std::fmt::Write,
{
dest: &mut PrinterT,
) -> Result<(), rspack_lightningcss::error::PrinterError> {
dest.write_char('@')?;
serialize_identifier(&self.name, dest)?;
if let Some(prelude) = &self.prelude {
Expand Down
36 changes: 14 additions & 22 deletions src/declaration.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//! CSS declarations.

use std::borrow::Cow;
use std::fmt::Write;
use std::ops::Range;

use crate::context::{DeclarationContext, PropertyHandlerContext};
use crate::error::{ParserError, PrinterError, PrinterErrorKind};
use crate::parser::ParserOptions;
use crate::printer::Printer;
use crate::properties::box_shadow::BoxShadowHandler;
use crate::properties::custom::{CustomProperty, CustomPropertyName};
use crate::properties::masking::MaskHandler;
Expand Down Expand Up @@ -125,10 +123,7 @@ impl<'i> DeclarationBlock<'i> {
}

impl<'i> ToCss for DeclarationBlock<'i> {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
let len = self.declarations.len() + self.important_declarations.len();
let mut i = 0;

Expand All @@ -153,10 +148,10 @@ impl<'i> ToCss for DeclarationBlock<'i> {

impl<'i> DeclarationBlock<'i> {
/// Writes the declarations to a CSS block, including starting and ending braces.
pub fn to_css_block<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
pub fn to_css_block<PrinterT: crate::printer::PrinterTrait>(
&self,
dest: &mut PrinterT,
) -> Result<(), PrinterError> {
dest.whitespace()?;
dest.write_char('{')?;
dest.indent();
Expand Down Expand Up @@ -190,16 +185,13 @@ impl<'i> DeclarationBlock<'i> {
}

/// Writes the declarations to a CSS declaration block.
pub fn to_css_declarations<W>(
pub fn to_css_declarations<PrinterT: crate::printer::PrinterTrait>(
&self,
dest: &mut Printer<W>,
dest: &mut PrinterT,
has_nested_rules: bool,
selectors: &SelectorList,
source_index: u32,
) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
) -> Result<(), PrinterError> {
let mut i = 0;
let len = self.len();

Expand All @@ -209,14 +201,14 @@ impl<'i> DeclarationBlock<'i> {
// The CSS modules `composes` property is handled specially, and omitted during printing.
// We need to add the classes it references to the list for the selectors in this rule.
if let crate::properties::Property::Composes(composes) = &decl {
if dest.is_nested() && dest.css_module.is_some() {
if dest.is_nested() && dest.css_module_config().is_some() {
return Err(dest.error(PrinterErrorKind::InvalidComposesNesting, composes.loc));
}

if let Some(css_module) = &mut dest.css_module {
css_module
.handle_composes(&selectors, &composes, source_index)
.map_err(|e| dest.error(e, composes.loc))?;
if let Some(result) = dest
.with_css_module_mut(|css_module| css_module.handle_composes(&selectors, &composes, source_index))
{
result.map_err(|e| dest.error(e, composes.loc))?;
continue;
}
}
Expand All @@ -226,7 +218,7 @@ impl<'i> DeclarationBlock<'i> {
}

decl.to_css(dest, $important)?;
if i != len - 1 || !dest.minify || has_nested_rules {
if i != len - 1 || !dest.options().minify || has_nested_rules {
dest.write_char(';')?;
}

Expand Down
25 changes: 10 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32642,14 +32642,11 @@ b\0 c');
)
.unwrap();
let result = stylesheet
.to_css(
PrinterOptions {
minify: true,
analyze_dependencies: Some(Default::default()),
..Default::default()
},
None::<&mut ()>,
)
.to_css(PrinterOptions {
minify: true,
analyze_dependencies: Some(Default::default()),
..Default::default()
})
.unwrap();
let dependencies = result.dependencies.unwrap();
assert_eq!(dependencies.len(), 1);
Expand Down Expand Up @@ -32680,13 +32677,11 @@ b\0 c');
.unwrap();
let mut sm = parcel_sourcemap::SourceMap::new("/");
let result = stylesheet
.to_css(
PrinterOptions {
minify: true,
..Default::default()
},
Some(&mut sm),
)
.to_css(PrinterOptions {
minify: true,
source_map: Some(&mut sm),
..Default::default()
})
.unwrap();
let column = result.code.find(".bar").unwrap() as u32;
let mapping = sm.find_closest_mapping(0, column).unwrap();
Expand Down
14 changes: 4 additions & 10 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro_rules! enum_property {
}

impl ToCss for $name {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
dest.write_str(self.as_str())?;
Ok(())
}
Expand Down Expand Up @@ -153,7 +153,7 @@ macro_rules! shorthand_property {
}

impl$(<$l>)? ToCss for $name$(<$l>)? {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError> where W: std::fmt::Write {
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
let mut needs_space = false;
macro_rules! print_one {
($k: ident, $t: ty) => {
Expand Down Expand Up @@ -756,10 +756,7 @@ macro_rules! rect_shorthand {
}

impl ToCss for $name {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
Rect::new(&self.top, &self.right, &self.bottom, &self.left).to_css(dest)
}
}
Expand Down Expand Up @@ -799,10 +796,7 @@ macro_rules! size_shorthand {
}

impl ToCss for $name {
fn to_css<W>(&self, dest: &mut Printer<W>) -> Result<(), PrinterError>
where
W: std::fmt::Write,
{
fn to_css<PrinterT: crate::printer::PrinterTrait>(&self, dest: &mut PrinterT) -> Result<(), PrinterError> {
Size2D(&self.$a_key, &self.$b_key).to_css(dest)
}
}
Expand Down
Loading
Loading