UNPKG

@tbela99/css-parser

Version:

CSS parser, minifier and validator for node and the browser

1,642 lines (1,635 loc) 1.21 MB
'use strict'; var process = require('node:process'); var node_util = require('node:util'); var node_stream = require('node:stream'); var node_fs = require('node:fs'); var promises = require('node:fs/promises'); /** * Syntax validation enum */ var SyntaxValidationResult; (function (SyntaxValidationResult) { /** valid syntax */ SyntaxValidationResult[SyntaxValidationResult["Valid"] = 0] = "Valid"; /** drop invalid syntax */ SyntaxValidationResult[SyntaxValidationResult["Drop"] = 1] = "Drop"; /** preserve unknown at-rules, declarations and pseudo-classes */ SyntaxValidationResult[SyntaxValidationResult["Lenient"] = 2] = "Lenient"; })(SyntaxValidationResult || (SyntaxValidationResult = {})); /** * Enum of node statuses */ exports.EnumAstNodeStatus = void 0; (function (EnumAstNodeStatus) { /** * Node passed validation */ EnumAstNodeStatus[EnumAstNodeStatus["Validated"] = 0] = "Validated"; /** * Node is invalid */ EnumAstNodeStatus[EnumAstNodeStatus["Invalid"] = 1] = "Invalid"; /** * node is not validated */ EnumAstNodeStatus[EnumAstNodeStatus["Unvalidated"] = 2] = "Unvalidated"; /** * Node did not pass validation, but is preserved. */ EnumAstNodeStatus[EnumAstNodeStatus["ValidationFailed"] = 3] = "ValidationFailed"; /** * Parsing the node is not supported yet or the node syntax definition does not exist. */ EnumAstNodeStatus[EnumAstNodeStatus["Unknown"] = 4] = "Unknown"; /** * Failed to parse the node. */ EnumAstNodeStatus[EnumAstNodeStatus["Unparsed"] = 5] = "Unparsed"; /** * Node is disallowed in the context */ EnumAstNodeStatus[EnumAstNodeStatus["Disallowed"] = 6] = "Disallowed"; /** * Node is malformed */ EnumAstNodeStatus[EnumAstNodeStatus["Malformed"] = 7] = "Malformed"; })(exports.EnumAstNodeStatus || (exports.EnumAstNodeStatus = {})); /** * Enum of validation levels * @deprecated */ exports.ValidationLevel = void 0; (function (ValidationLevel) { /** * disable validation */ ValidationLevel[ValidationLevel["None"] = 0] = "None"; /** * validate selectors */ ValidationLevel[ValidationLevel["Selector"] = 1] = "Selector"; /** * validate at-rules */ ValidationLevel[ValidationLevel["AtRule"] = 2] = "AtRule"; /** * validate declarations */ ValidationLevel[ValidationLevel["Declaration"] = 4] = "Declaration"; /** * validate selectors and at-rules */ ValidationLevel[ValidationLevel["Default"] = 3] = "Default"; /** * validate selectors, at-rules and declarations */ ValidationLevel[ValidationLevel["All"] = 7] = "All"; /** * Report only. Apply validation and report nodes that are marked as invalid */ ValidationLevel[ValidationLevel["ReportOnly"] = 8] = "ReportOnly"; })(exports.ValidationLevel || (exports.ValidationLevel = {})); /** * Enum of all token types */ exports.EnumToken = void 0; (function (EnumToken) { /** * comment token */ EnumToken[EnumToken["CommentTokenType"] = 0] = "CommentTokenType"; /** * cdata section token */ EnumToken[EnumToken["CDOCOMMTokenType"] = 1] = "CDOCOMMTokenType"; /** * style sheet node type */ EnumToken[EnumToken["StyleSheetNodeType"] = 2] = "StyleSheetNodeType"; /** * at-rule node type */ EnumToken[EnumToken["AtRuleNodeType"] = 3] = "AtRuleNodeType"; /** * rule node type */ EnumToken[EnumToken["RuleNodeType"] = 4] = "RuleNodeType"; /** * declaration node type */ EnumToken[EnumToken["DeclarationNodeType"] = 5] = "DeclarationNodeType"; /** * literal token type */ EnumToken[EnumToken["LiteralTokenType"] = 6] = "LiteralTokenType"; /** * identifier token type */ EnumToken[EnumToken["IdenTokenType"] = 7] = "IdenTokenType"; /** * dashed identifier token type */ EnumToken[EnumToken["DashedIdenTokenType"] = 8] = "DashedIdenTokenType"; /** * comma token type */ EnumToken[EnumToken["CommaTokenType"] = 9] = "CommaTokenType"; /** * colon token type */ EnumToken[EnumToken["ColonTokenType"] = 10] = "ColonTokenType"; /** * semicolon token type */ EnumToken[EnumToken["SemiColonTokenType"] = 11] = "SemiColonTokenType"; /** * number token type */ EnumToken[EnumToken["NumberTokenType"] = 12] = "NumberTokenType"; /** * at-rule token type */ EnumToken[EnumToken["AtRuleTokenType"] = 13] = "AtRuleTokenType"; /** * percentage token type */ EnumToken[EnumToken["PercentageTokenType"] = 14] = "PercentageTokenType"; /** * function token type */ EnumToken[EnumToken["FunctionTokenType"] = 15] = "FunctionTokenType"; /** * timeline function token type */ EnumToken[EnumToken["TimelineFunctionTokenType"] = 16] = "TimelineFunctionTokenType"; /** * timing function token type */ EnumToken[EnumToken["TimingFunctionTokenType"] = 17] = "TimingFunctionTokenType"; /** * url function token type */ EnumToken[EnumToken["UrlFunctionTokenType"] = 18] = "UrlFunctionTokenType"; /** * image function token type */ EnumToken[EnumToken["ImageFunctionTokenType"] = 19] = "ImageFunctionTokenType"; /** * string token type */ EnumToken[EnumToken["StringTokenType"] = 20] = "StringTokenType"; /** * unclosed string token type */ EnumToken[EnumToken["UnclosedStringTokenType"] = 21] = "UnclosedStringTokenType"; /** * dimension token type */ EnumToken[EnumToken["DimensionTokenType"] = 22] = "DimensionTokenType"; /** * length token type */ EnumToken[EnumToken["LengthTokenType"] = 23] = "LengthTokenType"; /** * angle token type */ EnumToken[EnumToken["AngleTokenType"] = 24] = "AngleTokenType"; /** * time token type */ EnumToken[EnumToken["TimeTokenType"] = 25] = "TimeTokenType"; /** * frequency token type */ EnumToken[EnumToken["FrequencyTokenType"] = 26] = "FrequencyTokenType"; /** * resolution token type */ EnumToken[EnumToken["ResolutionTokenType"] = 27] = "ResolutionTokenType"; /** * hash token type */ EnumToken[EnumToken["HashTokenType"] = 28] = "HashTokenType"; /** * block start token type */ EnumToken[EnumToken["BlockStartTokenType"] = 29] = "BlockStartTokenType"; /** * block end token type */ EnumToken[EnumToken["BlockEndTokenType"] = 30] = "BlockEndTokenType"; /** * attribute start token type */ EnumToken[EnumToken["AttrStartTokenType"] = 31] = "AttrStartTokenType"; /** * attribute end token type */ EnumToken[EnumToken["AttrEndTokenType"] = 32] = "AttrEndTokenType"; /** * start parentheses token type */ EnumToken[EnumToken["StartParensTokenType"] = 33] = "StartParensTokenType"; /** * end parentheses token type */ EnumToken[EnumToken["EndParensTokenType"] = 34] = "EndParensTokenType"; /** * parentheses token type */ EnumToken[EnumToken["ParensTokenType"] = 35] = "ParensTokenType"; /** * whitespace token type */ EnumToken[EnumToken["WhitespaceTokenType"] = 36] = "WhitespaceTokenType"; /** * include match token type */ EnumToken[EnumToken["IncludeMatchTokenType"] = 37] = "IncludeMatchTokenType"; /** * dash match token type */ EnumToken[EnumToken["DashMatchTokenType"] = 38] = "DashMatchTokenType"; /** * equal match token type */ EnumToken[EnumToken["EqualMatchTokenType"] = 39] = "EqualMatchTokenType"; /** * less than token type */ EnumToken[EnumToken["LtTokenType"] = 40] = "LtTokenType"; /** * less than or equal to token type */ EnumToken[EnumToken["LteTokenType"] = 41] = "LteTokenType"; /** * greater than token type */ EnumToken[EnumToken["GtTokenType"] = 42] = "GtTokenType"; /** * greater than or equal to token type */ EnumToken[EnumToken["GteTokenType"] = 43] = "GteTokenType"; /** * pseudo-class token type */ EnumToken[EnumToken["PseudoClassTokenType"] = 44] = "PseudoClassTokenType"; /** * pseudo-class function token type */ EnumToken[EnumToken["PseudoClassFuncTokenType"] = 45] = "PseudoClassFuncTokenType"; /** * delimiter token type */ EnumToken[EnumToken["DelimTokenType"] = 46] = "DelimTokenType"; /** * URL token type */ EnumToken[EnumToken["UrlTokenTokenType"] = 47] = "UrlTokenTokenType"; /** * end of file token type */ EnumToken[EnumToken["EOFTokenType"] = 48] = "EOFTokenType"; /** * important token type */ EnumToken[EnumToken["ImportantTokenType"] = 49] = "ImportantTokenType"; /** * color token type */ EnumToken[EnumToken["ColorTokenType"] = 50] = "ColorTokenType"; /** * attribute token type */ EnumToken[EnumToken["AttrTokenType"] = 51] = "AttrTokenType"; /** * bad comment token type */ EnumToken[EnumToken["BadCommentTokenType"] = 52] = "BadCommentTokenType"; /** * bad cdo token type */ EnumToken[EnumToken["BadCdoTokenType"] = 53] = "BadCdoTokenType"; /** * bad URL token type */ EnumToken[EnumToken["BadUrlTokenType"] = 54] = "BadUrlTokenType"; /** * bad string token type */ EnumToken[EnumToken["BadStringTokenType"] = 55] = "BadStringTokenType"; /** * binary expression token type */ EnumToken[EnumToken["BinaryExpressionTokenType"] = 56] = "BinaryExpressionTokenType"; /** * unary expression token type */ EnumToken[EnumToken["UnaryExpressionTokenType"] = 57] = "UnaryExpressionTokenType"; /** * flex token type */ EnumToken[EnumToken["FlexTokenType"] = 58] = "FlexTokenType"; /** * token list token type */ EnumToken[EnumToken["ListToken"] = 59] = "ListToken"; /* arithmetic tokens */ /** * addition token type */ EnumToken[EnumToken["Add"] = 60] = "Add"; /** * multiplication token type */ EnumToken[EnumToken["Mul"] = 61] = "Mul"; /** * division token type */ EnumToken[EnumToken["Div"] = 62] = "Div"; /** * subtraction token type */ EnumToken[EnumToken["Sub"] = 63] = "Sub"; /* new tokens */ /** * column combinator token type */ EnumToken[EnumToken["ColumnCombinatorTokenType"] = 64] = "ColumnCombinatorTokenType"; /** * contain match token type */ EnumToken[EnumToken["ContainMatchTokenType"] = 65] = "ContainMatchTokenType"; /** * start match token type */ EnumToken[EnumToken["StartMatchTokenType"] = 66] = "StartMatchTokenType"; /** * end match token type */ EnumToken[EnumToken["EndMatchTokenType"] = 67] = "EndMatchTokenType"; /** * match expression token type */ EnumToken[EnumToken["MatchExpressionTokenType"] = 68] = "MatchExpressionTokenType"; /** * namespace attribute token type */ EnumToken[EnumToken["NameSpaceAttributeTokenType"] = 69] = "NameSpaceAttributeTokenType"; /** * fraction token type */ EnumToken[EnumToken["FractionTokenType"] = 70] = "FractionTokenType"; /** * identifier list token type */ EnumToken[EnumToken["IdenListTokenType"] = 71] = "IdenListTokenType"; /** * grid template function token type */ EnumToken[EnumToken["GridTemplateFuncTokenType"] = 72] = "GridTemplateFuncTokenType"; /** * keyframe rule node type */ EnumToken[EnumToken["KeyFramesRuleNodeType"] = 73] = "KeyFramesRuleNodeType"; /** * class selector token type */ EnumToken[EnumToken["ClassSelectorTokenType"] = 74] = "ClassSelectorTokenType"; /** * universal selector token type */ EnumToken[EnumToken["UniversalSelectorTokenType"] = 75] = "UniversalSelectorTokenType"; /** * child combinator token type */ EnumToken[EnumToken["ChildCombinatorTokenType"] = 76] = "ChildCombinatorTokenType"; /** * descendant combinator token type */ EnumToken[EnumToken["DescendantCombinatorTokenType"] = 77] = "DescendantCombinatorTokenType"; /** * next sibling combinator token type */ EnumToken[EnumToken["NextSiblingCombinatorTokenType"] = 78] = "NextSiblingCombinatorTokenType"; /** * subsequent sibling combinator token type */ EnumToken[EnumToken["SubsequentSiblingCombinatorTokenType"] = 79] = "SubsequentSiblingCombinatorTokenType"; /** * nesting selector token type */ EnumToken[EnumToken["NestingSelectorTokenType"] = 80] = "NestingSelectorTokenType"; /** * Invalid rule token type * @deprecated */ EnumToken[EnumToken["InvalidRuleNodeType"] = 81] = "InvalidRuleNodeType"; /** * invalid class selector token type */ EnumToken[EnumToken["InvalidClassSelectorTokenType"] = 82] = "InvalidClassSelectorTokenType"; /** * invalid attribute token type */ EnumToken[EnumToken["InvalidAttrTokenType"] = 83] = "InvalidAttrTokenType"; /** * Invalid at rule token type * @deprecated */ EnumToken[EnumToken["InvalidAtRuleNodeType"] = 84] = "InvalidAtRuleNodeType"; /** * media query condition token type */ EnumToken[EnumToken["MediaQueryConditionTokenType"] = 85] = "MediaQueryConditionTokenType"; /** * media feature token type */ EnumToken[EnumToken["MediaFeatureTokenType"] = 86] = "MediaFeatureTokenType"; /** * media feature only token type */ EnumToken[EnumToken["OnlyTokenType"] = 87] = "OnlyTokenType"; /** * media feature not token type */ EnumToken[EnumToken["NotTokenType"] = 88] = "NotTokenType"; /** * media feature and token type */ EnumToken[EnumToken["AndTokenType"] = 89] = "AndTokenType"; /** * media feature or token type */ EnumToken[EnumToken["OrTokenType"] = 90] = "OrTokenType"; /** * pseudo page token type */ EnumToken[EnumToken["PseudoPageTokenType"] = 91] = "PseudoPageTokenType"; /** * pseudo element token type */ EnumToken[EnumToken["PseudoElementTokenType"] = 92] = "PseudoElementTokenType"; /** * keyframe at rule node type */ EnumToken[EnumToken["KeyframesAtRuleNodeType"] = 93] = "KeyframesAtRuleNodeType"; /** * invalid declaration node type. * @deprecated */ EnumToken[EnumToken["InvalidDeclarationNodeType"] = 94] = "InvalidDeclarationNodeType"; /* css module nodes */ /** * composes token node type */ EnumToken[EnumToken["ComposesSelectorNodeType"] = 95] = "ComposesSelectorNodeType"; /** * css variable token type */ EnumToken[EnumToken["CssVariableTokenType"] = 96] = "CssVariableTokenType"; /** * css variable import token type */ EnumToken[EnumToken["CssVariableImportTokenType"] = 97] = "CssVariableImportTokenType"; /** * css variable declaration map token type */ EnumToken[EnumToken["CssVariableDeclarationMapTokenType"] = 98] = "CssVariableDeclarationMapTokenType"; /** * media range query token type */ EnumToken[EnumToken["MediaRangeQueryTokenType"] = 99] = "MediaRangeQueryTokenType"; /** * invalid media query token type */ EnumToken[EnumToken["InvalidMediaQueryTokenType"] = 100] = "InvalidMediaQueryTokenType"; /** * supports query condition token type */ EnumToken[EnumToken["SupportsQueryConditionTokenType"] = 101] = "SupportsQueryConditionTokenType"; /** * supports query unary condition token type */ EnumToken[EnumToken["SupportsQueryUnaryConditionTokenType"] = 102] = "SupportsQueryUnaryConditionTokenType"; /** * when else query condition token type */ EnumToken[EnumToken["WhenElseQueryConditionTokenType"] = 103] = "WhenElseQueryConditionTokenType"; /** * when else query unary condition token type */ EnumToken[EnumToken["WhenElseUnaryConditionTokenType"] = 104] = "WhenElseUnaryConditionTokenType"; /** * container style range token type */ EnumToken[EnumToken["ContainerStyleRangeTokenType"] = 105] = "ContainerStyleRangeTokenType"; /** * '*' */ EnumToken[EnumToken["Star"] = 106] = "Star"; /** * '+' */ EnumToken[EnumToken["Plus"] = 107] = "Plus"; /** * '~' */ EnumToken[EnumToken["Tilda"] = 108] = "Tilda"; /** * '|' */ EnumToken[EnumToken["Pipe"] = 109] = "Pipe"; /** * '::' */ EnumToken[EnumToken["DoubleColonTokenType"] = 110] = "DoubleColonTokenType"; /** * math function token type such as'calc(' etc. */ EnumToken[EnumToken["MathFunctionTokenType"] = 111] = "MathFunctionTokenType"; /** * transform function token type such as 'translate(' etc. */ EnumToken[EnumToken["TransformFunctionTokenType"] = 112] = "TransformFunctionTokenType"; /** * when function token type such as 'supports(' etc. */ EnumToken[EnumToken["WhenElseFunctionTokenType"] = 113] = "WhenElseFunctionTokenType"; /** * general enclosed function token type 'font-tech(' etc. */ EnumToken[EnumToken["GeneralEnclosedFunctionTokenType"] = 114] = "GeneralEnclosedFunctionTokenType"; /** * supports function token type such as 'at-rule(' */ EnumToken[EnumToken["SupportsFunctionTokenType"] = 115] = "SupportsFunctionTokenType"; /** * container function token type such as 'style(' or 'scroll-state(' */ EnumToken[EnumToken["ContainerFunctionTokenType"] = 116] = "ContainerFunctionTokenType"; /** * unrecognized node token type */ EnumToken[EnumToken["RawNodeTokenType"] = 117] = "RawNodeTokenType"; /** * media query boolean token type * at-rule media not () * at-rule media only () */ EnumToken[EnumToken["MediaQueryUnaryFeatureTokenType"] = 118] = "MediaQueryUnaryFeatureTokenType"; /** * grid template function token type such as 'minmax(' */ EnumToken[EnumToken["GridTemplateFuncTokenDefType"] = 119] = "GridTemplateFuncTokenDefType"; /** * image function token type such as 'image(' etc. */ EnumToken[EnumToken["ImageFunctionTokenDefType"] = 120] = "ImageFunctionTokenDefType"; /** * function token type such as 'view(' etc. */ EnumToken[EnumToken["TimelineFunctionTokenDefType"] = 121] = "TimelineFunctionTokenDefType"; /** * function token type */ EnumToken[EnumToken["FunctionTokenDefType"] = 122] = "FunctionTokenDefType"; /** * timing function token type such as 'linear(' etc. */ EnumToken[EnumToken["TimingFunctionTokenDefType"] = 123] = "TimingFunctionTokenDefType"; /** * color function token type such as 'rgb(' etc. */ EnumToken[EnumToken["ColorFunctionTokenDefType"] = 124] = "ColorFunctionTokenDefType"; /** * math function token type such as 'calc(' etc. */ EnumToken[EnumToken["MathFunctionTokenDefType"] = 125] = "MathFunctionTokenDefType"; /** * container function token type such as 'style(' or 'scroll-state(' */ EnumToken[EnumToken["ContainerFunctionTokenDefType"] = 126] = "ContainerFunctionTokenDefType"; /** * url function token type 'url(' */ EnumToken[EnumToken["UrlFunctionTokenDefType"] = 127] = "UrlFunctionTokenDefType"; /** * pseudo-class function token type */ EnumToken[EnumToken["PseudoClassFunctionTokenDefType"] = 128] = "PseudoClassFunctionTokenDefType"; /** * transform function token type such as 'translate(' etc. */ EnumToken[EnumToken["TransformFunctionTokenDefType"] = 129] = "TransformFunctionTokenDefType"; /** * when function token type such as 'supports(' or 'media(' */ EnumToken[EnumToken["WhenElseFunctionTokenDefType"] = 130] = "WhenElseFunctionTokenDefType"; /** * general enclosed function token type 'font-tech(' etc. */ EnumToken[EnumToken["GeneralEnclosedFunctionTokenDefType"] = 131] = "GeneralEnclosedFunctionTokenDefType"; /** * supports function token type 'font-tech(' */ EnumToken[EnumToken["SupportsFunctionTokenDefType"] = 132] = "SupportsFunctionTokenDefType"; /** * CDOCOMMTokenType not allowed in this context */ EnumToken[EnumToken["InvalidCommentTokenType"] = 133] = "InvalidCommentTokenType"; /** * custom function token type '--function-name(' */ EnumToken[EnumToken["CustomFunctionTokenDefType"] = 134] = "CustomFunctionTokenDefType"; /** * custom function token type */ EnumToken[EnumToken["CustomFunctionTokenType"] = 135] = "CustomFunctionTokenType"; /** * function tokens such as 'var(', 'env(', 'if(') */ EnumToken[EnumToken["WildCardFunctionTokenDefType"] = 136] = "WildCardFunctionTokenDefType"; /** * function such as 'var()', 'env()', 'if()' */ EnumToken[EnumToken["WildCardFunctionTokenType"] = 137] = "WildCardFunctionTokenType"; /** * if condition token */ EnumToken[EnumToken["IfConditionTokenType"] = 138] = "IfConditionTokenType"; /** * if-Else condition token */ EnumToken[EnumToken["IfElseConditionTokenType"] = 139] = "IfElseConditionTokenType"; /** * wrapped values token type like {Arial, sans-serif} */ EnumToken[EnumToken["WrappedValuesTokenType"] = 140] = "WrappedValuesTokenType"; /* aliases */ /** * alias for time token type */ EnumToken[EnumToken["Time"] = 25] = "Time"; /** * alias for identifier token type */ EnumToken[EnumToken["Iden"] = 7] = "Iden"; /** * alias for end of file token type */ EnumToken[EnumToken["EOF"] = 48] = "EOF"; /** * alias for hash token type */ EnumToken[EnumToken["Hash"] = 28] = "Hash"; /** * alias for flex token type */ EnumToken[EnumToken["Flex"] = 58] = "Flex"; /** * alias for angle token type */ EnumToken[EnumToken["Angle"] = 24] = "Angle"; /** * alias for color token type */ EnumToken[EnumToken["Color"] = 50] = "Color"; /** * alias for comma token type */ EnumToken[EnumToken["Comma"] = 9] = "Comma"; /** * alias for string token type */ EnumToken[EnumToken["String"] = 20] = "String"; /** * alias for length token type */ EnumToken[EnumToken["Length"] = 23] = "Length"; /** * alias for number token type */ EnumToken[EnumToken["Number"] = 12] = "Number"; /** * alias for percentage token type */ EnumToken[EnumToken["Perc"] = 14] = "Perc"; /** * alias for literal token type */ EnumToken[EnumToken["Literal"] = 6] = "Literal"; /** * alias for comment token type */ EnumToken[EnumToken["Comment"] = 0] = "Comment"; /** * alias for url function token type */ EnumToken[EnumToken["UrlFunc"] = 18] = "UrlFunc"; /** * alias for dimension token type */ EnumToken[EnumToken["Dimension"] = 22] = "Dimension"; /** * alias for frequency token type */ EnumToken[EnumToken["Frequency"] = 26] = "Frequency"; /** * alias for resolution token type */ EnumToken[EnumToken["Resolution"] = 27] = "Resolution"; /** * alias for whitespace token type */ EnumToken[EnumToken["Whitespace"] = 36] = "Whitespace"; /** * alias for identifier list token type */ EnumToken[EnumToken["IdenList"] = 71] = "IdenList"; /** * alias for dashed identifier token type */ EnumToken[EnumToken["DashedIden"] = 8] = "DashedIden"; /** * alias for grid template function token type */ EnumToken[EnumToken["GridTemplateFunc"] = 72] = "GridTemplateFunc"; /** * alias for image function token type */ EnumToken[EnumToken["ImageFunc"] = 19] = "ImageFunc"; /** * alias for comment node type */ EnumToken[EnumToken["CommentNodeType"] = 0] = "CommentNodeType"; /** * alias for cdata section node type */ EnumToken[EnumToken["CDOCOMMNodeType"] = 1] = "CDOCOMMNodeType"; /** * alias for timing function token type */ EnumToken[EnumToken["TimingFunction"] = 17] = "TimingFunction"; /** * alias for timeline function token type */ EnumToken[EnumToken["TimelineFunction"] = 16] = "TimelineFunction"; })(exports.EnumToken || (exports.EnumToken = {})); /** * Supported color types enum */ exports.ColorType = void 0; (function (ColorType) { /** * deprecated system colors */ ColorType[ColorType["SYS"] = 0] = "SYS"; /** * deprecated system colors */ ColorType[ColorType["DPSYS"] = 1] = "DPSYS"; /** * named colors */ ColorType[ColorType["LIT"] = 2] = "LIT"; /** * colors as hex values */ ColorType[ColorType["HEX"] = 3] = "HEX"; /** * colors as rgb values */ ColorType[ColorType["RGBA"] = 4] = "RGBA"; /** * colors using rgb */ ColorType[ColorType["HSLA"] = 5] = "HSLA"; /** * colors using hwb */ ColorType[ColorType["HWB"] = 6] = "HWB"; /** * colors using cmyk */ ColorType[ColorType["CMYK"] = 7] = "CMYK"; /** * colors using oklab * */ ColorType[ColorType["OKLAB"] = 8] = "OKLAB"; /** * colors using oklch * */ ColorType[ColorType["OKLCH"] = 9] = "OKLCH"; /** * colors using lab */ ColorType[ColorType["LAB"] = 10] = "LAB"; /** * colors using lch */ ColorType[ColorType["LCH"] = 11] = "LCH"; /** * colors using color() function */ ColorType[ColorType["COLOR"] = 12] = "COLOR"; /** * color using srgb */ ColorType[ColorType["SRGB"] = 13] = "SRGB"; /** * color using prophoto-rgb */ ColorType[ColorType["PROPHOTO_RGB"] = 14] = "PROPHOTO_RGB"; /** * color using a98-rgb */ ColorType[ColorType["A98_RGB"] = 15] = "A98_RGB"; /** * color using rec2020 */ ColorType[ColorType["REC2020"] = 16] = "REC2020"; /** * color using display-p3 */ ColorType[ColorType["DISPLAY_P3"] = 17] = "DISPLAY_P3"; /** * color using srgb-linear */ ColorType[ColorType["SRGB_LINEAR"] = 18] = "SRGB_LINEAR"; /** * color using xyz-d50 */ ColorType[ColorType["XYZ_D50"] = 19] = "XYZ_D50"; /** * color using xyz-d65 */ ColorType[ColorType["XYZ_D65"] = 20] = "XYZ_D65"; /** * light-dark() color function */ ColorType[ColorType["LIGHT_DARK"] = 21] = "LIGHT_DARK"; /** * color-mix() color function */ ColorType[ColorType["COLOR_MIX"] = 22] = "COLOR_MIX"; /** * non-standard color */ ColorType[ColorType["NON_STD"] = 23] = "NON_STD"; /** * custom color */ ColorType[ColorType["CUSTOM_COLOR"] = 24] = "CUSTOM_COLOR"; /** * alpha() color function */ ColorType[ColorType["ALPHA"] = 25] = "ALPHA"; /** * color using display-p3-linear */ ColorType[ColorType["DISPLAY_P3_LINEAR"] = 26] = "DISPLAY_P3_LINEAR"; /** * alias for rgba */ ColorType[ColorType["RGB"] = 4] = "RGB"; /** * alias for hsl */ ColorType[ColorType["HSL"] = 5] = "HSL"; /** * alias for xyz-d65 */ ColorType[ColorType["XYZ"] = 20] = "XYZ"; /** * alias for cmyk */ ColorType[ColorType["DEVICE_CMYK"] = 7] = "DEVICE_CMYK"; })(exports.ColorType || (exports.ColorType = {})); /** * Supported module case transform */ exports.ModuleCaseTransformEnum = void 0; (function (ModuleCaseTransformEnum) { /** * export class names as-is */ ModuleCaseTransformEnum[ModuleCaseTransformEnum["IgnoreCase"] = 1] = "IgnoreCase"; /** * transform mapping key name to camel case */ ModuleCaseTransformEnum[ModuleCaseTransformEnum["CamelCase"] = 2] = "CamelCase"; /** * transform class names and mapping key name to camel case */ ModuleCaseTransformEnum[ModuleCaseTransformEnum["CamelCaseOnly"] = 4] = "CamelCaseOnly"; /** * transform mapping key name to dash case */ ModuleCaseTransformEnum[ModuleCaseTransformEnum["DashCase"] = 8] = "DashCase"; /** * transform class names and mapping key name to dash case */ ModuleCaseTransformEnum[ModuleCaseTransformEnum["DashCaseOnly"] = 16] = "DashCaseOnly"; })(exports.ModuleCaseTransformEnum || (exports.ModuleCaseTransformEnum = {})); /** * Supported module scope */ exports.ModuleScopeEnumOptions = void 0; (function (ModuleScopeEnumOptions) { /** * use the global scope */ ModuleScopeEnumOptions[ModuleScopeEnumOptions["Global"] = 32] = "Global"; /** * use the local scope */ ModuleScopeEnumOptions[ModuleScopeEnumOptions["Local"] = 64] = "Local"; /** * do not allow selector without an id or class */ ModuleScopeEnumOptions[ModuleScopeEnumOptions["Pure"] = 128] = "Pure"; /** * export using ICSS module format */ ModuleScopeEnumOptions[ModuleScopeEnumOptions["ICSS"] = 256] = "ICSS"; /** * use the shortest name possible. pattern is ignored. * it will produce names such as * * ```css * .a { * content: 'a'; * } * * .b { * content: 'b'; * } * * .c { * content: 'c'; * } * ... * ``` */ ModuleScopeEnumOptions[ModuleScopeEnumOptions["Shortest"] = 512] = "Shortest"; })(exports.ModuleScopeEnumOptions || (exports.ModuleScopeEnumOptions = {})); // https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Values_and_units#absolute_length_units /** * Convert length to px * @param value * @returns */ function length2Px(value) { let result = null; if (value.typ == exports.EnumToken.NumberTokenType) { result = +value.val; } else { switch (value.unit) { case "cm": // @ts-ignore result = value.val * 37.8; break; case "mm": // @ts-ignore result = value.val * 3.78; break; case "Q": // @ts-ignore result = (value.val * 37.8) / 40; break; case "in": // @ts-ignore result = value.val / 96; break; case "pc": // @ts-ignore result = value.val / 16; break; case "pt": // @ts-ignore result = (value.val * 4) / 3; break; case "px": result = +value.val; break; } } return isNaN(result) ? null : result; } /** * minify number * @param val */ function minifyNumber(val) { val = String(val); if (val === "0") { return "0"; } const chr = val.charAt(0); if (chr == "-") { const slice = val.slice(0, 2); if (slice == "-0") { return val.length == 2 ? "0" : "-" + val.slice(2); } } if (chr == "0") { return val.slice(1); } return val; } /** * Options for the walk function */ exports.WalkerOptionEnum = void 0; (function (WalkerOptionEnum) { /** * ignore the current node and its children */ WalkerOptionEnum[WalkerOptionEnum["Ignore"] = 1] = "Ignore"; /** * stop walking the tree */ WalkerOptionEnum[WalkerOptionEnum["Stop"] = 2] = "Stop"; /** * ignore the current node and process its children */ WalkerOptionEnum[WalkerOptionEnum["Children"] = 4] = "Children"; /** * ignore the current node children */ WalkerOptionEnum[WalkerOptionEnum["IgnoreChildren"] = 8] = "IgnoreChildren"; })(exports.WalkerOptionEnum || (exports.WalkerOptionEnum = {})); /** * Event types for the walkValues function */ exports.WalkerEvent = void 0; (function (WalkerEvent) { /** * enter node */ WalkerEvent[WalkerEvent["Enter"] = 1] = "Enter"; /** * leave node */ WalkerEvent[WalkerEvent["Leave"] = 2] = "Leave"; })(exports.WalkerEvent || (exports.WalkerEvent = {})); /** * Walk ast nodes * @param node initial node * @param filter control the walk process * @param reverse walk in reverse order * * ```ts * * import {walk} from '@tbela99/css-parser'; * * const css = ` * body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); } * * html, * body { * line-height: 1.474; * } * * .ruler { * * height: 10px; * } * `; * * for (const {node, parent, root} of walk(ast)) { * * // do something with node * } * ``` * * Using a {@link filter} function to control the ast traversal. the filter function returns a value of type {@link WalkerOption}. * * ```ts * import {EnumToken, transform, walk, WalkerOptionEnum} from '@tbela99/css-parser'; * * const css = ` * body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); } * * html, * body { * line-height: 1.474; * } * * .ruler { * * height: 10px; * } * `; * * function filter(node) { * * if (node.typ == EnumToken.AstRule && node.sel.includes('html')) { * * // skip the children of the current node * return WalkerOptionEnum.IgnoreChildren; * } * } * * const result = await transform(css); * for (const {node} of walk(result.ast, filter)) { * * console.error([EnumToken[node.typ]]); * } * * // [ "StyleSheetNodeType" ] * // [ "RuleNodeType" ] * // [ "DeclarationNodeType" ] * // [ "RuleNodeType" ] * // [ "DeclarationNodeType" ] * // [ "RuleNodeType" ] * // [ "DeclarationNodeType" ] * ``` */ function* walk(node, filter, reverse) { const parents = [node]; const root = node; const map = new Map(); let isNumeric = false; let i = 0; while ((node = parents[i++])) { let option = null; if (filter != null) { option = filter(node); isNumeric = typeof option == "number"; if (isNumeric) { if (option & exports.WalkerOptionEnum.Ignore) { continue; } if (option & exports.WalkerOptionEnum.Stop) { break; } } } if (!isNumeric || (option & exports.WalkerOptionEnum.Children) === 0) { // @ts-ignore yield { node, parent: map.get(node), root, // @ts-expect-error parents: function* () { let parent = map.get(node); while (parent != null) { yield parent; parent = map.get(parent); } }, }; } if ("chi" in node && (!isNumeric || (option & exports.WalkerOptionEnum.IgnoreChildren) === 0)) { parents.splice(i, 0, ...node.chi[reverse ? "toReversed" : "slice"]()); for (const child of node.chi) { map.set(child, node); } } } } /** * Walk ast node value tokens * @param values * @param root * @param filter * @param reverse * * Example: * * ```ts * * import {AstDeclaration, EnumToken, transform, walkValues} from '@tbela99/css-parser'; * * const css = ` * body { color: color(from var(--base-color) display-p3 r calc(g + 0.24) calc(b + 0.15)); } * `; * * const result = await transform(css); * const declaration = result.ast.chi[0].chi[0] as AstDeclaration; * * // walk the node attribute's tokens in reverse order * for (const {value} of walkValues(declaration.val, null, null,true)) { * * console.error([EnumToken[value.typ], value.val]); * } * * // [ "Color", "color" ] * // [ "FunctionTokenType", "calc" ] * // [ "Number", 0.15 ] * // [ "Add", undefined ] * // [ "Iden", "b" ] * // [ "Whitespace", undefined ] * // [ "FunctionTokenType", "calc" ] * // [ "Number", 0.24 ] * // [ "Add", undefined ] * // [ "Iden", "g" ] * // [ "Whitespace", undefined ] * // [ "Iden", "r" ] * // [ "Whitespace", undefined ] * // [ "Iden", "display-p3" ] * // [ "Whitespace", undefined ] * // [ "FunctionTokenType", "var" ] * // [ "DashedIden", "--base-color" ] * // [ "Whitespace", undefined ] * // [ "Iden", "from" ] * ``` */ function* walkValues(values, root = null, filter, reverse) { const stack = values.slice(); const map = new Map(); const used = new Set(); let previous = null; if (filter != null && typeof filter == "function") { filter = { event: exports.WalkerEvent.Enter, fn: filter, }; } else if (filter == null) { filter = { event: exports.WalkerEvent.Enter, }; } let isNumeric = false; let value; let option; let node; // const parents: Token[] = []; const eventType = filter.event ?? exports.WalkerEvent.Enter; while (stack.length > 0) { value = reverse ? stack.pop() : stack.shift(); option = null; node = map.get(value) ?? null; if (used.has(value)) { continue; } used.add(value); // parents.length = 0; // while (node != null) { // parents.push(node); // node = map.get(node) ?? null; // } if (filter.fn != null && eventType & exports.WalkerEvent.Enter) { const isValid = filter.type == null || value.typ == filter.type || (Array.isArray(filter.type) && filter.type.includes(value.typ)) || (typeof filter.type == "function" && filter.type(value)); if (isValid) { option = filter.fn(value, map.get(value) ?? root, exports.WalkerEvent.Enter, // @ts-expect-error function* () { // @ts-expect-error let parent = map.get(node); while (parent != null) { yield parent; parent = map.get(parent); } }); isNumeric = typeof option == "number"; if (isNumeric && option & exports.WalkerOptionEnum.Stop) { return; } if (isNumeric && option & exports.WalkerOptionEnum.Ignore) { continue; } // @ts-ignore if (option != null && typeof option == "object" && ("typ" in option || Array.isArray(option))) { const op = Array.isArray(option) ? option : [option]; for (const o of op) { map.set(o, map.get(value) ?? root); } stack[reverse ? "push" : "unshift"](...op); } } } yield { value, parent: map.get(value) ?? root, previousValue: previous, nextValue: stack[0] ?? null, // @ts-ignore root: root ?? null, // @ts-expect-error parents: function* () { // @ts-expect-error let result = map.get(node) ?? root; let next; do { yield result; next = map.get(result) ?? root; if (next == result) { break; } result = next; } while (result != null); }, }; if (!isNumeric || (option & exports.WalkerOptionEnum.IgnoreChildren) === 0) { if ("chi" in value) { const sliced = value.chi.slice(); for (const child of sliced) { map.set(child, value); } stack[reverse ? "push" : "unshift"](...sliced); } else { const values = []; if ("l" in value && value.l != null) { // @ts-ignore values.push(value.l); // @ts-ignore map.set(value.l, value); } if ("op" in value && typeof value.op == "object") { // @ts-ignore values.push(value.op); // @ts-ignore map.set(value.op, value); } if ("r" in value && value.r != null) { if (Array.isArray(value.r)) { for (const r of value.r) { // @ts-ignore values.push(r); // @ts-ignore map.set(r, value); } } else { // @ts-ignore values.push(value.r); // @ts-ignore map.set(value.r, value); } } if (values.length > 0) { stack[reverse ? "push" : "unshift"](...values); } } } if (eventType & exports.WalkerEvent.Leave && filter.fn != null) { const isValid = filter.type == null || value.typ == filter.type || (Array.isArray(filter.type) && filter.type.includes(value.typ)) || (typeof filter.type == "function" && filter.type(value)); if (isValid) { option = filter.fn(value, map.get(value), exports.WalkerEvent.Leave); // @ts-ignore if (option != null && ("typ" in option || Array.isArray(option))) { const op = Array.isArray(option) ? option : [option]; for (const o of op) { map.set(o, map.get(value) ?? root); } stack[reverse ? "push" : "unshift"](...op); } } } previous = value; } } /** * convert angle to degrees * @param angle * @returns */ function toDegrees(angle) { switch (angle.unit) { // case "deg": // return angle; case "rad": // @ts-expect-error angle.val *= 180 / Math.PI; angle.unit = "deg"; return angle; case "grad": // @ts-expect-error angle.val *= 0.9; angle.unit = "deg"; return angle; case "turn": // @ts-expect-error angle.val *= 360; angle.unit = "deg"; return angle; } return angle; } /** * * @param fn * @returns */ function memoize(fn) { const buckets = new Map(); return function (...args) { const n = args.length; let bucket = buckets.get(n); if (bucket == null) { bucket = new Map(); buckets.set(n, bucket); } if (n === 1) { const key = args[0]; const value = bucket.get(key); if (value != null) return bucket.get(key); const result = fn(key); bucket.set(key, result); return result; } let node = bucket; for (let i = 0; i < n; i++) { const key = args[i]; const next = node.get(key); if (next == null) { const newNode = new Map(); node.set(key, newNode); node = newNode; } else { node = next; } } const last = n === 0 ? undefined : args[n - 1]; const value = node.get(last); if (value != null) return value; const result = fn(...args); node.set(last, result); return result; }; } function dasherize(value) { return value.replace(/([A-Z])/g, (all, one) => `-${one.toLowerCase()}`); } function camelize(value) { return value.replace(/-([a-z])/g, (all, one) => one.toUpperCase()); } function equalsIgnoreCase(a, b) { if (a.length !== b.length) return false; for (let i = 0; i < a.length; i++) { let ca = a.charCodeAt(i); let cb = b.charCodeAt(i); // Normalize A-Z to a-z if (ca >= 65 && ca <= 90) ca += 32; if (cb >= 65 && cb <= 90) cb += 32; if (ca !== cb) return false; } return true; } var 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: "<percentage>" }, "-ms-content-zoom-limit-min": { syntax: "<percentage>" }, "-ms-content-zoom-snap": { syntax: "<'-ms-content-zoom-snap-type'> || <'-ms-content-zoom-snap-points'>" }, "-ms-content-zoom-snap-points": { syntax: "snapInterval( <percentage>, <percentage> ) | snapList( <percentage># )" }, "-ms-content-zoom-snap-type": { syntax: "none | proximity | mandatory" }, "-ms-content-zooming": { syntax: "none | zoom" }, "-ms-filter": { syntax: "<string>" }, "-ms-flow-from": { syntax: "[ none | <custom-ident> ]#" }, "-ms-flow-into": { syntax: "[ none | <custom-ident> ]#" }, "-ms-grid-columns": { syntax: "none | <track-list> | <auto-track-list>" }, "-ms-grid-rows": { syntax: "none | <track-list> | <auto-track-list>" }, "-ms-high-contrast-adjust": { syntax: "auto | none" }, "-ms-hyphenate-limit-chars": { syntax: "auto | <integer>{1,3}" }, "-ms-hyphenate-limit-lines": { syntax: "no-limit | <integer>" }, "-ms-hyphenate-limit-zone": { syntax: "<percentage> | <length>" }, "-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 | <length>" }, "-ms-scroll-limit-x-min": { syntax: "<length>" }, "-ms-scroll-limit-y-max": { syntax: "auto | <length>" }, "-ms-scroll-limit-y-min": { syntax: "<length>" }, "-ms-scroll-rails": { syntax: "none | railed" }, "-ms-scroll-snap-points-x": { syntax: "snapInterval( <length-percentage>, <length-percentage> ) | snapList( <length-percentage># )" }, "-ms-scroll-snap-points-y": { syntax: "snapInterval( <length-percentage>, <length-percentage> ) | snapList( <length-percentage># )" }, "-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: "<color>" }, "-ms-scrollbar-arrow-color": { syntax: "<color>" }, "-ms-scrollbar-base-color": { syntax: "<color>" }, "-ms-scrollbar-darkshadow-color": { syntax: "<color>" }, "-ms-scrollbar-face-color": { syntax: "<color>" }, "-ms-scrollbar-highlight-color": { syntax: "<color>" }, "-ms-scrollbar-shadow-color": { syntax: "<color>" }, "-ms-scrollbar-track-color": { syntax: "<color>" }, "-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: "<length>" }, "-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-dropdow