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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v1.4.5

- [x] Temporarily disable if syntax expansion

## v1.4.4

### Validation changes
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ $ deno add @tbela99/css-parser
## Syntax lowering
CSS-Parser can transform these modern CSS features into lower-level CSS syntax:
* **Nested CSS transpilation** to legacy-compatible syntax.
* **`if()` function transpilation** for broader browser compatibility.
* **`color-mix()` function conversion** convert new color-mix() syntax to any of the supported colors.

## Benchmark
Expand Down
45 changes: 44 additions & 1 deletion dist/index-umd-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -21543,7 +21543,48 @@
}
}
if (result.length > 0) {
replaceNodeOrValue(declarationNode.parent, declarationNode, result);
let invalidTokensTypes = new Set([
exports.EnumToken.WhitespaceTokenType,
exports.EnumToken.SemiColonTokenType,
exports.EnumToken.ColonTokenType
]);
for (i = 0; i < result.length; i++) {
if (result[i].typ === exports.EnumToken.DeclarationNodeType) {
for (const { value } of walkValues(result[i].val, result[i])) {
if (value.typ === exports.EnumToken.ImageFunctionTokenType && value.val.includes('-gradient')) {
let valid = true;
let j;
for (j = 0; j < value.chi.length; j++) {
if (value.chi[j].typ === exports.EnumToken.IdenTokenType && 'else' == value.chi[j].val) {
valid = false;
break;
}
if (invalidTokensTypes.has(value.chi[j].typ) &&
(value.chi[j + 1]?.typ === exports.EnumToken.CommaTokenType || j == value.chi.length - 1) &&
(j == 0 || exports.EnumToken.CommaTokenType == value.chi[j - 1]?.typ)) {
valid = false;
break;
}
}
if (!valid) {
result.splice(i--, 1);
break;
}
// const res = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (value as FunctionToken).val + '()'), createValidationContext((value as FunctionToken).chi), {
// });
// if (!res.success) {
// result.splice(i--, 1);
// break;
// }
}
}
}
}
// console.debug({result});
// throw new Error("Not implemented");
if (result.length > 0) {
replaceNodeOrValue(declarationNode.parent, declarationNode, result);
}
}
// else remove node?
return result;
Expand Down Expand Up @@ -30090,6 +30131,8 @@
if (options.resolveImport) {
options.resolveUrls = true;
}
// turn off expandIfSyntax for now
options.expandIfSyntax = false;
const startTime = performance.now();
const errors = [];
const src = options.src;
Expand Down
45 changes: 44 additions & 1 deletion dist/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22785,7 +22785,48 @@ function processNode(declarationNode, cache) {
}
}
if (result.length > 0) {
replaceNodeOrValue(declarationNode.parent, declarationNode, result);
let invalidTokensTypes = new Set([
exports.EnumToken.WhitespaceTokenType,
exports.EnumToken.SemiColonTokenType,
exports.EnumToken.ColonTokenType
]);
for (i = 0; i < result.length; i++) {
if (result[i].typ === exports.EnumToken.DeclarationNodeType) {
for (const { value } of walkValues(result[i].val, result[i])) {
if (value.typ === exports.EnumToken.ImageFunctionTokenType && value.val.includes('-gradient')) {
let valid = true;
let j;
for (j = 0; j < value.chi.length; j++) {
if (value.chi[j].typ === exports.EnumToken.IdenTokenType && 'else' == value.chi[j].val) {
valid = false;
break;
}
if (invalidTokensTypes.has(value.chi[j].typ) &&
(value.chi[j + 1]?.typ === exports.EnumToken.CommaTokenType || j == value.chi.length - 1) &&
(j == 0 || exports.EnumToken.CommaTokenType == value.chi[j - 1]?.typ)) {
valid = false;
break;
}
}
if (!valid) {
result.splice(i--, 1);
break;
}
// const res = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (value as FunctionToken).val + '()'), createValidationContext((value as FunctionToken).chi), {
// });
// if (!res.success) {
// result.splice(i--, 1);
// break;
// }
}
}
}
}
// console.debug({result});
// throw new Error("Not implemented");
if (result.length > 0) {
replaceNodeOrValue(declarationNode.parent, declarationNode, result);
}
}
// else remove node?
return result;
Expand Down Expand Up @@ -29855,6 +29896,8 @@ async function doParse(iter, options = {}) {
if (options.resolveImport) {
options.resolveUrls = true;
}
// turn off expandIfSyntax for now
options.expandIfSyntax = false;
const startTime = performance.now();
const errors = [];
const src = options.src;
Expand Down
45 changes: 43 additions & 2 deletions dist/lib/ast/features/if.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { replaceNodeOrValue } from '../../parser/utils/token.js';
import { cloneNode } from '../clone.js';
import { trimArray } from '../../validation/match.js';
import { findByValue } from '../find.js';
import { walk } from '../walk.js';
import { walk, walkValues } from '../walk.js';
import { eq } from '../../parser/utils/eq.js';

const nodeMatcher = (value) => value.typ === EnumToken.IfConditionTokenType ||
Expand Down Expand Up @@ -169,7 +169,48 @@ function processNode(declarationNode, cache) {
}
}
if (result.length > 0) {
replaceNodeOrValue(declarationNode.parent, declarationNode, result);
let invalidTokensTypes = new Set([
EnumToken.WhitespaceTokenType,
EnumToken.SemiColonTokenType,
EnumToken.ColonTokenType
]);
for (i = 0; i < result.length; i++) {
if (result[i].typ === EnumToken.DeclarationNodeType) {
for (const { value } of walkValues(result[i].val, result[i])) {
if (value.typ === EnumToken.ImageFunctionTokenType && value.val.includes('-gradient')) {
let valid = true;
let j;
for (j = 0; j < value.chi.length; j++) {
if (value.chi[j].typ === EnumToken.IdenTokenType && 'else' == value.chi[j].val) {
valid = false;
break;
}
if (invalidTokensTypes.has(value.chi[j].typ) &&
(value.chi[j + 1]?.typ === EnumToken.CommaTokenType || j == value.chi.length - 1) &&
(j == 0 || EnumToken.CommaTokenType == value.chi[j - 1]?.typ)) {
valid = false;
break;
}
}
if (!valid) {
result.splice(i--, 1);
break;
}
// const res = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (value as FunctionToken).val + '()'), createValidationContext((value as FunctionToken).chi), {
// });
// if (!res.success) {
// result.splice(i--, 1);
// break;
// }
}
}
}
}
// console.debug({result});
// throw new Error("Not implemented");
if (result.length > 0) {
replaceNodeOrValue(declarationNode.parent, declarationNode, result);
}
}
// else remove node?
return result;
Expand Down
2 changes: 2 additions & 0 deletions dist/lib/parser/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ async function doParse(iter, options = {}) {
if (options.resolveImport) {
options.resolveUrls = true;
}
// turn off expandIfSyntax for now
options.expandIfSyntax = false;
const startTime = performance.now();
const errors = [];
const src = options.src;
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.4.4",
"version": "1.4.5",
"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.4.4",
"version": "1.4.5",
"exports": {
".": "./dist/node.js",
"./node": "./dist/node.js",
Expand Down
70 changes: 67 additions & 3 deletions src/lib/ast/features/if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import { definedPropertySettings } from "../../syntax/constants.ts";
import { equalsIgnoreCase } from "../../parser/utils/text.ts";
import { replaceNodeOrValue } from "../../parser/utils/token.ts";
import { cloneNode } from "../../ast/clone.ts";
import { trimArray } from "../../validation/match.ts";
import { createValidationContext, matchAllSyntaxes, trimArray } from "../../validation/match.ts";
import { findByValue } from "../find.ts";
import { walk } from "../walk.ts";
import { walk, walkValues } from "../walk.ts";
import { eq } from "../../parser/utils/eq.ts";
import { getParsedSyntax } from "../../validation/config.ts";
import { ValidationSyntaxGroupEnum } from "../../validation/parser/typedef.ts";

const nodeMatcher = (value: Token) =>
value.typ === EnumToken.IfConditionTokenType ||
Expand Down Expand Up @@ -295,7 +297,69 @@ function processNode(declarationNode: AstDeclaration, cache: Set<AstNode>): AstN
}

if (result.length > 0) {
replaceNodeOrValue(declarationNode.parent, declarationNode, result);

let invalidTokensTypes = new Set([
EnumToken.WhitespaceTokenType,
EnumToken.SemiColonTokenType,
EnumToken.ColonTokenType
])

for (i = 0; i < result.length; i++) {
if (result[i].typ === EnumToken.DeclarationNodeType) {

for (const {value} of walkValues((result[i] as AstDeclaration).val, result[i])) {

if (value.typ === EnumToken.ImageFunctionTokenType && (value as FunctionToken).val.includes('-gradient')) {

let valid: boolean = true;
let j: number

for (j = 0; j < (value as FunctionToken).chi.length; j++) {

if ((value as FunctionToken).chi[j].typ === EnumToken.IdenTokenType && 'else' == ((value as FunctionToken).chi[j] as IdentToken).val) {

valid = false;
break;
}

if (invalidTokensTypes.has((value as FunctionToken).chi[j].typ) &&
((value as FunctionToken).chi[j + 1]?.typ === EnumToken.CommaTokenType || j == (value as FunctionToken).chi.length - 1) &&
(j == 0 || EnumToken.CommaTokenType == (value as FunctionToken).chi[j - 1]?.typ)) {

valid = false;
break;
}
}

if (!valid) {

result.splice(i--, 1);
break;
}



// const res = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (value as FunctionToken).val + '()'), createValidationContext((value as FunctionToken).chi), {

// });

// if (!res.success) {

// result.splice(i--, 1);
// break;
// }
}
}
}
}

// console.debug({result});
// throw new Error("Not implemented");

if (result.length > 0) {

replaceNodeOrValue(declarationNode.parent, declarationNode, result);
}
}
// else remove node?
return result;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ export async function doParse(
options.resolveUrls = true;
}

// turn off expandIfSyntax for now
options.expandIfSyntax = false;

const startTime: number = performance.now();
const errors: ErrorDescription[] = [];
const src: string = options.src as string;
Expand Down
66 changes: 33 additions & 33 deletions test/specs/code/at-rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -1333,40 +1333,40 @@ supports((selector(h2 > p)) and (font-tech(color-COLRv1))) {



it('do not minify supports() arguments #58', function () {
return transform(`

body {
background-color: if(
supports(color: oklch(0.7 0.185 232)): oklch(0.7 0.185 232);
else: #00adf3;
);
// it('do not minify supports() arguments #58', function () {
// return transform(`

// body {
// background-color: if(
// supports(color: oklch(0.7 0.185 232)): oklch(0.7 0.185 232);
// else: #00adf3;
// );

&::after {
content: if(
supports(color: oklch(0.7 0.185 232)): "Your browser supports OKLCH";
else: "Your browser does not support OKLCH";
);
}
}

`, {
beautify: true,
expandIfSyntax: true,
validation: true
}).then((result) => expect(result.code).equals(`body {
background-color: #00adf3;
@supports (color:oklch(.7 .185 232)) {
background-color: #00aefc
}
&:after {
content: "Your browser does not support OKLCH";
@supports (color:oklch(.7 .185 232)) {
content: "Your browser supports OKLCH"
}
}
}`));
});
// &::after {
// content: if(
// supports(color: oklch(0.7 0.185 232)): "Your browser supports OKLCH";
// else: "Your browser does not support OKLCH";
// );
// }
// }

// `, {
// beautify: true,
// expandIfSyntax: true,
// validation: true
// }).then((result) => expect(result.code).equals(`body {
// background-color: #00adf3;
// @supports (color:oklch(.7 .185 232)) {
// background-color: #00aefc
// }
// &:after {
// content: "Your browser does not support OKLCH";
// @supports (color:oklch(.7 .185 232)) {
// content: "Your browser supports OKLCH"
// }
// }
// }`));
// });



Expand Down
Loading
Loading