Skip to content

Commit 99d1360

Browse files
committed
fix validation issues #87
1 parent 752b59f commit 99d1360

File tree

16 files changed

+769
-332
lines changed

16 files changed

+769
-332
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v1.1.1
4+
5+
- [x] fix validation issues
6+
37
## v1.1.0
48

59
- [x] inline sourcemap

dist/index-umd-web.js

Lines changed: 151 additions & 60 deletions
Large diffs are not rendered by default.

dist/index.cjs

Lines changed: 151 additions & 60 deletions
Large diffs are not rendered by default.

dist/lib/validation/config.json.js

Lines changed: 47 additions & 47 deletions
Large diffs are not rendered by default.

dist/lib/validation/syntax.js

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ function doEvaluateSyntax(syntaxes, context, options) {
181181
let i = 0;
182182
let result;
183183
let token = null;
184+
// console.error(`>> doEvaluateSyntax: ${syntaxes.reduce((acc, curr) => acc + renderSyntax(curr), '')}\n>> ${JSON.stringify({syntaxes}, null, 1)}>> context: ${context.slice<Token>().reduce((acc, curr) => acc + renderToken(curr), '')}`);
184185
for (; i < syntaxes.length; i++) {
185186
syntax = syntaxes[i];
186187
if (context.done()) {
@@ -210,6 +211,7 @@ function doEvaluateSyntax(syntaxes, context, options) {
210211
}
211212
else {
212213
if (isVisited(token, syntax, 'doEvaluateSyntax', options)) {
214+
// console.error(`cyclic dependency: ${renderSyntax(syntax)}`);
213215
return {
214216
valid: SyntaxValidationResult.Drop,
215217
node: token,
@@ -357,6 +359,7 @@ function matchOccurence(syntax, context, options) {
357359
};
358360
}
359361
function match(syntax, context, options) {
362+
// console.error(`>> match(): ${renderSyntax(syntax)}\n>> ${JSON.stringify({syntax}, null, 1)}>> context: ${context.slice<Token>().reduce((acc, curr) => acc + renderToken(curr), '')}`);
360363
let success = false;
361364
let result;
362365
let token = context.peek();
@@ -438,20 +441,17 @@ function match(syntax, context, options) {
438441
context
439442
};
440443
}
441-
{
442-
result = doEvaluateSyntax((getParsedSyntax("syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */, syntax.val + '()')?.[0]).chi, createContext(token.chi), {
443-
...options,
444-
isRepeatable: null,
445-
isList: null,
446-
occurence: null,
447-
atLeastOnce: null
448-
});
449-
if (result.valid == SyntaxValidationResult.Valid) {
450-
context.next();
451-
result.context = context;
452-
return result;
453-
}
444+
// {
445+
// console.error(JSON.stringify({funcDef: (getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (syntax as ValidationFunctionDefinitionToken).val + '()')?.[0] as ValidationFunctionToken).chi}, null, 1))
446+
//
447+
// const child = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, (syntax as ValidationFunctionDefinitionToken).val + '()')?.[0] as ValidationFunctionToken;
448+
result = match(getParsedSyntax("syntaxes" /* ValidationSyntaxGroupEnum.Syntaxes */, syntax.val + '()')?.[0], context, options);
449+
if (result.valid == SyntaxValidationResult.Valid) {
450+
context.next();
451+
result.context = context;
452+
return result;
454453
}
454+
// }
455455
break;
456456
case ValidationTokenEnum.DeclarationType:
457457
return doEvaluateSyntax(getParsedSyntax("declarations" /* ValidationSyntaxGroupEnum.Declarations */, syntax.val), context, {
@@ -548,6 +548,7 @@ function match(syntax, context, options) {
548548
}
549549
function matchPropertyType(syntax, context, options) {
550550
if (![
551+
'bg-position',
551552
'length-percentage', 'flex', 'calc-sum', 'color', 'color-base', 'system-color', 'deprecated-system-color',
552553
'pseudo-class-selector', 'pseudo-element-selector'
553554
].includes(syntax.val)) {
@@ -577,6 +578,96 @@ function matchPropertyType(syntax, context, options) {
577578
return { ...result, context };
578579
}
579580
switch (syntax.val) {
581+
case 'bg-position': {
582+
let val;
583+
let keyworkMatchCount = 0;
584+
let lengthMatchCount = 0;
585+
let functionMatchCount = 0;
586+
let isBGX = false;
587+
let isBGY = false;
588+
while (token != null && keyworkMatchCount + lengthMatchCount + functionMatchCount < 3) {
589+
// match one value: keyword or length
590+
success = (token.typ == EnumToken.FunctionTokenType && wildCardFuncs.includes(token.val));
591+
if (success) {
592+
functionMatchCount++;
593+
context.next();
594+
token = context.peek();
595+
continue;
596+
}
597+
if (token.typ == EnumToken.WhitespaceTokenType) {
598+
context.next();
599+
token = context.peek();
600+
continue;
601+
}
602+
if (token.typ == EnumToken.IdenTokenType) {
603+
val = token.val.toLowerCase();
604+
success = ['left', 'center', 'right', 'top', 'center', 'bottom'].includes(val);
605+
if (!success) {
606+
break;
607+
}
608+
keyworkMatchCount++;
609+
if (keyworkMatchCount > 2) {
610+
return {
611+
valid: SyntaxValidationResult.Drop,
612+
node: token,
613+
syntax,
614+
error: `expected <length>`,
615+
context
616+
};
617+
}
618+
if (val == 'left' || val == 'right') {
619+
if (isBGX) {
620+
return {
621+
valid: SyntaxValidationResult.Drop,
622+
node: token,
623+
syntax,
624+
error: `top | bottom | <length-percentage>`,
625+
context
626+
};
627+
}
628+
isBGX = true;
629+
}
630+
if (val == 'top' || val == 'bottom') {
631+
if (isBGY) {
632+
return {
633+
valid: SyntaxValidationResult.Drop,
634+
node: token,
635+
syntax,
636+
error: `expected left | right | <length-percentage>`,
637+
context
638+
};
639+
}
640+
isBGY = true;
641+
}
642+
context.next();
643+
token = context.peek();
644+
continue;
645+
}
646+
success = token.typ == EnumToken.LengthTokenType || token.typ == EnumToken.PercentageTokenType || (token.typ == EnumToken.NumberTokenType && token.val == '0');
647+
if (!success) {
648+
break;
649+
}
650+
lengthMatchCount++;
651+
context.next();
652+
token = context.peek();
653+
}
654+
if (keyworkMatchCount + lengthMatchCount + functionMatchCount == 0) {
655+
return {
656+
valid: SyntaxValidationResult.Drop,
657+
node: token,
658+
syntax,
659+
error: `expected <bg-position>`,
660+
context
661+
};
662+
}
663+
return {
664+
valid: SyntaxValidationResult.Valid,
665+
node: token,
666+
syntax,
667+
error: '',
668+
context
669+
};
670+
}
580671
case 'calc-sum':
581672
success = (token.typ == EnumToken.FunctionTokenType && mathFuncs.includes(token.val)) ||
582673
// @ts-ignore

jsr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tbela99/css-parser",
3-
"version": "1.1.1-alpha3",
3+
"version": "1.1.1-alpha4",
44
"publish": {
55
"include": [
66
"src",

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@tbela99/css-parser",
33
"description": "CSS parser for node and the browser",
4-
"version": "v1.1.1-alpha3",
4+
"version": "v1.1.1-alpha4",
55
"exports": {
66
".": "./dist/node/index.js",
77
"./node": "./dist/node/index.js",
@@ -19,7 +19,6 @@
1919
"test": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
2020
"test:web": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.",
2121
"test:node": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
22-
"test:local": "mocha --reporter-options='maxDiffSize=1801920' \"test/**/local.spec.js\"",
2322
"test:cov": "c8 -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js -x 'dist/lib/validation/parser/*.js' --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' --timeout=10000 \"test/**/node.spec.js\"",
2423
"test:web-cov": "web-test-runner -x 'test/specs/**/*.js' -x dist/lib/validation/syntax.js,dist/lib/validation/parser \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
2524
"profile": "node --enable-source-maps --inspect-brk test/inspect.js",

src/lib/renderer/color/utils/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const k: number = Math.pow(29, 3) / Math.pow(3, 3);
8181
export const e: number = Math.pow(6, 3) / Math.pow(29, 3);
8282

8383
// color module v4
84-
export const systemColors: Set<string> = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText'].map(m => m.toLowerCase()));
84+
export const systemColors: Set<string> = new Set(['ActiveText', 'ButtonBorder', 'ButtonFace', 'ButtonText', 'Canvas', 'CanvasText', 'Field', 'FieldText', 'GrayText', 'Highlight', 'HighlightText', 'LinkText', 'Mark', 'MarkText', 'VisitedText', '-webkit-focus-ring-color'].map(m => m.toLowerCase()));
8585
// deprecated
8686
export const deprecatedSystemColors: Set<string> = new Set(['ActiveBorder', 'ActiveCaption', 'AppWorkspace', 'Background', 'ButtonFace', 'ButtonHighlight', 'ButtonShadow', 'ButtonText', 'CaptionText', 'GrayText', 'Highlight', 'HighlightText', 'InactiveBorder', 'InactiveCaption', 'InactiveCaptionText', 'InfoBackground', 'InfoText', 'Menu', 'MenuText', 'Scrollbar', 'ThreeDDarkShadow', 'ThreeDFace', 'ThreeDHighlight', 'ThreeDLightShadow', 'ThreeDShadow', 'Window', 'WindowFrame', 'WindowText'].map(t => t.toLowerCase()));
8787

0 commit comments

Comments
 (0)