@tokey/css-value-parser
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -8,23 +8,23 @@ import type { lengthValidUnits, angleValidUnits, frequencyValidUnits, resolutionValidUnits, timeValidUnits } from './units'; | ||
} | ||
export declare type BaseAstNode = Literal | CssWideKeyword | Space | Comment | Invalid | CustomIdent | DashedIdent | Call | String | Number | Integer | Length | Angle | Time | Percentage | Frequency | Resolution | Flex | UnknownUnit | Color; | ||
export declare type BuildVarAst = CSSValueAST<`build-var`> & { | ||
export type BaseAstNode = Literal | CssWideKeyword | Space | Comment | Invalid | CustomIdent | DashedIdent | Call | String | Number | Integer | Length | Angle | Time | Percentage | Frequency | Resolution | Flex | UnknownUnit | Color; | ||
export type BuildVarAst = CSSValueAST<`build-var`> & { | ||
subType: string; | ||
id: string; | ||
}; | ||
export declare type Literal = CSSValueAST<`literal`> & { | ||
export type Literal = CSSValueAST<`literal`> & { | ||
before: string; | ||
after: string; | ||
}; | ||
export declare type CssWideKeyword = CSSValueAST<`css-wide-keyword`> & { | ||
export type CssWideKeyword = CSSValueAST<`css-wide-keyword`> & { | ||
value: AnyCase<`inherit` | `unset` | `initial`>; | ||
}; | ||
export declare type Space = CSSValueAST<`space`> & { | ||
export type Space = CSSValueAST<`space`> & { | ||
before: string; | ||
after: string; | ||
}; | ||
export declare type Comment = CSSValueAST<`comment`> & { | ||
export type Comment = CSSValueAST<`comment`> & { | ||
before: string; | ||
after: string; | ||
}; | ||
export declare type Call<TYPE extends string = 'call'> = CSSValueAST<TYPE> & { | ||
export type Call<TYPE extends string = 'call'> = CSSValueAST<TYPE> & { | ||
args: CSSValueAST<any>[]; | ||
@@ -34,27 +34,27 @@ before: string; | ||
}; | ||
declare type Unit<TYPE extends string, UNIT extends string> = CSSValueAST<TYPE> & { | ||
type Unit<TYPE extends string, UNIT extends string> = CSSValueAST<TYPE> & { | ||
unit: UNIT; | ||
integer: boolean; | ||
}; | ||
export declare type Invalid = CSSValueAST<`invalid`>; | ||
export declare type UnknownUnit = Unit<`unknown-unit`, string>; | ||
export declare type CustomIdent = CSSValueAST<`<custom-ident>`>; | ||
export declare type DashedIdent = CSSValueAST<`<dashed-ident>`>; | ||
export declare type String = CSSValueAST<`<string>`>; | ||
export declare type Url = CSSValueAST<`<url>`>; | ||
export declare type Integer = CSSValueAST<`<integer>`>; | ||
export declare type Number = CSSValueAST<`<number>`>; | ||
export declare type Percentage = Unit<`<percentage>`, `%`>; | ||
export declare type Ratio = CSSValueAST<`<ratio>`>; | ||
export declare type Length = Unit<`<length>`, AnyCase<typeof lengthValidUnits[number]>>; | ||
export declare type Flex = Unit<`<flex>`, AnyCase<`fr`>>; | ||
export declare type Angle = Unit<`<angle>`, AnyCase<typeof angleValidUnits[number]>>; | ||
export declare type Time = Unit<`<time>`, AnyCase<typeof timeValidUnits[number]>>; | ||
export declare type Frequency = Unit<`<frequency>`, AnyCase<typeof frequencyValidUnits[number]>>; | ||
export declare type Resolution = Unit<`<resolution>`, AnyCase<typeof resolutionValidUnits[number]>>; | ||
export declare type Color = CSSValueAST<`<color>`>; | ||
export declare type Image = CSSValueAST<`<image>`>; | ||
export declare type Position = CSSValueAST<`<position>`>; | ||
export declare type Attribute = CSSValueAST<`<attr()>`>; | ||
declare type AnyCase<T extends string> = string extends T ? string : T extends `${infer F1}${infer F2}${infer R}` ? `${Uppercase<F1> | Lowercase<F1>}${Uppercase<F2> | Lowercase<F2>}${AnyCase<R>}` : T extends `${infer F}${infer R}` ? `${Uppercase<F> | Lowercase<F>}${AnyCase<R>}` : ''; | ||
export type Invalid = CSSValueAST<`invalid`>; | ||
export type UnknownUnit = Unit<`unknown-unit`, string>; | ||
export type CustomIdent = CSSValueAST<`<custom-ident>`>; | ||
export type DashedIdent = CSSValueAST<`<dashed-ident>`>; | ||
export type String = CSSValueAST<`<string>`>; | ||
export type Url = CSSValueAST<`<url>`>; | ||
export type Integer = CSSValueAST<`<integer>`>; | ||
export type Number = CSSValueAST<`<number>`>; | ||
export type Percentage = Unit<`<percentage>`, `%`>; | ||
export type Ratio = CSSValueAST<`<ratio>`>; | ||
export type Length = Unit<`<length>`, AnyCase<typeof lengthValidUnits[number]>>; | ||
export type Flex = Unit<`<flex>`, AnyCase<`fr`>>; | ||
export type Angle = Unit<`<angle>`, AnyCase<typeof angleValidUnits[number]>>; | ||
export type Time = Unit<`<time>`, AnyCase<typeof timeValidUnits[number]>>; | ||
export type Frequency = Unit<`<frequency>`, AnyCase<typeof frequencyValidUnits[number]>>; | ||
export type Resolution = Unit<`<resolution>`, AnyCase<typeof resolutionValidUnits[number]>>; | ||
export type Color = CSSValueAST<`<color>`>; | ||
export type Image = CSSValueAST<`<image>`>; | ||
export type Position = CSSValueAST<`<position>`>; | ||
export type Attribute = CSSValueAST<`<attr()>`>; | ||
type AnyCase<T extends string> = string extends T ? string : T extends `${infer F1}${infer F2}${infer R}` ? `${Uppercase<F1> | Lowercase<F1>}${Uppercase<F2> | Lowercase<F2>}${AnyCase<R>}` : T extends `${infer F}${infer R}` ? `${Uppercase<F> | Lowercase<F>}${AnyCase<R>}` : ''; | ||
export declare const invalid: (value: Partial<Invalid>) => Invalid; | ||
@@ -61,0 +61,0 @@ export declare const literal: (value: Partial<Literal>) => Literal; |
@@ -38,3 +38,3 @@ import type { CSSValueAST, BuildVarAst } from './ast-types'; | ||
} | ||
declare type MatchClassification = (node: CSSValueAST<any>, info: { | ||
type MatchClassification = (node: CSSValueAST<any>, info: { | ||
index: number; | ||
@@ -44,3 +44,3 @@ indexOfType: number; | ||
}) => boolean; | ||
declare type Classification = { | ||
type Classification = { | ||
value: CSSValueAST<any>[]; | ||
@@ -47,0 +47,0 @@ resolved: { |
import type { Token, Descriptors } from '@tokey/core'; | ||
declare type Delimiters = '(' | ')' | ',' | '/' | '+' | '-' | '*' | '#'; | ||
export declare type CSSValueToken = Token<Descriptors | Delimiters>; | ||
type Delimiters = '(' | ')' | ',' | '/' | '+' | '-' | '*' | '#' | '.'; | ||
export type CSSValueToken = Token<Descriptors | Delimiters>; | ||
export declare function tokenizeValue(source: string, options?: { | ||
@@ -5,0 +5,0 @@ offset?: number; |
@@ -32,3 +32,4 @@ "use strict"; | ||
char === '*' || | ||
char === '#'); | ||
char === '#' || | ||
char === '.'); | ||
//# sourceMappingURL=tokenizer.js.map |
import { BaseAstNode } from './ast-types'; | ||
export declare type ParseResults = Array<BaseAstNode>; | ||
export type ParseResults = Array<BaseAstNode>; | ||
export declare function parseCSSValue(source: string, _options?: { | ||
@@ -4,0 +4,0 @@ parseBuildVar?: () => { |
@@ -128,2 +128,14 @@ "use strict"; | ||
} | ||
else if (type === '.') { | ||
if (parseNumber(token, ast, source, s)) { | ||
// number parsed | ||
} | ||
else { | ||
ast.push((0, ast_types_1.literal)({ | ||
start, | ||
end, | ||
value, | ||
})); | ||
} | ||
} | ||
else if ((0, core_1.isComment)(type)) { | ||
@@ -190,3 +202,6 @@ ast.push((0, ast_types_1.comment)({ | ||
let nextToken = s.peek(peekCount); | ||
while (nextToken.type === `-` || nextToken.type === `+` || nextToken.type === `text`) { | ||
while (nextToken.type === `-` || | ||
nextToken.type === `+` || | ||
nextToken.type === `.` || | ||
nextToken.type === `text`) { | ||
const nextValue = numberValue + nextToken.value; | ||
@@ -279,3 +294,3 @@ // const validNumber = isNumber(nextValue); | ||
}; | ||
const NumberRegExp = /[-+]?(\d+\.?\d*|\d*\.?\d+)(e[-+]?\d+)?/i; | ||
const NumberRegExp = /^[-+]?(\d+\.?\d*|\d*\.?\d+)(e[-+]?\d+)?/i; | ||
const integerRegExp = /^[-+]?\d+$/i; // one or more decimal digits 0 through 9 (preceded by -/+ ) - https://www.w3.org/TR/css-values-4/#integer-value | ||
@@ -285,3 +300,4 @@ const validNumberRegex = [ | ||
/[-+]?\d+(e[-+]?\d*)?/, | ||
/[-+]/, // sign | ||
/[-+]/, | ||
/[-+]?\./, // optional-sign+dot | ||
] | ||
@@ -288,0 +304,0 @@ .map((r) => r.source) |
export declare const isWhitespace: (char: string) => boolean; | ||
export declare function parseValueSyntax(source: string): ValueSyntaxAstNode; | ||
declare type Range = [min: number, max: number]; | ||
type Range = [min: number, max: number]; | ||
interface Multipliers { | ||
@@ -50,5 +50,5 @@ range?: Range; | ||
} | ||
declare type Combinators = GroupNode | JuxtaposingNode | DoubleAmpersandNode | DoubleBarNode | BarNode; | ||
declare type Components = DataTypeNode | PropertyNode; | ||
export declare type ValueSyntaxAstNode = Components | KeywordNode | LiteralNode | Combinators; | ||
type Combinators = GroupNode | JuxtaposingNode | DoubleAmpersandNode | DoubleBarNode | BarNode; | ||
type Components = DataTypeNode | PropertyNode; | ||
export type ValueSyntaxAstNode = Components | KeywordNode | LiteralNode | Combinators; | ||
export declare function literal(name: string, enclosed?: boolean, multipliers?: Multipliers): LiteralNode; | ||
@@ -55,0 +55,0 @@ export declare function keyword(name: string, multipliers?: Multipliers): KeywordNode; |
@@ -190,33 +190,41 @@ "use strict"; | ||
else if (token.type === '{') { | ||
const node = lastParsedNode(ast); | ||
if (!node || isLowLevelGroup(node) || node.type === 'juxtaposing') { | ||
throw new Error('unexpected range modifier'); | ||
if (s.peekBack().type === 'space') { | ||
ast.push(literal(token.value, false)); | ||
} | ||
const start = s.eat('space').take('text'); | ||
if (!start) { | ||
throw new Error('missing range start value'); | ||
} | ||
const sep = s.eat('space').take(','); | ||
if (sep) { | ||
const end = s.eat('space').take('text'); | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
else { | ||
const node = lastParsedNode(ast); | ||
if (!node || isLowLevelGroup(node) || node.type === 'juxtaposing') { | ||
throw new Error('unexpected range modifier'); | ||
} | ||
(_b = node.multipliers) !== null && _b !== void 0 ? _b : (node.multipliers = {}); | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
const start = s.eat('space').take('text'); | ||
if (!start) { | ||
throw new Error('missing range start value'); | ||
} | ||
node.multipliers.range = [parseNumber(start.value), parseNumber((_c = end === null || end === void 0 ? void 0 : end.value) !== null && _c !== void 0 ? _c : '∞')]; | ||
} | ||
else { | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
const sep = s.eat('space').take(','); | ||
if (sep) { | ||
const end = s.eat('space').take('text'); | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
} | ||
(_b = node.multipliers) !== null && _b !== void 0 ? _b : (node.multipliers = {}); | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
} | ||
node.multipliers.range = [ | ||
parseNumber(start.value), | ||
parseNumber((_c = end === null || end === void 0 ? void 0 : end.value) !== null && _c !== void 0 ? _c : '∞'), | ||
]; | ||
} | ||
(_d = node.multipliers) !== null && _d !== void 0 ? _d : (node.multipliers = {}); | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
else { | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
} | ||
(_d = node.multipliers) !== null && _d !== void 0 ? _d : (node.multipliers = {}); | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
} | ||
node.multipliers.range = [parseNumber(start.value), parseNumber(start.value)]; | ||
} | ||
node.multipliers.range = [parseNumber(start.value), parseNumber(start.value)]; | ||
} | ||
@@ -242,2 +250,5 @@ } | ||
} | ||
else if (token.type === '}') { | ||
ast.push(literal(token.value, false)); | ||
} | ||
else { | ||
@@ -244,0 +255,0 @@ throw new Error(`UnHandled ${JSON.stringify(token)}`); |
{ | ||
"name": "@tokey/css-value-parser", | ||
"description": "value parser for css", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"main": "dist/index.js", | ||
@@ -13,5 +13,2 @@ "types": "dist/index.d.ts", | ||
}, | ||
"devDependencies": { | ||
"@webref/css": "^3.0.8" | ||
}, | ||
"keywords": [ | ||
@@ -18,0 +15,0 @@ "parser", |
@@ -13,3 +13,3 @@ import { | ||
type Delimiters = '(' | ')' | ',' | '/' | '+' | '-' | '*' | '#'; | ||
type Delimiters = '(' | ')' | ',' | '/' | '+' | '-' | '*' | '#' | '.'; | ||
@@ -46,2 +46,3 @@ export type CSSValueToken = Token<Descriptors | Delimiters>; | ||
char === '*' || | ||
char === '#'); | ||
char === '#' || | ||
char === '.'); |
@@ -181,2 +181,14 @@ import { | ||
} | ||
} else if (type === '.') { | ||
if (parseNumber(token, ast, source, s)) { | ||
// number parsed | ||
} else { | ||
ast.push( | ||
literal({ | ||
start, | ||
end, | ||
value, | ||
}) | ||
); | ||
} | ||
} else if (isComment(type)) { | ||
@@ -259,3 +271,8 @@ ast.push( | ||
let nextToken = s.peek(peekCount); | ||
while (nextToken.type === `-` || nextToken.type === `+` || nextToken.type === `text`) { | ||
while ( | ||
nextToken.type === `-` || | ||
nextToken.type === `+` || | ||
nextToken.type === `.` || | ||
nextToken.type === `text` | ||
) { | ||
const nextValue = numberValue + nextToken.value; | ||
@@ -352,3 +369,3 @@ // const validNumber = isNumber(nextValue); | ||
const NumberRegExp = /[-+]?(\d+\.?\d*|\d*\.?\d+)(e[-+]?\d+)?/i; | ||
const NumberRegExp = /^[-+]?(\d+\.?\d*|\d*\.?\d+)(e[-+]?\d+)?/i; | ||
const integerRegExp = /^[-+]?\d+$/i; // one or more decimal digits 0 through 9 (preceded by -/+ ) - https://www.w3.org/TR/css-values-4/#integer-value | ||
@@ -359,2 +376,3 @@ const validNumberRegex = [ | ||
/[-+]/, // sign | ||
/[-+]?\./, // optional-sign+dot | ||
] | ||
@@ -361,0 +379,0 @@ .map((r) => r.source) |
@@ -287,35 +287,42 @@ import { tokenize, createToken, Token, Seeker, getText, last } from '@tokey/core'; | ||
} else if (token.type === '{') { | ||
const node = lastParsedNode(ast); | ||
if (s.peekBack().type === 'space') { | ||
ast.push(literal(token.value, false)); | ||
} else { | ||
const node = lastParsedNode(ast); | ||
if (!node || isLowLevelGroup(node) || node.type === 'juxtaposing') { | ||
throw new Error('unexpected range modifier'); | ||
} | ||
if (!node || isLowLevelGroup(node) || node.type === 'juxtaposing') { | ||
throw new Error('unexpected range modifier'); | ||
} | ||
const start = s.eat('space').take('text'); | ||
if (!start) { | ||
throw new Error('missing range start value'); | ||
} | ||
const sep = s.eat('space').take(','); | ||
if (sep) { | ||
const end = s.eat('space').take('text'); | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
const start = s.eat('space').take('text'); | ||
if (!start) { | ||
throw new Error('missing range start value'); | ||
} | ||
const sep = s.eat('space').take(','); | ||
if (sep) { | ||
const end = s.eat('space').take('text'); | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
} | ||
node.multipliers ??= {}; | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
node.multipliers ??= {}; | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
} | ||
node.multipliers.range = [ | ||
parseNumber(start.value), | ||
parseNumber(end?.value ?? '∞'), | ||
]; | ||
} else { | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
} | ||
node.multipliers ??= {}; | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
} | ||
node.multipliers.range = [parseNumber(start.value), parseNumber(start.value)]; | ||
} | ||
node.multipliers.range = [parseNumber(start.value), parseNumber(end?.value ?? '∞')]; | ||
} else { | ||
const close = s.eat('space').take('}'); | ||
if (!close) { | ||
throw new Error('missing }'); | ||
} | ||
node.multipliers ??= {}; | ||
if (node.multipliers.range) { | ||
throw new Error('multiple multipliers on same node'); | ||
} | ||
node.multipliers.range = [parseNumber(start.value), parseNumber(start.value)]; | ||
} | ||
@@ -337,2 +344,4 @@ } else if (token.type === '#') { | ||
ast.push(s.take('|') ? doubleBar([]) : bar([])); | ||
} else if (token.type === '}') { | ||
ast.push(literal(token.value, false)); | ||
} else { | ||
@@ -339,0 +348,0 @@ throw new Error(`UnHandled ${JSON.stringify(token)}`); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138064
0
2778