From a579bd80c24de9dd2ebce352e378b0f798bb8ba3 Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Mon, 16 Oct 2023 18:48:48 -0400 Subject: [PATCH] fix #3452: insert space in `font` when minifying --- CHANGELOG.md | 17 +++++++++++++++++ internal/css_parser/css_decls_font.go | 3 +++ internal/css_parser/css_parser_test.go | 8 ++++++++ 3 files changed, 28 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c2ad8117f4..b3840779938 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,27 @@ ## Unreleased +* Fix a CSS `font` property minification bug ([#3452](https://github.com/evanw/esbuild/issues/3452)) + + This release fixes a bug where esbuild's CSS minifier didn't insert a space between the font size and the font family in the `font` CSS shorthand property in the edge case where the original source code didn't already have a space and the leading string token was shortened to an identifier: + + ```css + /* Original code */ + .foo { font: 16px"Menlo"; } + + /* Old output (with --minify) */ + .foo{font:16pxMenlo} + + /* New output (with --minify) */ + .foo{font:16px Menlo} + ``` + * Update to Unicode 15.1.0 The character tables that determine which characters form valid JavaScript identifiers have been updated from Unicode version 15.0.0 to the newly-released Unicode version 15.1.0. I'm not putting an example in the release notes because all of the new characters will likely just show up as little squares since fonts haven't been updated yet. But you can read https://www.unicode.org/versions/Unicode15.1.0/#Summary for more information about the changes. + This upgrade was contributed by [@JLHwung](https://github.com/JLHwung). + ## 0.19.4 * Fix printing of JavaScript decorators in tricky cases ([#3396](https://github.com/evanw/esbuild/issues/3396)) diff --git a/internal/css_parser/css_decls_font.go b/internal/css_parser/css_decls_font.go index 5256bb96ea7..c1644b428a5 100644 --- a/internal/css_parser/css_decls_font.go +++ b/internal/css_parser/css_decls_font.go @@ -97,6 +97,9 @@ func (p *parser) mangleFont(tokens []css_ast.Token) []css_ast.Token { // if family, ok := p.mangleFontFamily(tokens[pos:]); ok { + if len(result) > 0 && len(family) > 0 && family[0].Kind != css_lexer.TString { + family[0].Whitespace |= css_ast.WhitespaceBefore + } return append(result, family...) } return tokens diff --git a/internal/css_parser/css_parser_test.go b/internal/css_parser/css_parser_test.go index 546d1b4318c..bdfb631e12d 100644 --- a/internal/css_parser/css_parser_test.go +++ b/internal/css_parser/css_parser_test.go @@ -2215,6 +2215,14 @@ func TestFont(t *testing.T) { expectPrintedMangleMinify(t, "a { font: italic small-caps bold ultra-condensed 1rem/1.2 'aaa bbb' }", "a{font:italic small-caps 700 ultra-condensed 1rem/1.2 aaa bbb}", "") expectPrintedMangleMinify(t, "a { font: italic small-caps bold ultra-condensed 1rem / 1.2 'aaa bbb' }", "a{font:italic small-caps 700 ultra-condensed 1rem/1.2 aaa bbb}", "") + + // See: https://github.com/evanw/esbuild/issues/3452 + expectPrinted(t, "a { font: 10px'foo' }", "a {\n font: 10px\"foo\";\n}\n", "") + expectPrinted(t, "a { font: 10px'123' }", "a {\n font: 10px\"123\";\n}\n", "") + expectPrintedMangle(t, "a { font: 10px'foo' }", "a {\n font: 10px foo;\n}\n", "") + expectPrintedMangle(t, "a { font: 10px'123' }", "a {\n font: 10px\"123\";\n}\n", "") + expectPrintedMangleMinify(t, "a { font: 10px'foo' }", "a{font:10px foo}", "") + expectPrintedMangleMinify(t, "a { font: 10px'123' }", "a{font:10px\"123\"}", "") } func TestWarningUnexpectedCloseBrace(t *testing.T) {