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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/ROADMAP.draft.md
/.idea
/.DS_Store
/benchmark
/ROADMAP.md
/package-lock.json
test/*.ts
Expand Down
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
# Changelog

# v1.6.3

## Fix
- [x] fix infinity rendering as 1/0 instead of 0/0 (NaN)

```css

@media (min-width: 300px) and (max-width: 768px){
.s {

margin-left: calc(infinity + 1 + calc(infinity - 1 + calc(infinity / 1 + calc(infinity * 1))));
margin-right: calc(infinity + 1px + calc(infinity - 1 + calc(infinity / 1 + calc(infinity * 1))));

}

```
is parsed as

```css
@media (300px<=width<=768px) {
.s {
margin-left: calc(1/0);
margin-right: calc(1/0 + 1px)
}
}
```

# v1.6.2

## Fix
Expand Down
20 changes: 10 additions & 10 deletions benchmark/bench/minify.bench.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { bench, describe } from "vitest";
import { test } from "vitest";
import { minifiers } from "../src/minifiers.js";
import { fixtures } from "../src/fixtures.js";

Expand All @@ -8,15 +8,15 @@ import { fixtures } from "../src/fixtures.js";
const BENCH_OPTIONS = { time: 300, iterations: 5 };

for (const fixture of fixtures) {
describe(fixture.name, () => {
test(fixture.name, async ({ bench }) => {
for (const minifier of minifiers) {
bench(
minifier.id,
async () => {
await minifier.minify(fixture.css);
},
BENCH_OPTIONS,
);
console.error(`benchmarking ${fixture.name} with ${minifier.id}`);

try {
await bench(minifier.id, async () => {
return minifier.minify(fixture.css);
}).run(BENCH_OPTIONS);
} catch (err) {}
}
});
}
}
12 changes: 6 additions & 6 deletions benchmark/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
"private": true,
"scripts": {
"sizes": "node src/compute-sizes.js",
"bench": "vitest bench --run --outputJson=results/bench-results.json",
"bench": "vitest bench --run --reporter=json --outputFile=results/bench-results.json",
"report": "node src/generate-report.js",
"all": "npm run sizes && npm run bench && npm run report"
"all": "npm run sizes && npm run bench; npm run report"
},
"dependencies": {
"@tbela99/css-parser": "^1.6.1",
"@tbela99/css-parser2": "github:tbela99/css-parser#7862cfa",
"@tbela99/css-parser": "^1.6.2",
"@tbela99/css-parser2": "github:tbela99/css-parser#4303f4b",
"clean-css": "^5.3.3",
"css-tree": "^3.2.1",
"cssnano": "^9.0.3",
"cssnano": "^9.0.4",
"csso": "^5.0.5",
"esbuild": "^0.28.2",
"lightningcss": "^1.33.0",
"postcss": "^8.5.28",
"vitest": "^4.1.11"
"vitest": "^5.0.0"
}
}
29 changes: 19 additions & 10 deletions benchmark/src/generate-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ const sizes = JSON.parse(readFileSync(resultsDir + "sizes.json", "utf8"));

// benchRaw.files[].groups[].benchmarks[] -> { [fixtureName]: { [minifierId]: meanMs } }
const timings = {};
for (const file of benchRaw.files) {
for (const group of file.groups) {
// group.fullName looks like "bench/minify.bench.js > <fixtureName>"
const fixtureName = group.fullName.split(">").pop().trim();
timings[fixtureName] ??= {};
for (const b of group.benchmarks) {
timings[fixtureName][b.name] = b.mean; // milliseconds
for (const assertionResult of benchRaw.testResults[0]?.assertionResults ?? []) {
// for (const group of file.groups) {
// group.fullName looks like "bench/minify.bench.js > <fixtureName>"
// const fixtureName = assertionResult.title;
// timings[fixtureName] ??= {};
for (const b of assertionResult.benchmarks) {
timings[b.name] ??= {};

for (const task of b.tasks) {

const fixtureName = task.name;
timings[b.name][fixtureName] ??= {};
timings[b.name][fixtureName] = task.period; // milliseconds
}
}
// }
}

function fmtBytes(n) {
Expand Down Expand Up @@ -58,13 +65,15 @@ for (const m of minifiers) {
const totalMs = fixtures.reduce((sum, f) => sum + (timings[f.name]?.[m.id] ?? 0), 0);
totalRow += `<td>
<div class="metric"><span class="metric-label">final:</span> ${anyMissing ? fmtBytes(totalSize) + " (partial)" : fmtBytes(totalSize)}</div>
<div class="metric reduction${anyMissing ? " missing" : ''}">${anyMissing ? "n/a" : fmtReduction(totalOriginal, totalSize)}</div>
<div class="metric reduction${anyMissing ? " missing" : ""}">${anyMissing ? "n/a" : fmtReduction(totalOriginal, totalSize)}</div>
<div class="metric time"><span class="metric-label">time:</span> ${fmtMs(totalMs)}</div>
</td>`;
}
totalRow += `</tr>\n`;

const headerCells = minifiers.map((m) => `<th>${m.url != null ? `<a href="${m.url}" target="_top">${m.label}</a>` : m.label}</th>`).join("\n");
const headerCells = minifiers
.map((m) => `<th>${m.url != null ? `<a href="${m.url}" target="_top">${m.label}</a>` : m.label}</th>`)
.join("\n");

const html = `<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -144,4 +153,4 @@ Bench engine: Vitest bench (tinybench) -- time-boxed to 300ms per (file x librar
`;

writeFileSync(resultsDir + "benchmark.html", html);
console.log("wrote results/benchmark.html");
console.log("wrote results/benchmark.html");
18 changes: 9 additions & 9 deletions benchmark/src/minifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ export const minifiers = [
minify: (css) => csso.minify(css).css,
},
// infinite loop bug
{
id: "css-tree",
url: versions["css-tree"].url,
label: `css-tree - ${versions["css-tree"].version}`,
// css-tree has no dedicated minifier API; parse+generate already
// drops whitespace/comments, which is how the official benchmark
// treats it too (no property-level optimization, just compact output).
minify: (css) => csstree.generate(csstree.parse(css)),
},
// {
// id: "css-tree",
// url: versions["css-tree"].url,
// label: `css-tree - ${versions["css-tree"].version}`,
// // css-tree has no dedicated minifier API; parse+generate already
// // drops whitespace/comments, which is how the official benchmark
// // treats it too (no property-level optimization, just compact output).
// minify: (css) => csstree.generate(csstree.parse(css)),
// },
{
id: "esbuild",
url: versions.esbuild.url,
Expand Down
4 changes: 2 additions & 2 deletions dist/index-umd-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -26327,9 +26327,9 @@
? renderValue(token.val, options, cache)
: minifyNumber(token.val);
case exports.EnumToken.InfinityTokenType:
return "0/0";
return "1/0";
case exports.EnumToken.NegativeInfinityTokenType:
return "-0/0";
return "-1/0";
case exports.EnumToken.NaNTokenType:
return "NaN";
case exports.EnumToken.AtRuleTokenType:
Expand Down
4 changes: 2 additions & 2 deletions dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -26330,9 +26330,9 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
? renderValue(token.val, options, cache)
: minifyNumber(token.val);
case exports.EnumToken.InfinityTokenType:
return "0/0";
return "1/0";
case exports.EnumToken.NegativeInfinityTokenType:
return "-0/0";
return "-1/0";
case exports.EnumToken.NaNTokenType:
return "NaN";
case exports.EnumToken.AtRuleTokenType:
Expand Down
4 changes: 2 additions & 2 deletions dist/lib/renderer/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -1255,9 +1255,9 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
? renderValue(token.val, options, cache)
: minifyNumber(token.val);
case EnumToken.InfinityTokenType:
return "0/0";
return "1/0";
case EnumToken.NegativeInfinityTokenType:
return "-0/0";
return "-1/0";
case EnumToken.NaNTokenType:
return "NaN";
case EnumToken.AtRuleTokenType:
Expand Down
2 changes: 1 addition & 1 deletion jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tbela99/css-parser",
"version": "1.6.2",
"version": "1.6.3",
"publish": {
"include": [
"src",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@tbela99/css-parser",
"description": "CSS parser, minifier and validator for node and the browser",
"version": "1.6.2",
"version": "1.6.3",
"exports": {
".": "./dist/node.js",
"./node": "./dist/node.js",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/renderer/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1806,9 +1806,9 @@ export function renderValue(
: minifyNumber((token as NumberToken).val as number);

case EnumToken.InfinityTokenType:
return "0/0";
return "1/0";
case EnumToken.NegativeInfinityTokenType:
return "-0/0";
return "-1/0";

case EnumToken.NaNTokenType:
return "NaN";
Expand Down
4 changes: 2 additions & 2 deletions test/specs/code/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -1371,8 +1371,8 @@ color: blue;
background: alpha(from red/calc(1/0));
height: calc(1px*NaN);
width: calc(NaN);
margin-left: calc(0/0);
margin-right: calc(0/0 + 1px)
margin-left: calc(1/0);
margin-right: calc(1/0 + 1px)
}
}`);
});
Expand Down