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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
/deno.lock
/.repl_history
/ROADMAP.draft.md
/SYNTAX-CHANGELOG.md
/src/data/validation.origins.json
/.idea
/.DS_Store
/ROADMAP.md
Expand Down
46 changes: 45 additions & 1 deletion CHANGELOG.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,49 @@
# Changelog

# v2.1.0

## Bugs

- [x] do not strip unit from dash ident #180
- [x] deduplicate important rule #180
- [x] incorrectly parsing keyframe rule 0% as literal token #180

## Feature

- [x] now accepting arraybuffer as input data: transformSync(arraybuffer);
- [x] renamed 'word-wrap' to 'overflow-wrap', 'grid-gap' to 'gap', 'grid-row-gap' to 'row-gap', 'grid-column-gap' to 'column-gap', 'font-stretch' to 'font-width'.

## Validation
- [x] switching syntax definitions source from mdn-data to webref/css
- [x] the validation syntaxes are generated from the CSS definitions of [W3C webref](https://github.com/w3c/webref) instead of mdn-data (deprecated): `npm run syntax-update` runs `scripts/webref.ts`, `scripts/validation.ts` is removed
- [x] the units are listed in `scripts/local-patch.json`: `@webref/css` does not list them
- [x] a function or a type defined differently for different scopes keeps its definitions: the scoped definition is inlined in the syntax of its scope (`clip: rect( <top>, <right>, <bottom>, <left> ) | auto`, the `<basic-shape>` `rect()` is the shared definition)
- [x] a group is no longer matched when the value ends before its mandatory parts: `cubic-bezier(.4,0,.2)`, `[ <a> && <b> ]` with `<a>` only
- [x] the range of a numeric type is checked when it is the last token: `grid-row: 0`, `width: -10px`
- [x] `<x>#? , <y>` (`background`) leaves the last item to `<y>`, a list item must match all the tokens before the next comma, `var()` may be a part of a syntax (`var(--color) 65%` in a gradient)
- [x] the longhands that follow a shorthand that contains `var()` are not merged into it: `border: var(--b); border-color: red`
- [x] a value is invalid when the tokens are not all consumed by the syntax: `color: red blue`, `width: 10px 20px`
- [x] `:` is parsed as a colon (`ValidationTokenEnum.Colon`, matches `EnumToken.ColonTokenType`) in the validation syntaxes: `<ident-token> : <declaration-value>?`, `name: <value>`
- [x] a media feature length may be the number `0`: `@when media(width > 0)`
- [x] the `||` syntaxes are matched in the order that matches the most tokens: `transition: .2s ease-in-out transform`
- [x] the `&&` syntaxes are matched in any order, the optional syntaxes may be omitted: `[ <ident> && <length> ]` matches `thin 10px` and `10px thin`, `box-shadow: 0 0 red`
- [x] new length units: `rex`, `rcap`, `rch`, `ric`, `lvmin`, `svmax`. the unit functions (`isLengthUnit()`, `isFrequencyUnit()`, ...) are case-insensitive (`Q`, `Hz`, `kHz`)
- [x] `length2Px()` converts `in` (96px) and `pc` (16px) correctly, the units are case-insensitive
- [x] a number followed by `!important` is no longer dropped: `margin: 8px 0 0!important`
- [x] the `!important` longhands are not merged into a shorthand (the minifier threw an error): `column-gap: 16px !important; row-gap: 40px !important`
- [x] new token type `EnumToken.UniCodeRange`: `U+0025-00FF`, `U+4??` are read as a single token and rendered as they are. `u+a` is a selector in a rule prelude
- [x] `unicode-range` is validated (the validation threw an error)
- [x] the units are rendered in their canonical spelling: `20hz` is rendered `20Hz`, `2q` `2Q`, `6DB` `6dB`
- [x] the numbers with an exponent are parsed: `1.5e2!important`, `1e-3`, `1e+3`, `1.5e2px`
- [x] `<resolution>`, `<frequency>`, `<decibel>` and `<semitones>` are validated (the validation threw an error): `image-resolution`, `image-set()`, `voice-pitch`, `voice-volume`
- [x] the line endings are LF
- [x] `npm run syntax-update` prints a summary of the changes between the previous and the new syntax file (added, removed and changed declarations, types and functions, selectors, at-rules, descriptors, media features, units and languages) and appends it to `SYNTAX-CHANGES.md`, ignored by git
- [x] `npm run syntax-update` reports the removed definitions grouped by origin, with the url of the file that provided them: legacy mdn-data, mdn-data patch (csstree), `local-patch.json` or removed from webref. the origins are saved in `src/data/validation.origins.json`
- [x] the syntaxes are generated from the consolidated css definitions of `@webref/css`, pulled from a versioned url (`https://cdn.jsdelivr.net/npm/@webref/css@8.7.8/css.json`, updated manually: a new version may introduce breaking changes): the definitions of the latest level of a spec are used, the extensions and the descriptors defined by other specs are merged
- [x] the renamed properties (the legacy name aliases that are not vendor prefixed) are removed from the syntax file and reported as renamed, the parser renames them: `font-stretch` -> `font-width`, `grid-gap` -> `gap`, `grid-row-gap` -> `row-gap`, `grid-column-gap` -> `column-gap`, `word-wrap` -> `overflow-wrap`. `renamedStandardProperties` is updated manually: the report flags the renamed properties it does not contain
- [x] the at-rule descriptors defined by another spec are merged: `@font-face/size-adjust`
- [x] the value types are stored without their angle brackets (`types.color` for `<color>`)

# v2.0.1

rename file due to firefox blocking it
Expand Down Expand Up @@ -34,7 +78,7 @@ see the [migration guide](./files/migration.md)
- [x] removed `getNodeProperty()` and `setNodeProperty()`: use the arena methods (`arena.getState()`, `arena.setState()`, `arena.getErrors()`, ...) or the object view properties (`arena.toObject(id).state = ...`)
- [x] `WalkerEvent` and `WalkerOptionEnum` are declared with the other enums (they are still exported by the package)

performance
## performance

- [x] the parser recycles the tokens that are not attached to the tree (whitespace, `:`, `;`, `{`, `}`, ...): the arena allocates about half the nodes. `arena.recycle(id)` reuses the id of a detached leaf node
- [x] the initial arena capacity is one node for every 4 bytes of css
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ npm run config-update
npm run syntax-update
```

`config-update` regenerates `src/data/properties.json`. `syntax-update` regenerates `src/data/validation.json` from upstream CSS data and therefore requires an internet connection. Review the generated changes and include them when they are relevant to your contribution.
`config-update` regenerates `src/data/properties.json`. `syntax-update` regenerates `src/data/validation.json` from the consolidated [W3C webref](https://github.com/w3c/webref) CSS definitions (`https://cdn.jsdelivr.net/npm/@webref/css@8.7.8/css.json`, `@webref/css` is not a dependency: update the version in `scripts/webref.ts` to use a newer version. mdn-data and the csstree patch are also pulled from versioned urls) and therefore requires an internet connection. It prints a summary of the syntax changes and appends it to `SYNTAX-CHANGES.md` (ignored by git): port the relevant changes to `CHANGELOG.md`. The removed definitions are grouped by origin (legacy mdn-data, mdn-data patch, `scripts/local-patch.json` or removed from webref), using the origins saved in `src/data/validation.origins.json`: commit it with `src/data/validation.json`. The properties and the selectors that webref does not define keep their mdn-data definition, with the types they reference (the non standard properties `box-orient`, `font-smooth`, `-ms-user-select`, `-webkit-tap-highlight-color`, the pseudo elements `::-webkit-slider-thumb`): the results are the same as with the syntax files generated from mdn-data. The other definitions that webref does not provide (prose syntaxes, units) are added to `scripts/local-patch.json`. The renamed properties (the legacy name aliases of webref that are not vendor prefixed) are reported as renamed: add them to `renamedStandardProperties` (`src/lib/syntax/syntax.ts`). The vendor prefixed legacy name aliases are stored as aliases of the standard property, with no other key (`"-webkit-align-content": { "alias": "align-content" }`): they are validated with its syntax and they are not renamed. When mdn-data, the csstree patch or `scripts/local-patch.json` provide a legacy syntax for a vendor prefixed alias, the syntax of the standard property is extended with it (`-webkit-mask-clip: <'mask-clip'> | [ <coord-box> | no-clip | border | padding | content | text ]#`): add the legacy syntax of a vendor prefixed alias to `scripts/local-patch.json`, the script adds the syntax of the standard property. The sources are applied in this order: mdn-data, the csstree patch, `scripts/local-patch.json`. A patch replaces the legacy syntax of the previous sources, or extends it when its syntax starts with `|`. Review the generated changes and include them when they are relevant to your contribution.

## Building

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ $ deno add @tbela99/css-parser
* **CSS variable inlining** where values can be safely resolved.
* **Duplicate declaration removal** to eliminate redundant rules.
* **`@import` flattening** to produce self-contained stylesheets.
* **Binary input** — the css can be a string, an `ArrayBuffer` or an `ArrayBufferView` (utf-8 bytes, `Uint8Array`, `Buffer`): an ASCII buffer is tokenized without copy.
* **51.5kb** once minified and gzipped.

## Vendor prefix removal
Expand Down Expand Up @@ -113,7 +114,7 @@ Performance vs execution speed comparison
| File: tailwind.css | Speed optimization | Minification optimization |
| --------------------- | ------------------ | ------------------------- |
| Size: 2,380,419 bytes | 1,890,835 bytes | 1,633,188 bytes |
| Time | 128.67 ms | 268.86 ms |
| Time | 116.61 ms | 224.18 ms |
| Size reduction | -20.6% | -31.4% |

## Playground
Expand Down
6 changes: 3 additions & 3 deletions benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
"all": "npm run sizes && npm run bench; npm run report"
},
"dependencies": {
"@tbela99/css-parser": "github:tbela99/css-parser#007a292",
"@tbela99/css-parser2": "github:tbela99/css-parser#007a292",
"@tbela99/css-parser": "github:tbela99/css-parser#v2.0.1",
"@tbela99/css-parser2": "github:tbela99/css-parser#d237344",
"clean-css": "^5.3.3",
"css-tree": "^3.2.1",
"cssnano": "^9.9.0",
"csso": "^5.0.5",
"esbuild": "^0.28.2",
"lightningcss": "^1.33.0",
"postcss": "^8.5.32",
"postcss": "^8.5.33",
"vitest": "^5.1.0",
"yuku-css": "^0.2.0"
}
Expand Down
71 changes: 48 additions & 23 deletions benchmark/src/minifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,24 @@ const cssnanoProcessor = postcss([cssnano]);
* official table's "name - version" formatting.
*/
export const minifiers = [
{
id: "clean-css",
url: versions["clean-css"].url,
label: `clean-css - ${versions["clean-css"].version}`,
minify: (css) => cleanCssInstance.minify(css).styles,
},
{
id: "cssnano",
url: versions.cssnano.url,
label: `cssnano - ${versions.cssnano.version}`,
minify: async (css) => (await cssnanoProcessor.process(css, { from: undefined })).css,
},
{
id: "csso",
url: versions.csso.url,
label: `csso - ${versions.csso.version}`,
minify: (css) => csso.minify(css).css,
},
// {
// id: "clean-css",
// url: versions["clean-css"].url,
// label: `clean-css - ${versions["clean-css"].version}`,
// minify: (css) => cleanCssInstance.minify(css).styles,
// },
// {
// id: "cssnano",
// url: versions.cssnano.url,
// label: `cssnano - ${versions.cssnano.version}`,
// minify: async (css) => (await cssnanoProcessor.process(css, { from: undefined })).css,
// },
// {
// id: "csso",
// url: versions.csso.url,
// label: `csso - ${versions.csso.version}`,
// minify: (css) => csso.minify(css).css,
// },
// infinite loop bug
{
id: "css-tree",
Expand Down Expand Up @@ -127,9 +127,18 @@ export const minifiers = [
minify: (css) => yukuTransform(css).code,
},
{
id: "css-parser",
id: "css-parser-default",
url: versions["css-parser"].url,
label: `@tbela99/css-parser - ${versions["css-parser"].version}`,
minify: async (css) =>
tbelaTransform({
input: css,
}).code,
},
{
id: "css-parser-speed",
url: versions["css-parser"].url,
label: `@tbela99/css-parser (speed) - ${versions["css-parser"].version}`,
label: `@tbela99/css-parser (fast) - ${versions["css-parser"].version}`,
title: `settings: { minify: false, beautify: false, removeEmpty: true, removeComments: true, convertColor: true }`,
minify: async (css) =>
tbelaTransform({
Expand All @@ -145,7 +154,23 @@ export const minifiers = [
{
id: "css-parser-dev",
url: versions["css-parser-dev"].url,
label: `@tbela99/css-parser-dev (default) - ${versions["css-parser-dev"].version}`,
minify: async (css) => tbelaDevTransform({ input: css }).code,
}
label: `@tbela99/css-parser-dev - ${versions["css-parser-dev"].version}`,
minify: async (css) => tbelaDevTransform({ input: Buffer.from(css) }).code,
},
{
id: "css-parser-dev-speed",
url: versions["css-parser-dev"].url,
label: `@tbela99/css-parser-dev (fast) - ${versions["css-parser-dev"].version}`,
title: `settings: { minify: false, beautify: false, removeEmpty: true, removeComments: true, convertColor: true }`,
minify: async (css) =>
tbelaDevTransform({
input: Buffer.from(css),
minify: false,
beautify: false,
removeEmpty: true,
removeComments: true,
convertColor: true,
// minifyValues: true,
}).code,
},
];
Loading
Loading