diff --git a/.npmignore b/.npmignore index 004919b90..63941a84e 100644 --- a/.npmignore +++ b/.npmignore @@ -7,6 +7,7 @@ /benchmark /tools /ROADMAP.md +/CONTRIBUTING.md /rollup.config.js /tsconfig.json /src diff --git a/CHANGELOG.md b/CHANGELOG.md index e3015e454..ee72b46f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,19 @@ # Changelog +# v1.7.0 + +- [x] implement CSS [Custom Functions and Mixins Modules and Level 1](https://drafts.csswg.org/css-mixins/#apply-rule) +- [x] allow \ [outside parenthesis](https://developer.mozilla.org/en-US/docs/Web/CSS/Guides/Media_queries/Using#testing_for_multiple_features_with_or) + +```ts +@media ((color) and (hover)) or (monochrome) { + /* … */ +} +``` + # v1.6.9 -Add missing files from the dist folder. +fix import path #166 from package.json # v1.6.8 diff --git a/benchmark/package.json b/benchmark/package.json index 2b2615324..6cff98432 100644 --- a/benchmark/package.json +++ b/benchmark/package.json @@ -10,8 +10,8 @@ "all": "npm run sizes && npm run bench; npm run report" }, "dependencies": { - "@tbela99/css-parser": "^1.6.9", - "@tbela99/css-parser2": "github:tbela99/css-parser#5920da6", + "@tbela99/css-parser": "^1.7.0", + "@tbela99/css-parser2": "github:tbela99/css-parser#89f2136", "clean-css": "^5.3.3", "css-tree": "^3.2.1", "cssnano": "^9.9.0", @@ -21,4 +21,4 @@ "postcss": "^8.5.31", "vitest": "^5.0.5" } -} +} \ No newline at end of file diff --git a/dist/index-umd-web.js b/dist/index-umd-web.js index cb1e002c8..c5f4f4962 100644 --- a/dist/index-umd-web.js +++ b/dist/index-umd-web.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ (function (global, factory) { @@ -5721,7 +5721,13 @@ syntax: " @bottom-right-corner { }" }, "@function": { - syntax: " @function (#?) [returns ]? { }" + syntax: " @function #?) [returns ]? { }" + }, + "@mixin": { + syntax: " @mixin [ #? ) | ]{}" + }, + "@apply": { + syntax: " @apply [ | ] [ { } ]?;" }, "@left-top": { syntax: " @left-top { }" @@ -11770,6 +11776,11 @@ [LOC]: { ...position }, }; } + /** + * parse mdn data syntax + * @param syntax + * @returns + */ function parseSyntax(syntax) { const stack = []; let tokens = []; @@ -12313,8 +12324,8 @@ } } } - const block = blockStart == -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd)); - const prelude = trimSyntaxArray(blockStart == -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart)); + let block = blockStart == -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd)); + let prelude = trimSyntaxArray(blockStart == -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart)); let propertyDescriptors = null; // @ts-expect-error if (node.descriptors != null) { @@ -12325,7 +12336,24 @@ propertyDescriptors[key] = parseSyntax(typeof value === "string" ? value : value.syntax); } } + let optionalBody = false; + if (block == null || block.length === 0) { + let k = prelude.length - 1; + if (ValidationTokenEnum.SemiColon === prelude[k].typ) { + k--; + } + if (k >= 0 && ValidationTokenEnum.Bracket === prelude[k].typ) { + if (prelude[k].chi[0].typ === + ValidationTokenEnum.OpenCurlyBrace && + prelude[k].chi.at(-1).typ === + ValidationTokenEnum.CloseCurlyBrace) { + block = prelude[k].chi.slice(1, -1); + optionalBody = prelude[k].isOptional ?? false; + } + } + } return { + optionalBody, acceptAnyDeclaration: node.syntax.includes(""), acceptAnyRule: node.syntax.includes("") || node.syntax.includes(""), @@ -12413,15 +12441,6 @@ } return tokens; } - /** - * is a media feature - * @param featureName - * @returns - */ - // export function isMFName(featureName: string): boolean { - // // @ts-expect-error - // return featureName.startsWith("--") || config.mediaFeatures[featureName.toLowerCase()] != null; - // } /** * * @param featureName @@ -12942,9 +12961,6 @@ case exports.EnumToken.NumberTokenType: case exports.EnumToken.LiteralTokenType: case exports.EnumToken.DimensionTokenType: - // if (stack.at(-1)?.typ === EnumToken.CommaTokenType) { - // stack.pop(); - // } if (stack.at(-1)?.typ === exports.EnumToken.PseudoClassFunctionTokenDefType) { break; } @@ -13213,43 +13229,9 @@ continue; } } - // custom function token - if (token.typ === exports.EnumToken.CustomFunctionTokenDefType) { - if (syntaxes[i].typ != ValidationTokenEnum.PropertyType || - syntaxes[i + 1]?.typ != ValidationTokenEnum.OpenParenthesis || - syntaxes[i + 2]?.typ == null) { - if (!isOptional) { - return { - success: false, - errors: [ - { - action: "drop", - message: "could not match syntax", - node: token, - syntax: syntaxes[i], - }, - ], - syntaxToken: syntaxes[i], - valid: true, - context, - token: null, - }; - } - break; - } - const range = trimArray(context.peekRange()); - context.next(); - i++; - result = matchSyntax([syntaxes[i + 1]], createValidationContext(range.slice(1, -1)), options); - if (result.success) { - context.update(range.at(-1)); - i += 2; - } - return result; - } - if (tokensfuncDefMap.has(token.typ) && - // @ts-ignore - token.typ === exports.EnumToken.WildCardFunctionTokenDefType) { + if ( + // @ts-ignore + token.typ === exports.EnumToken.WildCardFunctionTokenDefType) { const range = trimArray(context.peekRange()); result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, token.val + "()")?.[0]?.chi, createValidationContext(range.slice(1, -1)), options); if (result.success) { @@ -13274,6 +13256,9 @@ context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return result; } if (syntaxes[i].isList) { @@ -13294,6 +13279,9 @@ context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return { success: false, valid: true, @@ -13318,6 +13306,9 @@ context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return { success: false, valid: true, @@ -13722,6 +13713,20 @@ syntaxToken: syntaxes[i], errors: [], }; + case ValidationTokenEnum.OpenCurlyBrace: + if (token.typ === exports.EnumToken.BlockStartTokenType) { + options.visited.get(token).delete(syntaxes[i]); + context.next(); + break; + } + return { + success: false, + valid: true, + token, + context, + syntaxToken: syntaxes[i], + errors: [], + }; case ValidationTokenEnum.CloseParenthesis: if (token.typ === exports.EnumToken.EndParensTokenType) { options.visited.get(token).delete(syntaxes[i]); @@ -13923,6 +13928,7 @@ }; } switch (property.val) { + case "function-parameter": case "declaration-value": { // consume a declaration value @@ -13932,18 +13938,43 @@ // if semicolon or end curly brace is encountered, stop let expectToken = true; let token; + const stack = []; token = context.peek(); - while ((token = context.peek()) != null) { - if (token.typ == exports.EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) ; - if (expectToken) { - expectToken = false; - } - if (token.typ == exports.EnumToken.CommaTokenType) { - expectToken = true; + if (token != null) { + success = false; + while ((token = context.peek()) != null) { + if (token.typ == exports.EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) { + stack.push(token); + } + if (expectToken) { + expectToken = false; + } + if (token.typ == exports.EnumToken.CommaTokenType) { + expectToken = true; + } + if (token.typ == exports.EnumToken.EndParensTokenType) { + if (stack.length > 0) { + stack.pop(); + } + else { + expectToken = false; + break; + } + } + context.next(); } - context.next(); } success = !expectToken; + if (success) { + return { + success, + valid: true, + token: null, + context, + syntaxToken: property, + errors: [], + }; + } } break; case "calc-sum": { @@ -13976,9 +14007,6 @@ } break; } - // case "at-keyword-token": - // success = context.peek()?.typ === EnumToken.AtRuleTokenType; - // break; case "an+b": { let token = context.peek(); @@ -14043,9 +14071,6 @@ Object.assign(token, { typ: exports.EnumToken.Add }); } token = context.peek(2); - // if (token == null) { - // break; - // } if (token?.typ === exports.EnumToken.NumberTokenType && Number.isInteger(token.val) && !token.toString().includes(".") && @@ -14153,9 +14178,6 @@ break; case "dimension": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == exports.EnumToken.DimensionTokenType || token?.typ == exports.EnumToken.AngleTokenType || @@ -14170,18 +14192,12 @@ case "angle": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ === exports.EnumToken.AngleTokenType; } break; case "angle-percentage": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ === exports.EnumToken.AngleTokenType || token?.typ === exports.EnumToken.PercentageTokenType; } break; @@ -14197,9 +14213,6 @@ case "length-percentage": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == exports.EnumToken.PercentageTokenType || token?.typ == exports.EnumToken.LengthTokenType || @@ -14211,9 +14224,6 @@ break; case "length": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == exports.EnumToken.LengthTokenType || (token?.typ == exports.EnumToken.NumberTokenType && token?.val === 0); @@ -14228,12 +14238,6 @@ success = token.typ == exports.EnumToken.TimeTokenType; } break; - // case "hex-color": { - // const token = context.peek(); - // success = - // token != null && token.typ == EnumToken.ColorTokenType && (token as ColorToken).kin == ColorType.HEX; - // break; - // } case "color": case "color-function": { @@ -14241,13 +14245,6 @@ const errors = []; if (tokensfuncDefMap.has(token?.typ)) { const range = context.peekRange(); - // if ( - // range.some( - // (token) => - // token.typ == EnumToken.IdenTokenType && - // "hslrgbchwmyk".split("").some((t) => equalsIgnoreCase(t, (token as IdentToken).val)), - // ) - // ) { const newRange = range.map((t) => cloneNode(t, true)); // @ts-ignore parseTokens(newRange, { parseColor: true }, errors); @@ -14272,27 +14269,6 @@ syntaxToken: null, errors, }; - // } - // const result = matchSyntax( - // getParsedSyntax( - // ValidationSyntaxGroupEnum.Syntaxes, - // (token as FunctionToken).val + "()", - // ) as ValidationFunctionToken[], - // createValidationContext(range), - // options, - // ); - // if (result.success) { - // context.update(range.at(-1) as Token); - // return { - // success: true, - // valid: true, - // token: context.peek(), - // context, - // syntaxToken: null, - // errors: [], - // }; - // } - // return result; } // @ts-expect-error else if ("chi" in token) { @@ -14311,10 +14287,6 @@ } } break; - // case "url-token": - // success = - // context.peek()?.typ == EnumToken.StringTokenType || context.peek()?.typ == EnumToken.UrlTokenTokenType; - // break; case "type-selector": case "compound-selector": case "complex-selector": @@ -14339,15 +14311,6 @@ }; } case "function-token": - // if ( - // context.peek()?.typ == EnumToken.IdenTokenType && - // context.peek(1, false)?.typ == EnumToken.StartParensTokenType - // ) { - // context.next(); - // context.next(); - // success = true; - // break; - // } if (tokensfuncDefMap.has(context.peek()?.typ)) { success = true; context.next(); @@ -14369,17 +14332,6 @@ config$3.languages.some((t) => t === token.val || token.val.startsWith(t + "-")); break; } - // case "named-color": { - // const token = context.peek(); - // success = - // token != null && - // token.typ == EnumToken.ColorTokenType && - // ((token as ColorToken).kin == ColorType.LIT || - // (token as ColorToken).kin == ColorType.SYS || - // (token as ColorToken).kin == ColorType.DPSYS || - // (token as ColorToken).kin == ColorType.NON_STD); - // break; - // } case "size-feature": { const range = context.peekRange(); const filteredRange = range.filter((token) => token.typ !== exports.EnumToken.WhitespaceTokenType && token.typ !== exports.EnumToken.CommentTokenType); @@ -14410,20 +14362,6 @@ errors: [], }; } - // if (filteredRange.length === 3) { - // success = isValue(filteredRange[2]); - // if (success) { - // context.update(filteredRange[2]); - // return { - // success, - // valid: true, - // token: filteredRange[2], - // context, - // syntaxToken: property, - // errors: [], - // }; - // } - // } break; } case "style-feature": { @@ -14433,16 +14371,6 @@ // match const range = context.peekRange(); const filteredRange = range.filter((token) => token.typ !== exports.EnumToken.WhitespaceTokenType && token.typ !== exports.EnumToken.CommentTokenType); - // if (filteredRange.length === 0) { - // return { - // success: false, - // valid: true, - // token: context.peek(), - // context, - // syntaxToken: property, - // errors: [], - // }; - // } if (filteredRange.length === 1) { success = filteredRange[0].typ === exports.EnumToken.IdenTokenType || @@ -14457,31 +14385,6 @@ errors: [], }; } - // if ( - // filteredRange[1]?.typ === EnumToken.ColonTokenType || - // filteredRange[1]?.typ === EnumToken.DelimTokenType - // ) { - // const result = isDeclarationValue(filteredRange.slice(2)); - // if (result.success) { - // context.update(filteredRange.at(-1)!); - // return { - // success: true, - // valid: true, - // token: filteredRange.at(-1)!, - // context, - // syntaxToken: property, - // errors: [], - // }; - // } - // return { - // success: false, - // valid: false, - // token: context.peek(), - // context, - // syntaxToken: property, - // errors: result.errors, - // }; - // } return { success: false, valid: false, @@ -14493,10 +14396,6 @@ } case "url": { const token = context.peek(); - // if (token == null) { - // success = false; - // break; - // } let l; if (token?.typ == exports.EnumToken.UrlFunctionTokenDefType) { l = 0; @@ -14510,11 +14409,6 @@ (token.typ == exports.EnumToken.StringTokenType || token.typ == exports.EnumToken.IdenTokenType || token.typ == exports.EnumToken.UrlTokenTokenType)) { - // if (token.typ === EnumToken.IdenTokenType) { - // while (context.peek(l + 1)?.typ === EnumToken.ClassSelectorTokenType) { - // l++; - // } - // } while ((token = context.peek(++l))) { if (token.typ !== exports.EnumToken.WhitespaceTokenType && token.typ !== exports.EnumToken.CommentTokenType) { break; @@ -14578,10 +14472,46 @@ } break; } + case "dashed-function": { + const token = context.peek(); + success = token?.typ == exports.EnumToken.CustomFunctionTokenDefType; + if (success) { + context.next(); + return { + success: true, + valid: true, + token, + context, + syntaxToken: null, + errors: [], + }; + } + break; + } + case "block-contents": + success = context.peek()?.typ == exports.EnumToken.StartParensTokenType; + if (success) { + context.next(); + return { + success: true, + valid: true, + token: null, + context, + syntaxToken: null, + errors: [], + }; + } + return { + success: false, + valid: true, + token, + context, + syntaxToken: property, + errors: [], + }; default: if (!(property.val in config$3.syntaxes)) { console.debug("Unknown syntax", context.peek(), property); - console.debug(context.getRemainingTokens()); throw new Error(`Unexpected validation property ${property.val}`); } return matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, property.val), context, options); @@ -20085,7 +20015,7 @@ if (value instanceof PropertyMap) { for (const [k, v] of value.declarations) { if (k == value.config.shorthand) { - for (const [key, val] of Object.entries(value.config.properties)) { + for (const [key] of Object.entries(value.config.properties)) { mapped[key] = cloneNode(v); for (let i = 0; i < v.val.length; i++) { // @ts-ignore @@ -20873,6 +20803,16 @@ let result; for (const declaration of declarations) { name = declaration.typ != exports.EnumToken.DeclarationNodeType ? null : declaration.nam; + // do not compute shorthand for invalid declarations + if (name == null) { + this.declarations.set(objectHash(declaration), declaration); + continue; + } + // @functiom / result + if (declaration[STATE] & exports.EnumAstNodeStatus.Unknown && equalsIgnoreCase("result", name)) { + this.declarations.set(declaration.nam, declaration); + continue; + } if (declaration[STATE] & (exports.EnumAstNodeStatus.Invalid | exports.EnumAstNodeStatus.Unknown | exports.EnumAstNodeStatus.ValidationFailed) || declaration.typ != exports.EnumToken.DeclarationNodeType || @@ -21110,32 +21050,49 @@ let l; const rules = []; const declarations = []; - // @ts-ignore - for (; k < j; k++) { - l = k; - // capture comments with the next token - while (l + 1 < j) { - // @ts-ignore - const node = ast.chi[l]; - if (node.typ == exports.EnumToken.CommentNodeType) { - l++; - continue; - } - break; + let doNotSort = false; + if (ast.typ == exports.EnumToken.AtRuleNodeType) { + switch (ast.nam) { + case "function": + case "mixin": + doNotSort = true; + break; } + } + // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example + if (doNotSort) { + for (const node of ast.chi) { + declarations.push(node); + } + } + else { // @ts-ignore - const node = ast.chi[l]; - if (node.typ == exports.EnumToken.DeclarationNodeType) { - for (let m = k; m <= l; m++) { - declarations.push(ast.chi[m]); + for (; k < j; k++) { + l = k; + // capture comments with the next token + while (l + 1 < j) { + // @ts-ignore + const node = ast.chi[l]; + if (node.typ == exports.EnumToken.CommentNodeType) { + l++; + continue; + } + break; } - } - else { - for (let m = k; m <= l; m++) { - rules.push(ast.chi[m]); + // @ts-ignore + const node = ast.chi[l]; + if (node.typ == exports.EnumToken.DeclarationNodeType) { + for (let m = k; m <= l; m++) { + declarations.push(ast.chi[m]); + } } + else { + for (let m = k; m <= l; m++) { + rules.push(ast.chi[m]); + } + } + k = l; } - k = l; } if (declarations.length > 0) { ast.chi.length = 0; @@ -21155,6 +21112,7 @@ exports.EnumToken.LteTokenType, exports.EnumToken.ChildCombinatorTokenType, exports.EnumToken.PipeTokenType, + exports.EnumToken.ColonTokenType ]); const trimSpaceAfter = new Set([ ...tokensfuncSet.values(), @@ -23343,8 +23301,35 @@ previous = ast.chi[--nodeIndex]; } node = ast.chi[i]; - if (node.typ === exports.EnumToken.AtRuleNodeType && node.nam === "font-face") { - continue; + if (node.typ === exports.EnumToken.AtRuleNodeType) { + if (node.nam === "font-face") { + continue; + } + // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example + if (equalsIgnoreCase(node.nam, "function")) { + const chi = node.chi; + let k = chi?.length; + let slice = false; + while (k--) { + if (chi[k].typ === exports.EnumToken.CommentNodeType || + chi[k].typ === exports.EnumToken.WhitespaceTokenType) { + continue; + } + if (chi[k].typ === exports.EnumToken.DeclarationNodeType && + equalsIgnoreCase(chi[k].nam, "result")) { + slice = true; + break; + } + } + if (k > 0 && slice) { + while (k--) { + if (chi[k].typ === exports.EnumToken.AtRuleNodeType) { + chi.splice(k, 1); + continue; + } + } + } + } } if (node.typ === exports.EnumToken.KeyframesAtRuleNodeType) { if (previous?.typ === exports.EnumToken.KeyframesAtRuleNodeType && @@ -25879,7 +25864,7 @@ case exports.EnumToken.KeyframesRuleNodeType: case exports.EnumToken.KeyframesAtRuleNodeType: if ([exports.EnumToken.AtRuleNodeType, exports.EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) { - return `${indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`; + return `${(indent.length > 0 ? options.newLine : "") + indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`; } const lineMapLength = linesMap ? linesMap.getLineStarts().length : 0; const prelude = (indent.length > 0 ? options.newLine : "") + @@ -29625,7 +29610,7 @@ Object.assign(stream[i], { typ: val === "and" ? exports.EnumToken.AndTokenType : exports.EnumToken.OrTokenType, }); - if (val === "or" && scopes.length <= 1) { + if (val === "or" && (scopes.length == 1 && scopes[0].size > 0 && !scopes[0].has(exports.EnumToken.OrTokenType))) { success = false; errors.push({ action: "drop", @@ -30141,7 +30126,7 @@ break; } if ("and" === val || "or" === val) { - if ("or" === val && scopes.length === 1) { + if ("or" === val && scopes.length === 1 && scopes[0].size > 0) { const fileName = options.source.getFileName() ?? ""; const [line, column] = options.source.getOffsets(stream[i]?.[LOCSTA]); return { @@ -31224,19 +31209,10 @@ key = visitors[i][0]; value = visitors[i][1]; if (Number.isInteger(+key)) { - // if (Array.isArray(value)) { - // visitors.splice(i + 1, 0, ...Object.entries(value)); - // continue; - // } if (typeof value == "function") { key = value.name; } } - // if (Array.isArray(value)) { - // // @ts-ignore - // visitors.splice(i + 1, 0, ...value.map((item) => [key, item])); - // continue; - // } if (key in exports.EnumToken) { if (typeof value == "function") { if (!valuesHandlers.has(exports.EnumToken[key])) { @@ -31596,7 +31572,7 @@ if (tokens.length > 0) { node = parseNode(tokens, context, options, errors, stats, invalidNodes); if (node != null) { - if ("chi" in node /* && node.typ != EnumToken.InvalidRuleNodeType */) { + if ("chi" in node) { stack.push(node); context = node; } @@ -32368,14 +32344,9 @@ let parensMatch = 0; let curlyBracketMatch = 0; let tokenizer = iter instanceof Promise ? await iter : iter; - // ast[ROOT] = ast; ast[LOCSRCID] = options.source.id; ast[LOCSTA] = 0; ast[LOCEND] = 0; - // if (Array.isArray(iter)) { - // // @ts-expect-error - // iter = iter[Symbol.iterator]() as Iterator; - // } while (!tokenizer.done()) { tokenizer.next(); if (tokenizer.unit != null) { @@ -32616,6 +32587,11 @@ case exports.EnumToken.AtRuleNodeType: case exports.EnumToken.KeyframesRuleNodeType: case exports.EnumToken.KeyframesAtRuleNodeType: + if (nodes[i][TOKENS] == null) { + if (nodes[i].typ == exports.EnumToken.RuleNodeType) { + nodes[i][TOKENS] = parseString(nodes[i].sel); + } + } for (const token of nodes[i][TOKENS]) { subNodes.push(token); } @@ -33610,7 +33586,7 @@ else if (context.typ === exports.EnumToken.AtRuleNodeType && "page" === context.nam && pageMarginBoxType.has(atRuleName.toLowerCase())) { - if (parseAsBlock === false) { + if (parseAsBlock === false && syntaxRules.optionalBody === false) { errors.push({ action: "drop", node: atRule, @@ -33649,7 +33625,7 @@ if (parseAsBlock === null) { parseAsBlock = blockAllowed; } - if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed) { + if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed && !syntaxRules.optionalBody) { errors.push({ action: "drop", node: atRule, @@ -34778,7 +34754,7 @@ } } - var version = "1.6.8"; + var version = "1.7.0"; /** * Load file or url @@ -35205,6 +35181,7 @@ exports.parseFile = parseFile; exports.parseString = parseString; exports.parseSync = parseSync; + exports.parseSyntax = parseSyntax; exports.render = render; exports.renderToken = renderValue; exports.replaceNodeOrValue = replaceNodeOrValue; diff --git a/dist/index.cjs b/dist/index.cjs index 956baf354..4397e5913 100644 --- a/dist/index.cjs +++ b/dist/index.cjs @@ -5717,7 +5717,13 @@ var atRules = { syntax: " @bottom-right-corner { }" }, "@function": { - syntax: " @function (#?) [returns ]? { }" + syntax: " @function #?) [returns ]? { }" + }, + "@mixin": { + syntax: " @mixin [ #? ) | ]{}" + }, + "@apply": { + syntax: " @apply [ | ] [ { } ]?;" }, "@left-top": { syntax: " @left-top { }" @@ -11766,6 +11772,11 @@ function* tokenizeSyntax(syntax, position = { [LOC]: { ...position }, }; } +/** + * parse mdn data syntax + * @param syntax + * @returns + */ function parseSyntax(syntax) { const stack = []; let tokens = []; @@ -12309,8 +12320,8 @@ const getSyntaxRule = memoize((group, key) => { } } } - const block = blockStart == -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd)); - const prelude = trimSyntaxArray(blockStart == -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart)); + let block = blockStart == -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd)); + let prelude = trimSyntaxArray(blockStart == -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart)); let propertyDescriptors = null; // @ts-expect-error if (node.descriptors != null) { @@ -12321,7 +12332,24 @@ const getSyntaxRule = memoize((group, key) => { propertyDescriptors[key] = parseSyntax(typeof value === "string" ? value : value.syntax); } } + let optionalBody = false; + if (block == null || block.length === 0) { + let k = prelude.length - 1; + if (ValidationTokenEnum.SemiColon === prelude[k].typ) { + k--; + } + if (k >= 0 && ValidationTokenEnum.Bracket === prelude[k].typ) { + if (prelude[k].chi[0].typ === + ValidationTokenEnum.OpenCurlyBrace && + prelude[k].chi.at(-1).typ === + ValidationTokenEnum.CloseCurlyBrace) { + block = prelude[k].chi.slice(1, -1); + optionalBody = prelude[k].isOptional ?? false; + } + } + } return { + optionalBody, acceptAnyDeclaration: node.syntax.includes(""), acceptAnyRule: node.syntax.includes("") || node.syntax.includes(""), @@ -12409,15 +12437,6 @@ function trimArray(tokens) { } return tokens; } -/** - * is a media feature - * @param featureName - * @returns - */ -// export function isMFName(featureName: string): boolean { -// // @ts-expect-error -// return featureName.startsWith("--") || config.mediaFeatures[featureName.toLowerCase()] != null; -// } /** * * @param featureName @@ -12938,9 +12957,6 @@ function matchSelectorSyntax(stream, errors, options, nested = true) { case exports.EnumToken.NumberTokenType: case exports.EnumToken.LiteralTokenType: case exports.EnumToken.DimensionTokenType: - // if (stack.at(-1)?.typ === EnumToken.CommaTokenType) { - // stack.pop(); - // } if (stack.at(-1)?.typ === exports.EnumToken.PseudoClassFunctionTokenDefType) { break; } @@ -13209,43 +13225,9 @@ function matchSyntax(syntaxes, context, options) { continue; } } - // custom function token - if (token.typ === exports.EnumToken.CustomFunctionTokenDefType) { - if (syntaxes[i].typ != ValidationTokenEnum.PropertyType || - syntaxes[i + 1]?.typ != ValidationTokenEnum.OpenParenthesis || - syntaxes[i + 2]?.typ == null) { - if (!isOptional) { - return { - success: false, - errors: [ - { - action: "drop", - message: "could not match syntax", - node: token, - syntax: syntaxes[i], - }, - ], - syntaxToken: syntaxes[i], - valid: true, - context, - token: null, - }; - } - break; - } - const range = trimArray(context.peekRange()); - context.next(); - i++; - result = matchSyntax([syntaxes[i + 1]], createValidationContext(range.slice(1, -1)), options); - if (result.success) { - context.update(range.at(-1)); - i += 2; - } - return result; - } - if (tokensfuncDefMap.has(token.typ) && - // @ts-ignore - token.typ === exports.EnumToken.WildCardFunctionTokenDefType) { + if ( + // @ts-ignore + token.typ === exports.EnumToken.WildCardFunctionTokenDefType) { const range = trimArray(context.peekRange()); result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, token.val + "()")?.[0]?.chi, createValidationContext(range.slice(1, -1)), options); if (result.success) { @@ -13270,6 +13252,9 @@ function matchSyntax(syntaxes, context, options) { context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return result; } if (syntaxes[i].isList) { @@ -13290,6 +13275,9 @@ function matchSyntax(syntaxes, context, options) { context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return { success: false, valid: true, @@ -13314,6 +13302,9 @@ function matchSyntax(syntaxes, context, options) { context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return { success: false, valid: true, @@ -13718,6 +13709,20 @@ function matchSyntax(syntaxes, context, options) { syntaxToken: syntaxes[i], errors: [], }; + case ValidationTokenEnum.OpenCurlyBrace: + if (token.typ === exports.EnumToken.BlockStartTokenType) { + options.visited.get(token).delete(syntaxes[i]); + context.next(); + break; + } + return { + success: false, + valid: true, + token, + context, + syntaxToken: syntaxes[i], + errors: [], + }; case ValidationTokenEnum.CloseParenthesis: if (token.typ === exports.EnumToken.EndParensTokenType) { options.visited.get(token).delete(syntaxes[i]); @@ -13919,6 +13924,7 @@ function matchProperty(property, context, options) { }; } switch (property.val) { + case "function-parameter": case "declaration-value": { // consume a declaration value @@ -13928,18 +13934,43 @@ function matchProperty(property, context, options) { // if semicolon or end curly brace is encountered, stop let expectToken = true; let token; + const stack = []; token = context.peek(); - while ((token = context.peek()) != null) { - if (token.typ == exports.EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) ; - if (expectToken) { - expectToken = false; - } - if (token.typ == exports.EnumToken.CommaTokenType) { - expectToken = true; + if (token != null) { + success = false; + while ((token = context.peek()) != null) { + if (token.typ == exports.EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) { + stack.push(token); + } + if (expectToken) { + expectToken = false; + } + if (token.typ == exports.EnumToken.CommaTokenType) { + expectToken = true; + } + if (token.typ == exports.EnumToken.EndParensTokenType) { + if (stack.length > 0) { + stack.pop(); + } + else { + expectToken = false; + break; + } + } + context.next(); } - context.next(); } success = !expectToken; + if (success) { + return { + success, + valid: true, + token: null, + context, + syntaxToken: property, + errors: [], + }; + } } break; case "calc-sum": { @@ -13972,9 +14003,6 @@ function matchProperty(property, context, options) { } break; } - // case "at-keyword-token": - // success = context.peek()?.typ === EnumToken.AtRuleTokenType; - // break; case "an+b": { let token = context.peek(); @@ -14039,9 +14067,6 @@ function matchProperty(property, context, options) { Object.assign(token, { typ: exports.EnumToken.Add }); } token = context.peek(2); - // if (token == null) { - // break; - // } if (token?.typ === exports.EnumToken.NumberTokenType && Number.isInteger(token.val) && !token.toString().includes(".") && @@ -14149,9 +14174,6 @@ function matchProperty(property, context, options) { break; case "dimension": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == exports.EnumToken.DimensionTokenType || token?.typ == exports.EnumToken.AngleTokenType || @@ -14166,18 +14188,12 @@ function matchProperty(property, context, options) { case "angle": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ === exports.EnumToken.AngleTokenType; } break; case "angle-percentage": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ === exports.EnumToken.AngleTokenType || token?.typ === exports.EnumToken.PercentageTokenType; } break; @@ -14193,9 +14209,6 @@ function matchProperty(property, context, options) { case "length-percentage": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == exports.EnumToken.PercentageTokenType || token?.typ == exports.EnumToken.LengthTokenType || @@ -14207,9 +14220,6 @@ function matchProperty(property, context, options) { break; case "length": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == exports.EnumToken.LengthTokenType || (token?.typ == exports.EnumToken.NumberTokenType && token?.val === 0); @@ -14224,12 +14234,6 @@ function matchProperty(property, context, options) { success = token.typ == exports.EnumToken.TimeTokenType; } break; - // case "hex-color": { - // const token = context.peek(); - // success = - // token != null && token.typ == EnumToken.ColorTokenType && (token as ColorToken).kin == ColorType.HEX; - // break; - // } case "color": case "color-function": { @@ -14237,13 +14241,6 @@ function matchProperty(property, context, options) { const errors = []; if (tokensfuncDefMap.has(token?.typ)) { const range = context.peekRange(); - // if ( - // range.some( - // (token) => - // token.typ == EnumToken.IdenTokenType && - // "hslrgbchwmyk".split("").some((t) => equalsIgnoreCase(t, (token as IdentToken).val)), - // ) - // ) { const newRange = range.map((t) => cloneNode(t, true)); // @ts-ignore parseTokens(newRange, { parseColor: true }, errors); @@ -14268,27 +14265,6 @@ function matchProperty(property, context, options) { syntaxToken: null, errors, }; - // } - // const result = matchSyntax( - // getParsedSyntax( - // ValidationSyntaxGroupEnum.Syntaxes, - // (token as FunctionToken).val + "()", - // ) as ValidationFunctionToken[], - // createValidationContext(range), - // options, - // ); - // if (result.success) { - // context.update(range.at(-1) as Token); - // return { - // success: true, - // valid: true, - // token: context.peek(), - // context, - // syntaxToken: null, - // errors: [], - // }; - // } - // return result; } // @ts-expect-error else if ("chi" in token) { @@ -14307,10 +14283,6 @@ function matchProperty(property, context, options) { } } break; - // case "url-token": - // success = - // context.peek()?.typ == EnumToken.StringTokenType || context.peek()?.typ == EnumToken.UrlTokenTokenType; - // break; case "type-selector": case "compound-selector": case "complex-selector": @@ -14335,15 +14307,6 @@ function matchProperty(property, context, options) { }; } case "function-token": - // if ( - // context.peek()?.typ == EnumToken.IdenTokenType && - // context.peek(1, false)?.typ == EnumToken.StartParensTokenType - // ) { - // context.next(); - // context.next(); - // success = true; - // break; - // } if (tokensfuncDefMap.has(context.peek()?.typ)) { success = true; context.next(); @@ -14365,17 +14328,6 @@ function matchProperty(property, context, options) { config$3.languages.some((t) => t === token.val || token.val.startsWith(t + "-")); break; } - // case "named-color": { - // const token = context.peek(); - // success = - // token != null && - // token.typ == EnumToken.ColorTokenType && - // ((token as ColorToken).kin == ColorType.LIT || - // (token as ColorToken).kin == ColorType.SYS || - // (token as ColorToken).kin == ColorType.DPSYS || - // (token as ColorToken).kin == ColorType.NON_STD); - // break; - // } case "size-feature": { const range = context.peekRange(); const filteredRange = range.filter((token) => token.typ !== exports.EnumToken.WhitespaceTokenType && token.typ !== exports.EnumToken.CommentTokenType); @@ -14406,20 +14358,6 @@ function matchProperty(property, context, options) { errors: [], }; } - // if (filteredRange.length === 3) { - // success = isValue(filteredRange[2]); - // if (success) { - // context.update(filteredRange[2]); - // return { - // success, - // valid: true, - // token: filteredRange[2], - // context, - // syntaxToken: property, - // errors: [], - // }; - // } - // } break; } case "style-feature": { @@ -14429,16 +14367,6 @@ function matchProperty(property, context, options) { // match const range = context.peekRange(); const filteredRange = range.filter((token) => token.typ !== exports.EnumToken.WhitespaceTokenType && token.typ !== exports.EnumToken.CommentTokenType); - // if (filteredRange.length === 0) { - // return { - // success: false, - // valid: true, - // token: context.peek(), - // context, - // syntaxToken: property, - // errors: [], - // }; - // } if (filteredRange.length === 1) { success = filteredRange[0].typ === exports.EnumToken.IdenTokenType || @@ -14453,31 +14381,6 @@ function matchProperty(property, context, options) { errors: [], }; } - // if ( - // filteredRange[1]?.typ === EnumToken.ColonTokenType || - // filteredRange[1]?.typ === EnumToken.DelimTokenType - // ) { - // const result = isDeclarationValue(filteredRange.slice(2)); - // if (result.success) { - // context.update(filteredRange.at(-1)!); - // return { - // success: true, - // valid: true, - // token: filteredRange.at(-1)!, - // context, - // syntaxToken: property, - // errors: [], - // }; - // } - // return { - // success: false, - // valid: false, - // token: context.peek(), - // context, - // syntaxToken: property, - // errors: result.errors, - // }; - // } return { success: false, valid: false, @@ -14489,10 +14392,6 @@ function matchProperty(property, context, options) { } case "url": { const token = context.peek(); - // if (token == null) { - // success = false; - // break; - // } let l; if (token?.typ == exports.EnumToken.UrlFunctionTokenDefType) { l = 0; @@ -14506,11 +14405,6 @@ function matchProperty(property, context, options) { (token.typ == exports.EnumToken.StringTokenType || token.typ == exports.EnumToken.IdenTokenType || token.typ == exports.EnumToken.UrlTokenTokenType)) { - // if (token.typ === EnumToken.IdenTokenType) { - // while (context.peek(l + 1)?.typ === EnumToken.ClassSelectorTokenType) { - // l++; - // } - // } while ((token = context.peek(++l))) { if (token.typ !== exports.EnumToken.WhitespaceTokenType && token.typ !== exports.EnumToken.CommentTokenType) { break; @@ -14574,10 +14468,46 @@ function matchProperty(property, context, options) { } break; } + case "dashed-function": { + const token = context.peek(); + success = token?.typ == exports.EnumToken.CustomFunctionTokenDefType; + if (success) { + context.next(); + return { + success: true, + valid: true, + token, + context, + syntaxToken: null, + errors: [], + }; + } + break; + } + case "block-contents": + success = context.peek()?.typ == exports.EnumToken.StartParensTokenType; + if (success) { + context.next(); + return { + success: true, + valid: true, + token: null, + context, + syntaxToken: null, + errors: [], + }; + } + return { + success: false, + valid: true, + token, + context, + syntaxToken: property, + errors: [], + }; default: if (!(property.val in config$3.syntaxes)) { console.debug("Unknown syntax", context.peek(), property); - console.debug(context.getRemainingTokens()); throw new Error(`Unexpected validation property ${property.val}`); } return matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, property.val), context, options); @@ -20081,7 +20011,7 @@ class PropertyMap { if (value instanceof PropertyMap) { for (const [k, v] of value.declarations) { if (k == value.config.shorthand) { - for (const [key, val] of Object.entries(value.config.properties)) { + for (const [key] of Object.entries(value.config.properties)) { mapped[key] = cloneNode(v); for (let i = 0; i < v.val.length; i++) { // @ts-ignore @@ -20869,6 +20799,16 @@ class PropertyList { let result; for (const declaration of declarations) { name = declaration.typ != exports.EnumToken.DeclarationNodeType ? null : declaration.nam; + // do not compute shorthand for invalid declarations + if (name == null) { + this.declarations.set(objectHash(declaration), declaration); + continue; + } + // @functiom / result + if (declaration[STATE] & exports.EnumAstNodeStatus.Unknown && equalsIgnoreCase("result", name)) { + this.declarations.set(declaration.nam, declaration); + continue; + } if (declaration[STATE] & (exports.EnumAstNodeStatus.Invalid | exports.EnumAstNodeStatus.Unknown | exports.EnumAstNodeStatus.ValidationFailed) || declaration.typ != exports.EnumToken.DeclarationNodeType || @@ -21106,32 +21046,49 @@ class ComputeShorthandFeature { let l; const rules = []; const declarations = []; - // @ts-ignore - for (; k < j; k++) { - l = k; - // capture comments with the next token - while (l + 1 < j) { - // @ts-ignore - const node = ast.chi[l]; - if (node.typ == exports.EnumToken.CommentNodeType) { - l++; - continue; - } - break; + let doNotSort = false; + if (ast.typ == exports.EnumToken.AtRuleNodeType) { + switch (ast.nam) { + case "function": + case "mixin": + doNotSort = true; + break; } + } + // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example + if (doNotSort) { + for (const node of ast.chi) { + declarations.push(node); + } + } + else { // @ts-ignore - const node = ast.chi[l]; - if (node.typ == exports.EnumToken.DeclarationNodeType) { - for (let m = k; m <= l; m++) { - declarations.push(ast.chi[m]); + for (; k < j; k++) { + l = k; + // capture comments with the next token + while (l + 1 < j) { + // @ts-ignore + const node = ast.chi[l]; + if (node.typ == exports.EnumToken.CommentNodeType) { + l++; + continue; + } + break; } - } - else { - for (let m = k; m <= l; m++) { - rules.push(ast.chi[m]); + // @ts-ignore + const node = ast.chi[l]; + if (node.typ == exports.EnumToken.DeclarationNodeType) { + for (let m = k; m <= l; m++) { + declarations.push(ast.chi[m]); + } } + else { + for (let m = k; m <= l; m++) { + rules.push(ast.chi[m]); + } + } + k = l; } - k = l; } if (declarations.length > 0) { ast.chi.length = 0; @@ -21151,6 +21108,7 @@ const trimTokenSpace = new Set([ exports.EnumToken.LteTokenType, exports.EnumToken.ChildCombinatorTokenType, exports.EnumToken.PipeTokenType, + exports.EnumToken.ColonTokenType ]); const trimSpaceAfter = new Set([ ...tokensfuncSet.values(), @@ -23339,8 +23297,35 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent, previous = ast.chi[--nodeIndex]; } node = ast.chi[i]; - if (node.typ === exports.EnumToken.AtRuleNodeType && node.nam === "font-face") { - continue; + if (node.typ === exports.EnumToken.AtRuleNodeType) { + if (node.nam === "font-face") { + continue; + } + // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example + if (equalsIgnoreCase(node.nam, "function")) { + const chi = node.chi; + let k = chi?.length; + let slice = false; + while (k--) { + if (chi[k].typ === exports.EnumToken.CommentNodeType || + chi[k].typ === exports.EnumToken.WhitespaceTokenType) { + continue; + } + if (chi[k].typ === exports.EnumToken.DeclarationNodeType && + equalsIgnoreCase(chi[k].nam, "result")) { + slice = true; + break; + } + } + if (k > 0 && slice) { + while (k--) { + if (chi[k].typ === exports.EnumToken.AtRuleNodeType) { + chi.splice(k, 1); + continue; + } + } + } + } } if (node.typ === exports.EnumToken.KeyframesAtRuleNodeType) { if (previous?.typ === exports.EnumToken.KeyframesAtRuleNodeType && @@ -25875,7 +25860,7 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro case exports.EnumToken.KeyframesRuleNodeType: case exports.EnumToken.KeyframesAtRuleNodeType: if ([exports.EnumToken.AtRuleNodeType, exports.EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) { - return `${indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`; + return `${(indent.length > 0 ? options.newLine : "") + indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`; } const lineMapLength = linesMap ? linesMap.getLineStarts().length : 0; const prelude = (indent.length > 0 ? options.newLine : "") + @@ -29621,7 +29606,7 @@ function parseMediaqueryList(stream, options) { Object.assign(stream[i], { typ: val === "and" ? exports.EnumToken.AndTokenType : exports.EnumToken.OrTokenType, }); - if (val === "or" && scopes.length <= 1) { + if (val === "or" && (scopes.length == 1 && scopes[0].size > 0 && !scopes[0].has(exports.EnumToken.OrTokenType))) { success = false; errors.push({ action: "drop", @@ -30137,7 +30122,7 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) { break; } if ("and" === val || "or" === val) { - if ("or" === val && scopes.length === 1) { + if ("or" === val && scopes.length === 1 && scopes[0].size > 0) { const fileName = options.source.getFileName() ?? ""; const [line, column] = options.source.getOffsets(stream[i]?.[LOCSTA]); return { @@ -31220,19 +31205,10 @@ function parseVisitors(visitorsDef, errors) { key = visitors[i][0]; value = visitors[i][1]; if (Number.isInteger(+key)) { - // if (Array.isArray(value)) { - // visitors.splice(i + 1, 0, ...Object.entries(value)); - // continue; - // } if (typeof value == "function") { key = value.name; } } - // if (Array.isArray(value)) { - // // @ts-ignore - // visitors.splice(i + 1, 0, ...value.map((item) => [key, item])); - // continue; - // } if (key in exports.EnumToken) { if (typeof value == "function") { if (!valuesHandlers.has(exports.EnumToken[key])) { @@ -31592,7 +31568,7 @@ function doParseSync(tokenizer, options = {}) { if (tokens.length > 0) { node = parseNode(tokens, context, options, errors, stats, invalidNodes); if (node != null) { - if ("chi" in node /* && node.typ != EnumToken.InvalidRuleNodeType */) { + if ("chi" in node) { stack.push(node); context = node; } @@ -32364,14 +32340,9 @@ async function doParse(iter, options = {}) { let parensMatch = 0; let curlyBracketMatch = 0; let tokenizer = iter instanceof Promise ? await iter : iter; - // ast[ROOT] = ast; ast[LOCSRCID] = options.source.id; ast[LOCSTA] = 0; ast[LOCEND] = 0; - // if (Array.isArray(iter)) { - // // @ts-expect-error - // iter = iter[Symbol.iterator]() as Iterator; - // } while (!tokenizer.done()) { tokenizer.next(); if (tokenizer.unit != null) { @@ -32612,6 +32583,11 @@ async function doParse(iter, options = {}) { case exports.EnumToken.AtRuleNodeType: case exports.EnumToken.KeyframesRuleNodeType: case exports.EnumToken.KeyframesAtRuleNodeType: + if (nodes[i][TOKENS] == null) { + if (nodes[i].typ == exports.EnumToken.RuleNodeType) { + nodes[i][TOKENS] = parseString(nodes[i].sel); + } + } for (const token of nodes[i][TOKENS]) { subNodes.push(token); } @@ -33606,7 +33582,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) { else if (context.typ === exports.EnumToken.AtRuleNodeType && "page" === context.nam && pageMarginBoxType.has(atRuleName.toLowerCase())) { - if (parseAsBlock === false) { + if (parseAsBlock === false && syntaxRules.optionalBody === false) { errors.push({ action: "drop", node: atRule, @@ -33645,7 +33621,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) { if (parseAsBlock === null) { parseAsBlock = blockAllowed; } - if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed) { + if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed && !syntaxRules.optionalBody) { errors.push({ action: "drop", node: atRule, @@ -34774,7 +34750,7 @@ function setNodeProperty(node, key, value) { } } -var version = "1.6.8"; +var version = "1.7.0"; /** * Load file or url @@ -35251,6 +35227,7 @@ exports.parseDeclarations = parseDeclarations; exports.parseFile = parseFile; exports.parseString = parseString; exports.parseSync = parseSync; +exports.parseSyntax = parseSyntax; exports.render = render; exports.renderToken = renderValue; exports.replaceNodeOrValue = replaceNodeOrValue; diff --git a/dist/index.d.ts b/dist/index.d.ts index 779062c99..ab7e3d1fd 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -6687,6 +6687,13 @@ declare function setNodeProperty(node: AstNode$1, key: 'errors', value: ErrorDes */ declare function setNodeProperty(node: AstNode$1, key: 'tokens', value: Token$1[] | null): void; +/** + * parse mdn data syntax + * @param syntax + * @returns + */ +declare function parseSyntax(syntax: string): ValidationToken$1[]; + declare let version: string; /** @@ -7076,5 +7083,5 @@ declare function transform(options: ParseInputStreamOptions & TransformOptions): */ declare function transform(options: ParseInputFileOptions & TransformOptions): Promise; -export { ColorType$1 as ColorType, EnumAstNodeStatus$1 as EnumAstNodeStatus, EnumToken, FeatureWalkMode, ModuleCaseTransformEnum, ModuleScopeEnumOptions, ResponseType, SourceMap, ValidationLevel, WalkerEvent, WalkerOptionEnum, cloneNode, convertColor, dirname, expand, find, findAll, findByValue, findLast, getNodeProperty, isOkLabClose, load, minify, okLabDistance, parse, parseDeclarations, parseFile, parseString, parseSync, render, renderValue as renderToken, replaceNodeOrValue, resolve, setNodeProperty, transform, transformFile, transformSync, version, walk, walkValues }; +export { ColorType$1 as ColorType, EnumAstNodeStatus$1 as EnumAstNodeStatus, EnumToken, FeatureWalkMode, ModuleCaseTransformEnum, ModuleScopeEnumOptions, ResponseType, SourceMap, ValidationLevel, WalkerEvent, WalkerOptionEnum, cloneNode, convertColor, dirname, expand, find, findAll, findByValue, findLast, getNodeProperty, isOkLabClose, load, minify, okLabDistance, parse, parseDeclarations, parseFile, parseString, parseSync, parseSyntax, render, renderValue as renderToken, replaceNodeOrValue, resolve, setNodeProperty, transform, transformFile, transformSync, version, walk, walkValues }; export type { AddToken, AndToken, AngleToken, AstAtRule, AstComment, AstDeclaration, AstInvalidAtRule, AstInvalidDeclaration, AstInvalidRule, AstKeyframesAtRule, AstKeyframesRule, AstNode$1 as AstNode, AstNodeStatus, AstRule, AstRuleList, AstStyleSheet, AstValueMatcher, AtRuleToken, AtRuleVisitorHandler, AttrEndToken, AttrStartToken, AttrToken, Background, BackgroundAttachmentMapping, BackgroundPosition, BackgroundPositionClass, BackgroundPositionConstraints, BackgroundPositionMapping, BackgroundProperties, BackgroundRepeat, BackgroundRepeatMapping, BackgroundSize, BackgroundSizeMapping, BadCDOCommentToken, BadCommentToken, BadStringToken, BadUrlToken, BaseToken, BinaryExpressionNode, BinaryExpressionToken, BlockEndToken, BlockStartToken, Border, BorderColor, BorderColorClass, BorderProperties, BorderRadius, CDOCommentToken, ChildCombinatorToken, ClassSelectorToken, ColonToken, ColorToken, ColumnCombinatorToken, CommaToken, CommentToken, ComposesSelectorToken, ConstraintsMapping, ContainMatchToken, ContainerStyleRangeToken, CssTypeTokenType, CssVariableImportTokenType, CssVariableMapTokenType, CssVariableToken, DashMatchToken, DashedIdentToken, DeclarationVisitorHandler, DelimToken, DescendantCombinatorToken, DimensionToken, DivToken, DoubleColonToken, EOFToken, EndMatchToken, EqualMatchToken, ErrorDescription$1 as ErrorDescription, FlexToken, Font, FontFamily, FontProperties, FontWeight, FontWeightConstraints, FontWeightMapping, FractionToken, FrequencyToken, FunctionDefToken, FunctionImageToken, FunctionToken, FunctionURLToken, GenericVisitorAstNodeHandlerMap, GenericVisitorAstNodeSyncHandlerMap, GenericVisitorAsyncResult, GenericVisitorHandler, GenericVisitorResult, GenericVisitorSyncHandler, GenericVisitorSyncResult, GreaterThanOrEqualToken, GreaterThanToken, GridTemplateFuncToken, HashToken, IdentListToken, IdentToken, IfConditionToken, IfElseConditionToken, ImportantToken, IncludeMatchToken, InfinityToken, InvalidAttrToken, InvalidClassSelectorToken, InvalidMediaQueryToken, LengthToken, LessThanOrEqualToken, LessThanToken, LineHeight, ListToken, LiteralToken, LoadResult, Map$1 as Map, MatchExpressionToken, MatchedSelector, MediaFeatureOnlyToken, MediaFeatureToken, MediaQueryConditionToken, MediaQueryUnaryFeatureToken, MediaRangeQueryToken, MinifyFeature, MinifyFeatureOptions, MinifyOptions, ModuleAsyncOptions, ModuleSyncOptions, MulToken, NaNToken, NameSpaceAttributeToken, NegativeInfinityToken, NestingSelectorToken, NextSiblingCombinatorToken, NotToken, NumberToken, OptimizedSelector, OptimizedSelectorToken, OrToken, Outline, OutlineProperties, ParensEndToken, ParensStartToken, ParensToken, ParseInfo$1 as ParseInfo, ParseInputFileOptions, ParseInputOptions, ParseInputStreamOptions, ParseResult, ParseResultStats, ParseSourceOptions, ParseTokenOptions, ParsedVisitorHandlers, ParserOptions, ParserSourceMapOptions, ParserSyncOptions, PercentageToken, Prefix, PropertiesConfig, PropertiesConfigProperties, PropertyListOptions, PropertyMapType, PropertySetType, PropertyType, PseudoClassFunctionToken, PseudoClassToken, PseudoElementToken, PseudoPageToken, PurpleBackgroundAttachment, RawNodeToken, RawSelectorTokens, RenderOptions, RenderResult, ResolutionToken, ResolvedPath, RuleVisitorHandler, SemiColonToken, Separator, ShorthandDef, ShorthandMapType, ShorthandProperties, ShorthandPropertyType, ShorthandType, SinglePropertyType, SinglePropertyTypeMapping, SourceLocation, SourceMapObject, StartMatchToken, StringToken, SubToken, SubsequentCombinatorToken, SupportsQueryConditionToken, SupportsQueryUnaryConditionToken, TimeToken, TimelineFunctionToken, TimingFunctionToken, Token$1 as Token, TokenSearchResult, TokenizeResult, TransformOptions, TransformResult, TransformSyncOptions, UnaryExpression, UnaryExpressionNode, UnclosedStringToken, UniversalSelectorToken, UrlToken, ValidationConfiguration, ValidationMediaFeature, ValidationOptions, ValidationResult, ValidationSelectorOptions, ValidationSyntaxNode, ValidationSyntaxResult, ValidationToken$1 as ValidationToken, Value, ValueVisitorHandler, ValueVisitorSyncHandler, VariableScopeInfo, VisitorNodeMap, VisitorSyncNodeMap, WalkAttributesResult, WalkResult, WalkerFilter, WalkerOption, WalkerOptions, WalkerValueFilter, WhenElseQueryConditionToken, WhenElseUnaryConditionToken, WhitespaceToken, WrappedValuesToken }; diff --git a/dist/package.json.js b/dist/package.json.js index 60ef566f3..4b3105063 100644 --- a/dist/package.json.js +++ b/dist/package.json.js @@ -2,9 +2,9 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ -var version = "1.6.8"; +var version = "1.7.0"; export { version }; diff --git a/dist/src/data/properties.json.js b/dist/src/data/properties.json.js index 60f886b19..ec0755048 100644 --- a/dist/src/data/properties.json.js +++ b/dist/src/data/properties.json.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ var properties = { diff --git a/dist/src/data/validation.json.js b/dist/src/data/validation.json.js index dda1ec13e..1777cbd82 100644 --- a/dist/src/data/validation.json.js +++ b/dist/src/data/validation.json.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ var declarations = { @@ -4712,7 +4712,13 @@ var atRules = { syntax: " @bottom-right-corner { }" }, "@function": { - syntax: " @function (#?) [returns ]? { }" + syntax: " @function #?) [returns ]? { }" + }, + "@mixin": { + syntax: " @mixin [ #? ) | ]{}" + }, + "@apply": { + syntax: " @apply [ | ] [ { } ]?;" }, "@left-top": { syntax: " @left-top { }" diff --git a/dist/src/lib/ast/clone.js b/dist/src/lib/ast/clone.js index 4c43a3b52..45da11734 100644 --- a/dist/src/lib/ast/clone.js +++ b/dist/src/lib/ast/clone.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from './types.js'; diff --git a/dist/src/lib/ast/expand.js b/dist/src/lib/ast/expand.js index 5677f978d..8631060c4 100644 --- a/dist/src/lib/ast/expand.js +++ b/dist/src/lib/ast/expand.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { splitRule } from './minify.js'; diff --git a/dist/src/lib/ast/features/calc.js b/dist/src/lib/ast/features/calc.js index 77b3d79c1..70c5619c0 100644 --- a/dist/src/lib/ast/features/calc.js +++ b/dist/src/lib/ast/features/calc.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../types.js'; diff --git a/dist/src/lib/ast/features/if.js b/dist/src/lib/ast/features/if.js index 90eb588fc..2b16865d2 100644 --- a/dist/src/lib/ast/features/if.js +++ b/dist/src/lib/ast/features/if.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../types.js'; diff --git a/dist/src/lib/ast/features/index.js b/dist/src/lib/ast/features/index.js index e0065d5f7..9a04c5e8b 100644 --- a/dist/src/lib/ast/features/index.js +++ b/dist/src/lib/ast/features/index.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ export { ComputePrefixFeature } from './prefix.js'; diff --git a/dist/src/lib/ast/features/inlinecssvariables.js b/dist/src/lib/ast/features/inlinecssvariables.js index 00265176d..0175c54a3 100644 --- a/dist/src/lib/ast/features/inlinecssvariables.js +++ b/dist/src/lib/ast/features/inlinecssvariables.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../types.js'; diff --git a/dist/src/lib/ast/features/prefix.js b/dist/src/lib/ast/features/prefix.js index 62340357c..fc9ad3062 100644 --- a/dist/src/lib/ast/features/prefix.js +++ b/dist/src/lib/ast/features/prefix.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumAstNodeStatus, EnumToken } from '../types.js'; diff --git a/dist/src/lib/ast/features/shorthand.js b/dist/src/lib/ast/features/shorthand.js index d90b5e1bc..5c9486024 100644 --- a/dist/src/lib/ast/features/shorthand.js +++ b/dist/src/lib/ast/features/shorthand.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { PropertyList } from '../../parser/declaration/list.js'; @@ -37,32 +37,49 @@ class ComputeShorthandFeature { let l; const rules = []; const declarations = []; - // @ts-ignore - for (; k < j; k++) { - l = k; - // capture comments with the next token - while (l + 1 < j) { - // @ts-ignore - const node = ast.chi[l]; - if (node.typ == EnumToken.CommentNodeType) { - l++; - continue; - } - break; + let doNotSort = false; + if (ast.typ == EnumToken.AtRuleNodeType) { + switch (ast.nam) { + case "function": + case "mixin": + doNotSort = true; + break; + } + } + // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example + if (doNotSort) { + for (const node of ast.chi) { + declarations.push(node); } + } + else { // @ts-ignore - const node = ast.chi[l]; - if (node.typ == EnumToken.DeclarationNodeType) { - for (let m = k; m <= l; m++) { - declarations.push(ast.chi[m]); + for (; k < j; k++) { + l = k; + // capture comments with the next token + while (l + 1 < j) { + // @ts-ignore + const node = ast.chi[l]; + if (node.typ == EnumToken.CommentNodeType) { + l++; + continue; + } + break; } - } - else { - for (let m = k; m <= l; m++) { - rules.push(ast.chi[m]); + // @ts-ignore + const node = ast.chi[l]; + if (node.typ == EnumToken.DeclarationNodeType) { + for (let m = k; m <= l; m++) { + declarations.push(ast.chi[m]); + } + } + else { + for (let m = k; m <= l; m++) { + rules.push(ast.chi[m]); + } } + k = l; } - k = l; } if (declarations.length > 0) { ast.chi.length = 0; diff --git a/dist/src/lib/ast/features/transform.js b/dist/src/lib/ast/features/transform.js index ac57ab13b..5125becfa 100644 --- a/dist/src/lib/ast/features/transform.js +++ b/dist/src/lib/ast/features/transform.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken, EnumAstNodeStatus } from '../types.js'; diff --git a/dist/src/lib/ast/features/type.js b/dist/src/lib/ast/features/type.js index 7cfa1ffcf..94e50baf3 100644 --- a/dist/src/lib/ast/features/type.js +++ b/dist/src/lib/ast/features/type.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/ast/find.js b/dist/src/lib/ast/find.js index 12778c0d6..a00fcdb8d 100644 --- a/dist/src/lib/ast/find.js +++ b/dist/src/lib/ast/find.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from './types.js'; diff --git a/dist/src/lib/ast/math/expression.js b/dist/src/lib/ast/math/expression.js index 1ec1c4010..a5f7a7d8a 100644 --- a/dist/src/lib/ast/math/expression.js +++ b/dist/src/lib/ast/math/expression.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { equalsIgnoreCase } from '../../parser/utils/text.js'; diff --git a/dist/src/lib/ast/math/math.js b/dist/src/lib/ast/math/math.js index 6958b5711..384199115 100644 --- a/dist/src/lib/ast/math/math.js +++ b/dist/src/lib/ast/math/math.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { minifyNumber } from '../../syntax/syntax.js'; diff --git a/dist/src/lib/ast/minify.js b/dist/src/lib/ast/minify.js index 96251edf6..900f0442d 100644 --- a/dist/src/lib/ast/minify.js +++ b/dist/src/lib/ast/minify.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { eq } from '../parser/utils/eq.js'; @@ -17,6 +17,7 @@ import { TOKENS, PARENT, OPTIMIZED, RAW, combinators, LOCEND, LOCSTA, LOCSRCID } import { replaceNodeOrValue } from '../parser/utils/token.js'; import { parseString } from '../parser/parse.js'; import { replaceCompound } from './expand.js'; +import { equalsIgnoreCase } from '../parser/utils/text.js'; const notEndingWith = ["(", "["].concat(combinators); const rules = [ @@ -366,8 +367,35 @@ function doMinify(ast, options = {}, recursive = false, errors, nestingContent, previous = ast.chi[--nodeIndex]; } node = ast.chi[i]; - if (node.typ === EnumToken.AtRuleNodeType && node.nam === "font-face") { - continue; + if (node.typ === EnumToken.AtRuleNodeType) { + if (node.nam === "font-face") { + continue; + } + // https://drafts.csswg.org/css-mixins/#:~:text=The%20value%20of%20the%20result%20descriptor%20is%20always%2016px%20in%20the%20above%20example + if (equalsIgnoreCase(node.nam, "function")) { + const chi = node.chi; + let k = chi?.length; + let slice = false; + while (k--) { + if (chi[k].typ === EnumToken.CommentNodeType || + chi[k].typ === EnumToken.WhitespaceTokenType) { + continue; + } + if (chi[k].typ === EnumToken.DeclarationNodeType && + equalsIgnoreCase(chi[k].nam, "result")) { + slice = true; + break; + } + } + if (k > 0 && slice) { + while (k--) { + if (chi[k].typ === EnumToken.AtRuleNodeType) { + chi.splice(k, 1); + continue; + } + } + } + } } if (node.typ === EnumToken.KeyframesAtRuleNodeType) { if (previous?.typ === EnumToken.KeyframesAtRuleNodeType && diff --git a/dist/src/lib/ast/node.js b/dist/src/lib/ast/node.js index 47b2477c2..27b4279a7 100644 --- a/dist/src/lib/ast/node.js +++ b/dist/src/lib/ast/node.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { TOKENS, ERRORS, STATE, LOCSRCID, LOCSTA, LOCEND, PARENT } from '../syntax/constants.js'; diff --git a/dist/src/lib/ast/transform/compute.js b/dist/src/lib/ast/transform/compute.js index 246cc9d69..7d2efe1f8 100644 --- a/dist/src/lib/ast/transform/compute.js +++ b/dist/src/lib/ast/transform/compute.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, multiply, toZero } from './utils.js'; diff --git a/dist/src/lib/ast/transform/matrix.js b/dist/src/lib/ast/transform/matrix.js index d5b2764f7..7018586e2 100644 --- a/dist/src/lib/ast/transform/matrix.js +++ b/dist/src/lib/ast/transform/matrix.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, is2DMatrix } from './utils.js'; diff --git a/dist/src/lib/ast/transform/minify.js b/dist/src/lib/ast/transform/minify.js index d49484231..2ebb85859 100644 --- a/dist/src/lib/ast/transform/minify.js +++ b/dist/src/lib/ast/transform/minify.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, multiply, decompose, round, toZero } from './utils.js'; diff --git a/dist/src/lib/ast/transform/perspective.js b/dist/src/lib/ast/transform/perspective.js index e0ecb65ca..bae839293 100644 --- a/dist/src/lib/ast/transform/perspective.js +++ b/dist/src/lib/ast/transform/perspective.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, multiply } from './utils.js'; diff --git a/dist/src/lib/ast/transform/rotate.js b/dist/src/lib/ast/transform/rotate.js index f3b76405d..8006044fd 100644 --- a/dist/src/lib/ast/transform/rotate.js +++ b/dist/src/lib/ast/transform/rotate.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, multiply } from './utils.js'; diff --git a/dist/src/lib/ast/transform/scale.js b/dist/src/lib/ast/transform/scale.js index ad41c8049..2169e17fe 100644 --- a/dist/src/lib/ast/transform/scale.js +++ b/dist/src/lib/ast/transform/scale.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, multiply } from './utils.js'; diff --git a/dist/src/lib/ast/transform/skew.js b/dist/src/lib/ast/transform/skew.js index 079cf49ae..967830bf4 100644 --- a/dist/src/lib/ast/transform/skew.js +++ b/dist/src/lib/ast/transform/skew.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, multiply } from './utils.js'; diff --git a/dist/src/lib/ast/transform/translate.js b/dist/src/lib/ast/transform/translate.js index 50963647d..051ccd363 100644 --- a/dist/src/lib/ast/transform/translate.js +++ b/dist/src/lib/ast/transform/translate.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { identity, multiply } from './utils.js'; diff --git a/dist/src/lib/ast/transform/utils.js b/dist/src/lib/ast/transform/utils.js index bcec65fe0..74ff33181 100644 --- a/dist/src/lib/ast/transform/utils.js +++ b/dist/src/lib/ast/transform/utils.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { epsilon } from '../../syntax/constants.js'; diff --git a/dist/src/lib/ast/types.js b/dist/src/lib/ast/types.js index 7ebb73a82..b04a80360 100644 --- a/dist/src/lib/ast/types.js +++ b/dist/src/lib/ast/types.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/ast/walk.js b/dist/src/lib/ast/walk.js index 45bc0f54e..151a46373 100644 --- a/dist/src/lib/ast/walk.js +++ b/dist/src/lib/ast/walk.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { PARENT, TOKENS } from '../syntax/constants.js'; diff --git a/dist/src/lib/fs/resolve.js b/dist/src/lib/fs/resolve.js index be4068e3f..c19d711e7 100644 --- a/dist/src/lib/fs/resolve.js +++ b/dist/src/lib/fs/resolve.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { memoize } from '../parser/utils/cache.js'; diff --git a/dist/src/lib/parser/declaration/list.js b/dist/src/lib/parser/declaration/list.js index 30a01767c..eacd48381 100644 --- a/dist/src/lib/parser/declaration/list.js +++ b/dist/src/lib/parser/declaration/list.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { PropertySet } from './set.js'; @@ -33,6 +33,16 @@ class PropertyList { let result; for (const declaration of declarations) { name = declaration.typ != EnumToken.DeclarationNodeType ? null : declaration.nam; + // do not compute shorthand for invalid declarations + if (name == null) { + this.declarations.set(objectHash(declaration), declaration); + continue; + } + // @functiom / result + if (declaration[STATE] & EnumAstNodeStatus.Unknown && equalsIgnoreCase("result", name)) { + this.declarations.set(declaration.nam, declaration); + continue; + } if (declaration[STATE] & (EnumAstNodeStatus.Invalid | EnumAstNodeStatus.Unknown | EnumAstNodeStatus.ValidationFailed) || declaration.typ != EnumToken.DeclarationNodeType || diff --git a/dist/src/lib/parser/declaration/map.js b/dist/src/lib/parser/declaration/map.js index e22a1002d..6649c6883 100644 --- a/dist/src/lib/parser/declaration/map.js +++ b/dist/src/lib/parser/declaration/map.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { eq } from '../utils/eq.js'; @@ -206,7 +206,7 @@ class PropertyMap { if (value instanceof PropertyMap) { for (const [k, v] of value.declarations) { if (k == value.config.shorthand) { - for (const [key, val] of Object.entries(value.config.properties)) { + for (const [key] of Object.entries(value.config.properties)) { mapped[key] = cloneNode(v); for (let i = 0; i < v.val.length; i++) { // @ts-ignore diff --git a/dist/src/lib/parser/declaration/set.js b/dist/src/lib/parser/declaration/set.js index 925fca65c..5adb1c94f 100644 --- a/dist/src/lib/parser/declaration/set.js +++ b/dist/src/lib/parser/declaration/set.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { eq } from '../utils/eq.js'; diff --git a/dist/src/lib/parser/linesmap.js b/dist/src/lib/parser/linesmap.js index e43c9858b..e57a799e4 100644 --- a/dist/src/lib/parser/linesmap.js +++ b/dist/src/lib/parser/linesmap.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/parser/parse.js b/dist/src/lib/parser/parse.js index 701bbdc9f..d5855be16 100644 --- a/dist/src/lib/parser/parse.js +++ b/dist/src/lib/parser/parse.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { isIdentColor, parseColor, isColor } from '../syntax/syntax.js'; @@ -324,19 +324,10 @@ function parseVisitors(visitorsDef, errors) { key = visitors[i][0]; value = visitors[i][1]; if (Number.isInteger(+key)) { - // if (Array.isArray(value)) { - // visitors.splice(i + 1, 0, ...Object.entries(value)); - // continue; - // } if (typeof value == "function") { key = value.name; } } - // if (Array.isArray(value)) { - // // @ts-ignore - // visitors.splice(i + 1, 0, ...value.map((item) => [key, item])); - // continue; - // } if (key in EnumToken) { if (typeof value == "function") { if (!valuesHandlers.has(EnumToken[key])) { @@ -696,7 +687,7 @@ function doParseSync(tokenizer, options = {}) { if (tokens.length > 0) { node = parseNode(tokens, context, options, errors, stats, invalidNodes); if (node != null) { - if ("chi" in node /* && node.typ != EnumToken.InvalidRuleNodeType */) { + if ("chi" in node) { stack.push(node); context = node; } @@ -1468,14 +1459,9 @@ async function doParse(iter, options = {}) { let parensMatch = 0; let curlyBracketMatch = 0; let tokenizer = iter instanceof Promise ? await iter : iter; - // ast[ROOT] = ast; ast[LOCSRCID] = options.source.id; ast[LOCSTA] = 0; ast[LOCEND] = 0; - // if (Array.isArray(iter)) { - // // @ts-expect-error - // iter = iter[Symbol.iterator]() as Iterator; - // } while (!tokenizer.done()) { tokenizer.next(); if (tokenizer.unit != null) { @@ -1716,6 +1702,11 @@ async function doParse(iter, options = {}) { case EnumToken.AtRuleNodeType: case EnumToken.KeyframesRuleNodeType: case EnumToken.KeyframesAtRuleNodeType: + if (nodes[i][TOKENS] == null) { + if (nodes[i].typ == EnumToken.RuleNodeType) { + nodes[i][TOKENS] = parseString(nodes[i].sel); + } + } for (const token of nodes[i][TOKENS]) { subNodes.push(token); } @@ -2710,7 +2701,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) { else if (context.typ === EnumToken.AtRuleNodeType && "page" === context.nam && pageMarginBoxType.has(atRuleName.toLowerCase())) { - if (parseAsBlock === false) { + if (parseAsBlock === false && syntaxRules.optionalBody === false) { errors.push({ action: "drop", node: atRule, @@ -2749,7 +2740,7 @@ function parseAtRule(stream, context, options, errors, parseAsBlock = null) { if (parseAsBlock === null) { parseAsBlock = blockAllowed; } - if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed) { + if (syntax != null && atRule.nam !== "layer" && parseAsBlock !== blockAllowed && !syntaxRules.optionalBody) { errors.push({ action: "drop", node: atRule, diff --git a/dist/src/lib/parser/source.js b/dist/src/lib/parser/source.js index 0ac41f39d..f57180135 100644 --- a/dist/src/lib/parser/source.js +++ b/dist/src/lib/parser/source.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { SourceMap } from '../printer/sourcemap/sourcemap.js'; diff --git a/dist/src/lib/parser/tokenize.js b/dist/src/lib/parser/tokenize.js index 2fa9b2590..7add55acf 100644 --- a/dist/src/lib/parser/tokenize.js +++ b/dist/src/lib/parser/tokenize.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken, ColorType } from '../ast/types.js'; diff --git a/dist/src/lib/parser/utils/angle.js b/dist/src/lib/parser/utils/angle.js index f22b5a5c1..1c79fbfb7 100644 --- a/dist/src/lib/parser/utils/angle.js +++ b/dist/src/lib/parser/utils/angle.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/parser/utils/at-rule-container.js b/dist/src/lib/parser/utils/at-rule-container.js index acb720ba6..845fb11af 100644 --- a/dist/src/lib/parser/utils/at-rule-container.js +++ b/dist/src/lib/parser/utils/at-rule-container.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/parser/utils/at-rule-font-feature-values.js b/dist/src/lib/parser/utils/at-rule-font-feature-values.js index dd16cbc91..293a0d62a 100644 --- a/dist/src/lib/parser/utils/at-rule-font-feature-values.js +++ b/dist/src/lib/parser/utils/at-rule-font-feature-values.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { getSyntaxRule } from '../../validation/config.js'; diff --git a/dist/src/lib/parser/utils/at-rule-generic.js b/dist/src/lib/parser/utils/at-rule-generic.js index 9e43a706a..4b4609087 100644 --- a/dist/src/lib/parser/utils/at-rule-generic.js +++ b/dist/src/lib/parser/utils/at-rule-generic.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/parser/utils/at-rule-import.js b/dist/src/lib/parser/utils/at-rule-import.js index 2585f66ed..8fe57f2f3 100644 --- a/dist/src/lib/parser/utils/at-rule-import.js +++ b/dist/src/lib/parser/utils/at-rule-import.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/parser/utils/at-rule-media.js b/dist/src/lib/parser/utils/at-rule-media.js index d133c9b09..7495e37c9 100644 --- a/dist/src/lib/parser/utils/at-rule-media.js +++ b/dist/src/lib/parser/utils/at-rule-media.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; @@ -115,7 +115,7 @@ function parseMediaqueryList(stream, options) { Object.assign(stream[i], { typ: val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType, }); - if (val === "or" && scopes.length <= 1) { + if (val === "or" && (scopes.length == 1 && scopes[0].size > 0 && !scopes[0].has(EnumToken.OrTokenType))) { success = false; errors.push({ action: "drop", diff --git a/dist/src/lib/parser/utils/at-rule-support.js b/dist/src/lib/parser/utils/at-rule-support.js index 08a0107bc..66d902623 100644 --- a/dist/src/lib/parser/utils/at-rule-support.js +++ b/dist/src/lib/parser/utils/at-rule-support.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; @@ -191,7 +191,7 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) { break; } if ("and" === val || "or" === val) { - if ("or" === val && scopes.length === 1) { + if ("or" === val && scopes.length === 1 && scopes[0].size > 0) { const fileName = options.source.getFileName() ?? ""; const [line, column] = options.source.getOffsets(stream[i]?.[LOCSTA]); return { diff --git a/dist/src/lib/parser/utils/at-rule-when-else.js b/dist/src/lib/parser/utils/at-rule-when-else.js index b8a8674b3..d2cf6cb88 100644 --- a/dist/src/lib/parser/utils/at-rule-when-else.js +++ b/dist/src/lib/parser/utils/at-rule-when-else.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/parser/utils/at-rule.js b/dist/src/lib/parser/utils/at-rule.js index 7e834477c..eba9674aa 100644 --- a/dist/src/lib/parser/utils/at-rule.js +++ b/dist/src/lib/parser/utils/at-rule.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js'; diff --git a/dist/src/lib/parser/utils/base64.js b/dist/src/lib/parser/utils/base64.js index 870c7ef8d..de2db5035 100644 --- a/dist/src/lib/parser/utils/base64.js +++ b/dist/src/lib/parser/utils/base64.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/parser/utils/cache.js b/dist/src/lib/parser/utils/cache.js index 41923d323..e876a713f 100644 --- a/dist/src/lib/parser/utils/cache.js +++ b/dist/src/lib/parser/utils/cache.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/parser/utils/config.js b/dist/src/lib/parser/utils/config.js index c1023ca5f..dfd322f58 100644 --- a/dist/src/lib/parser/utils/config.js +++ b/dist/src/lib/parser/utils/config.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import config from '../../../data/properties.json.js'; diff --git a/dist/src/lib/parser/utils/declaration.js b/dist/src/lib/parser/utils/declaration.js index 934d65e80..8449abd6a 100644 --- a/dist/src/lib/parser/utils/declaration.js +++ b/dist/src/lib/parser/utils/declaration.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken, EnumAstNodeStatus, ColorType } from '../../ast/types.js'; diff --git a/dist/src/lib/parser/utils/eq.js b/dist/src/lib/parser/utils/eq.js index 17d1e2937..691a0fbb8 100644 --- a/dist/src/lib/parser/utils/eq.js +++ b/dist/src/lib/parser/utils/eq.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/parser/utils/hash.js b/dist/src/lib/parser/utils/hash.js index 1c87f8465..7a9d1b7af 100644 --- a/dist/src/lib/parser/utils/hash.js +++ b/dist/src/lib/parser/utils/hash.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ // Alphabet: a-z, A-Z, 0-9, _, - diff --git a/dist/src/lib/parser/utils/selector.js b/dist/src/lib/parser/utils/selector.js index 987ba86b7..14aa7551a 100644 --- a/dist/src/lib/parser/utils/selector.js +++ b/dist/src/lib/parser/utils/selector.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js'; diff --git a/dist/src/lib/parser/utils/text.js b/dist/src/lib/parser/utils/text.js index 13ab0b25c..d286acc74 100644 --- a/dist/src/lib/parser/utils/text.js +++ b/dist/src/lib/parser/utils/text.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/lib/parser/utils/token.js b/dist/src/lib/parser/utils/token.js index ac8e51db0..93091c984 100644 --- a/dist/src/lib/parser/utils/token.js +++ b/dist/src/lib/parser/utils/token.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; @@ -17,6 +17,7 @@ const trimTokenSpace = new Set([ EnumToken.LteTokenType, EnumToken.ChildCombinatorTokenType, EnumToken.PipeTokenType, + EnumToken.ColonTokenType ]); const trimSpaceAfter = new Set([ ...tokensfuncSet.values(), diff --git a/dist/src/lib/parser/utils/type.js b/dist/src/lib/parser/utils/type.js index 6965a3b69..0dcc7cd2e 100644 --- a/dist/src/lib/parser/utils/type.js +++ b/dist/src/lib/parser/utils/type.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/printer/render.js b/dist/src/lib/printer/render.js index c2409d296..733b84bc3 100644 --- a/dist/src/lib/printer/render.js +++ b/dist/src/lib/printer/render.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { convertColor, getAngle } from '../syntax/color/color.js'; @@ -321,7 +321,7 @@ function renderAstNode(data, options, sourcemaps, sourceLocation, linesMap, erro case EnumToken.KeyframesRuleNodeType: case EnumToken.KeyframesAtRuleNodeType: if ([EnumToken.AtRuleNodeType, EnumToken.KeyframesAtRuleNodeType].includes(data.typ) && !("chi" in data)) { - return `${indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`; + return `${(indent.length > 0 ? options.newLine : "") + indent}@${data.nam}${data.val === "" ? "" : options.indent || " "}${data.val};`; } const lineMapLength = linesMap ? linesMap.getLineStarts().length : 0; const prelude = (indent.length > 0 ? options.newLine : "") + diff --git a/dist/src/lib/printer/sourcemap/lib/codec.js b/dist/src/lib/printer/sourcemap/lib/codec.js index 1a0656563..1743f0e4c 100644 --- a/dist/src/lib/printer/sourcemap/lib/codec.js +++ b/dist/src/lib/printer/sourcemap/lib/codec.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ // from https://github.com/Rich-Harris/vlq/tree/master diff --git a/dist/src/lib/printer/sourcemap/sourcemap.js b/dist/src/lib/printer/sourcemap/sourcemap.js index a624fea12..4869b85f9 100644 --- a/dist/src/lib/printer/sourcemap/sourcemap.js +++ b/dist/src/lib/printer/sourcemap/sourcemap.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { toBase64 } from '../../parser/utils/base64.js'; diff --git a/dist/src/lib/syntax/color/a98rgb.js b/dist/src/lib/syntax/color/a98rgb.js index e4fe75c50..56dba4045 100644 --- a/dist/src/lib/syntax/color/a98rgb.js +++ b/dist/src/lib/syntax/color/a98rgb.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { xyz2srgb } from './srgb.js'; diff --git a/dist/src/lib/syntax/color/alpha.js b/dist/src/lib/syntax/color/alpha.js index 018757a09..9bc0fb777 100644 --- a/dist/src/lib/syntax/color/alpha.js +++ b/dist/src/lib/syntax/color/alpha.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { ColorType, EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/cmyk.js b/dist/src/lib/syntax/color/cmyk.js index b7ffc08ad..3dff39a98 100644 --- a/dist/src/lib/syntax/color/cmyk.js +++ b/dist/src/lib/syntax/color/cmyk.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { ColorType, EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/color-mix.js b/dist/src/lib/syntax/color/color-mix.js index d68029c47..a06f2433d 100644 --- a/dist/src/lib/syntax/color/color-mix.js +++ b/dist/src/lib/syntax/color/color-mix.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken, ColorType, EnumAstNodeStatus } from '../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/color.js b/dist/src/lib/syntax/color/color.js index 23701e758..ddbcf66d2 100644 --- a/dist/src/lib/syntax/color/color.js +++ b/dist/src/lib/syntax/color/color.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { ColorType, EnumToken } from '../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/hex.js b/dist/src/lib/syntax/color/hex.js index dd1707fc7..a8a10f7ee 100644 --- a/dist/src/lib/syntax/color/hex.js +++ b/dist/src/lib/syntax/color/hex.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken, ColorType } from '../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/hsl.js b/dist/src/lib/syntax/color/hsl.js index 2e317c5e1..c34b5478d 100644 --- a/dist/src/lib/syntax/color/hsl.js +++ b/dist/src/lib/syntax/color/hsl.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { color2srgbvalues, getNumber } from './color.js'; diff --git a/dist/src/lib/syntax/color/hwb.js b/dist/src/lib/syntax/color/hwb.js index 5669d153c..996fbb5a0 100644 --- a/dist/src/lib/syntax/color/hwb.js +++ b/dist/src/lib/syntax/color/hwb.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { getColorComponents } from './utils/components.js'; diff --git a/dist/src/lib/syntax/color/lab.js b/dist/src/lib/syntax/color/lab.js index fad2c18db..e2ab666ee 100644 --- a/dist/src/lib/syntax/color/lab.js +++ b/dist/src/lib/syntax/color/lab.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { e, k, D50 } from '../constants.js'; diff --git a/dist/src/lib/syntax/color/lch.js b/dist/src/lib/syntax/color/lch.js index 897f17ee9..5508251f0 100644 --- a/dist/src/lib/syntax/color/lch.js +++ b/dist/src/lib/syntax/color/lch.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { getColorComponents } from './utils/components.js'; diff --git a/dist/src/lib/syntax/color/oklab.js b/dist/src/lib/syntax/color/oklab.js index 12ed73c6b..6466b6778 100644 --- a/dist/src/lib/syntax/color/oklab.js +++ b/dist/src/lib/syntax/color/oklab.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { getColorComponents } from './utils/components.js'; diff --git a/dist/src/lib/syntax/color/oklch.js b/dist/src/lib/syntax/color/oklch.js index ed9d491b9..acd8c2708 100644 --- a/dist/src/lib/syntax/color/oklch.js +++ b/dist/src/lib/syntax/color/oklch.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { getColorComponents } from './utils/components.js'; diff --git a/dist/src/lib/syntax/color/p3.js b/dist/src/lib/syntax/color/p3.js index f995b89ce..14e027d05 100644 --- a/dist/src/lib/syntax/color/p3.js +++ b/dist/src/lib/syntax/color/p3.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { lsrgb2srgbvalues, xyz2srgb, srgb2lsrgbvalues } from './srgb.js'; diff --git a/dist/src/lib/syntax/color/prophotorgb.js b/dist/src/lib/syntax/color/prophotorgb.js index 98e57c221..f40d1b1f1 100644 --- a/dist/src/lib/syntax/color/prophotorgb.js +++ b/dist/src/lib/syntax/color/prophotorgb.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { XYZ_D65_to_D50, xyzd502srgb } from './xyzd50.js'; diff --git a/dist/src/lib/syntax/color/rec2020.js b/dist/src/lib/syntax/color/rec2020.js index dde0c4824..6d897a699 100644 --- a/dist/src/lib/syntax/color/rec2020.js +++ b/dist/src/lib/syntax/color/rec2020.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { xyz2srgb } from './srgb.js'; diff --git a/dist/src/lib/syntax/color/relative-color.js b/dist/src/lib/syntax/color/relative-color.js index 81a883974..89dbbf1bc 100644 --- a/dist/src/lib/syntax/color/relative-color.js +++ b/dist/src/lib/syntax/color/relative-color.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { convertColor, getNumber } from './color.js'; diff --git a/dist/src/lib/syntax/color/rgb.js b/dist/src/lib/syntax/color/rgb.js index 49e935b18..e213f2947 100644 --- a/dist/src/lib/syntax/color/rgb.js +++ b/dist/src/lib/syntax/color/rgb.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { color2srgbvalues, minmax } from './color.js'; diff --git a/dist/src/lib/syntax/color/srgb.js b/dist/src/lib/syntax/color/srgb.js index be44568f2..89e2c7725 100644 --- a/dist/src/lib/syntax/color/srgb.js +++ b/dist/src/lib/syntax/color/srgb.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { getColorComponents } from './utils/components.js'; diff --git a/dist/src/lib/syntax/color/utils/color-type.js b/dist/src/lib/syntax/color/utils/color-type.js index 9c79e36cc..1fbb2d7e8 100644 --- a/dist/src/lib/syntax/color/utils/color-type.js +++ b/dist/src/lib/syntax/color/utils/color-type.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { ColorType } from '../../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/utils/colorspace.js b/dist/src/lib/syntax/color/utils/colorspace.js index 863697876..6e81b725a 100644 --- a/dist/src/lib/syntax/color/utils/colorspace.js +++ b/dist/src/lib/syntax/color/utils/colorspace.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { equalsIgnoreCase } from '../../../parser/utils/text.js'; diff --git a/dist/src/lib/syntax/color/utils/components.js b/dist/src/lib/syntax/color/utils/components.js index 9c8b3dc3b..c1de7aead 100644 --- a/dist/src/lib/syntax/color/utils/components.js +++ b/dist/src/lib/syntax/color/utils/components.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { ColorType, EnumToken } from '../../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/utils/distance.js b/dist/src/lib/syntax/color/utils/distance.js index dd345b615..c7fb0064b 100644 --- a/dist/src/lib/syntax/color/utils/distance.js +++ b/dist/src/lib/syntax/color/utils/distance.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { convertColor } from '../color.js'; diff --git a/dist/src/lib/syntax/color/utils/make-color.js b/dist/src/lib/syntax/color/utils/make-color.js index c555264ac..2ff5abe2e 100644 --- a/dist/src/lib/syntax/color/utils/make-color.js +++ b/dist/src/lib/syntax/color/utils/make-color.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken, ColorType } from '../../../ast/types.js'; diff --git a/dist/src/lib/syntax/color/utils/matrix.js b/dist/src/lib/syntax/color/utils/matrix.js index ffa8f1709..a48ed71a5 100644 --- a/dist/src/lib/syntax/color/utils/matrix.js +++ b/dist/src/lib/syntax/color/utils/matrix.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ // from https://www.w3.org/TR/css-color-4/multiply-matrices.js diff --git a/dist/src/lib/syntax/color/xyz.js b/dist/src/lib/syntax/color/xyz.js index 6ac1eda87..600cfbc1c 100644 --- a/dist/src/lib/syntax/color/xyz.js +++ b/dist/src/lib/syntax/color/xyz.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { multiplyMatrices } from './utils/matrix.js'; diff --git a/dist/src/lib/syntax/color/xyzd50.js b/dist/src/lib/syntax/color/xyzd50.js index fbd8973d6..c8acfc162 100644 --- a/dist/src/lib/syntax/color/xyzd50.js +++ b/dist/src/lib/syntax/color/xyzd50.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { lsrgb2srgbvalues } from './srgb.js'; diff --git a/dist/src/lib/syntax/constants.js b/dist/src/lib/syntax/constants.js index bb5adb3cf..b8dc2fb8d 100644 --- a/dist/src/lib/syntax/constants.js +++ b/dist/src/lib/syntax/constants.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../ast/types.js'; diff --git a/dist/src/lib/syntax/syntax.js b/dist/src/lib/syntax/syntax.js index 849c7d8c3..fbeb6e4b2 100644 --- a/dist/src/lib/syntax/syntax.js +++ b/dist/src/lib/syntax/syntax.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { isOkLabClose } from './color/utils/distance.js'; diff --git a/dist/src/lib/validation/config.js b/dist/src/lib/validation/config.js index 1bf33b659..76783c928 100644 --- a/dist/src/lib/validation/config.js +++ b/dist/src/lib/validation/config.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import config from '../../data/validation.json.js'; @@ -77,8 +77,8 @@ const getSyntaxRule = memoize((group, key) => { } } } - const block = blockStart == -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd)); - const prelude = trimSyntaxArray(blockStart == -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart)); + let block = blockStart == -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd)); + let prelude = trimSyntaxArray(blockStart == -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart)); let propertyDescriptors = null; // @ts-expect-error if (node.descriptors != null) { @@ -89,7 +89,24 @@ const getSyntaxRule = memoize((group, key) => { propertyDescriptors[key] = parseSyntax(typeof value === "string" ? value : value.syntax); } } + let optionalBody = false; + if (block == null || block.length === 0) { + let k = prelude.length - 1; + if (ValidationTokenEnum.SemiColon === prelude[k].typ) { + k--; + } + if (k >= 0 && ValidationTokenEnum.Bracket === prelude[k].typ) { + if (prelude[k].chi[0].typ === + ValidationTokenEnum.OpenCurlyBrace && + prelude[k].chi.at(-1).typ === + ValidationTokenEnum.CloseCurlyBrace) { + block = prelude[k].chi.slice(1, -1); + optionalBody = prelude[k].isOptional ?? false; + } + } + } return { + optionalBody, acceptAnyDeclaration: node.syntax.includes(""), acceptAnyRule: node.syntax.includes("") || node.syntax.includes(""), diff --git a/dist/src/lib/validation/match.js b/dist/src/lib/validation/match.js index 69cff96ea..ac453b185 100644 --- a/dist/src/lib/validation/match.js +++ b/dist/src/lib/validation/match.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { getParsedSyntax, getSyntaxConfig } from './config.js'; @@ -50,15 +50,6 @@ function trimArray(tokens) { } return tokens; } -/** - * is a media feature - * @param featureName - * @returns - */ -// export function isMFName(featureName: string): boolean { -// // @ts-expect-error -// return featureName.startsWith("--") || config.mediaFeatures[featureName.toLowerCase()] != null; -// } /** * * @param featureName @@ -579,9 +570,6 @@ function matchSelectorSyntax(stream, errors, options, nested = true) { case EnumToken.NumberTokenType: case EnumToken.LiteralTokenType: case EnumToken.DimensionTokenType: - // if (stack.at(-1)?.typ === EnumToken.CommaTokenType) { - // stack.pop(); - // } if (stack.at(-1)?.typ === EnumToken.PseudoClassFunctionTokenDefType) { break; } @@ -850,43 +838,9 @@ function matchSyntax(syntaxes, context, options) { continue; } } - // custom function token - if (token.typ === EnumToken.CustomFunctionTokenDefType) { - if (syntaxes[i].typ != ValidationTokenEnum.PropertyType || - syntaxes[i + 1]?.typ != ValidationTokenEnum.OpenParenthesis || - syntaxes[i + 2]?.typ == null) { - if (!isOptional) { - return { - success: false, - errors: [ - { - action: "drop", - message: "could not match syntax", - node: token, - syntax: syntaxes[i], - }, - ], - syntaxToken: syntaxes[i], - valid: true, - context, - token: null, - }; - } - break; - } - const range = trimArray(context.peekRange()); - context.next(); - i++; - result = matchSyntax([syntaxes[i + 1]], createValidationContext(range.slice(1, -1)), options); - if (result.success) { - context.update(range.at(-1)); - i += 2; - } - return result; - } - if (tokensfuncDefMap.has(token.typ) && - // @ts-ignore - token.typ === EnumToken.WildCardFunctionTokenDefType) { + if ( + // @ts-ignore + token.typ === EnumToken.WildCardFunctionTokenDefType) { const range = trimArray(context.peekRange()); result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, token.val + "()")?.[0]?.chi, createValidationContext(range.slice(1, -1)), options); if (result.success) { @@ -911,6 +865,9 @@ function matchSyntax(syntaxes, context, options) { context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return result; } if (syntaxes[i].isList) { @@ -931,6 +888,9 @@ function matchSyntax(syntaxes, context, options) { context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return { success: false, valid: true, @@ -955,6 +915,9 @@ function matchSyntax(syntaxes, context, options) { context.update(result.context.current()); continue; } + if (isOptional) { + continue; + } return { success: false, valid: true, @@ -1359,6 +1322,20 @@ function matchSyntax(syntaxes, context, options) { syntaxToken: syntaxes[i], errors: [], }; + case ValidationTokenEnum.OpenCurlyBrace: + if (token.typ === EnumToken.BlockStartTokenType) { + options.visited.get(token).delete(syntaxes[i]); + context.next(); + break; + } + return { + success: false, + valid: true, + token, + context, + syntaxToken: syntaxes[i], + errors: [], + }; case ValidationTokenEnum.CloseParenthesis: if (token.typ === EnumToken.EndParensTokenType) { options.visited.get(token).delete(syntaxes[i]); @@ -1560,6 +1537,7 @@ function matchProperty(property, context, options) { }; } switch (property.val) { + case "function-parameter": case "declaration-value": { // consume a declaration value @@ -1569,18 +1547,43 @@ function matchProperty(property, context, options) { // if semicolon or end curly brace is encountered, stop let expectToken = true; let token; + const stack = []; token = context.peek(); - while ((token = context.peek()) != null) { - if (token.typ == EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) ; - if (expectToken) { - expectToken = false; - } - if (token.typ == EnumToken.CommaTokenType) { - expectToken = true; + if (token != null) { + success = false; + while ((token = context.peek()) != null) { + if (token.typ == EnumToken.StartParensTokenType || tokensfuncDefMap.has(token?.typ)) { + stack.push(token); + } + if (expectToken) { + expectToken = false; + } + if (token.typ == EnumToken.CommaTokenType) { + expectToken = true; + } + if (token.typ == EnumToken.EndParensTokenType) { + if (stack.length > 0) { + stack.pop(); + } + else { + expectToken = false; + break; + } + } + context.next(); } - context.next(); } success = !expectToken; + if (success) { + return { + success, + valid: true, + token: null, + context, + syntaxToken: property, + errors: [], + }; + } } break; case "calc-sum": { @@ -1613,9 +1616,6 @@ function matchProperty(property, context, options) { } break; } - // case "at-keyword-token": - // success = context.peek()?.typ === EnumToken.AtRuleTokenType; - // break; case "an+b": { let token = context.peek(); @@ -1680,9 +1680,6 @@ function matchProperty(property, context, options) { Object.assign(token, { typ: EnumToken.Add }); } token = context.peek(2); - // if (token == null) { - // break; - // } if (token?.typ === EnumToken.NumberTokenType && Number.isInteger(token.val) && !token.toString().includes(".") && @@ -1790,9 +1787,6 @@ function matchProperty(property, context, options) { break; case "dimension": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == EnumToken.DimensionTokenType || token?.typ == EnumToken.AngleTokenType || @@ -1807,18 +1801,12 @@ function matchProperty(property, context, options) { case "angle": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ === EnumToken.AngleTokenType; } break; case "angle-percentage": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ === EnumToken.AngleTokenType || token?.typ === EnumToken.PercentageTokenType; } break; @@ -1834,9 +1822,6 @@ function matchProperty(property, context, options) { case "length-percentage": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == EnumToken.PercentageTokenType || token?.typ == EnumToken.LengthTokenType || @@ -1848,9 +1833,6 @@ function matchProperty(property, context, options) { break; case "length": { const token = context.peek(); - // if (token == null) { - // break; - // } success = token?.typ == EnumToken.LengthTokenType || (token?.typ == EnumToken.NumberTokenType && token?.val === 0); @@ -1865,12 +1847,6 @@ function matchProperty(property, context, options) { success = token.typ == EnumToken.TimeTokenType; } break; - // case "hex-color": { - // const token = context.peek(); - // success = - // token != null && token.typ == EnumToken.ColorTokenType && (token as ColorToken).kin == ColorType.HEX; - // break; - // } case "color": case "color-function": { @@ -1878,13 +1854,6 @@ function matchProperty(property, context, options) { const errors = []; if (tokensfuncDefMap.has(token?.typ)) { const range = context.peekRange(); - // if ( - // range.some( - // (token) => - // token.typ == EnumToken.IdenTokenType && - // "hslrgbchwmyk".split("").some((t) => equalsIgnoreCase(t, (token as IdentToken).val)), - // ) - // ) { const newRange = range.map((t) => cloneNode(t, true)); // @ts-ignore parseTokens(newRange, { parseColor: true }, errors); @@ -1909,27 +1878,6 @@ function matchProperty(property, context, options) { syntaxToken: null, errors, }; - // } - // const result = matchSyntax( - // getParsedSyntax( - // ValidationSyntaxGroupEnum.Syntaxes, - // (token as FunctionToken).val + "()", - // ) as ValidationFunctionToken[], - // createValidationContext(range), - // options, - // ); - // if (result.success) { - // context.update(range.at(-1) as Token); - // return { - // success: true, - // valid: true, - // token: context.peek(), - // context, - // syntaxToken: null, - // errors: [], - // }; - // } - // return result; } // @ts-expect-error else if ("chi" in token) { @@ -1948,10 +1896,6 @@ function matchProperty(property, context, options) { } } break; - // case "url-token": - // success = - // context.peek()?.typ == EnumToken.StringTokenType || context.peek()?.typ == EnumToken.UrlTokenTokenType; - // break; case "type-selector": case "compound-selector": case "complex-selector": @@ -1976,15 +1920,6 @@ function matchProperty(property, context, options) { }; } case "function-token": - // if ( - // context.peek()?.typ == EnumToken.IdenTokenType && - // context.peek(1, false)?.typ == EnumToken.StartParensTokenType - // ) { - // context.next(); - // context.next(); - // success = true; - // break; - // } if (tokensfuncDefMap.has(context.peek()?.typ)) { success = true; context.next(); @@ -2006,17 +1941,6 @@ function matchProperty(property, context, options) { config.languages.some((t) => t === token.val || token.val.startsWith(t + "-")); break; } - // case "named-color": { - // const token = context.peek(); - // success = - // token != null && - // token.typ == EnumToken.ColorTokenType && - // ((token as ColorToken).kin == ColorType.LIT || - // (token as ColorToken).kin == ColorType.SYS || - // (token as ColorToken).kin == ColorType.DPSYS || - // (token as ColorToken).kin == ColorType.NON_STD); - // break; - // } case "size-feature": { const range = context.peekRange(); const filteredRange = range.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType); @@ -2047,20 +1971,6 @@ function matchProperty(property, context, options) { errors: [], }; } - // if (filteredRange.length === 3) { - // success = isValue(filteredRange[2]); - // if (success) { - // context.update(filteredRange[2]); - // return { - // success, - // valid: true, - // token: filteredRange[2], - // context, - // syntaxToken: property, - // errors: [], - // }; - // } - // } break; } case "style-feature": { @@ -2070,16 +1980,6 @@ function matchProperty(property, context, options) { // match const range = context.peekRange(); const filteredRange = range.filter((token) => token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType); - // if (filteredRange.length === 0) { - // return { - // success: false, - // valid: true, - // token: context.peek(), - // context, - // syntaxToken: property, - // errors: [], - // }; - // } if (filteredRange.length === 1) { success = filteredRange[0].typ === EnumToken.IdenTokenType || @@ -2094,31 +1994,6 @@ function matchProperty(property, context, options) { errors: [], }; } - // if ( - // filteredRange[1]?.typ === EnumToken.ColonTokenType || - // filteredRange[1]?.typ === EnumToken.DelimTokenType - // ) { - // const result = isDeclarationValue(filteredRange.slice(2)); - // if (result.success) { - // context.update(filteredRange.at(-1)!); - // return { - // success: true, - // valid: true, - // token: filteredRange.at(-1)!, - // context, - // syntaxToken: property, - // errors: [], - // }; - // } - // return { - // success: false, - // valid: false, - // token: context.peek(), - // context, - // syntaxToken: property, - // errors: result.errors, - // }; - // } return { success: false, valid: false, @@ -2130,10 +2005,6 @@ function matchProperty(property, context, options) { } case "url": { const token = context.peek(); - // if (token == null) { - // success = false; - // break; - // } let l; if (token?.typ == EnumToken.UrlFunctionTokenDefType) { l = 0; @@ -2147,11 +2018,6 @@ function matchProperty(property, context, options) { (token.typ == EnumToken.StringTokenType || token.typ == EnumToken.IdenTokenType || token.typ == EnumToken.UrlTokenTokenType)) { - // if (token.typ === EnumToken.IdenTokenType) { - // while (context.peek(l + 1)?.typ === EnumToken.ClassSelectorTokenType) { - // l++; - // } - // } while ((token = context.peek(++l))) { if (token.typ !== EnumToken.WhitespaceTokenType && token.typ !== EnumToken.CommentTokenType) { break; @@ -2215,10 +2081,46 @@ function matchProperty(property, context, options) { } break; } + case "dashed-function": { + const token = context.peek(); + success = token?.typ == EnumToken.CustomFunctionTokenDefType; + if (success) { + context.next(); + return { + success: true, + valid: true, + token, + context, + syntaxToken: null, + errors: [], + }; + } + break; + } + case "block-contents": + success = context.peek()?.typ == EnumToken.StartParensTokenType; + if (success) { + context.next(); + return { + success: true, + valid: true, + token: null, + context, + syntaxToken: null, + errors: [], + }; + } + return { + success: false, + valid: true, + token, + context, + syntaxToken: property, + errors: [], + }; default: if (!(property.val in config.syntaxes)) { console.debug("Unknown syntax", context.peek(), property); - console.debug(context.getRemainingTokens()); throw new Error(`Unexpected validation property ${property.val}`); } return matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, property.val), context, options); diff --git a/dist/src/lib/validation/parser/parse.js b/dist/src/lib/validation/parser/parse.js index 615c19ce0..780ec6bac 100644 --- a/dist/src/lib/validation/parser/parse.js +++ b/dist/src/lib/validation/parser/parse.js @@ -2,13 +2,13 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; -import { isIdentStart, isIdent, parseDimension, isPseudo } from '../../syntax/syntax.js'; -import { ValidationTokenEnum } from './typedef.js'; import { LOC } from '../../syntax/constants.js'; +import { ValidationTokenEnum } from './typedef.js'; +import { isIdentStart, isIdent, parseDimension, isPseudo } from '../../syntax/syntax.js'; const SymbolsMapTokens = { "&&": ValidationTokenEnum.Ampersand, @@ -264,6 +264,11 @@ function* tokenizeSyntax(syntax, position = { [LOC]: { ...position }, }; } +/** + * parse mdn data syntax + * @param syntax + * @returns + */ function parseSyntax(syntax) { const stack = []; let tokens = []; diff --git a/dist/src/lib/validation/parser/typedef.js b/dist/src/lib/validation/parser/typedef.js index 4ddbac08d..e6af6e00b 100644 --- a/dist/src/lib/validation/parser/typedef.js +++ b/dist/src/lib/validation/parser/typedef.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ var ValidationTokenEnum; diff --git a/dist/src/lib/validation/utils/list.js b/dist/src/lib/validation/utils/list.js index 487ea934a..f225a2e14 100644 --- a/dist/src/lib/validation/utils/list.js +++ b/dist/src/lib/validation/utils/list.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../../ast/types.js'; diff --git a/dist/src/node.js b/dist/src/node.js index d54e607ac..13eba51b4 100644 --- a/dist/src/node.js +++ b/dist/src/node.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { deprecate } from 'node:util'; @@ -33,6 +33,7 @@ export { replaceNodeOrValue } from './lib/parser/utils/token.js'; export { SourceMap } from './lib/printer/sourcemap/sourcemap.js'; export { FeatureWalkMode } from './lib/ast/features/type.js'; export { getNodeProperty, setNodeProperty } from './lib/ast/node.js'; +export { parseSyntax } from './lib/validation/parser/parse.js'; export { version } from '../package.json.js'; /** diff --git a/dist/src/types.js b/dist/src/types.js index c725cb63a..aac57e18a 100644 --- a/dist/src/types.js +++ b/dist/src/types.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ /** diff --git a/dist/src/utils/sync.js b/dist/src/utils/sync.js index c6cfde1f6..866c83b87 100644 --- a/dist/src/utils/sync.js +++ b/dist/src/utils/sync.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { EnumToken } from '../lib/ast/types.js'; diff --git a/dist/src/web.js b/dist/src/web.js index c2c9e84b1..d6b429219 100644 --- a/dist/src/web.js +++ b/dist/src/web.js @@ -2,7 +2,7 @@ CSS parser, minifier and validator for node and the browser Author(s): Thierry Bela - Version: 1.6.8 + Version: 1.7.0 License: MIT */ import { doParse, doParseSync } from './lib/parser/parse.js'; @@ -28,6 +28,7 @@ export { PropertyList } from './lib/parser/declaration/list.js'; export { SourceMap } from './lib/printer/sourcemap/sourcemap.js'; export { FeatureWalkMode } from './lib/ast/features/type.js'; export { getNodeProperty, setNodeProperty } from './lib/ast/node.js'; +export { parseSyntax } from './lib/validation/parser/parse.js'; export { version } from '../package.json.js'; /** diff --git a/jsr.json b/jsr.json index 6f2902941..f7bc3024b 100644 --- a/jsr.json +++ b/jsr.json @@ -1,6 +1,6 @@ { "name": "@tbela99/css-parser", - "version": "1.6.9", + "version": "1.7.0", "publish": { "include": [ "src", @@ -23,6 +23,7 @@ "docs", "llms.txt", "package-lock.json", + "CONTRIBUTING.md", "ROADMAP.md", "rollup.config.js", "scripts", diff --git a/package.json b/package.json index faf680ff9..fb24690e3 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.9", + "version": "1.7.0", "exports": { ".": "./dist/src/node.js", "./node": "./dist/src/node.js", @@ -64,12 +64,13 @@ "@rollup/plugin-commonjs": "^29.0.3", "@rollup/plugin-json": "^6.1.0", "@rollup/plugin-node-resolve": "^16.0.3", - "@rollup/plugin-typescript": "^12.3.0", + "@rollup/plugin-typescript": "^12.3.1", "@types/chai": "^5.2.3", "@types/chai-as-promised": "^8.0.2", "@types/mocha": "^10.0.10", "@types/node": "^26.6.5", - "@types/web": "^0.0.358", + "@types/web": "^0.0.359", + "@typescript/typescript6": "^6.0.2", "@web/test-runner": "^1.0.0", "@web/test-runner-playwright": "^1.0.0", "chai": "^6.3.0", @@ -77,8 +78,8 @@ "esno": "^4.8.0", "markdown-it-task-lists": "^2.1.1", "mocha": "^12.0.3", - "playwright": "^1.64.0", - "rollup": "^4.63.6", + "playwright": "^1.64.1", + "rollup": "^4.65.0", "rollup-plugin-dts": "^6.5.1", "rollup-plugin-version-injector": "^1.3.3", "superc8": "^12.7.0", diff --git a/scripts/local-patch.json b/scripts/local-patch.json index 382854a11..d1348a723 100644 --- a/scripts/local-patch.json +++ b/scripts/local-patch.json @@ -79,7 +79,13 @@ "syntax": " @bottom-right-corner { }" }, "@function": { - "syntax": " @function (#?) [returns ]? { }" + "syntax": " @function #?) [returns ]? { }" + }, + "@mixin": { + "syntax": " @mixin [ #? ) | ]{}" + }, + "@apply": { + "syntax": " @apply [ | ] [ { } ]?;" }, "@left-top": { "syntax": " @left-top { }" diff --git a/src/data/validation.json b/src/data/validation.json index 0fe14b61e..62765968a 100644 --- a/src/data/validation.json +++ b/src/data/validation.json @@ -1 +1 @@ -{"declarations":{"-ms-accelerator":{"syntax":"false | true"},"-ms-block-progression":{"syntax":"tb | rl | bt | lr"},"-ms-content-zoom-chaining":{"syntax":"none | chained"},"-ms-content-zoom-limit":{"syntax":"<'-ms-content-zoom-limit-min'> <'-ms-content-zoom-limit-max'>"},"-ms-content-zoom-limit-max":{"syntax":""},"-ms-content-zoom-limit-min":{"syntax":""},"-ms-content-zoom-snap":{"syntax":"<'-ms-content-zoom-snap-type'> || <'-ms-content-zoom-snap-points'>"},"-ms-content-zoom-snap-points":{"syntax":"snapInterval( , ) | snapList( # )"},"-ms-content-zoom-snap-type":{"syntax":"none | proximity | mandatory"},"-ms-content-zooming":{"syntax":"none | zoom"},"-ms-filter":{"syntax":""},"-ms-flow-from":{"syntax":"[ none | ]#"},"-ms-flow-into":{"syntax":"[ none | ]#"},"-ms-grid-columns":{"syntax":"none | | "},"-ms-grid-rows":{"syntax":"none | | "},"-ms-high-contrast-adjust":{"syntax":"auto | none"},"-ms-hyphenate-limit-chars":{"syntax":"auto | {1,3}"},"-ms-hyphenate-limit-lines":{"syntax":"no-limit | "},"-ms-hyphenate-limit-zone":{"syntax":" | "},"-ms-ime-align":{"syntax":"auto | after"},"-ms-overflow-style":{"syntax":" auto | none | scrollbar | -ms-autohiding-scrollbar"},"-ms-scroll-chaining":{"syntax":"chained | none"},"-ms-scroll-limit":{"syntax":"<'-ms-scroll-limit-x-min'> <'-ms-scroll-limit-y-min'> <'-ms-scroll-limit-x-max'> <'-ms-scroll-limit-y-max'>"},"-ms-scroll-limit-x-max":{"syntax":"auto | "},"-ms-scroll-limit-x-min":{"syntax":""},"-ms-scroll-limit-y-max":{"syntax":"auto | "},"-ms-scroll-limit-y-min":{"syntax":""},"-ms-scroll-rails":{"syntax":"none | railed"},"-ms-scroll-snap-points-x":{"syntax":"snapInterval( , ) | snapList( # )"},"-ms-scroll-snap-points-y":{"syntax":"snapInterval( , ) | snapList( # )"},"-ms-scroll-snap-type":{"syntax":"none | proximity | mandatory"},"-ms-scroll-snap-x":{"syntax":"<'-ms-scroll-snap-type'> <'-ms-scroll-snap-points-x'>"},"-ms-scroll-snap-y":{"syntax":"<'-ms-scroll-snap-type'> <'-ms-scroll-snap-points-y'>"},"-ms-scroll-translation":{"syntax":"none | vertical-to-horizontal"},"-ms-scrollbar-3dlight-color":{"syntax":""},"-ms-scrollbar-arrow-color":{"syntax":""},"-ms-scrollbar-base-color":{"syntax":""},"-ms-scrollbar-darkshadow-color":{"syntax":""},"-ms-scrollbar-face-color":{"syntax":""},"-ms-scrollbar-highlight-color":{"syntax":""},"-ms-scrollbar-shadow-color":{"syntax":""},"-ms-scrollbar-track-color":{"syntax":""},"-ms-text-autospace":{"syntax":"none | ideograph-alpha | ideograph-numeric | ideograph-parenthesis | ideograph-space"},"-ms-touch-select":{"syntax":"grippers | none"},"-ms-user-select":{"syntax":"none | element | text"},"-ms-wrap-flow":{"syntax":"auto | both | start | end | maximum | clear"},"-ms-wrap-margin":{"syntax":""},"-ms-wrap-through":{"syntax":"wrap | none"},"-moz-appearance":{"syntax":"none | button | button-arrow-down | button-arrow-next | button-arrow-previous | button-arrow-up | button-bevel | button-focus | caret | checkbox | checkbox-container | checkbox-label | checkmenuitem | dualbutton | groupbox | listbox | listitem | menuarrow | menubar | menucheckbox | menuimage | menuitem | menuitemtext | menulist | menulist-button | menulist-text | menulist-textfield | menupopup | menuradio | menuseparator | meterbar | meterchunk | progressbar | progressbar-vertical | progresschunk | progresschunk-vertical | radio | radio-container | radio-label | radiomenuitem | range | range-thumb | resizer | resizerpanel | scale-horizontal | scalethumbend | scalethumb-horizontal | scalethumbstart | scalethumbtick | scalethumb-vertical | scale-vertical | scrollbarbutton-down | scrollbarbutton-left | scrollbarbutton-right | scrollbarbutton-up | scrollbarthumb-horizontal | scrollbarthumb-vertical | scrollbartrack-horizontal | scrollbartrack-vertical | searchfield | separator | sheet | spinner | spinner-downbutton | spinner-textfield | spinner-upbutton | splitter | statusbar | statusbarpanel | tab | tabpanel | tabpanels | tab-scroll-arrow-back | tab-scroll-arrow-forward | textfield | textfield-multiline | toolbar | toolbarbutton | toolbarbutton-dropdown | toolbargripper | toolbox | tooltip | treeheader | treeheadercell | treeheadersortarrow | treeitem | treeline | treetwisty | treetwistyopen | treeview | -moz-mac-unified-toolbar | -moz-win-borderless-glass | -moz-win-browsertabbar-toolbox | -moz-win-communicationstext | -moz-win-communications-toolbox | -moz-win-exclude-glass | -moz-win-glass | -moz-win-mediatext | -moz-win-media-toolbox | -moz-window-button-box | -moz-window-button-box-maximized | -moz-window-button-close | -moz-window-button-maximize | -moz-window-button-minimize | -moz-window-button-restore | -moz-window-frame-bottom | -moz-window-frame-left | -moz-window-frame-right | -moz-window-titlebar | -moz-window-titlebar-maximized"},"-moz-binding":{"syntax":" | none"},"-moz-border-bottom-colors":{"syntax":"+ | none"},"-moz-border-left-colors":{"syntax":"+ | none"},"-moz-border-right-colors":{"syntax":"+ | none"},"-moz-border-top-colors":{"syntax":"+ | none"},"-moz-context-properties":{"syntax":"none | [ fill | fill-opacity | stroke | stroke-opacity ]#"},"-moz-float-edge":{"syntax":"border-box | content-box | margin-box | padding-box"},"-moz-force-broken-image-icon":{"syntax":"0 | 1"},"-moz-orient":{"syntax":"inline | block | horizontal | vertical"},"-moz-outline-radius":{"syntax":"{1,4} [ / {1,4} ]?"},"-moz-outline-radius-bottomleft":{"syntax":""},"-moz-outline-radius-bottomright":{"syntax":""},"-moz-outline-radius-topleft":{"syntax":""},"-moz-outline-radius-topright":{"syntax":""},"-moz-stack-sizing":{"syntax":"ignore | stretch-to-fit"},"-moz-text-blink":{"syntax":"none | blink"},"-moz-user-focus":{"syntax":"ignore | normal | select-after | select-before | select-menu | select-same | select-all | none"},"-moz-user-input":{"syntax":"auto | none | enabled | disabled"},"-moz-user-modify":{"syntax":"read-only | read-write | write-only"},"-moz-window-dragging":{"syntax":"drag | no-drag"},"-moz-window-shadow":{"syntax":"default | menu | tooltip | sheet | none"},"-webkit-appearance":{"syntax":" none | button | button-bevel | caps-lock-indicator | caret | checkbox | default-button | inner-spin-button | listbox | listitem | media-controls-background | media-controls-fullscreen-background | media-current-time-display | media-enter-fullscreen-button | media-exit-fullscreen-button | media-fullscreen-button | media-mute-button | media-overlay-play-button | media-play-button | media-seek-back-button | media-seek-forward-button | media-slider | media-sliderthumb | media-time-remaining-display | media-toggle-closed-captions-button | media-volume-slider | media-volume-slider-container | media-volume-sliderthumb | menulist | menulist-button | menulist-text | menulist-textfield | meter | progress-bar | progress-bar-value | push-button | radio | scrollbarbutton-down | scrollbarbutton-left | scrollbarbutton-right | scrollbarbutton-up | scrollbargripper-horizontal | scrollbargripper-vertical | scrollbarthumb-horizontal | scrollbarthumb-vertical | scrollbartrack-horizontal | scrollbartrack-vertical | searchfield | searchfield-cancel-button | searchfield-decoration | searchfield-results-button | searchfield-results-decoration | slider-horizontal | slider-vertical | sliderthumb-horizontal | sliderthumb-vertical | square-button | textarea | textfield | -apple-pay-button"},"-webkit-border-after":{"syntax":"<'border-top'>"},"-webkit-border-after-color":{"syntax":"<'border-top-color'>"},"-webkit-border-after-style":{"syntax":"<'border-top-style'>"},"-webkit-border-after-width":{"syntax":"<'border-top-width'>"},"-webkit-border-before":{"syntax":"<'border-top'>"},"-webkit-border-before-color":{"syntax":"<'border-top-color'>"},"-webkit-border-before-style":{"syntax":"<'border-top-style'>"},"-webkit-border-before-width":{"syntax":"<'border-top-width'>"},"-webkit-border-end":{"syntax":"<'border-top'>"},"-webkit-border-end-color":{"syntax":"<'border-top-color'>"},"-webkit-border-end-style":{"syntax":"<'border-top-style'>"},"-webkit-border-end-width":{"syntax":"<'border-top-width'>"},"-webkit-border-start":{"syntax":"<'border-top'>"},"-webkit-border-start-color":{"syntax":"<'border-top-color'>"},"-webkit-border-start-style":{"syntax":"<'border-top-style'>"},"-webkit-border-start-width":{"syntax":"<'border-top-width'>"},"-webkit-box-reflect":{"syntax":"[ above | below | right | left ]? ? ?"},"-webkit-line-clamp":{"syntax":"none | "},"-webkit-mask":{"syntax":"[ || [ / ]? || || [ | border | padding | content | text ] || [ | border | padding | content ] ]#"},"-webkit-mask-attachment":{"syntax":"#"},"-webkit-mask-clip":{"syntax":"[ | no-clip | border | padding | content | text ]#"},"-webkit-mask-composite":{"syntax":"#"},"-webkit-mask-image":{"syntax":"#"},"-webkit-mask-origin":{"syntax":"[ | border | padding | content ]#"},"-webkit-mask-position":{"syntax":"#"},"-webkit-mask-position-x":{"syntax":"[ | left | center | right ]#"},"-webkit-mask-position-y":{"syntax":"[ | top | center | bottom ]#"},"-webkit-mask-repeat":{"syntax":"#"},"-webkit-mask-repeat-x":{"syntax":"repeat | no-repeat | space | round"},"-webkit-mask-repeat-y":{"syntax":"repeat | no-repeat | space | round"},"-webkit-mask-size":{"syntax":"#"},"-webkit-overflow-scrolling":{"syntax":"auto | touch"},"-webkit-tap-highlight-color":{"syntax":""},"-webkit-text-fill-color":{"syntax":""},"-webkit-text-stroke":{"syntax":" || "},"-webkit-text-stroke-color":{"syntax":""},"-webkit-text-stroke-width":{"syntax":""},"-webkit-touch-callout":{"syntax":"default | none"},"-webkit-user-modify":{"syntax":"read-only | read-write | read-write-plaintext-only"},"-webkit-user-select":{"syntax":" auto | none | text | all"},"accent-color":{"syntax":"auto | "},"align-content":{"syntax":"normal | | | ? "},"align-items":{"syntax":"normal | stretch | | [ ? ] | anchor-center"},"align-self":{"syntax":"auto | normal | stretch | | ? | anchor-center"},"align-tracks":{"syntax":"[ normal | | | ? ]#"},"alignment-baseline":{"syntax":" auto | baseline | before-edge | text-before-edge | middle | central | after-edge | text-after-edge | ideographic | alphabetic | hanging | mathematical"},"all":{"syntax":"initial | inherit | unset | revert | revert-layer"},"anchor-name":{"syntax":"none | #"},"anchor-scope":{"syntax":"none | all | #"},"animation":{"syntax":"#"},"animation-composition":{"syntax":"#"},"animation-delay":{"syntax":"