@dazl/shorthands-opener 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/compounds/compound-css-data.d.ts +22 -0
- package/dist/compounds/compound-css-data.d.ts.map +1 -0
- package/dist/compounds/compound-css-data.js +2 -0
- package/dist/compounds/compound-css-data.js.map +1 -0
- package/dist/compounds/compound-mapper.d.ts +4 -0
- package/dist/compounds/compound-mapper.d.ts.map +1 -0
- package/dist/compounds/compound-mapper.js +10 -0
- package/dist/compounds/compound-mapper.js.map +1 -0
- package/dist/compounds/compound-parser-utils.d.ts +3 -0
- package/dist/compounds/compound-parser-utils.d.ts.map +1 -0
- package/dist/compounds/compound-parser-utils.js +33 -0
- package/dist/compounds/compound-parser-utils.js.map +1 -0
- package/dist/compounds/compound-types.d.ts +26 -0
- package/dist/compounds/compound-types.d.ts.map +1 -0
- package/dist/compounds/compound-types.js +2 -0
- package/dist/compounds/compound-types.js.map +1 -0
- package/dist/compounds/compound-value-parsers.d.ts +6 -0
- package/dist/compounds/compound-value-parsers.d.ts.map +1 -0
- package/dist/compounds/compound-value-parsers.js +5 -0
- package/dist/compounds/compound-value-parsers.js.map +1 -0
- package/dist/compounds/index.d.ts +6 -0
- package/dist/compounds/index.d.ts.map +1 -0
- package/dist/compounds/index.js +6 -0
- package/dist/compounds/index.js.map +1 -0
- package/dist/compounds/parsers/index.d.ts +2 -0
- package/dist/compounds/parsers/index.d.ts.map +1 -0
- package/dist/compounds/parsers/index.js +2 -0
- package/dist/compounds/parsers/index.js.map +1 -0
- package/dist/compounds/parsers/shadow-compound.d.ts +5 -0
- package/dist/compounds/parsers/shadow-compound.d.ts.map +1 -0
- package/dist/compounds/parsers/shadow-compound.js +39 -0
- package/dist/compounds/parsers/shadow-compound.js.map +1 -0
- package/dist/css-data-types/data-types-consts.d.ts +212 -0
- package/dist/css-data-types/data-types-consts.d.ts.map +1 -0
- package/dist/css-data-types/data-types-consts.js +790 -0
- package/dist/css-data-types/data-types-consts.js.map +1 -0
- package/dist/css-data-types/data-types-predicates.d.ts +32 -0
- package/dist/css-data-types/data-types-predicates.d.ts.map +1 -0
- package/dist/css-data-types/data-types-predicates.js +353 -0
- package/dist/css-data-types/data-types-predicates.js.map +1 -0
- package/dist/css-data-types/data-types-state-machines.d.ts +14 -0
- package/dist/css-data-types/data-types-state-machines.d.ts.map +1 -0
- package/dist/css-data-types/data-types-state-machines.js +129 -0
- package/dist/css-data-types/data-types-state-machines.js.map +1 -0
- package/dist/css-data-types/data-types-types.d.ts +26 -0
- package/dist/css-data-types/data-types-types.d.ts.map +1 -0
- package/dist/css-data-types/data-types-types.js +2 -0
- package/dist/css-data-types/data-types-types.js.map +1 -0
- package/dist/css-data-types/data-types-utils.d.ts +31 -0
- package/dist/css-data-types/data-types-utils.d.ts.map +1 -0
- package/dist/css-data-types/data-types-utils.js +176 -0
- package/dist/css-data-types/data-types-utils.js.map +1 -0
- package/dist/css-data-types/data-types.d.ts +64 -0
- package/dist/css-data-types/data-types.d.ts.map +1 -0
- package/dist/css-data-types/data-types.js +304 -0
- package/dist/css-data-types/data-types.js.map +1 -0
- package/dist/css-data-types/index.d.ts +7 -0
- package/dist/css-data-types/index.d.ts.map +1 -0
- package/dist/css-data-types/index.js +7 -0
- package/dist/css-data-types/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/shorthands/index.d.ts +8 -0
- package/dist/shorthands/index.d.ts.map +1 -0
- package/dist/shorthands/index.js +8 -0
- package/dist/shorthands/index.js.map +1 -0
- package/dist/shorthands/openers/background-position-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/background-position-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/background-position-shorthand.js +173 -0
- package/dist/shorthands/openers/background-position-shorthand.js.map +1 -0
- package/dist/shorthands/openers/background-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/background-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/background-shorthand.js +88 -0
- package/dist/shorthands/openers/background-shorthand.js.map +1 -0
- package/dist/shorthands/openers/border-image-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/border-image-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/border-image-shorthand.js +16 -0
- package/dist/shorthands/openers/border-image-shorthand.js.map +1 -0
- package/dist/shorthands/openers/border-radius-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/border-radius-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/border-radius-shorthand.js +61 -0
- package/dist/shorthands/openers/border-radius-shorthand.js.map +1 -0
- package/dist/shorthands/openers/border-shorthand.d.ts +20 -0
- package/dist/shorthands/openers/border-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/border-shorthand.js +90 -0
- package/dist/shorthands/openers/border-shorthand.js.map +1 -0
- package/dist/shorthands/openers/flex-flow-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/flex-flow-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/flex-flow-shorthand.js +13 -0
- package/dist/shorthands/openers/flex-flow-shorthand.js.map +1 -0
- package/dist/shorthands/openers/flex-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/flex-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/flex-shorthand.js +55 -0
- package/dist/shorthands/openers/flex-shorthand.js.map +1 -0
- package/dist/shorthands/openers/font-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/font-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/font-shorthand.js +60 -0
- package/dist/shorthands/openers/font-shorthand.js.map +1 -0
- package/dist/shorthands/openers/gap-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/gap-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/gap-shorthand.js +28 -0
- package/dist/shorthands/openers/gap-shorthand.js.map +1 -0
- package/dist/shorthands/openers/grid-axis-shorthand.d.ts +6 -0
- package/dist/shorthands/openers/grid-axis-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/grid-axis-shorthand.js +36 -0
- package/dist/shorthands/openers/grid-axis-shorthand.js.map +1 -0
- package/dist/shorthands/openers/grid-gap-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/grid-gap-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/grid-gap-shorthand.js +21 -0
- package/dist/shorthands/openers/grid-gap-shorthand.js.map +1 -0
- package/dist/shorthands/openers/grid-shorthand.d.ts +8 -0
- package/dist/shorthands/openers/grid-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/grid-shorthand.js +145 -0
- package/dist/shorthands/openers/grid-shorthand.js.map +1 -0
- package/dist/shorthands/openers/grid-template-shorthand.d.ts +10 -0
- package/dist/shorthands/openers/grid-template-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/grid-template-shorthand.js +113 -0
- package/dist/shorthands/openers/grid-template-shorthand.js.map +1 -0
- package/dist/shorthands/openers/index.d.ts +22 -0
- package/dist/shorthands/openers/index.d.ts.map +1 -0
- package/dist/shorthands/openers/index.js +22 -0
- package/dist/shorthands/openers/index.js.map +1 -0
- package/dist/shorthands/openers/list-style-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/list-style-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/list-style-shorthand.js +15 -0
- package/dist/shorthands/openers/list-style-shorthand.js.map +1 -0
- package/dist/shorthands/openers/margin-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/margin-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/margin-shorthand.js +11 -0
- package/dist/shorthands/openers/margin-shorthand.js.map +1 -0
- package/dist/shorthands/openers/outline-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/outline-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/outline-shorthand.js +14 -0
- package/dist/shorthands/openers/outline-shorthand.js.map +1 -0
- package/dist/shorthands/openers/overflow-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/overflow-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/overflow-shorthand.js +21 -0
- package/dist/shorthands/openers/overflow-shorthand.js.map +1 -0
- package/dist/shorthands/openers/padding-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/padding-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/padding-shorthand.js +11 -0
- package/dist/shorthands/openers/padding-shorthand.js.map +1 -0
- package/dist/shorthands/openers/place-content-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/place-content-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/place-content-shorthand.js +29 -0
- package/dist/shorthands/openers/place-content-shorthand.js.map +1 -0
- package/dist/shorthands/openers/place-items-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/place-items-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/place-items-shorthand.js +23 -0
- package/dist/shorthands/openers/place-items-shorthand.js.map +1 -0
- package/dist/shorthands/openers/text-decoration-shorthand.d.ts +5 -0
- package/dist/shorthands/openers/text-decoration-shorthand.d.ts.map +1 -0
- package/dist/shorthands/openers/text-decoration-shorthand.js +14 -0
- package/dist/shorthands/openers/text-decoration-shorthand.js.map +1 -0
- package/dist/shorthands/shorthand-css-data.d.ts +120 -0
- package/dist/shorthands/shorthand-css-data.d.ts.map +1 -0
- package/dist/shorthands/shorthand-css-data.js +48 -0
- package/dist/shorthands/shorthand-css-data.js.map +1 -0
- package/dist/shorthands/shorthand-mapper.d.ts +6 -0
- package/dist/shorthands/shorthand-mapper.d.ts.map +1 -0
- package/dist/shorthands/shorthand-mapper.js +72 -0
- package/dist/shorthands/shorthand-mapper.js.map +1 -0
- package/dist/shorthands/shorthand-parser-errors.d.ts +25 -0
- package/dist/shorthands/shorthand-parser-errors.d.ts.map +1 -0
- package/dist/shorthands/shorthand-parser-errors.js +41 -0
- package/dist/shorthands/shorthand-parser-errors.js.map +1 -0
- package/dist/shorthands/shorthand-parser-utils.d.ts +24 -0
- package/dist/shorthands/shorthand-parser-utils.d.ts.map +1 -0
- package/dist/shorthands/shorthand-parser-utils.js +398 -0
- package/dist/shorthands/shorthand-parser-utils.js.map +1 -0
- package/dist/shorthands/shorthand-types.d.ts +64 -0
- package/dist/shorthands/shorthand-types.d.ts.map +1 -0
- package/dist/shorthands/shorthand-types.js +2 -0
- package/dist/shorthands/shorthand-types.js.map +1 -0
- package/dist/shorthands/shorthands-ast-evaluation.d.ts +5 -0
- package/dist/shorthands/shorthands-ast-evaluation.d.ts.map +1 -0
- package/dist/shorthands/shorthands-ast-evaluation.js +19 -0
- package/dist/shorthands/shorthands-ast-evaluation.js.map +1 -0
- package/dist/tokenizers/css-value-tokenizer.d.ts +43 -0
- package/dist/tokenizers/css-value-tokenizer.d.ts.map +1 -0
- package/dist/tokenizers/css-value-tokenizer.js +154 -0
- package/dist/tokenizers/css-value-tokenizer.js.map +1 -0
- package/dist/tokenizers/index.d.ts +2 -0
- package/dist/tokenizers/index.d.ts.map +1 -0
- package/dist/tokenizers/index.js +2 -0
- package/dist/tokenizers/index.js.map +1 -0
- package/package.json +54 -0
- package/src/compounds/compound-css-data.ts +24 -0
- package/src/compounds/compound-mapper.ts +21 -0
- package/src/compounds/compound-parser-utils.ts +47 -0
- package/src/compounds/compound-types.ts +35 -0
- package/src/compounds/compound-value-parsers.ts +12 -0
- package/src/compounds/index.ts +5 -0
- package/src/compounds/parsers/index.ts +1 -0
- package/src/compounds/parsers/shadow-compound.ts +56 -0
- package/src/css-data-types/data-types-consts.ts +877 -0
- package/src/css-data-types/data-types-predicates.ts +477 -0
- package/src/css-data-types/data-types-state-machines.ts +169 -0
- package/src/css-data-types/data-types-types.ts +43 -0
- package/src/css-data-types/data-types-utils.ts +258 -0
- package/src/css-data-types/data-types.ts +435 -0
- package/src/css-data-types/index.ts +6 -0
- package/src/index.ts +4 -0
- package/src/shorthands/index.ts +7 -0
- package/src/shorthands/openers/background-position-shorthand.ts +180 -0
- package/src/shorthands/openers/background-shorthand.ts +161 -0
- package/src/shorthands/openers/border-image-shorthand.ts +35 -0
- package/src/shorthands/openers/border-radius-shorthand.ts +93 -0
- package/src/shorthands/openers/border-shorthand.ts +198 -0
- package/src/shorthands/openers/flex-flow-shorthand.ts +24 -0
- package/src/shorthands/openers/flex-shorthand.ts +79 -0
- package/src/shorthands/openers/font-shorthand.ts +85 -0
- package/src/shorthands/openers/gap-shorthand.ts +47 -0
- package/src/shorthands/openers/grid-axis-shorthand.ts +61 -0
- package/src/shorthands/openers/grid-gap-shorthand.ts +40 -0
- package/src/shorthands/openers/grid-shorthand.ts +260 -0
- package/src/shorthands/openers/grid-template-shorthand.ts +176 -0
- package/src/shorthands/openers/index.ts +21 -0
- package/src/shorthands/openers/list-style-shorthand.ts +33 -0
- package/src/shorthands/openers/margin-shorthand.ts +20 -0
- package/src/shorthands/openers/outline-shorthand.ts +27 -0
- package/src/shorthands/openers/overflow-shorthand.ts +40 -0
- package/src/shorthands/openers/padding-shorthand.ts +21 -0
- package/src/shorthands/openers/place-content-shorthand.ts +49 -0
- package/src/shorthands/openers/place-items-shorthand.ts +43 -0
- package/src/shorthands/openers/text-decoration-shorthand.ts +27 -0
- package/src/shorthands/shorthand-css-data.ts +210 -0
- package/src/shorthands/shorthand-mapper.ts +157 -0
- package/src/shorthands/shorthand-parser-errors.ts +47 -0
- package/src/shorthands/shorthand-parser-utils.ts +602 -0
- package/src/shorthands/shorthand-types.ts +107 -0
- package/src/shorthands/shorthands-ast-evaluation.ts +38 -0
- package/src/tokenizers/css-value-tokenizer.ts +220 -0
- package/src/tokenizers/index.ts +1 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { CSSCodeAst } from '../tokenizers/index.js';
|
|
2
|
+
import type { CSSAstNode, ParseShorthandAPI, EvaluatedAst, AstEvaluator } from './shorthand-types.js';
|
|
3
|
+
|
|
4
|
+
const createEvaluateAst =
|
|
5
|
+
<V, T>(
|
|
6
|
+
handleExpression: (node: V, api: ParseShorthandAPI<V>) => T[],
|
|
7
|
+
handleNonExpression: (node: CSSCodeAst) => T,
|
|
8
|
+
): AstEvaluator<V, T> =>
|
|
9
|
+
(ast, api) => {
|
|
10
|
+
const res: T[] = [];
|
|
11
|
+
for (const node of ast) {
|
|
12
|
+
if (api.isExpression(node)) {
|
|
13
|
+
res.push(...handleExpression(node, api));
|
|
14
|
+
} else {
|
|
15
|
+
res.push(handleNonExpression(node));
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return res;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const evaluateExpression = <V>(node: V, api: ParseShorthandAPI<V>) => evaluateAstInner(api.getValue(node), api);
|
|
22
|
+
|
|
23
|
+
const evaluateAstInner = <V>(ast: CSSCodeAst[], api: ParseShorthandAPI<V>): CSSCodeAst[] =>
|
|
24
|
+
createEvaluateAst<V, CSSCodeAst>(
|
|
25
|
+
(node, api) => evaluateExpression(node, api),
|
|
26
|
+
(node) => node,
|
|
27
|
+
)(ast, api);
|
|
28
|
+
|
|
29
|
+
const evaluateCodeNode = <V>(value: CSSCodeAst): EvaluatedAst<V> => ({ value });
|
|
30
|
+
export const codeToEvaluatedAst = <V>(ast: CSSCodeAst[]): EvaluatedAst<V>[] =>
|
|
31
|
+
ast.map((value) => evaluateCodeNode<V>(value));
|
|
32
|
+
|
|
33
|
+
// TODO: Always have origin to values that don't come from an initial value (that are specified directly)
|
|
34
|
+
export const evaluateAst = <V>(ast: CSSAstNode<V>[], api: ParseShorthandAPI<V>): EvaluatedAst<V>[] =>
|
|
35
|
+
createEvaluateAst<V, EvaluatedAst<V>>(
|
|
36
|
+
(origin, api) => evaluateExpression(origin, api).map((value) => ({ value, origin })),
|
|
37
|
+
evaluateCodeNode,
|
|
38
|
+
)(ast, api);
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Token,
|
|
5
|
+
type Descriptors,
|
|
6
|
+
tokenize,
|
|
7
|
+
isStringDelimiter,
|
|
8
|
+
isWhitespace,
|
|
9
|
+
getJSCommentStartType,
|
|
10
|
+
getMultilineCommentStartType,
|
|
11
|
+
isCommentEnd,
|
|
12
|
+
createToken,
|
|
13
|
+
getText,
|
|
14
|
+
getUnclosedComment,
|
|
15
|
+
} from '@tokey/core';
|
|
16
|
+
|
|
17
|
+
export type { Descriptors };
|
|
18
|
+
export type Delimiters = '(' | ')' | ',' | '/';
|
|
19
|
+
export type SeparatorTokens = 'line-comment' | 'multi-comment' | 'space';
|
|
20
|
+
export type CSSValueCodeToken = Token<Descriptors | Delimiters>;
|
|
21
|
+
export type CSSSeparatorTokens = Token<SeparatorTokens>;
|
|
22
|
+
export type CSSCodeAst = StringNode | MethodCall | TextNode | CommaNode | SlashNode;
|
|
23
|
+
export interface ASTNode<Types = Descriptors> {
|
|
24
|
+
type: Types;
|
|
25
|
+
text: string;
|
|
26
|
+
start: number;
|
|
27
|
+
end: number;
|
|
28
|
+
before: CSSSeparatorTokens[];
|
|
29
|
+
after: CSSSeparatorTokens[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface MethodCall extends ASTNode<'call'> {
|
|
33
|
+
name: string;
|
|
34
|
+
args: CSSCodeAst[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface StringNode extends ASTNode<'string'> {}
|
|
38
|
+
export interface TextNode extends ASTNode<'text'> {}
|
|
39
|
+
export interface CommaNode extends ASTNode<','> {}
|
|
40
|
+
export interface SlashNode extends ASTNode<'/'> {}
|
|
41
|
+
|
|
42
|
+
export const DEFAULT_POSITION = -1;
|
|
43
|
+
export const URL_CALL_TOKEN = 'url';
|
|
44
|
+
|
|
45
|
+
export const isSeparatorToken = (token: CSSValueCodeToken): token is CSSSeparatorTokens => {
|
|
46
|
+
const { type } = token;
|
|
47
|
+
return type === 'line-comment' || type === 'multi-comment' || type === 'space';
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const isCSSCodeAst = (ast: unknown): ast is CSSCodeAst => {
|
|
51
|
+
const node = ast as CSSCodeAst;
|
|
52
|
+
return (
|
|
53
|
+
node.text !== undefined &&
|
|
54
|
+
node.type !== undefined &&
|
|
55
|
+
(node.type === 'string' ||
|
|
56
|
+
node.type === 'call' ||
|
|
57
|
+
node.type === 'text' ||
|
|
58
|
+
node.type === ',' ||
|
|
59
|
+
node.type === '/')
|
|
60
|
+
);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export function createCssValueAST(source: string, parseLineComments = false): CSSCodeAst[] {
|
|
64
|
+
return parseDeclValueTokens(
|
|
65
|
+
source,
|
|
66
|
+
tokenize<CSSValueCodeToken>(source, {
|
|
67
|
+
isDelimiter,
|
|
68
|
+
isStringDelimiter,
|
|
69
|
+
isWhitespace,
|
|
70
|
+
shouldAddToken,
|
|
71
|
+
createToken,
|
|
72
|
+
getCommentStartType: parseLineComments ? getJSCommentStartType : getMultilineCommentStartType,
|
|
73
|
+
isCommentEnd,
|
|
74
|
+
getUnclosedComment,
|
|
75
|
+
}),
|
|
76
|
+
).ast;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const isDelimiter = (char: string) => char === '(' || char === ')' || char === ',' || char === '/';
|
|
80
|
+
|
|
81
|
+
const shouldAddToken = () => true;
|
|
82
|
+
|
|
83
|
+
function parseDeclValueTokens(
|
|
84
|
+
source: string,
|
|
85
|
+
tokens: CSSValueCodeToken[],
|
|
86
|
+
startAtIdx = 0,
|
|
87
|
+
): { ast: CSSCodeAst[]; stoppedAtIdx: number } {
|
|
88
|
+
const ast: CSSCodeAst[] = [];
|
|
89
|
+
let before: CSSSeparatorTokens[] = [];
|
|
90
|
+
for (let i = startAtIdx; i < tokens.length; i++) {
|
|
91
|
+
const token = tokens[i]!;
|
|
92
|
+
if (token.type === ')') {
|
|
93
|
+
const lastAst = ast[ast.length - 1];
|
|
94
|
+
if (lastAst && before.length) {
|
|
95
|
+
lastAst.after = before;
|
|
96
|
+
before = [];
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
ast,
|
|
100
|
+
stoppedAtIdx: i,
|
|
101
|
+
};
|
|
102
|
+
} else if (isSeparatorToken(token)) {
|
|
103
|
+
before.push(token);
|
|
104
|
+
} else if (token.type === 'text' && tokens[i + 1]?.type === '(') {
|
|
105
|
+
const res = parseDeclValueTokens(source, tokens, i + 2);
|
|
106
|
+
const methodText = getText(tokens, i, res.stoppedAtIdx + 1, source);
|
|
107
|
+
i = res.stoppedAtIdx;
|
|
108
|
+
res.ast = getUrlTokensAst(token, res.ast);
|
|
109
|
+
ast.push({
|
|
110
|
+
type: 'call',
|
|
111
|
+
text: methodText,
|
|
112
|
+
start: token.start,
|
|
113
|
+
end: token.start + methodText.length,
|
|
114
|
+
before,
|
|
115
|
+
after: [],
|
|
116
|
+
name: token.value,
|
|
117
|
+
args: res.ast,
|
|
118
|
+
});
|
|
119
|
+
before = [];
|
|
120
|
+
} else {
|
|
121
|
+
ast.push({
|
|
122
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
123
|
+
type: token.type as any, // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
124
|
+
text: token.value,
|
|
125
|
+
start: token.start,
|
|
126
|
+
end: token.end,
|
|
127
|
+
before,
|
|
128
|
+
after: [],
|
|
129
|
+
});
|
|
130
|
+
before = [];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return {
|
|
135
|
+
ast,
|
|
136
|
+
stoppedAtIdx: tokens.length,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function getUrlTokensAst(token: CSSValueCodeToken, ast: CSSCodeAst[]): CSSCodeAst[] {
|
|
141
|
+
if (token.value !== URL_CALL_TOKEN || ast.length === 0) {
|
|
142
|
+
return ast;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const pushFixedTextNodeToFixedAst = () => {
|
|
146
|
+
if (fixedTextNode) {
|
|
147
|
+
fixedTextNode.end = fixedTextNode.start + fixedTextNode.text.length;
|
|
148
|
+
fixedAst.push(fixedTextNode);
|
|
149
|
+
fixedTextNode = null;
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
let fixedTextNode: TextNode | null = null;
|
|
154
|
+
const fixedAst: CSSCodeAst[] = [];
|
|
155
|
+
for (const node of ast) {
|
|
156
|
+
if (node.type === '/' || node.type === 'text') {
|
|
157
|
+
if (!fixedTextNode) {
|
|
158
|
+
fixedTextNode = {
|
|
159
|
+
type: 'text',
|
|
160
|
+
text: '',
|
|
161
|
+
before: node.before,
|
|
162
|
+
after: [],
|
|
163
|
+
start: node.start,
|
|
164
|
+
end: DEFAULT_POSITION,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
fixedTextNode.text += node.text;
|
|
168
|
+
} else {
|
|
169
|
+
pushFixedTextNodeToFixedAst();
|
|
170
|
+
fixedAst.push(node);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
pushFixedTextNodeToFixedAst();
|
|
175
|
+
|
|
176
|
+
return fixedAst;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface NodePositions {
|
|
180
|
+
beforeStart: number;
|
|
181
|
+
beforeEnd: number;
|
|
182
|
+
start: number;
|
|
183
|
+
end: number;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export const getNodePositions = (text: string, pos?: number, spaceBefore?: string): NodePositions => ({
|
|
187
|
+
beforeStart: !spaceBefore || pos === undefined ? DEFAULT_POSITION : pos - spaceBefore.length,
|
|
188
|
+
beforeEnd: !spaceBefore || pos === undefined ? DEFAULT_POSITION : pos,
|
|
189
|
+
start: pos === undefined ? DEFAULT_POSITION : pos,
|
|
190
|
+
end: pos === undefined ? DEFAULT_POSITION : pos + text.length,
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
export const valueTextNode = (text: string, pos?: number, spaceBefore?: string): TextNode => {
|
|
194
|
+
const { start, end, beforeEnd, beforeStart } = getNodePositions(text, pos, spaceBefore);
|
|
195
|
+
return {
|
|
196
|
+
type: 'text',
|
|
197
|
+
start,
|
|
198
|
+
end,
|
|
199
|
+
before: spaceBefore
|
|
200
|
+
? [
|
|
201
|
+
{
|
|
202
|
+
type: 'space',
|
|
203
|
+
start: beforeStart,
|
|
204
|
+
end: beforeEnd,
|
|
205
|
+
value: spaceBefore,
|
|
206
|
+
},
|
|
207
|
+
]
|
|
208
|
+
: [],
|
|
209
|
+
after: [],
|
|
210
|
+
text,
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
export const getTokensText = (tokens: CSSSeparatorTokens[]): string => {
|
|
215
|
+
return tokens.map((token) => token.value).join('');
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export const getFullText = (ast: CSSCodeAst): string => {
|
|
219
|
+
return getTokensText(ast.before) + ast.text + getTokensText(ast.after);
|
|
220
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './css-value-tokenizer.js';
|