Skip to content

Commit

Permalink
feat: add more CSS-wide keywords in font-family
Browse files Browse the repository at this point in the history
  • Loading branch information
yisibl committed Mar 28, 2022
1 parent 2244c09 commit 5e343f2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
35 changes: 26 additions & 9 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,23 @@

If the esbuild binary executable is corrupted or missing, previously there was one situation where esbuild's JavaScript API could hang instead of generating an error. This release changes esbuild's library code to generate an error instead in this case.

* Fix when CSS-wide keywords(`initial`, `inherit`, `unset`, `revert`, `revert-layer`, `default`) is present in the `font-family`, must keep the quotation marks, otherwise it will be invalid.

```css
/* Original code */
.foo { font-family: 'revert'; }
a { font-family: 'revert-layer', 'Segoe UI', serif; }

/* Old output (with --minify) */
/* invalid rules */
.foo{font-family:revert}
a{font-family:revert-layer,Segoe UI,serif}

/* New output (with --minify) */
.foo{font-family:"revert"}
a{font-family:"revert-layer",Segoe UI,serif}
```

## 0.14.28

* Add support for some new CSS rules ([#2115](https://github.com/evanw/esbuild/issues/2115), [#2116](https://github.com/evanw/esbuild/issues/2116), [#2117](https://github.com/evanw/esbuild/issues/2117))
Expand Down Expand Up @@ -3586,15 +3603,15 @@ In addition to the breaking changes above, the following features are also inclu

* Minify the syntax `Infinity` to `1 / 0` ([#1385](https://github.com/evanw/esbuild/pull/1385))

The `--minify-syntax` flag (automatically enabled by `--minify`) will now minify the expression `Infinity` to `1 / 0`, which uses fewer bytes:

```js
// Original code
const a = Infinity;

// Output with "--minify-syntax"
const a = 1 / 0;
```
The `--minify-syntax` flag (automatically enabled by `--minify`) will now minify the expression `Infinity` to `1 / 0`, which uses fewer bytes:
```js
// Original code
const a = Infinity;
// Output with "--minify-syntax"
const a = 1 / 0;
```

This change was contributed by [@Gusted](https://github.com/Gusted).

Expand Down
25 changes: 16 additions & 9 deletions internal/css_parser/css_decls_font_family.go
Expand Up @@ -7,13 +7,23 @@ import (
"github.com/evanw/esbuild/internal/css_lexer"
)

// Specification: https://drafts.csswg.org/css-values-4/#common-keywords
var wideKeywords = map[string]bool{
"initial": true,
"inherit": true,
"unset": true,
// CSS-wide keywords and reserved keyword
// These keywords usually require special handling when parsing.
// The latest definition is in CSS Cascading and Inheritance Level 5:
// https://drafts.csswg.org/css-cascade-5/#defaulting-keywords
// Old Specification: https://drafts.csswg.org/css-values-4/#common-keywords
var CSSWideAndReservedKeywords = map[string]bool{
"initial": true,
"inherit": true,
"unset": true,
"revert": true,
"revert-layer": true,
"default": true, // CSS reserved keyword
}

// Font family names that happen to be the same as a keyword value
// must be quoted to prevent confusion with the keywords with the same names.
// UAs must not consider these keywords as matching the <family-name> type.
// Specification: https://drafts.csswg.org/css-fonts/#generic-font-families
var genericFamilyNames = map[string]bool{
"serif": true,
Expand Down Expand Up @@ -118,10 +128,7 @@ func isValidCustomIdent(text string, predefinedKeywords map[string]bool) bool {
if predefinedKeywords[loweredText] {
return false
}
if wideKeywords[loweredText] {
return false
}
if loweredText == "default" {
if CSSWideAndReservedKeywords[loweredText] {
return false
}
if loweredText == "" {
Expand Down
6 changes: 6 additions & 0 deletions internal/css_parser/css_parser_test.go
Expand Up @@ -1788,6 +1788,12 @@ func TestFontFamily(t *testing.T) {

expectPrintedMangleMinify(t, "a {font-family: 'aaa bbb', serif }", "a{font-family:aaa bbb,serif}")
expectPrintedMangleMinify(t, "a {font-family: 'aaa bbb', 'ccc ddd' }", "a{font-family:aaa bbb,ccc ddd}")
expectPrintedMangleMinify(t, "a {font-family: 'initial', serif;}", "a{font-family:\"initial\",serif}")
expectPrintedMangleMinify(t, "a {font-family: 'inherit', serif;}", "a{font-family:\"inherit\",serif}")
expectPrintedMangleMinify(t, "a {font-family: 'unset', serif;}", "a{font-family:\"unset\",serif}")
expectPrintedMangleMinify(t, "a {font-family: 'revert', serif;}", "a{font-family:\"revert\",serif}")
expectPrintedMangleMinify(t, "a {font-family: 'revert-layer', 'Segoe UI', serif;}", "a{font-family:\"revert-layer\",Segoe UI,serif}")
expectPrintedMangleMinify(t, "a {font-family: 'default', serif;}", "a{font-family:\"default\",serif}")
}

func TestFont(t *testing.T) {
Expand Down

0 comments on commit 5e343f2

Please sign in to comment.