From 0986901d93ccde6cc55095a203044b58f2ca2390 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 16 Oct 2026 20:25:12 +0000 Subject: [PATCH 1/5] css: keep whitespace and comments in token lists A token list is a value with a grammar that the parser does not know: a custom property, an unparsed or unknown declaration, an unknown at-rule, the arguments of an unknown function or pseudo-class. The parser dropped the whitespace next to a delimiter, a function and a closing bracket, and it skipped comments. The printer then guessed the blanks back. `--sel: .a .b` printed as `. a. b`, or as `.a.b` with --minify, and `a/* c */b` printed as `ab`. The parser now stores every whitespace and comment token, and the printer writes the stored tokens and adds none. When it minifies, each run of whitespace and comments between two tokens prints as one space, or as `/**/` when the run holds only comments, and the run at each end of a list does not print. A number keeps the sign it was written with, and a `\` keeps the newline after it. `bun pm diff` compares a print with one form for what no grammar reads in these values: the amount of whitespace, comments, and the blank next to a comma or next to `*` and `/` in a math function. This is a port of lightningcss 32f1f52f ("Do not remove whitespace in token lists") and aa2ed1e3. --- docs/pm/cli/pm.mdx | 1 + src/css/printer.rs | 12 + src/css/properties/custom.rs | 396 ++++++++------- src/css_jsc/css_internals.rs | 30 +- src/js/internal-for-testing.ts | 1 + src/runtime/cli/pm_diff_normalize.rs | 1 + src/runtime/dispatch_js2native.rs | 5 +- test/cli/install/bun-pm-diff.test.ts | 16 +- test/js/bun/css/css.test.ts | 475 +++++++++++++++++- .../bun/css/token-list-backtracking.test.ts | 2 +- test/js/bun/css/token-list-roundtrip.test.ts | 182 +++++++ test/js/bun/css/util.ts | 20 +- 12 files changed, 938 insertions(+), 203 deletions(-) create mode 100644 test/js/bun/css/token-list-roundtrip.test.ts diff --git a/docs/pm/cli/pm.mdx b/docs/pm/cli/pm.mdx index f4c0aef0c65b..fad706168f9a 100644 --- a/docs/pm/cli/pm.mdx +++ b/docs/pm/cli/pm.mdx @@ -292,6 +292,7 @@ On a terminal, code is compared by meaning and shown as written. Both sides of e - A comment edit, a changed type annotation, or any change in control flow is a change, shown in the author's words. - Unchanged text whose meaning moved — code that became unreachable, or dead code that came alive because a condition elsewhere flipped — is marked `~`. - Minified bundles have no readable original, so an un-minified re-print stands in for it (one declaration, call and property per line, each numbered by its `line:col` in the original) and the same folded comparison decides what changed — with the short names two builds' minifiers handed out differently renamed in lockstep; `dist/*.min.js` diffs down to the lines that actually changed rather than one enormous `-`/`+` pair. The re-print is used only when it parses back to the same program as the file; otherwise the file's own lines are shown. `.json` and `.css` files are compared and shown as their canonical print (`normalized`), so a reformat around one edit is one edit; identical prints read `formatting only`. +- In a `.css` file, a custom property, a value that holds `var()` and an unknown at-rule keep the tokens they were written with. The amount of whitespace, comments, and the blank next to a comma or next to `*` and `/` in `calc()` are formatting. A blank that appears or disappears anywhere else is a change: `--sel: .a .b` and `--sel: .a.b` are two values. - The summary at the end calls out what a reviewer looks for first: new `install`/`postinstall` scripts, dependency and entry-point changes, a file that newly imports `child_process`/`fs`/`net`/`http`/`vm`/…, new package imports, growth in `eval()` / `new Function()` / `process.env` / `fetch()` use, newly executable files, a text file that became binary, and terminal escape sequences or bidirectional-text controls appearing in a release. For a published version, `bun install` takes dependencies, `bin`, `os` and `cpu` from the registry's entry for it: where that entry and the `package.json` in the tarball disagree, the summary shows both and goes by the registry. `*.map` files are summarised as regenerated rather than shown. Files over 64 MB are diffed as text (`too large to normalize`). - Line tints follow the terminal: light or dark is taken from `COLORFGBG` or by asking the terminal for its background colour (OSC 11), and exact colours are used when `COLORTERM=truecolor`. - Nothing in a package can drive your terminal: control characters, escape sequences and bidirectional-text overrides in file contents and names are drawn as `␛`, `^M`, `‹U+202E›`, with `NO_COLOR` too. Permission changes show as `mode 644 → 755` / `new file mode 100755`. diff --git a/src/css/printer.rs b/src/css/printer.rs index 43ba326b3326..6e73e38b23ca 100644 --- a/src/css/printer.rs +++ b/src/css/printer.rs @@ -33,6 +33,13 @@ pub struct PrinterOptions<'a> { /// A mapping of pseudo classes to replace with class names that can be applied /// from JavaScript. Useful for polyfills, for example. pub pseudo_classes: Option>, + /// Whether to print each raw token list (a custom property, an unparsed + /// value, an unknown at-rule) in one form for a diff, while the rest of + /// the output stays formatted. Runs of whitespace and comments collapse + /// as `minify` collapses them, and a blank next to a comma, or next to + /// `*` and `/` in a math function, is not kept. A blank anywhere else + /// stays: it can be a descendant combinator. `bun pm diff` sets it. + pub canonical_token_lists: bool, } impl<'a> PrinterOptions<'a> { @@ -50,6 +57,7 @@ impl<'a> PrinterOptions<'a> { }, analyze_dependencies: None, pseudo_classes: None, + canonical_token_lists: false, } } } @@ -120,6 +128,9 @@ pub struct Printer<'a> { pub(crate) line: u32, pub(crate) col: u32, pub(crate) minify: bool, + /// `minify`, or `PrinterOptions::canonical_token_lists`. The second without the first is + /// the diff print. + pub(crate) collapse_token_whitespace: bool, pub(crate) targets: Targets, pub(crate) vendor_prefix: css::VendorPrefix, /// True while nested rules are being re-serialized for a non-final vendor @@ -279,6 +290,7 @@ impl<'a> Printer<'a> { sources: None, dest, minify: options.minify, + collapse_token_whitespace: options.minify || options.canonical_token_lists, targets: options.targets, dependencies: if options.analyze_dependencies.is_some() { Some(BumpVec::new_in(arena)) diff --git a/src/css/properties/custom.rs b/src/css/properties/custom.rs index ff887d55c14a..44e4b239df6c 100644 --- a/src/css/properties/custom.rs +++ b/src/css/properties/custom.rs @@ -15,6 +15,7 @@ use crate::printer::Printer; use crate::values as css_values; use css_values::angle::Angle; +use css_values::calc::CalcUnit; use css_values::color::{ColorFallbackKind, CssColor}; use css_values::ident::{ CustomIdent, CustomIdentFns, DashedIdent, DashedIdentReference, Ident, IdentFns, @@ -300,21 +301,30 @@ impl TokenList { // deinit(): body only freed owned `Vec` fields — handled by `Drop` on `Vec`. pub fn to_css(&self, dest: &mut Printer, is_custom_property: bool) -> PrintResult<()> { + // Unless minifying, the `:`, `,` or `/` that the caller wrote ends in a blank, and + // that blank is the whole of a whitespace-only value (`--foo: ;`). if !dest.minify && self.v.len() == 1 && self.v[0].is_whitespace() { return Ok(()); } + self.write_tokens(dest, is_custom_property, false) + } - let mut has_whitespace = false; - for (i, token_or_value) in self.v.iter().enumerate() { + /// Prints the stored tokens. The grammar of the list is unknown: a blank can be a + /// descendant combinator (`--sel: .a .b`) or a separator for another tool, so the printer + /// adds none. `in_math` is for the arguments of a math function. + fn write_tokens( + &self, + dest: &mut Printer, + is_custom_property: bool, + in_math: bool, + ) -> PrintResult<()> { + let Some(tokens) = self.tokens_to_print(dest)? else { + return Ok(()); + }; + for (i, token_or_value) in tokens.iter().enumerate() { match token_or_value { - TokenOrValue::Color(color) => { - color.to_css(dest)?; - has_whitespace = false; - } - TokenOrValue::UnresolvedColor(color) => { - color.to_css(dest, is_custom_property)?; - has_whitespace = false; - } + TokenOrValue::Color(color) => color.to_css(dest)?, + TokenOrValue::UnresolvedColor(color) => color.to_css(dest, is_custom_property)?, TokenOrValue::Url(url) => { if dest.dependencies.is_some() && is_custom_property @@ -331,100 +341,30 @@ impl TokenList { ); } ext::url_to_css(url, dest)?; - has_whitespace = false; - } - TokenOrValue::Var(var) => { - var.to_css(dest, is_custom_property)?; - has_whitespace = self.write_whitespace_if_needed(i, dest)?; - } - TokenOrValue::Env(env) => { - env.to_css(dest, is_custom_property)?; - has_whitespace = self.write_whitespace_if_needed(i, dest)?; - } - TokenOrValue::Function(f) => { - f.to_css(dest, is_custom_property)?; - has_whitespace = self.write_whitespace_if_needed(i, dest)?; } + TokenOrValue::Var(var) => var.to_css(dest, is_custom_property)?, + TokenOrValue::Env(env) => env.to_css(dest, is_custom_property)?, + TokenOrValue::Function(f) => f.to_css(dest, is_custom_property)?, TokenOrValue::Length(v) => { // Do not serialize unitless zero lengths in custom properties as it may break calc(). let (value, unit) = v.to_unit_value(); css_parser::serializer::serialize_dimension(value, unit, dest)?; - has_whitespace = false; - } - TokenOrValue::Angle(v) => { - v.to_css(dest)?; - has_whitespace = false; - } - TokenOrValue::Time(v) => { - v.to_css(dest)?; - has_whitespace = false; - } - TokenOrValue::Resolution(v) => { - v.to_css(dest)?; - has_whitespace = false; - } - TokenOrValue::DashedIdent(v) => { - dest.write_dashed_ident(v, true)?; - has_whitespace = false; - } - TokenOrValue::AnimationName(v) => { - v.to_css(dest)?; - has_whitespace = false; } + TokenOrValue::Angle(v) => v.to_css(dest)?, + TokenOrValue::Time(v) => v.to_css(dest)?, + TokenOrValue::Resolution(v) => v.to_css(dest)?, + TokenOrValue::DashedIdent(v) => dest.write_dashed_ident(v, true)?, + TokenOrValue::AnimationName(v) => v.to_css(dest)?, TokenOrValue::Token(token) => match token { - Token::Delim(d) => { - if *d == b'+' as u32 || *d == b'-' as u32 { - dest.write_char(b' ')?; - debug_assert!(*d <= 0x7F); - dest.write_char(*d as u8)?; - dest.write_char(b' ')?; - } else { - let ws_before = - !has_whitespace && (*d == b'/' as u32 || *d == b'*' as u32); - debug_assert!(*d <= 0x7F); - dest.delim(*d as u8, ws_before)?; - // `delim()` emits no surrounding whitespace when minifying, so - // consecutive `/` and `*` delims would be printed adjacently and - // form a `/*` or `*/` comment delimiter. Emit a real space when - // the next token is the other half of that pair. - if dest.minify && (*d == b'/' as u32 || *d == b'*' as u32) { - let needs_space = match self.v.get(i + 1) { - Some(TokenOrValue::Token(Token::Delim(next))) => { - (*d == b'/' as u32 && *next == b'*' as u32) - || (*d == b'*' as u32 && *next == b'/' as u32) - } - Some(TokenOrValue::Token(Token::SubstringMatch)) => { - *d == b'/' as u32 - } - _ => false, - }; - if needs_space { - dest.write_char(b' ')?; - } - } - } - has_whitespace = true; - } - Token::Comma => { - dest.delim(b',', false)?; - has_whitespace = true; - } - Token::CloseParen | Token::CloseSquare | Token::CloseCurly => { - token.to_css(dest)?; - has_whitespace = self.write_whitespace_if_needed(i, dest)?; - } Token::Dimension(dim) => { - css_parser::serializer::serialize_dimension(dim.num.value, dim.unit, dest)?; - has_whitespace = false; - } - Token::Number(v) => { - CSSNumberFns::to_css(v.value, dest)?; - has_whitespace = false; + let value = write_sign(&dim.num, dest)?; + css_parser::serializer::serialize_dimension(value, dim.unit, dest)?; } - _ => { - token.to_css(dest)?; - has_whitespace = matches!(token, Token::Whitespace(_)); + Token::Number(num) => { + let value = write_sign(num, dest)?; + CSSNumberFns::to_css(value, dest)?; } + _ => Self::write_token(tokens, i, token, in_math, dest)?, }, } } @@ -432,33 +372,132 @@ impl TokenList { } pub(crate) fn to_css_raw(&self, dest: &mut Printer) -> PrintResult<()> { - for token_or_value in self.v.iter() { - if let TokenOrValue::Token(token) = token_or_value { - token.to_css(dest)?; - } else { + let Some(tokens) = self.tokens_to_print(dest)? else { + return Ok(()); + }; + for (i, token_or_value) in tokens.iter().enumerate() { + let TokenOrValue::Token(token) = token_or_value else { return Err(dest.add_fmt_error()); - } + }; + Self::write_token(tokens, i, token, false, dest)?; } Ok(()) } - pub(crate) fn write_whitespace_if_needed( - &self, + /// The tokens that print. When whitespace and comments are collapsed, the run of them at + /// each end of the list does not print: the text next to it is the printer's own, so + /// nothing there can join two tokens. `None` if nothing else is left, which is a blank + /// value, and this prints it. + fn tokens_to_print(&self, dest: &mut Printer) -> PrintResult> { + if !dest.collapse_token_whitespace { + return Ok(Some(&self.v)); + } + let Some(start) = self.v.iter().position(|t| !t.is_whitespace_or_comment()) else { + // Printed as `/**/`, a comment alone is an empty value, which older browsers + // reject. The diff print has its blank from the separator before the list. + if dest.minify && !self.v.is_empty() { + dest.write_char(b' ')?; + } + return Ok(None); + }; + let end = self + .v + .iter() + .rposition(|t| !t.is_whitespace_or_comment()) + .map_or(start, |last| last + 1); + Ok(Some(&self.v[start..end])) + } + + /// Prints a token whose text depends on the tokens next to it: whitespace, a comment and + /// `\` always, a comma and a math operator in the diff print. Any other token prints as + /// it is. + fn write_token( + tokens: &[TokenOrValue], i: usize, + token: &Token, + in_math: bool, dest: &mut Printer, - ) -> PrintResult { - if !dest.minify - && i != self.v.len() - 1 - && !matches!( - &self.v[i + 1], - TokenOrValue::Token(Token::Comma | Token::CloseParen) - ) - { - // Whitespace is removed during parsing, so add it back if we aren't minifying. - dest.write_char(b' ')?; - Ok(true) + ) -> PrintResult<()> { + let diff_print = dest.collapse_token_whitespace && !dest.minify; + match token { + Token::Whitespace(_) | Token::Comment(_) if dest.collapse_token_whitespace => { + Self::write_collapsed_run(tokens, i, in_math, dest) + } + Token::Whitespace(text) => write_line_breaks_as_lf(text, dest), + Token::Comment(text) => { + dest.write_str("/*")?; + write_line_breaks_as_lf(text, dest)?; + dest.write_str("*/") + } + Token::Delim(d) if *d == u32::from(b'\\') => { + // The tokenizer makes this token only where a newline follows it. Before + // any other text the `\` is an escape. + dest.write_char(b'\\')?; + let newline_follows = !dest.collapse_token_whitespace + && tokens.get(i + 1).is_some_and(TokenOrValue::is_whitespace); + if newline_follows { + Ok(()) + } else { + dest.write_char(b'\n') + } + } + Token::Comma if diff_print => { + dest.write_char(b',')?; + if i + 1 < tokens.len() { + dest.write_char(b' ')?; + } + Ok(()) + } + Token::Delim(d) if diff_print && in_math && is_math_operator(*d) => { + dest.write_char(b' ')?; + dest.write_char(*d as u8)?; + dest.write_char(b' ') + } + _ => token.to_css(dest), + } + } + + /// Prints the one separator of the run of whitespace and comments that `i` is in, at the + /// first token of the run: a blank if the run holds whitespace, else `/**/`. A comment can + /// be all that keeps two tokens apart (`a/**/b` is not `ab`, `/` `/**/` `*` is not `/*`), + /// so it never prints as nothing, which is what lightningcss does. + fn write_collapsed_run( + tokens: &[TokenOrValue], + i: usize, + in_math: bool, + dest: &mut Printer, + ) -> PrintResult<()> { + let before = i.checked_sub(1).and_then(|previous| tokens.get(previous)); + if before.is_some_and(TokenOrValue::is_whitespace_or_comment) { + return Ok(()); + } + let run = tokens[i..] + .iter() + .take_while(|t| t.is_whitespace_or_comment()); + let (mut len, mut has_whitespace) = (0, false); + for t in run { + len += 1; + has_whitespace |= t.is_whitespace(); + } + let after = tokens.get(i + len); + // `\` printed the newline that it needs. + if matches!(before, Some(TokenOrValue::Token(Token::Delim(d))) if *d == u32::from(b'\\')) { + return Ok(()); + } + // No grammar reads a blank next to a comma, or next to `*` and `/` in a math + // function, so the diff print gives these one form. + let has_one_form = |t: &TokenOrValue| match t { + TokenOrValue::Token(Token::Comma) => true, + TokenOrValue::Token(Token::Delim(d)) => in_math && is_math_operator(*d), + _ => false, + }; + if !dest.minify && (before.is_some_and(has_one_form) || after.is_some_and(has_one_form)) { + return Ok(()); + } + if has_whitespace { + dest.write_char(b' ') } else { - Ok(false) + dest.write_str("/**/") } } @@ -492,7 +531,7 @@ impl TokenList { loop { let state = input.state(); - let Ok(token) = input.next_including_whitespace() else { + let Ok(token) = input.next_including_whitespace_and_comments() else { break; }; match token { @@ -558,40 +597,26 @@ impl TokenList { return Err(input.new_custom_error(ParserError::maximum_nesting_depth)); } - let mut last_is_delim = false; - let mut last_is_whitespace = false; - + // Whitespace and comments are stored like any other token. What a minified list + // can lose is decided when it is printed. loop { let state = input.state(); - let Ok(tok) = input.next_including_whitespace() else { + let Ok(tok) = input.next_including_whitespace_and_comments() else { break; }; // Clone the token so we can call &mut methods on `input` below. let tok = *tok; match &tok { - Token::Whitespace(_) | Token::Comment(_) => { - // Skip whitespace if the last token was a delimiter. - // Otherwise, replace all whitespace and comments with a single space character. - if !last_is_delim { - tokens.push(TokenOrValue::Token(Token::Whitespace(b" "))); - last_is_whitespace = true; - } - continue; - } Token::Function(f) => { // Attempt to parse embedded color values into hex tokens. if let Some(color) = try_parse_color_token(f, &state, input) { tokens.push(TokenOrValue::Color(color)); - last_is_delim = false; - last_is_whitespace = false; continue; } let failures_before = input.token_list_parse_failures(); match input.try_parse(|i| UnresolvedColor::parse(i, f, options, depth)) { Ok(color) => { tokens.push(TokenOrValue::UnresolvedColor(color)); - last_is_delim = false; - last_is_whitespace = false; continue; } Err(err) => { @@ -610,16 +635,12 @@ impl TokenList { if strings::eql(*f, b"url") { input.reset(&state); tokens.push(TokenOrValue::Url(ext::url_parse(input)?)); - last_is_delim = false; - last_is_whitespace = false; } else if strings::eql(*f, b"var") { let var = input.parse_nested_block(|input2| { let thevar = Variable::parse(input2, options, depth + 1)?; Ok(TokenOrValue::Var(thevar)) })?; tokens.push(var); - last_is_delim = true; - last_is_whitespace = false; } else if strings::eql(*f, b"env") { let env = input.parse_nested_block(|input2| { let env = @@ -627,8 +648,6 @@ impl TokenList { Ok(TokenOrValue::Env(env)) })?; tokens.push(env); - last_is_delim = true; - last_is_whitespace = false; } else { let arguments = input.parse_nested_block(|input2| { TokenListFns::parse(input2, options, depth + 1) @@ -639,8 +658,6 @@ impl TokenList { }, arguments, })); - last_is_delim = true; // Whitespace is not required after any of these chars. - last_is_whitespace = false; } continue; } @@ -652,15 +669,11 @@ impl TokenList { }; tokens.push(TokenOrValue::Color(CssColor::Rgba(rgba))); } - last_is_delim = false; - last_is_whitespace = false; continue; } Token::UnquotedUrl(_) => { input.reset(&state); tokens.push(TokenOrValue::Url(ext::url_parse(input)?)); - last_is_delim = false; - last_is_whitespace = false; continue; } Token::Ident(name) => { @@ -668,8 +681,6 @@ impl TokenList { tokens.push(TokenOrValue::DashedIdent(DashedIdent { v: std::ptr::from_ref::<[u8]>(*name), })); - last_is_delim = false; - last_is_whitespace = false; continue; } } @@ -685,12 +696,13 @@ impl TokenList { TokenListFns::parse_into(input2, tokens, options, depth + 1) })?; tokens.push(TokenOrValue::Token(closing_delimiter)); - last_is_delim = true; // Whitespace is not required after any of these chars. - last_is_whitespace = false; continue; } - Token::Dimension(_) => { - let value = if let Ok(length) = LengthValue::try_from_token(&tok) { + Token::Dimension(dim) => { + // A typed value does not keep a written `+`, and `2n+1px` is not `2n1px`. + let value = if dim.num.has_sign && dim.num.value >= 0.0 { + TokenOrValue::Token(tok) + } else if let Ok(length) = LengthValue::try_from_token(&tok) { TokenOrValue::Length(length) } else if let Ok(angle) = Angle::try_from_token(&tok) { TokenOrValue::Angle(angle) @@ -703,9 +715,6 @@ impl TokenList { }; tokens.push(value); - - last_is_delim = false; - last_is_whitespace = false; continue; } _ => {} @@ -717,18 +726,7 @@ impl TokenList { location: state.source_location(), }); } - last_is_delim = matches!(&tok, Token::Delim(_) | Token::Comma); - - // If this is a delimiter, and the last token was whitespace, - // replace the whitespace with the delimiter since both are not required. - if last_is_delim && last_is_whitespace { - let last = tokens.last_mut().expect("unreachable"); - *last = TokenOrValue::Token(tok); - } else { - tokens.push(TokenOrValue::Token(tok)); - } - - last_is_whitespace = false; + tokens.push(TokenOrValue::Token(tok)); } Ok(()) @@ -838,6 +836,39 @@ impl TokenList { pub type TokenListFns = TokenList; +/// `*` and `/`: in the arguments of a math function no grammar reads the blanks next to them. +fn is_math_operator(delim: u32) -> bool { + delim == u32::from(b'*') || delim == u32::from(b'/') +} + +/// Writes the sign that a number was written with and returns the value to print after it. +/// The sign is part of the token: without it `2n+1` prints as `2n1`. +fn write_sign(num: &crate::Num, dest: &mut Printer) -> PrintResult { + if !num.has_sign { + return Ok(num.value); + } + dest.write_char(if num.value.is_sign_negative() { + b'-' + } else { + b'+' + })?; + Ok(num.value.abs()) +} + +/// Prints the text of whitespace or of a comment as it was written, except that each line +/// break that CSS reads as one (CR LF, CR, FF) prints as `\n`: the output does not follow the +/// line endings of the checkout. +fn write_line_breaks_as_lf(text: &[u8], dest: &mut Printer) -> PrintResult<()> { + let mut rest = text; + while let Some(i) = strings::index_of_any(rest, b"\r\x0C") { + dest.write_bytes(&rest[..i])?; + dest.write_char(b'\n')?; + let is_crlf = rest[i] == b'\r' && rest.get(i + 1) == Some(&b'\n'); + rest = &rest[i + 1 + usize::from(is_crlf)..]; + } + dest.write_bytes(rest) +} + pub(crate) type Fallbacks = (SupportsCondition, TokenList); /// A color value with an unresolved alpha value (e.g. a variable). @@ -911,7 +942,7 @@ impl UnresolvedColor { css_parser::to_css::integer(conv(*g), dest)?; dest.write_char(b' ')?; css_parser::to_css::integer(conv(*b), dest)?; - dest.delim(b'/', true)?; + Self::write_alpha_slash(alpha, dest)?; alpha.to_css(dest, is_custom_property)?; dest.write_char(b')') } @@ -938,7 +969,7 @@ impl UnresolvedColor { Percentage { v: *s }.to_css(dest)?; dest.write_char(b' ')?; Percentage { v: *l }.to_css(dest)?; - dest.delim(b'/', true)?; + Self::write_alpha_slash(alpha, dest)?; alpha.to_css(dest, is_custom_property)?; dest.write_char(b')') } @@ -956,7 +987,7 @@ impl UnresolvedColor { } dest.write_str("light-dark(")?; - light.to_css(dest, is_custom_property)?; + light.write_tokens(dest, is_custom_property, false)?; dest.delim(b',', false)?; dark.to_css(dest, is_custom_property)?; dest.write_char(b')') @@ -964,6 +995,23 @@ impl UnresolvedColor { } } + /// Writes the `/` before an unresolved alpha. When minifying, no blank + /// follows it, and a `*` or `*=` directly after it would open a comment. + fn write_alpha_slash(alpha: &TokenList, dest: &mut Printer) -> PrintResult<()> { + dest.delim(b'/', true)?; + // The whitespace and comments at the start of the list do not print. + let first = alpha.v.iter().find(|t| !t.is_whitespace_or_comment()); + let opens_comment = match first { + Some(TokenOrValue::Token(Token::Delim(d))) => *d == u32::from(b'*'), + Some(TokenOrValue::Token(Token::SubstringMatch)) => true, + _ => false, + }; + if dest.minify && opens_comment { + dest.write_char(b' ')?; + } + Ok(()) + } + fn parse( input: &mut Parser, f: &[u8], @@ -1301,7 +1349,12 @@ impl Function { fn to_css(&self, dest: &mut Printer, is_custom_property: bool) -> PrintResult<()> { IdentFns::to_css(&self.name, dest)?; dest.write_char(b'(')?; - self.arguments.to_css(dest, is_custom_property)?; + // Only the diff print reads this. + let in_math = dest.collapse_token_whitespace + && !dest.minify + && CalcUnit::get_any_case(self.name.v()).is_some(); + self.arguments + .write_tokens(dest, is_custom_property, in_math)?; dest.write_char(b')') } @@ -1354,6 +1407,13 @@ impl TokenOrValue { fn is_whitespace(&self) -> bool { matches!(self, TokenOrValue::Token(Token::Whitespace(_))) } + + fn is_whitespace_or_comment(&self) -> bool { + matches!( + self, + TokenOrValue::Token(Token::Whitespace(_) | Token::Comment(_)) + ) + } } // ─── Clone / Debug shims ─────────────────────────────────────────────────── diff --git a/src/css_jsc/css_internals.rs b/src/css_jsc/css_internals.rs index a7141d16c9f6..54cc963efd3b 100644 --- a/src/css_jsc/css_internals.rs +++ b/src/css_jsc/css_internals.rs @@ -13,6 +13,8 @@ pub(crate) enum TestKind { Normal, Minify, Prefix, + /// The print that `bun pm diff` compares: no minify pass, canonical token lists. + Canonical, } #[derive(PartialEq, Eq, Clone, Copy)] @@ -58,6 +60,10 @@ pub fn _test(global: &JSGlobalObject, frame: &CallFrame) -> JsResult { testing_impl(global, frame, TestKind::Normal, TestCategory::Normal) } +pub fn canonical_test(global: &JSGlobalObject, frame: &CallFrame) -> JsResult { + testing_impl(global, frame, TestKind::Canonical, TestCategory::Normal) +} + /// Shared arg-validation for the test-only CSS internals: pulls the next arg, /// throws "{fn_name}: expected {expected_n} arguments, got {got_n}" if absent, /// throws "{fn_name}: expected {arg_label} to be a string" if not a string, @@ -168,13 +174,16 @@ fn testing_impl( ) { Ok(ret) => { let (mut stylesheet, extra) = ret; - let mut minify_options = MinifyOptions::default(); - minify_options.targets.browsers = browsers; - match stylesheet.minify(alloc, &minify_options, &extra) { - Ok(_) => {} - Err(err) => { - return Err(global - .throw_value(global.create_error_instance(format_args!("{}", err.kind)))); + if test_kind != TestKind::Canonical { + let mut minify_options = MinifyOptions::default(); + minify_options.targets.browsers = browsers; + match stylesheet.minify(alloc, &minify_options, &extra) { + Ok(_) => {} + Err(err) => { + return Err(global.throw_value( + global.create_error_instance(format_args!("{}", err.kind)), + )); + } } } @@ -183,11 +192,8 @@ fn testing_impl( let result = match stylesheet.to_css( alloc, &PrinterOptions { - minify: match test_kind { - TestKind::Minify => true, - TestKind::Normal => false, - TestKind::Prefix => false, - }, + minify: test_kind == TestKind::Minify, + canonical_token_lists: test_kind == TestKind::Canonical, targets: Targets { browsers, ..Default::default() diff --git a/src/js/internal-for-testing.ts b/src/js/internal-for-testing.ts index fa6de0bb5c6a..56a23b11f67a 100644 --- a/src/js/internal-for-testing.ts +++ b/src/js/internal-for-testing.ts @@ -138,6 +138,7 @@ export const cssInternals = { minifyTest: $newRustFunction("css_internals.rs", "minifyTest", 3), prefixTest: $newRustFunction("css_internals.rs", "prefixTest", 3), _test: $newRustFunction("css_internals.rs", "_test", 3), + canonicalTest: $newRustFunction("css_internals.rs", "canonicalTest", 3), attrTest: $newRustFunction("css_internals.rs", "attrTest", 3), }; diff --git a/src/runtime/cli/pm_diff_normalize.rs b/src/runtime/cli/pm_diff_normalize.rs index 1613736d4aca..818e026f6b44 100644 --- a/src/runtime/cli/pm_diff_normalize.rs +++ b/src/runtime/cli/pm_diff_normalize.rs @@ -942,6 +942,7 @@ fn normalize_css(path: &[u8], bytes: &[u8]) -> Option { alloc, &bun_css::PrinterOptions { minify: false, + canonical_token_lists: true, ..bun_css::PrinterOptions::default() }, Some(bun_css::ImportInfo::init_outside_of_bundler( diff --git a/src/runtime/dispatch_js2native.rs b/src/runtime/dispatch_js2native.rs index 30676941b365..0578b37621ca 100644 --- a/src/runtime/dispatch_js2native.rs +++ b/src/runtime/dispatch_js2native.rs @@ -75,12 +75,13 @@ pub(crate) fn bun_get_use_system_ca( mod css { pub(crate) use bun_css_jsc::css_internals::{ - _test, attr_test, minify_error_test_with_options, minify_test, minify_test_with_options, - prefix_test, prefix_test_with_options, test_with_options, + _test, attr_test, canonical_test, minify_error_test_with_options, minify_test, + minify_test_with_options, prefix_test, prefix_test_with_options, test_with_options, }; } pub(crate) use css::_test as css_jsc_css_internals__test; pub(crate) use css::attr_test as css_jsc_css_internals_attr_test; +pub(crate) use css::canonical_test as css_jsc_css_internals_canonical_test; pub(crate) use css::minify_error_test_with_options as css_jsc_css_internals_minify_error_test_with_options; pub(crate) use css::minify_test as css_jsc_css_internals_minify_test; pub(crate) use css::minify_test_with_options as css_jsc_css_internals_minify_test_with_options; diff --git a/test/cli/install/bun-pm-diff.test.ts b/test/cli/install/bun-pm-diff.test.ts index 63383f06815b..657b3a281362 100644 --- a/test/cli/install/bun-pm-diff.test.ts +++ b/test/cli/install/bun-pm-diff.test.ts @@ -822,11 +822,17 @@ describe.concurrent("bun pm diff (canonical re-print)", () => { "b/index.js": `"use strict"\n\nmodule.exports = function add(a, b) {\n\treturn a + b\n}\n`, "a/style.css": `.a{color:red;margin:0 auto}`, "b/style.css": `.a {\n color: red;\n margin: 0 auto;\n}\n`, + // Values that the CSS parser keeps as tokens, as a minifier prints them and as `bun fmt` prints them, with + // one value wrapped and commented by hand. The blank next to a comma and next to `*` and `/` in calc(), + // the amount of whitespace, and a comment are formatting. + "a/tokens.css": `.a{--shadow:0 0 1px red,0 0 2px blue;font-family:var(--f),sans-serif;color:rgba(1,2,3,var(--o));width:calc(var(--a)*2/3);margin:var(--x) auto}@apply a b;`, + "b/tokens.css": `.a {\n --shadow: 0 0 1px red, /* second layer */\n 0 0 2px blue;\n font-family: var(--f), sans-serif;\n color: rgba(1, 2, 3, var(--o));\n width: calc(var(--a) * 2 / 3);\n margin: var(--x) auto /* centered */;\n}\n@apply a b;\n`, }); expect(text).toMatch(/\nindex\.js ─+ formatting only\n/); expect(text).toMatch(/\npackage\.json ─+ formatting only\n/); expect(text).toMatch(/\nstyle\.css ─+ formatting only\n/); - expect(text).toContain("3 formatting only"); + expect(text).toMatch(/\ntokens\.css ─+ formatting only\n/); + expect(text).toContain("4 formatting only"); expect(changedLines(text)).toEqual([]); expect(exitCode).toBe(0); }); @@ -2156,6 +2162,9 @@ describe.concurrent("bun pm diff (engine invariants)", () => { "b/s.css": ".a { color: blue }\n.b { margin: 0 }\n", "a/bad.css": ".a{color:red}\n", "b/bad.css": ".a{color:red\n.b{{{\n", + // A blank that is not next to a comma can be a descendant combinator, so one that goes or comes is a change. + "a/sel.css": ".a{--sel:.x .y;--w:var(--m)var(--n)}\n", + "b/sel.css": ".a{--sel:.x.y;--w:var(--m) var(--n)}\n", "a/d.json": '{"a":1,"b":[1,2]}\n', "b/d.json": '{\n "a": 2,\n "b": [1, 2]\n}\n', "a/bad.json": '{"a":1}\n', @@ -2166,6 +2175,11 @@ describe.concurrent("bun pm diff (engine invariants)", () => { expect(text).toContain("│- color: red;"); expect(text).toContain("│+ color: #00f;"); expect(text).not.toContain("margin"); + expect(text).toMatch(/\nsel\.css ─+ normalized \+2 -2\n/); + expect(text).toContain("│- --sel: .x .y;"); + expect(text).toContain("│+ --sel: .x.y;"); + expect(text).toContain("│- --w: var(--m)var(--n);"); + expect(text).toContain("│+ --w: var(--m) var(--n);"); expect(text).toMatch(/\nd\.json ─+ normalized \+1 -1\n/); expect(text).toContain('│+ "a": 2,'); expect(text).not.toMatch(/│[-+].*"b"/); diff --git a/test/js/bun/css/css.test.ts b/test/js/bun/css/css.test.ts index 6e7b32d3b5e6..46f0b1c731a1 100644 --- a/test/js/bun/css/css.test.ts +++ b/test/js/bun/css/css.test.ts @@ -3,11 +3,14 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ import { describe, expect, test } from "bun:test"; -import { bunEnv, bunExe } from "harness"; +import { bunEnv, bunExe, tempDir } from "harness"; import { join } from "path"; import { + Browsers, + canonical_test, cssTest, cssTestTodo, + exact_test, indoc, minify_error_test_with_options, minify_test, @@ -98,21 +101,32 @@ describe("css tests", () => { }`, ); - // Adjacent `/` and `*` delim tokens must not be printed as `/*` or `*/` - // when minifying, which would open/close a comment and swallow the rest of - // the stylesheet. - minify_test(":root { --a: x / * y }\n.k { color: red }", ":root{--a:x/ *y}.k{color:red}"); - minify_test(":root { --a: x * / y }\n.k { color: red }", ":root{--a:x* /y}.k{color:red}"); - minify_test(":root { --a: x / * / * y }", ":root{--a:x/ * / *y}"); - minify_test(".foo { unknown-prop: a / * b }", ".foo{unknown-prop:a/ *b}"); - minify_test(":root { --a: f(x / * y) }", ":root{--a:f(x/ *y)}"); - minify_test(":root { --a: x / *= y }", ":root{--a:x/ *= y}"); - // A lone `/` or `*` delim must still minify without extra whitespace. - minify_test(":root { --a: 16 / 9 }", ":root{--a:16/9}"); - minify_test(":root { --a: x * y }", ":root{--a:x*y}"); - minify_test(":root { --a: x / / y }", ":root{--a:x//y}"); - // Non-minified output is unchanged. - cssTest(":root { --a: x / * y }", ":root {\n --a: x / * y;\n}\n"); + // A `/` printed directly before a `*` opens a comment that swallows the + // rest of the stylesheet. The author's blanks between the two stay, as one + // space each, so the minifier never prints them side by side. + minify_test(":root { --a: x / * y }\n.k { color: red }", ":root{--a:x / * y}.k{color:red}"); + minify_test(":root { --a: x * / y }\n.k { color: red }", ":root{--a:x * / y}.k{color:red}"); + minify_test(":root { --a: x / * / * y }", ":root{--a:x / * / * y}"); + minify_test(".foo { unknown-prop: a / * b }", ".foo{unknown-prop:a / * b}"); + minify_test(":root { --a: f(x / * y) }", ":root{--a:f(x / * y)}"); + minify_test(":root { --a: x / *= y }", ":root{--a:x / *= y}"); + // When only comments keep the `/` and the `*` apart, one empty comment + // takes their place. + minify_test(":root { --a: x //**/* y }\n.k { color: red }", ":root{--a:x //**/* y}.k{color:red}"); + minify_test(":root { --a: x //* c *//* d */* y }\n.k { color: red }", ":root{--a:x //**/* y}.k{color:red}"); + minify_test(".a:foo(a//**/*b) { color: red }\n.k { color: blue }", ".a:foo(a//**/*b){color:red}.k{color:#00f}"); + // The `/` before an alpha that is only known at runtime comes from the + // printer, not from the author's tokens. + minify_test(":root { --a: rgb(1 2 3 / *x) }\n.k { color: red }", ":root{--a:rgb(1 2 3/ *x)}.k{color:red}"); + minify_test(":root { --a: hsl(1 2% 3% / *= x) }\n.k { color: red }", ":root{--a:hsl(1 2% 3%/ *= x)}.k{color:red}"); + minify_test(":root { --a: rgb(1 2 3 //* c */*x) }\n.k { color: red }", ":root{--a:rgb(1 2 3/ *x)}.k{color:red}"); + // A `/` or a `*` keeps the blanks it was written with and gets no other. + minify_test(":root { --a: 16 / 9 }", ":root{--a:16 / 9}"); + minify_test(":root { --a: 16/9 }", ":root{--a:16/9}"); + minify_test(":root { --a: x * y }", ":root{--a:x * y}"); + minify_test(":root { --a: x*y }", ":root{--a:x*y}"); + minify_test(":root { --a: x / / y }", ":root{--a:x / / y}"); + exact_test(":root { --a: x / * y }", ":root {\n --a: x / * y;\n}\n"); // Leading/trailing whitespace around a custom-property value is dropped. minify_test(":root{--a: x}", ":root{--a:x}"); @@ -131,7 +145,369 @@ describe("css tests", () => { // and printed back from them, in order; the legacy comma form is not a color // here and is left as written. minify_test(":root{--a: rgb(4.7% 13.3% 22% / var(--alpha))}", ":root{--a:rgb(12 34 56/var(--alpha))}"); - minify_test(":root{--a: rgb(12, 34, 56, var(--alpha))}", ":root{--a:rgb(12,34,56,var(--alpha))}"); + minify_test(":root{--a: rgb(12, 34, 56, var(--alpha))}", ":root{--a:rgb(12, 34, 56, var(--alpha))}"); + + // A token list is a value whose grammar is unknown: a custom property, an + // unknown or unparsed declaration, an unknown at-rule, the arguments of an + // unknown function or pseudo-class. A blank there can be a descendant + // combinator (`--sel: .a .b`) or a separator that another tool reads + // (`@apply a b`). So each blank comes out where the author wrote it and + // nowhere else: as written by default, as one space when minified. + describe("whitespace and comments in token lists", () => { + // Each output is also printed again, in the mode that made it: it must come back as it is. + function token_list_test(source: string, normal: string, minified: string) { + exact_test(source, normal); + minify_test(source, minified); + if (normal !== source) exact_test(normal, normal); + if (minified !== source) minify_test(minified, minified); + } + + // [declaration, default output, minified output] + const declarations: [string, string, string][] = [ + // Next to a delimiter. + ["--sel: .a .b", "--sel: .a .b", "--sel:.a .b"], + ["--sel: .a.b", "--sel: .a.b", "--sel:.a.b"], + ["--sel: .dark *", "--sel: .dark *", "--sel:.dark *"], + ["--sel: > a ~ b", "--sel: > a ~ b", "--sel:> a ~ b"], + ["--sel: a>b", "--sel: a>b", "--sel:a>b"], + ["--q: width >= 600px", "--q: width >= 600px", "--q:width >= 600px"], + ["--x: a+b", "--x: a+b", "--x:a+b"], + ["--x: a + b", "--x: a + b", "--x:a + b"], + ["--x: 1/2", "--x: 1/2", "--x:1/2"], + ["--x: a , b", "--x: a , b", "--x:a , b"], + ["--x: a,b", "--x: a,b", "--x:a,b"], + ["--x: a:b c : d", "--x: a:b c : d", "--x:a:b c : d"], + // After a closing bracket, a function, var() and env(). + ["--x: (a) (b) [c] d", "--x: (a) (b) [c] d", "--x:(a) (b) [c] d"], + ["--x: (a)(b)[c]d", "--x: (a)(b)[c]d", "--x:(a)(b)[c]d"], + ["--x: {a: b; c: d} e", "--x: {a: b; c: d} e", "--x:{a: b; c: d} e"], + ["--x: f(a) g(b)", "--x: f(a) g(b)", "--x:f(a) g(b)"], + ["--x: f(a)g(b)", "--x: f(a)g(b)", "--x:f(a)g(b)"], + ["--x: var(--a) var(--b)", "--x: var(--a) var(--b)", "--x:var(--a) var(--b)"], + ["--x: var(--a)var(--b)", "--x: var(--a)var(--b)", "--x:var(--a)var(--b)"], + // In an unknown property and in a known property that holds var(). + ["unknown-prop: .a .b", "unknown-prop: .a .b", "unknown-prop:.a .b"], + ["margin: var(--a) auto", "margin: var(--a) auto", "margin:var(--a) auto"], + ["width: calc(var(--a) + 2px)", "width: calc(var(--a) + 2px)", "width:calc(var(--a) + 2px)"], + ["width: calc(var(--a)*2)", "width: calc(var(--a)*2)", "width:calc(var(--a)*2)"], + ["grid-area: var(--a) / var(--b)", "grid-area: var(--a) / var(--b)", "grid-area:var(--a) / var(--b)"], + ["font: var(--a)/1.5 x", "font: var(--a)/1.5 x", "font:var(--a)/1.5 x"], + // Syntax that a browser drops when a blank is added to it. + [ + "width: attr(data-w type(), 100px)", + "width: attr(data-w type(), 100px)", + "width:attr(data-w type(), 100px)", + ], + [ + "--x: attr(data-count type(+))", + "--x: attr(data-count type(+))", + "--x:attr(data-count type(+))", + ], + [ + "color: if(media(width >= 600px): red; else: blue)", + "color: if(media(width >= 600px): red; else: blue)", + "color:if(media(width >= 600px): red; else: blue)", + ], + // The blank after the comma of a fallback is the printer's. The rest + // of the fallback is the author's. + ["--x: var(--a, 1px) x", "--x: var(--a, 1px) x", "--x:var(--a,1px) x"], + ["--x: var(--a,1px)", "--x: var(--a, 1px)", "--x:var(--a,1px)"], + ["--x: var(--a,)", "--x: var(--a, )", "--x:var(--a,)"], + ["--x: var(--a, )", "--x: var(--a, )", "--x:var(--a, )"], + [ + "padding: env(safe-area-inset-top, 1px) 2px", + "padding: env(safe-area-inset-top, 1px) 2px", + "padding:env(safe-area-inset-top,1px) 2px", + ], + // The edges of the arguments of a function are trimmed. An argument + // list that is only whitespace is kept. + ["--x: f( a , b )", "--x: f(a , b)", "--x:f(a , b)"], + ["--x: f( )", "--x: f( )", "--x:f( )"], + ["--x: light-dark( , x)", "--x: light-dark( , x)", "--x:light-dark( ,x)"], + // A run of whitespace is as written by default and one space when minified. + ["--x: a b", "--x: a b", "--x:a b"], + ["--x: a\n b", "--x: a\n b", "--x:a b"], + ["--x: a\tb", "--x: a\tb", "--x:a b"], + ["--x: f(a,\n b)", "--x: f(a,\n b)", "--x:f(a, b)"], + // A line break prints as `\n`, whatever the line endings of the source are. + ["--x: a\r\n b", "--x: a\n b", "--x:a b"], + ["--x: a\rb\fc", "--x: a\nb\nc", "--x:a b c"], + ["--x: a/* c\r\nd */b", "--x: a/* c\nd */b", "--x:a/**/b"], + // A comment is as written by default. Minified, it goes when a blank + // is next to it, and leaves `/**/` when it alone keeps two tokens apart. + ["--x: a/* c */b", "--x: a/* c */b", "--x:a/**/b"], + ["--x: b /* c */ d", "--x: b /* c */ d", "--x:b d"], + ["--x: b /* c *//* d */ e", "--x: b /* c *//* d */ e", "--x:b e"], + ["--x: 1 +/**/2", "--x: 1 +/**/2", "--x:1 +/**/2"], + // Minified, the whitespace and the comments at each end of a list go. + ["--x: f(/* c */a)", "--x: f(/* c */a)", "--x:f(a)"], + ["--x: f( /* c */ a )", "--x: f(/* c */ a)", "--x:f(a)"], + ["--x: a /* c */", "--x: a /* c */", "--x:a"], + ["--x: a/* c */", "--x: a/* c */", "--x:a"], + ["--x: /* c */b", "--x: /* c */b", "--x:b"], + ["--x: /* c */ a", "--x: /* c */ a", "--x:a"], + ["margin: var(--a) /* c */", "margin: var(--a) /* c */", "margin:var(--a)"], + // Minified, a value of only whitespace and comments is one blank. With + // no blank it is an empty value, which older browsers reject. + ["--x: /* off */", "--x: /* off */", "--x: "], + ["--x:/* off */", "--x: /* off */", "--x: "], + ["margin: var(--m, /* none */)", "margin: var(--m, /* none */)", "margin:var(--m, )"], + [ + "--tw-ring-inset: var(--tw-empty,/*!*/ /*!*/)", + "--tw-ring-inset: var(--tw-empty, /*!*/ /*!*/)", + "--tw-ring-inset:var(--tw-empty, )", + ], + ["--x: f(/**/)", "--x: f(/**/)", "--x:f( )"], + // A number keeps the sign it was written with. Without it the number + // joins the token before it. + ["--x: 1+2", "--x: 1+2", "--x:1+2"], + ["--x: 1-0", "--x: 1-0", "--x:1-0"], + ["--x: a+1", "--x: a+1", "--x:a+1"], + ["--nth: 2n+1", "--nth: 2n+1", "--nth:2n+1"], + ["--x: 1px+2px", "--x: 1px+2px", "--x:1px+2px"], + ["--x: calc(100%+2rem)", "--x: calc(100%+2rem)", "--x:calc(100%+2rem)"], + ["--x: +1px -1px +.5 -.5", "--x: +1px -1px +.5 -.5", "--x:+1px -1px +.5 -.5"], + ["width: calc(1px+2px)", "width: calc(1px+2px)", "width:calc(1px+2px)"], + // `\` is a token only before a newline, so a newline follows it in the + // output too. Before `)` or `;` it is an escape. + ["--x: f(a\\\n)", "--x: f(a\\\n)", "--x:f(a\\\n)"], + ["--x: a\\\n", "--x: a\\\n", "--x:a\\\n"], + ["--x: a\\\n b", "--x: a\\\n b", "--x:a\\\nb"], + ]; + for (const [declaration, normal, minified] of declarations) { + token_list_test(`.c { ${declaration}; }`, `.c {\n ${normal};\n}\n`, `.c{${minified}}`); + } + + // [stylesheet, default output, minified output] + const sheets: [string, string, string][] = [ + // The prelude and the block of an unknown at-rule. + [ + "@custom-variant dark (&:is(.dark *));", + "@custom-variant dark (&:is(.dark *));\n", + "@custom-variant dark (&:is(.dark *));", + ], + ["@apply outline-ring/50;", "@apply outline-ring/50;\n", "@apply outline-ring/50;"], + [ + "@apply text-[#fff] bg-[#000] font-sans;", + "@apply text-[#fff] bg-[#000] font-sans;\n", + "@apply text-[#fff] bg-[#000] font-sans;", + ], + ["@apply a /* c */ ;", "@apply a /* c */;\n", "@apply a;"], + ["@foo (a:b) and (c: d);", "@foo (a:b) and (c: d);\n", "@foo (a:b) and (c: d);"], + ["@foo a . b { c .d>e }", "@foo a . b {\n c .d>e\n}\n", "@foo a . b{c .d>e}"], + [ + "@function --f(--x +) returns { result: var(--x); }", + "@function --f(--x +) returns {\n result: var(--x);\n}\n", + "@function --f(--x +) returns {result: var(--x);}", + ], + // The arguments of an unknown functional pseudo-class and pseudo-element. + [".c:foo(a .b > c) { color: red }", ".c:foo(a .b > c) {\n color: red;\n}\n", ".c:foo(a .b > c){color:red}"], + [".c:foo( a .b >c ) { color: red }", ".c:foo( a .b >c ) {\n color: red;\n}\n", ".c:foo(a .b >c){color:red}"], + [".c:foo(a/* c */b) { color: red }", ".c:foo(a/* c */b) {\n color: red;\n}\n", ".c:foo(a/**/b){color:red}"], + [ + ".c::foo(a /* c */ .b) { color: red }", + ".c::foo(a /* c */ .b) {\n color: red;\n}\n", + ".c::foo(a .b){color:red}", + ], + // The other places that hold a token list. + [ + `@property --x { syntax: "*"; inherits: false; initial-value: .a .b; }`, + `@property --x {\n syntax: "*";\n inherits: false;\n initial-value: .a .b;\n}\n`, + `@property --x{syntax:"*";inherits:false;initial-value:.a .b}`, + ], + [ + "@container style(--sel: .a .b) { .c { color: red } }", + "@container style(--sel: .a .b) {\n .c {\n color: red;\n }\n}\n", + "@container style(--sel:.a .b){.c{color:red}}", + ], + [ + "@font-face { font-family: x; unknown-desc: .a .b; }", + "@font-face {\n font-family: x;\n unknown-desc: .a .b;\n}\n", + "@font-face{font-family:x;unknown-desc:.a .b}", + ], + // Two values that differ only by a blank are two values. The rules do not merge. + [ + ".c { --sel: .a .b } .d { --sel: .a.b }", + ".c {\n --sel: .a .b;\n}\n\n.d {\n --sel: .a.b;\n}\n", + ".c{--sel:.a .b}.d{--sel:.a.b}", + ], + // The lines of a value are the author's. A value that runs over more + // than one line keeps the indentation of its source, not of the output. + [ + "div {\n color: blue;\n\n --button:focus {\n color: red;\n }\n}", + "div {\n color: #00f;\n --button: focus {\n color: red;\n };\n}\n", + "div{color:#00f;--button:focus { color: red; }}", + ], + [ + "@foo test {\n div {\n color: red;\n }\n }", + "@foo test {\n div {\n color: red;\n }\n}\n", + "@foo test{div { color: red; }}", + ], + ["\t.c {\n\t\t--x: a\n\t\t\tb;\n\t}", ".c {\n --x: a\n\t\t\tb;\n}\n", ".c{--x:a b}"], + [" .c {\n --x: a\n b;\n }", ".c {\n --x: a\n b;\n}\n", ".c{--x:a b}"], + ]; + for (const [sheet, normal, minified] of sheets) { + token_list_test(sheet, normal, minified); + } + + // The expectations that lightningcss changed with this behavior + // (32f1f52f "Do not remove whitespace in token lists", and aa2ed1e3). + minify_test( + ".foo { width: calc(100% - 2 (2 * var(--card-margin))); }", + ".foo{width:calc(100% - 2 (2 * var(--card-margin)))}", + ); + exact_test( + ".test {\n width: calc(var(--test) + 2px);\n width: calc(var(--test) - 2px);\n}\n", + ".test {\n width: calc(var(--test) + 2px);\n width: calc(var(--test) - 2px);\n}\n", + ); + minify_test( + '@font-face {src: url("foo.ttf") tech(palettes color-colrv0 variations) format(opentype);}', + "@font-face{src:url(foo.ttf) tech(palettes color-colrv0 variations) format(opentype)}", + ); + minify_test('@font-face {src: local("") url("test.woff");}', '@font-face{src:local("") url(test.woff)}'); + minify_test( + "@font-palette-values --Cooler { font-family: Handover Sans; base-palette: 3; override-colors: 1 rgb(43, 12, 9), 3 var(--highlight); }", + "@font-palette-values --Cooler{font-family:Handover Sans;base-palette:3;override-colors:1 #2b0c09, 3 var(--highlight)}", + ); + minify_test( + ".foo { color: color-mix(in srgb, currentColor, blue); }", + ".foo{color:color-mix(in srgb, currentColor, blue)}", + ); + minify_test( + ".foo { color: color-mix(in srgb, blue, accentcolor); }", + ".foo{color:color-mix(in srgb, blue, accentcolor)}", + ); + minify_test( + ".foo { transition: var(--foo, 20px),\nvar(--bar, 40px); }", + ".foo{transition:var(--foo,20px), var(--bar,40px)}", + ); + minify_test(".foo { background: var(--color) var(--image); }", ".foo{background:var(--color) var(--image)}"); + minify_test( + ".foo { height: calc(var(--spectrum-global-dimension-size-300) / 2); }", + ".foo{height:calc(var(--spectrum-global-dimension-size-300) / 2)}", + ); + minify_test( + ".foo { color: var(--color, rgb(var(--red), var(--green), 0)); }", + ".foo{color:var(--color,rgb(var(--red), var(--green), 0))}", + ); + minify_test( + ".foo { background-color: attr(data-color type()); }", + ".foo{background-color:attr(data-color type())}", + ); + exact_test( + ".foo { background-color: attr(data-color type()); }", + ".foo {\n background-color: attr(data-color type());\n}\n", + ); + exact_test(".foo { transform: var(--bar, ) }", ".foo {\n transform: var(--bar, );\n}\n"); + exact_test(".foo { transform: env(--bar, ) }", ".foo {\n transform: env(--bar, );\n}\n"); + + // The space toggle that lowers light-dark() is a value of one blank. + exact_test( + ".foo { color-scheme: light; }", + ".foo {\n --buncss-light: initial;\n --buncss-dark: ;\n color-scheme: light;\n}\n", + { chrome: 90 << 16 }, + ); + minify_test(".foo { color-scheme: light; }", ".foo{--buncss-light:initial;--buncss-dark: ;color-scheme:light}", { + chrome: 90 << 16, + }); + + // A list that the printer copies or rewrites for a target keeps its + // tokens in each copy: a run of blanks, a comment that is the only + // separator, a value on two lines. + // [stylesheet, targets, default output, minified output] + const lowered: [string, Browsers, string, string][] = [ + // light-dark() as a pair of var(). + [ + ".c { color: light-dark(var(--a) var(--b), var(--c)/**/var(--d)); }", + { chrome: 90 << 16 }, + ".c {\n color: var(--buncss-light, var(--a) var(--b)) var(--buncss-dark, var(--c)/**/var(--d));\n}\n", + ".c{color:var(--buncss-light,var(--a) var(--b))var(--buncss-dark,var(--c)/**/var(--d))}", + ], + [ + ".c { --x: light-dark(var(--a)\n var(--b), var(--c)); }", + { chrome: 90 << 16 }, + ".c {\n --x: var(--buncss-light, var(--a)\n var(--b)) var(--buncss-dark, var(--c));\n}\n", + ".c{--x:var(--buncss-light,var(--a) var(--b))var(--buncss-dark,var(--c))}", + ], + // rgb() and hsl() with an alpha that is only known at runtime, in the comma form. + [ + ".c { --x: rgb(1 2 3 / var(--a) var(--b)); --y: hsl(10 20% 30% / var(--a)/**/var(--b)); --z: rgb(1 2 3 / var(--a)\n var(--b)); }", + { chrome: 60 << 16 }, + ".c {\n --x: rgba(1, 2, 3, var(--a) var(--b));\n --y: hsla(10, 20%, 30%, var(--a)/**/var(--b));\n --z: rgba(1, 2, 3, var(--a)\n var(--b));\n}\n", + ".c{--x:rgba(1,2,3,var(--a) var(--b));--y:hsla(10,20%,30%,var(--a)/**/var(--b));--z:rgba(1,2,3,var(--a) var(--b))}", + ], + // The copies for a color that the targets do not all support. + [ + ".c { background: var(--a) lab(40% 56.6 39)/**/var(--b); }", + { chrome: 90 << 16 }, + ".c {\n background: var(--a) #b32323/**/var(--b);\n}\n\n@supports (color: lab(0% 0 0)) {\n .c {\n background: var(--a) lab(40% 56.6 39)/**/var(--b);\n }\n}\n", + ".c{background:var(--a) #b32323/**/var(--b)}@supports (color:lab(0% 0 0)){.c{background:var(--a) lab(40% 56.6 39)/**/var(--b)}}", + ], + [ + ".c { background: var(--a)\n lab(40% 56.6 39); }", + { chrome: 87 << 16, firefox: 78 << 16, safari: 14 << 16, edge: 88 << 16 }, + ".c {\n background: var(--a)\n #b32323;\n}\n\n@supports (color: color(display-p3 0 0 0)) {\n .c {\n background: var(--a)\n color(display-p3 .643308 .192455 .167712);\n }\n}\n\n@supports (color: lab(0% 0 0)) {\n .c {\n background: var(--a)\n lab(40% 56.6 39);\n }\n}\n", + ".c{background:var(--a) #b32323}@supports (color:color(display-p3 0 0 0)){.c{background:var(--a) color(display-p3 .643308 .192455 .167712)}}@supports (color:lab(0% 0 0)){.c{background:var(--a) lab(40% 56.6 39)}}", + ], + // The copies for vendor prefixes. + [ + ".c { transform: var(--a)/**/var(--b) var(--c); }", + { firefox: 6 << 16, safari: 6 << 16 }, + ".c {\n -webkit-transform: var(--a)/**/var(--b) var(--c);\n -moz-transform: var(--a)/**/var(--b) var(--c);\n transform: var(--a)/**/var(--b) var(--c);\n}\n", + ".c{-webkit-transform:var(--a)/**/var(--b) var(--c);-moz-transform:var(--a)/**/var(--b) var(--c);transform:var(--a)/**/var(--b) var(--c)}", + ], + [ + ".c { transform: var(--a)\n var(--b); }", + { firefox: 6 << 16, safari: 6 << 16 }, + ".c {\n -webkit-transform: var(--a)\n var(--b);\n -moz-transform: var(--a)\n var(--b);\n transform: var(--a)\n var(--b);\n}\n", + ".c{-webkit-transform:var(--a) var(--b);-moz-transform:var(--a) var(--b);transform:var(--a) var(--b)}", + ], + ]; + for (const [sheet, browsers, normal, minified] of lowered) { + exact_test(sheet, normal, browsers); + minify_test(sheet, minified, browsers); + } + + // `bun pm diff` compares a print that is not minified and that has one + // form for what no grammar reads in a token list: the amount of + // whitespace, comments, and the blank next to a comma or next to `*` and + // `/` in a math function. A blank anywhere else is kept: two values that + // differ by it are two values. + // [stylesheet, the print that is compared] + const canonical: [string, string][] = [ + [".c { --x: a\n b c; }", ".c {\n --x: a b c;\n}\n"], + [".c { margin: var(--a) auto; }", ".c {\n margin: var(--a) auto;\n}\n"], + [".c { --x: a/* c */b; }", ".c {\n --x: a/**/b;\n}\n"], + [".c { --x: /* c */ a /* d */; }", ".c {\n --x: a;\n}\n"], + [".c { --x: ; }", ".c {\n --x: ;\n}\n"], + [".c { --x:; }", ".c {\n --x: ;\n}\n"], + [".c { --x: /* c */; }", ".c {\n --x: ;\n}\n"], + [".c { --x: var(--a, ); }", ".c {\n --x: var(--a, );\n}\n"], + [".c { --x: var(--a,); }", ".c {\n --x: var(--a, );\n}\n"], + [".c { --x: f( ); }", ".c {\n --x: f();\n}\n"], + [".c { --x: f(); }", ".c {\n --x: f();\n}\n"], + [".c { --f:Inter,system-ui , sans-serif; }", ".c {\n --f: Inter, system-ui, sans-serif;\n}\n"], + [".c { --f: Inter, system-ui, sans-serif; }", ".c {\n --f: Inter, system-ui, sans-serif;\n}\n"], + [".c { --t: rgba(1,2,3,var(--o)); }", ".c {\n --t: rgba(1, 2, 3, var(--o));\n}\n"], + [".c { --x: f(a,); }", ".c {\n --x: f(a,);\n}\n"], + [".c { --c: calc(var(--w)*2/3); }", ".c {\n --c: calc(var(--w) * 2 / 3);\n}\n"], + [".c { --c: calc( var(--w) * 2 / 3 ); }", ".c {\n --c: calc(var(--w) * 2 / 3);\n}\n"], + [".c { --c: min(1px*2,3px/4); }", ".c {\n --c: min(1px * 2, 3px / 4);\n}\n"], + [".c { --r: 16/9; }", ".c {\n --r: 16/9;\n}\n"], + [".c { --r: 16 / 9; }", ".c {\n --r: 16 / 9;\n}\n"], + [".c { --sel: .a .b; }", ".c {\n --sel: .a .b;\n}\n"], + [".c { --sel: .a.b; }", ".c {\n --sel: .a.b;\n}\n"], + [".c { --x: rgb(1 2 3 / *x); }", ".c {\n --x: rgb(1 2 3 / *x);\n}\n"], + [".c { --x: a\\\n b; }", ".c {\n --x: a\\\nb;\n}\n"], + [".c:foo( a b ) { color: red }", ".c:foo(a b) {\n color: red;\n}\n"], + [".c:foo(a,b) { color: red }", ".c:foo(a, b) {\n color: red;\n}\n"], + ["@apply a b , c;", "@apply a b, c;\n"], + ]; + for (const [sheet, print] of canonical) { + canonical_test(sheet, print); + if (print !== sheet) canonical_test(print, print); + } + }); }); describe("pseudo-class edge case", () => { @@ -8322,4 +8698,69 @@ console.log(line.length - line.trimStart().length);`, expect(stdout.trim()).toBe("300"); expect(exitCode).toBe(0); }); + + describe.concurrent("bun build gives back the text of a custom property and of an unknown at-rule", () => { + const sheet = [ + ".c { --sel: .a .b; --all: .dark *; }", + "@custom-variant dark (&:is(.dark *));", + "@apply outline-ring/50;", + "@apply text-[#fff] bg-[#000] font-sans;", + // A value on two lines, in a nested rule of a source that indents by four. + ".a {", + " .b {", + " --m: 1px", + " 2px;", + " }", + "}", + "", + ].join("\n"); + const expected = { + normal: [ + ".c {", + " --sel: .a .b;", + " --all: .dark *;", + "}", + "", + "@custom-variant dark (&:is(.dark *));", + "", + "@apply outline-ring/50;", + "", + "@apply text-[#fff] bg-[#000] font-sans;", + "", + ".a .b {", + " --m: 1px", + " 2px;", + "}", + "", + ].join("\n"), + minified: + ".c{--sel:.a .b;--all:.dark *}@custom-variant dark (&:is(.dark *));@apply outline-ring/50;@apply text-[#fff] bg-[#000] font-sans;.a .b{--m:1px 2px}\n", + }; + // The default output starts with a `/* path */` line that names the source file. + const withoutSourceLine = (css: string) => css.replace(/^\/\*.*\*\/\n/, ""); + + for (const minify of [false, true]) { + test(`bun build${minify ? " --minify" : ""}`, async () => { + using dir = tempDir("css-token-list-cli", { "sheet.css": sheet }); + await using proc = Bun.spawn({ + cmd: [bunExe(), "build", "./sheet.css", "--outdir", "out", ...(minify ? ["--minify"] : [])], + env: bunEnv, + cwd: String(dir), + stdout: "pipe", + stderr: "pipe", + }); + const [, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(withoutSourceLine(await Bun.file(join(String(dir), "out", "sheet.css")).text())).toBe( + minify ? expected.minified : expected.normal, + ); + expect(exitCode).toBe(0); + }); + + test(`Bun.build${minify ? " with minify" : ""}`, async () => { + using dir = tempDir("css-token-list-api", { "sheet.css": sheet }); + const result = await Bun.build({ entrypoints: [join(String(dir), "sheet.css")], minify }); + expect(withoutSourceLine(await result.outputs[0].text())).toBe(minify ? expected.minified : expected.normal); + }); + } + }); }); diff --git a/test/js/bun/css/token-list-backtracking.test.ts b/test/js/bun/css/token-list-backtracking.test.ts index adb71e65870e..6c50a9d919de 100644 --- a/test/js/bun/css/token-list-backtracking.test.ts +++ b/test/js/bun/css/token-list-backtracking.test.ts @@ -116,7 +116,7 @@ test("valid and recovered color function values are unchanged", () => { [".a{--x: light-dark(var(--l), var(--d))}", ".a{--x:light-dark(var(--l),var(--d))}"], [".a{--x: rgb(1 1 1/var(--a))}", ".a{--x:rgb(1 1 1/var(--a))}"], [".a{--x: hsl(120deg 50% 50%/var(--o, 0.5))}", ".a{--x:hsl(120 50% 50%/var(--o,.5))}"], - [".a{--x: rgb(var(--r) 0 0/1)}", ".a{--x:rgb(var(--r)0 0/1)}"], + [".a{--x: rgb(var(--r) 0 0/1)}", ".a{--x:rgb(var(--r) 0 0/1)}"], [".a{--x: f(light-dark(red))}", ".a{--x:f(light-dark(red))}"], [".a{--x: var(--a, light-dark(x y))}", ".a{--x:var(--a,light-dark(x y))}"], [".a{--x: env(safe-area-inset-top, light-dark(a, b))}", ".a{--x:env(safe-area-inset-top,light-dark(a,b))}"], diff --git a/test/js/bun/css/token-list-roundtrip.test.ts b/test/js/bun/css/token-list-roundtrip.test.ts new file mode 100644 index 000000000000..802e4a6749fb --- /dev/null +++ b/test/js/bun/css/token-list-roundtrip.test.ts @@ -0,0 +1,182 @@ +// Seeded round-trip test of CSS token lists. A token list is a value whose grammar the parser +// does not know, so it must print with the tokens it was written with. Replay a failure with +// BUN_CSS_TOKEN_FUZZ_SEED= (and BUN_CSS_TOKEN_FUZZ_ITERS for a soak). +import { cssInternals } from "bun:internal-for-testing"; +import { expect, test } from "bun:test"; + +const { _test, minifyTest, canonicalTest } = cssInternals; + +const DEFAULT_SEED = 0x746f6b73; +const SEED = + process.env.BUN_CSS_TOKEN_FUZZ_SEED !== undefined ? Number(process.env.BUN_CSS_TOKEN_FUZZ_SEED) >>> 0 : DEFAULT_SEED; +const ITERS = (() => { + const n = Number(process.env.BUN_CSS_TOKEN_FUZZ_ITERS) | 0; + return n > 0 ? n : 150; +})(); + +class Rng { + private a: number; + constructor(seed: number) { + this.a = seed >>> 0; + } + next(): number { + this.a = (this.a + 0x6d2b79f5) | 0; + let t = Math.imul(this.a ^ (this.a >>> 15), 1 | this.a); + t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; + return ((t ^ (t >>> 14)) >>> 0) / 4294967296; + } + int(n: number): number { + return Math.floor(this.next() * n); + } + chance(p: number): boolean { + return this.next() < p; + } + pick(arr: readonly T[]): T { + return arr[this.int(arr.length)]; + } +} + +// Text that prints as it is written, whatever is next to it. No numbers, hashes, colors or +// url(): the printer gives those its own spelling. No `!`, `;`, `{` or `}`: they end the value. +const IDENTS = ["a", "b", "foo", "bar-baz", "x1", "_y"] as const; +const DELIMS = [".", "*", ">", "~", "+", "-", "|", "=", "&", "%", "^", "$", "?", "<", "@", "/", ":", ","] as const; +const OTHER = ['"s"', '"a b"', "var(--v)"] as const; +const SPACES = [" ", " ", " ", " ", "\n", "\t", "\n ", "\r\n"] as const; +const COMMENTS = ["/**/", "/* c */", "/* a\n b */"] as const; + +type Part = { text: string; kind: "atom" | "space" | "comment" }; + +function genParts(rng: Rng, depth: number): Part[] { + const parts: Part[] = []; + const atom = (text: string) => { + const last = parts[parts.length - 1]; + if (last?.kind === "atom") { + // `/` then `*` opens a comment, and a name then `(` is a function, whose arguments + // lose the blank at each end. + const opensComment = last.text.endsWith("/") && text.startsWith("*"); + const makesFunction = text === "(" && /[A-Za-z0-9_-]$/.test(last.text); + if (opensComment || makesFunction) parts.push({ text: " ", kind: "space" }); + } + parts.push({ text, kind: "atom" }); + }; + const count = 1 + rng.int(7); + for (let i = 0; i < count; i++) { + if (i > 0) { + // Zero, one or two pieces of whitespace and comments between two atoms. + for (let n = rng.int(3); n > 0; n--) { + if (rng.chance(0.7)) parts.push({ text: rng.pick(SPACES), kind: "space" }); + else parts.push({ text: rng.pick(COMMENTS), kind: "comment" }); + } + } + const roll = rng.int(10); + if (roll < 4) atom(rng.pick(IDENTS)); + else if (roll < 8) atom(rng.pick(DELIMS)); + else if (roll < 9 || depth >= 2) atom(rng.pick(OTHER)); + else { + const [open, close] = rng.pick([ + ["(", ")"], + ["[", "]"], + ] as const); + atom(open); + if (rng.chance(0.3)) parts.push({ text: rng.pick(SPACES), kind: "space" }); + for (const inner of genParts(rng, depth + 1)) { + if (inner.kind === "atom") atom(inner.text); + else parts.push(inner); + } + if (rng.chance(0.3)) parts.push({ text: rng.pick(SPACES), kind: "space" }); + atom(close); + } + } + return parts; +} + +const asWritten = (parts: Part[]) => + parts + .map(p => p.text) + .join("") + .replaceAll("\r\n", "\n"); + +// One separator for each run of whitespace and comments: a blank if the run holds whitespace, +// an empty comment if it holds only comments. `canonical` is the print that `bun pm diff` +// compares: there the blank next to a comma has one form too. +function collapsed(parts: Part[], canonical: boolean): string { + let out = ""; + for (let i = 0; i < parts.length;) { + const part = parts[i]; + if (part.kind === "atom") { + out += part.text; + if (canonical && part.text === "," && i + 1 < parts.length) out += " "; + i++; + continue; + } + let end = i; + let hasSpace = false; + while (parts[end].kind !== "atom") { + hasSpace ||= parts[end].kind === "space"; + end++; + } + const nextToComma = parts[i - 1].text === "," || parts[end].text === ","; + if (!(canonical && nextToComma)) out += hasSpace ? " " : "/**/"; + i = end; + } + return out; +} + +// [name, the tokens before the generated ones, the source and its outputs for a value `v`] +type Frame = (v: string) => [source: string, normal: string, minified: string]; +const frames: [string, Part[], Frame][] = [ + ["a custom property", [], v => [`.c { --x: ${v}; }`, `.c {\n --x: ${v};\n}\n`, `.c{--x:${v}}`]], + ["an unknown property", [], v => [`.c { foo-bar: ${v}; }`, `.c {\n foo-bar: ${v};\n}\n`, `.c{foo-bar:${v}}`]], + [ + "a known property with var()", + [ + { text: "var(--m)", kind: "atom" }, + { text: " ", kind: "space" }, + ], + v => [`.c { margin: ${v}; }`, `.c {\n margin: ${v};\n}\n`, `.c{margin:${v}}`], + ], + ["the prelude of an unknown at-rule", [], v => [`@foo ${v};`, `@foo ${v};\n`, `@foo ${v};`]], + [ + "the arguments of an unknown pseudo-class", + [], + v => [`.c:foo(${v}) { color: red }`, `.c:foo(${v}) {\n color: red;\n}\n`, `.c:foo(${v}){color:red}`], + ], +]; + +for (const [index, [name, before, frame]] of frames.entries()) { + // Runs `check` on each generated value and collects what it reports, up to three. + const failures = (check: (parts: Part[], source: string) => object | undefined) => { + const rng = new Rng(SEED + index); + const found: object[] = []; + for (let iteration = 0; iteration < ITERS && found.length < 3; iteration++) { + const parts = before.concat(genParts(rng, 0)); + const source = frame(parts.map(p => p.text).join(""))[0]; + const failure = check(parts, source); + if (failure) found.push({ seed: SEED, iteration, source, ...failure }); + } + return found; + }; + + test(`${name} prints with the tokens it was written with`, () => { + expect( + failures((parts, source) => { + const expected = { default: frame(asWritten(parts))[1], minified: frame(collapsed(parts, false))[2] }; + const actual = { default: _test(source, ""), minified: minifyTest(source, "") }; + // Each output, printed again in the mode that made it, must not change. + const again = { default: _test(actual.default, ""), minified: minifyTest(actual.minified, "") }; + if (!Bun.deepEquals(actual, expected) || !Bun.deepEquals(again, expected)) return { expected, actual, again }; + }), + ).toEqual([]); + }); + + test(`${name} has one form in the print that bun pm diff compares`, () => { + expect( + failures((parts, source) => { + const expected = frame(collapsed(parts, true))[1]; + const actual = canonicalTest(source, ""); + const again = canonicalTest(actual, ""); + if (actual !== expected || again !== expected) return { expected, actual, again }; + }), + ).toEqual([]); + }); +} diff --git a/test/js/bun/css/util.ts b/test/js/bun/css/util.ts index 69694f0adcef..c7d8036b6dd6 100644 --- a/test/js/bun/css/util.ts +++ b/test/js/bun/css/util.ts @@ -5,6 +5,7 @@ const { minifyTestWithOptions, testWithOptions, _test, + canonicalTest, prefixTestWithOptions, prefixTest, minifyTest, @@ -47,9 +48,10 @@ export function minify_error_test_with_options(source: string, expectedError: st }); } -export function minify_test(source: string, expected: string) { +export function minify_test(source: string, expected: string, browsers?: Browsers) { test(source, () => { - expect(minifyTestWithOptions(source, expected)).toEqual(expected); + const output = browsers ? minifyTest(source, expected, browsers) : minifyTestWithOptions(source, expected); + expect(output).toEqual(expected); }); } @@ -78,6 +80,20 @@ export function cssTestTodo(source: string, expected: string, browsers?: Browser }); } +/** Like `cssTest`, but compares byte for byte. `cssTest` ignores all whitespace, so it cannot see a lost or added blank. */ +export function exact_test(source: string, expected: string, browsers?: Browsers) { + test(source, () => { + expect(_test(source, expected, browsers)).toBe(expected); + }); +} + +/** The print that `bun pm diff` compares: not minified, each token list in its one canonical form. */ +export function canonical_test(source: string, expected: string) { + test(source, () => { + expect(canonicalTest(source, expected)).toBe(expected); + }); +} + export function attrTest(source: string, expected: string, minify: boolean, targets?: Browsers) { return __attrTest(source, expected, minify, targets); } From 2db82e4527b025c661ef6bfa3e7afcc2922ca222 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 16 Oct 2026 21:54:05 +0000 Subject: [PATCH 2/5] css: print a token list without a call for each token `TokenList::write_token` is the body of the print loop. Inline it, print a comma directly, and print a whitespace token of one blank without the scan for line breaks. `bun build` of the 15 sheets in test/js/bun/css/files, user instructions against main: +0.38% before this commit, -0.12% after. With --minify: +0.60% before, +0.24% after. --- src/css/properties/custom.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/css/properties/custom.rs b/src/css/properties/custom.rs index 44e4b239df6c..dd75b1d2e487 100644 --- a/src/css/properties/custom.rs +++ b/src/css/properties/custom.rs @@ -388,6 +388,7 @@ impl TokenList { /// each end of the list does not print: the text next to it is the printer's own, so /// nothing there can join two tokens. `None` if nothing else is left, which is a blank /// value, and this prints it. + #[inline] fn tokens_to_print(&self, dest: &mut Printer) -> PrintResult> { if !dest.collapse_token_whitespace { return Ok(Some(&self.v)); @@ -410,7 +411,8 @@ impl TokenList { /// Prints a token whose text depends on the tokens next to it: whitespace, a comment and /// `\` always, a comma and a math operator in the diff print. Any other token prints as - /// it is. + /// it is. This is the body of the print loop, and a call costs more than most of its arms. + #[inline(always)] fn write_token( tokens: &[TokenOrValue], i: usize, @@ -423,6 +425,8 @@ impl TokenList { Token::Whitespace(_) | Token::Comment(_) if dest.collapse_token_whitespace => { Self::write_collapsed_run(tokens, i, in_math, dest) } + // One blank is nearly all of the whitespace in values, and it holds no line break. + Token::Whitespace([b' ']) => dest.write_char(b' '), Token::Whitespace(text) => write_line_breaks_as_lf(text, dest), Token::Comment(text) => { dest.write_str("/*")?; @@ -441,9 +445,9 @@ impl TokenList { dest.write_char(b'\n') } } - Token::Comma if diff_print => { + Token::Comma => { dest.write_char(b',')?; - if i + 1 < tokens.len() { + if diff_print && i + 1 < tokens.len() { dest.write_char(b' ')?; } Ok(()) From 6ec79fadef31b6f36f51b08f79837f84e5b0ddb7 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Fri, 16 Oct 2026 22:12:43 +0000 Subject: [PATCH 3/5] css: cut the comments on the token list printer to one line each --- src/css/printer.rs | 10 ++------- src/css/properties/custom.rs | 43 ++++++++++-------------------------- 2 files changed, 14 insertions(+), 39 deletions(-) diff --git a/src/css/printer.rs b/src/css/printer.rs index 6e73e38b23ca..64a31e9acf7f 100644 --- a/src/css/printer.rs +++ b/src/css/printer.rs @@ -33,12 +33,7 @@ pub struct PrinterOptions<'a> { /// A mapping of pseudo classes to replace with class names that can be applied /// from JavaScript. Useful for polyfills, for example. pub pseudo_classes: Option>, - /// Whether to print each raw token list (a custom property, an unparsed - /// value, an unknown at-rule) in one form for a diff, while the rest of - /// the output stays formatted. Runs of whitespace and comments collapse - /// as `minify` collapses them, and a blank next to a comma, or next to - /// `*` and `/` in a math function, is not kept. A blank anywhere else - /// stays: it can be a descendant combinator. `bun pm diff` sets it. + /// Print each raw token list in one form, for a diff. `bun pm diff` sets it. pub canonical_token_lists: bool, } @@ -128,8 +123,7 @@ pub struct Printer<'a> { pub(crate) line: u32, pub(crate) col: u32, pub(crate) minify: bool, - /// `minify`, or `PrinterOptions::canonical_token_lists`. The second without the first is - /// the diff print. + /// Set by `minify` and by `canonical_token_lists`. The second alone is the diff print. pub(crate) collapse_token_whitespace: bool, pub(crate) targets: Targets, pub(crate) vendor_prefix: css::VendorPrefix, diff --git a/src/css/properties/custom.rs b/src/css/properties/custom.rs index dd75b1d2e487..c93580563325 100644 --- a/src/css/properties/custom.rs +++ b/src/css/properties/custom.rs @@ -301,17 +301,14 @@ impl TokenList { // deinit(): body only freed owned `Vec` fields — handled by `Drop` on `Vec`. pub fn to_css(&self, dest: &mut Printer, is_custom_property: bool) -> PrintResult<()> { - // Unless minifying, the `:`, `,` or `/` that the caller wrote ends in a blank, and - // that blank is the whole of a whitespace-only value (`--foo: ;`). + // The caller's separator wrote the blank of a whitespace-only value (`--foo: ;`). if !dest.minify && self.v.len() == 1 && self.v[0].is_whitespace() { return Ok(()); } self.write_tokens(dest, is_custom_property, false) } - /// Prints the stored tokens. The grammar of the list is unknown: a blank can be a - /// descendant combinator (`--sel: .a .b`) or a separator for another tool, so the printer - /// adds none. `in_math` is for the arguments of a math function. + /// Prints the tokens as stored. It adds no blank: one can be a combinator (`--sel: .a .b`). fn write_tokens( &self, dest: &mut Printer, @@ -384,18 +381,14 @@ impl TokenList { Ok(()) } - /// The tokens that print. When whitespace and comments are collapsed, the run of them at - /// each end of the list does not print: the text next to it is the printer's own, so - /// nothing there can join two tokens. `None` if nothing else is left, which is a blank - /// value, and this prints it. + /// The tokens to print: collapsing drops the run at each end. `None` if none is left. #[inline] fn tokens_to_print(&self, dest: &mut Printer) -> PrintResult> { if !dest.collapse_token_whitespace { return Ok(Some(&self.v)); } let Some(start) = self.v.iter().position(|t| !t.is_whitespace_or_comment()) else { - // Printed as `/**/`, a comment alone is an empty value, which older browsers - // reject. The diff print has its blank from the separator before the list. + // A blank, not `/**/`: older browsers reject a value that is only a comment. if dest.minify && !self.v.is_empty() { dest.write_char(b' ')?; } @@ -409,9 +402,7 @@ impl TokenList { Ok(Some(&self.v[start..end])) } - /// Prints a token whose text depends on the tokens next to it: whitespace, a comment and - /// `\` always, a comma and a math operator in the diff print. Any other token prints as - /// it is. This is the body of the print loop, and a call costs more than most of its arms. + /// Prints one token. Inlined, because it is the body of the print loop. #[inline(always)] fn write_token( tokens: &[TokenOrValue], @@ -434,8 +425,7 @@ impl TokenList { dest.write_str("*/") } Token::Delim(d) if *d == u32::from(b'\\') => { - // The tokenizer makes this token only where a newline follows it. Before - // any other text the `\` is an escape. + // The tokenizer makes a `\` delim only before a newline, so one has to follow. dest.write_char(b'\\')?; let newline_follows = !dest.collapse_token_whitespace && tokens.get(i + 1).is_some_and(TokenOrValue::is_whitespace); @@ -461,10 +451,7 @@ impl TokenList { } } - /// Prints the one separator of the run of whitespace and comments that `i` is in, at the - /// first token of the run: a blank if the run holds whitespace, else `/**/`. A comment can - /// be all that keeps two tokens apart (`a/**/b` is not `ab`, `/` `/**/` `*` is not `/*`), - /// so it never prints as nothing, which is what lightningcss does. + /// Prints the run at `i` as one blank, or `/**/` for comments only: `a/**/b` is not `ab`. fn write_collapsed_run( tokens: &[TokenOrValue], i: usize, @@ -488,8 +475,7 @@ impl TokenList { if matches!(before, Some(TokenOrValue::Token(Token::Delim(d))) if *d == u32::from(b'\\')) { return Ok(()); } - // No grammar reads a blank next to a comma, or next to `*` and `/` in a math - // function, so the diff print gives these one form. + // The diff print drops a blank next to a comma or a math operator: no grammar reads it. let has_one_form = |t: &TokenOrValue| match t { TokenOrValue::Token(Token::Comma) => true, TokenOrValue::Token(Token::Delim(d)) => in_math && is_math_operator(*d), @@ -601,8 +587,7 @@ impl TokenList { return Err(input.new_custom_error(ParserError::maximum_nesting_depth)); } - // Whitespace and comments are stored like any other token. What a minified list - // can lose is decided when it is printed. + // Every token is stored, whitespace and comments too. Only the printer drops one. loop { let state = input.state(); let Ok(tok) = input.next_including_whitespace_and_comments() else { @@ -845,8 +830,7 @@ fn is_math_operator(delim: u32) -> bool { delim == u32::from(b'*') || delim == u32::from(b'/') } -/// Writes the sign that a number was written with and returns the value to print after it. -/// The sign is part of the token: without it `2n+1` prints as `2n1`. +/// Writes the sign a number was written with (`2n+1` is not `2n1`). Returns the magnitude. fn write_sign(num: &crate::Num, dest: &mut Printer) -> PrintResult { if !num.has_sign { return Ok(num.value); @@ -859,9 +843,7 @@ fn write_sign(num: &crate::Num, dest: &mut Printer) -> PrintResult { Ok(num.value.abs()) } -/// Prints the text of whitespace or of a comment as it was written, except that each line -/// break that CSS reads as one (CR LF, CR, FF) prints as `\n`: the output does not follow the -/// line endings of the checkout. +/// Writes `text` with each CR LF, CR and FF as `\n`, whatever line ends the checkout has. fn write_line_breaks_as_lf(text: &[u8], dest: &mut Printer) -> PrintResult<()> { let mut rest = text; while let Some(i) = strings::index_of_any(rest, b"\r\x0C") { @@ -999,8 +981,7 @@ impl UnresolvedColor { } } - /// Writes the `/` before an unresolved alpha. When minifying, no blank - /// follows it, and a `*` or `*=` directly after it would open a comment. + /// Writes the `/` before the alpha, and a blank if a `*` after it would open a comment. fn write_alpha_slash(alpha: &TokenList, dest: &mut Printer) -> PrintResult<()> { dest.delim(b'/', true)?; // The whitespace and comments at the start of the list do not print. From 63007a6ce9e94b78b9c722f56e5b543c0adec597 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 19 Oct 2026 13:17:50 +0000 Subject: [PATCH 4/5] css: print a var() or env() fallback in a math function with the math rule The diff print drops the blank next to `*` and `/` in the arguments of a math function. A `var()` or `env()` fallback in those arguments printed without that rule, so `calc(var(--x,10px*2))` and `calc(var(--x, 10px * 2))` had two forms in `bun pm diff`. A fallback behind a function with a grammar that is not known keeps its blanks: `calc(foo(var(--w, a*b)))` prints as written. --- src/css/properties/custom.rs | 36 +++++++++++++++++++++++----- test/cli/install/bun-pm-diff.test.ts | 4 ++-- test/js/bun/css/css.test.ts | 13 ++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/css/properties/custom.rs b/src/css/properties/custom.rs index fe06f899c849..df9638e8d570 100644 --- a/src/css/properties/custom.rs +++ b/src/css/properties/custom.rs @@ -302,11 +302,21 @@ impl TokenList { // deinit(): body only freed owned `Vec` fields — handled by `Drop` on `Vec`. pub fn to_css(&self, dest: &mut Printer, is_custom_property: bool) -> PrintResult<()> { + self.write_value(dest, is_custom_property, false) + } + + /// `to_css` for a list that can be in a math function, as a `var()` or `env()` fallback is. + fn write_value( + &self, + dest: &mut Printer, + is_custom_property: bool, + in_math: bool, + ) -> PrintResult<()> { // The caller's separator wrote the blank of a whitespace-only value (`--foo: ;`). if !dest.minify && self.v.len() == 1 && self.v[0].is_whitespace() { return Ok(()); } - self.write_tokens(dest, is_custom_property, false) + self.write_tokens(dest, is_custom_property, in_math) } /// Prints the tokens as stored. It adds no blank: one can be a combinator (`--sel: .a .b`). @@ -340,8 +350,8 @@ impl TokenList { } ext::url_to_css(url, dest)?; } - TokenOrValue::Var(var) => var.to_css(dest, is_custom_property)?, - TokenOrValue::Env(env) => env.to_css(dest, is_custom_property)?, + TokenOrValue::Var(var) => var.to_css(dest, is_custom_property, in_math)?, + TokenOrValue::Env(env) => env.write(dest, is_custom_property, in_math)?, TokenOrValue::Function(f) => f.to_css(dest, is_custom_property)?, TokenOrValue::Length(v) => { // Do not serialize unitless zero lengths in custom properties as it may break calc(). @@ -1110,12 +1120,17 @@ impl Variable { Ok(Variable { name, fallback }) } - fn to_css(&self, dest: &mut Printer, is_custom_property: bool) -> PrintResult<()> { + fn to_css( + &self, + dest: &mut Printer, + is_custom_property: bool, + in_math: bool, + ) -> PrintResult<()> { dest.write_str("var(")?; ext::dashed_ident_ref_to_css(&self.name, dest)?; if let Some(fallback) = &self.fallback { dest.delim(b',', false)?; - fallback.to_css(dest, is_custom_property)?; + fallback.write_value(dest, is_custom_property, in_math)?; } dest.write_char(b')') } @@ -1182,6 +1197,15 @@ impl EnvironmentVariable { } pub(crate) fn to_css(&self, dest: &mut Printer, is_custom_property: bool) -> PrintResult<()> { + self.write(dest, is_custom_property, false) + } + + fn write( + &self, + dest: &mut Printer, + is_custom_property: bool, + in_math: bool, + ) -> PrintResult<()> { dest.write_str("env(")?; self.name.to_css(dest)?; @@ -1192,7 +1216,7 @@ impl EnvironmentVariable { if let Some(fallback) = &self.fallback { dest.delim(b',', false)?; - fallback.to_css(dest, is_custom_property)?; + fallback.write_value(dest, is_custom_property, in_math)?; } dest.write_char(b')') diff --git a/test/cli/install/bun-pm-diff.test.ts b/test/cli/install/bun-pm-diff.test.ts index 71ac2616dc21..0c27121f32eb 100644 --- a/test/cli/install/bun-pm-diff.test.ts +++ b/test/cli/install/bun-pm-diff.test.ts @@ -826,8 +826,8 @@ describe.concurrent("bun pm diff (canonical re-print)", () => { // Values that the CSS parser keeps as tokens, as a minifier prints them and as `bun fmt` prints them, with // one value wrapped and commented by hand. The blank next to a comma and next to `*` and `/` in calc(), // the amount of whitespace, and a comment are formatting. - "a/tokens.css": `.a{--shadow:0 0 1px red,0 0 2px blue;font-family:var(--f),sans-serif;color:rgba(1,2,3,var(--o));width:calc(var(--a)*2/3);margin:var(--x) auto}@apply a b;`, - "b/tokens.css": `.a {\n --shadow: 0 0 1px red, /* second layer */\n 0 0 2px blue;\n font-family: var(--f), sans-serif;\n color: rgba(1, 2, 3, var(--o));\n width: calc(var(--a) * 2 / 3);\n margin: var(--x) auto /* centered */;\n}\n@apply a b;\n`, + "a/tokens.css": `.a{--shadow:0 0 1px red,0 0 2px blue;font-family:var(--f),sans-serif;color:rgba(1,2,3,var(--o));width:calc(var(--a)*2/3);height:calc(var(--h,10px*2));margin:var(--x) auto}@apply a b;`, + "b/tokens.css": `.a {\n --shadow: 0 0 1px red, /* second layer */\n 0 0 2px blue;\n font-family: var(--f), sans-serif;\n color: rgba(1, 2, 3, var(--o));\n width: calc(var(--a) * 2 / 3);\n height: calc(var(--h, 10px * 2));\n margin: var(--x) auto /* centered */;\n}\n@apply a b;\n`, }); expect(text).toMatch(/\nindex\.js ─+ formatting only\n/); expect(text).toMatch(/\npackage\.json ─+ formatting only\n/); diff --git a/test/js/bun/css/css.test.ts b/test/js/bun/css/css.test.ts index 46f0b1c731a1..74e800ebaa83 100644 --- a/test/js/bun/css/css.test.ts +++ b/test/js/bun/css/css.test.ts @@ -493,6 +493,19 @@ describe("css tests", () => { [".c { --c: calc(var(--w)*2/3); }", ".c {\n --c: calc(var(--w) * 2 / 3);\n}\n"], [".c { --c: calc( var(--w) * 2 / 3 ); }", ".c {\n --c: calc(var(--w) * 2 / 3);\n}\n"], [".c { --c: min(1px*2,3px/4); }", ".c {\n --c: min(1px * 2, 3px / 4);\n}\n"], + // A fallback in a math function is a part of its arguments. + [".c { --c: calc(var(--w,10px*2)); }", ".c {\n --c: calc(var(--w, 10px * 2));\n}\n"], + [ + ".c { --c: calc(1px + var(--w, var(--v,10px/2))); }", + ".c {\n --c: calc(1px + var(--w, var(--v, 10px / 2)));\n}\n", + ], + [ + ".c { --c: max(env(safe-area-inset-top,1px*2),3px); }", + ".c {\n --c: max(env(safe-area-inset-top, 1px * 2), 3px);\n}\n", + ], + // Not in a math function, or behind a function with a grammar that is not known: kept. + [".c { --c: var(--w, 10px*2); }", ".c {\n --c: var(--w, 10px*2);\n}\n"], + [".c { --c: calc(foo(var(--w, a*b))); }", ".c {\n --c: calc(foo(var(--w, a*b)));\n}\n"], [".c { --r: 16/9; }", ".c {\n --r: 16/9;\n}\n"], [".c { --r: 16 / 9; }", ".c {\n --r: 16 / 9;\n}\n"], [".c { --sel: .a .b; }", ".c {\n --sel: .a .b;\n}\n"], From 9af91ad41096775946027013abc2d6ea0a000a30 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 19 Oct 2026 13:31:19 +0000 Subject: [PATCH 5/5] css: compare whitespace tokens without their text On main the parser stored each run of whitespace as one blank, so two rules whose values differ only in the amount of whitespace merged. With the text kept as written they did not merge, although `--minify` prints the same bytes for both. Two whitespace tokens are now equal, and hash the same, whatever their text. The merged rule prints the whitespace of the rule that stays. --- src/css/properties/custom.rs | 8 +++++--- test/js/bun/css/css.test.ts | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/css/properties/custom.rs b/src/css/properties/custom.rs index df9638e8d570..156316236d13 100644 --- a/src/css/properties/custom.rs +++ b/src/css/properties/custom.rs @@ -214,8 +214,9 @@ impl CssEql for Token { | (BadString(a), BadString(b)) | (UnquotedUrl(a), UnquotedUrl(b)) | (BadUrl(a), BadUrl(b)) - | (Whitespace(a), Whitespace(b)) | (Comment(a), Comment(b)) => strings::eql(a, b), + // The amount of whitespace is formatting, so two rules that differ by it can merge. + (Whitespace(_), Whitespace(_)) => true, (Delim(a), Delim(b)) => a == b, (Number(a), Number(b)) => a.eql(b), (Dimension(a), Dimension(b)) => a.eql(b), @@ -258,8 +259,9 @@ impl CssHash for Token { hasher.update(&(self.kind() as u32).to_ne_bytes()); match self { Ident(v) | Function(v) | AtKeyword(v) | UnrestrictedHash(v) | IdHash(v) - | QuotedString(v) | BadString(v) | UnquotedUrl(v) | BadUrl(v) | Whitespace(v) - | Comment(v) => hasher.update(v), + | QuotedString(v) | BadString(v) | UnquotedUrl(v) | BadUrl(v) | Comment(v) => { + hasher.update(v) + } Delim(d) => hasher.update(&d.to_ne_bytes()), Number(n) => n.hash(hasher), Dimension(d) => d.hash(hasher), diff --git a/test/js/bun/css/css.test.ts b/test/js/bun/css/css.test.ts index 74e800ebaa83..0ac847a7ca32 100644 --- a/test/js/bun/css/css.test.ts +++ b/test/js/bun/css/css.test.ts @@ -331,6 +331,21 @@ describe("css tests", () => { ".c {\n --sel: .a .b;\n}\n\n.d {\n --sel: .a.b;\n}\n", ".c{--sel:.a .b}.d{--sel:.a.b}", ], + // Two values that differ only in the amount of whitespace are one value. The rules + // merge, and the merged rule keeps the whitespace of the first. + [".c { --x: a b } .d { --x: a b }", ".c, .d {\n --x: a b;\n}\n", ".c,.d{--x:a b}"], + [ + ".c { margin: var(--m)\n auto } .d { margin: var(--m) auto }", + ".c, .d {\n margin: var(--m)\n auto;\n}\n", + ".c,.d{margin:var(--m) auto}", + ], + // The same holds for the arguments of an unknown pseudo-class: a later rule with the + // same selector replaces the earlier one. + [ + ".c:foo(a b) { color: red } .k { color: green } .c:foo(a b) { color: blue }", + ".k {\n color: green;\n}\n\n.c:foo(a b) {\n color: #00f;\n}\n", + ".k{color:green}.c:foo(a b){color:#00f}", + ], // The lines of a value are the author's. A value that runs over more // than one line keeps the indentation of its source, not of the output. [