diff --git a/.gitignore b/.gitignore index 091c51e3..1631267c 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ /ROADMAP.draft.md /.idea /.DS_Store -/benchmark /ROADMAP.md /package-lock.json test/*.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ff60da7..2124863e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/benchmark/bench/minify.bench.js b/benchmark/bench/minify.bench.js index 432ff801..c1e325a5 100644 --- a/benchmark/bench/minify.bench.js +++ b/benchmark/bench/minify.bench.js @@ -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"; @@ -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) {} } }); -} \ No newline at end of file +} diff --git a/benchmark/package.json b/benchmark/package.json index 524036bb..299f8a3e 100644 --- a/benchmark/package.json +++ b/benchmark/package.json @@ -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" } } \ No newline at end of file diff --git a/benchmark/src/generate-report.js b/benchmark/src/generate-report.js index efd35eb6..8effbf06 100644 --- a/benchmark/src/generate-report.js +++ b/benchmark/src/generate-report.js @@ -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 > " - 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 > " + // 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) { @@ -58,13 +65,15 @@ for (const m of minifiers) { const totalMs = fixtures.reduce((sum, f) => sum + (timings[f.name]?.[m.id] ?? 0), 0); totalRow += `
final: ${anyMissing ? fmtBytes(totalSize) + " (partial)" : fmtBytes(totalSize)}
-
${anyMissing ? "n/a" : fmtReduction(totalOriginal, totalSize)}
+
${anyMissing ? "n/a" : fmtReduction(totalOriginal, totalSize)}
time: ${fmtMs(totalMs)}
`; } totalRow += `\n`; -const headerCells = minifiers.map((m) => `${m.url != null ? `${m.label}` : m.label}`).join("\n"); +const headerCells = minifiers + .map((m) => `${m.url != null ? `${m.label}` : m.label}`) + .join("\n"); const html = ` @@ -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"); \ No newline at end of file diff --git a/benchmark/src/minifiers.js b/benchmark/src/minifiers.js index c2e6003e..57ee284e 100644 --- a/benchmark/src/minifiers.js +++ b/benchmark/src/minifiers.js @@ -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, diff --git a/dist/index-umd-web.js b/dist/index-umd-web.js index 90d15b5f..17b449a0 100644 --- a/dist/index-umd-web.js +++ b/dist/index-umd-web.js @@ -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: diff --git a/dist/index.cjs b/dist/index.cjs index e6ab761a..6ca07731 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -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: diff --git a/dist/lib/renderer/render.js b/dist/lib/renderer/render.js index 169b3f0a..461d83c7 100644 --- a/dist/lib/renderer/render.js +++ b/dist/lib/renderer/render.js @@ -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: diff --git a/jsr.json b/jsr.json index 4c0f3c33..f7f63acb 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@tbela99/css-parser", - "version": "1.6.2", + "version": "1.6.3", "publish": { "include": [ "src", diff --git a/package.json b/package.json index 81df1fa1..585eacd1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/lib/renderer/render.ts b/src/lib/renderer/render.ts index 8cfb6072..753316ac 100644 --- a/src/lib/renderer/render.ts +++ b/src/lib/renderer/render.ts @@ -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"; diff --git a/test/specs/code/block.js b/test/specs/code/block.js index 54d7397e..f4aad5e8 100644 --- a/test/specs/code/block.js +++ b/test/specs/code/block.js @@ -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) } }`); });