@ismail-elkorchi/css-parser 0.1.0 → 0.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 +67 -69
- package/dist/internal/cssom/declarations.d.ts +36 -0
- package/dist/internal/cssom/declarations.js +177 -0
- package/dist/internal/generated/css-data.d.ts +2 -0
- package/dist/internal/generated/css-data.js +16229 -0
- package/dist/internal/grammar/catalog-types.d.ts +26 -0
- package/dist/internal/grammar/value-definition.d.ts +63 -0
- package/dist/internal/grammar/value-definition.js +439 -0
- package/dist/internal/properties/matcher.d.ts +29 -0
- package/dist/internal/properties/matcher.js +791 -0
- package/dist/internal/properties/registry.d.ts +24 -0
- package/dist/internal/properties/registry.js +52 -0
- package/dist/internal/selectors/matcher.d.ts +96 -0
- package/dist/internal/selectors/matcher.js +616 -0
- package/dist/internal/selectors/parser.d.ts +2 -0
- package/dist/internal/selectors/parser.js +702 -0
- package/dist/internal/selectors/specificity.d.ts +3 -0
- package/dist/internal/selectors/specificity.js +77 -0
- package/dist/internal/selectors/types.d.ts +110 -0
- package/dist/internal/syntax/ast.d.ts +70 -0
- package/dist/internal/syntax/ast.js +1 -0
- package/dist/internal/syntax/characters.d.ts +8 -0
- package/dist/internal/syntax/characters.js +45 -0
- package/dist/internal/syntax/encoding.d.ts +15 -0
- package/dist/internal/syntax/encoding.js +161 -0
- package/dist/internal/syntax/input.d.ts +23 -0
- package/dist/internal/syntax/input.js +184 -0
- package/dist/internal/syntax/parser.d.ts +26 -0
- package/dist/internal/syntax/parser.js +581 -0
- package/dist/internal/syntax/resources.d.ts +28 -0
- package/dist/internal/syntax/resources.js +135 -0
- package/dist/internal/syntax/serialize.d.ts +10 -0
- package/dist/internal/syntax/serialize.js +630 -0
- package/dist/internal/syntax/token-stream.d.ts +16 -0
- package/dist/internal/syntax/token-stream.js +66 -0
- package/dist/internal/syntax/tokenizer.d.ts +21 -0
- package/dist/internal/syntax/tokenizer.js +571 -0
- package/dist/internal/syntax/tokens.d.ts +115 -0
- package/dist/internal/syntax/tokens.js +1 -0
- package/dist/internal/syntax/types.d.ts +46 -0
- package/dist/internal/syntax/types.js +1 -0
- package/dist/mod.d.ts +7 -1
- package/dist/mod.js +7 -1
- package/dist/public/edits.d.ts +12 -0
- package/dist/public/edits.js +195 -0
- package/dist/public/mod.d.ts +13 -35
- package/dist/public/mod.js +12 -1740
- package/dist/public/parse.d.ts +37 -0
- package/dist/public/parse.js +298 -0
- package/dist/public/traversal.d.ts +13 -0
- package/dist/public/traversal.js +96 -0
- package/dist/public/types.d.ts +78 -264
- package/package.json +32 -53
- package/THIRD_PARTY_NOTICES.md +0 -19
- package/dist/internal/csstree-runtime.d.ts +0 -20
- package/dist/internal/csstree-runtime.js +0 -21
- package/dist/internal/encoding/mod.d.ts +0 -1
- package/dist/internal/encoding/mod.js +0 -1
- package/dist/internal/encoding/sniff.d.ts +0 -14
- package/dist/internal/encoding/sniff.js +0 -95
- package/dist/internal/serializer/mod.d.ts +0 -1
- package/dist/internal/serializer/mod.js +0 -1
- package/dist/internal/serializer/serialize.d.ts +0 -3
- package/dist/internal/serializer/serialize.js +0 -89
- package/dist/internal/tokenizer/mod.d.ts +0 -2
- package/dist/internal/tokenizer/mod.js +0 -1
- package/dist/internal/tokenizer/tokenize.d.ts +0 -2
- package/dist/internal/tokenizer/tokenize.js +0 -39
- package/dist/internal/tokenizer/tokens.d.ts +0 -23
- package/dist/internal/tree/build.d.ts +0 -2
- package/dist/internal/tree/build.js +0 -85
- package/dist/internal/tree/mod.d.ts +0 -2
- package/dist/internal/tree/mod.js +0 -1
- package/dist/internal/tree/types.d.ts +0 -25
- package/dist/internal/vendor/csstree/LICENSE +0 -19
- package/dist/internal/vendor/csstree/csstree.esm.js +0 -12
- package/dist/internal/version.d.ts +0 -1
- package/dist/internal/version.js +0 -1
- package/dist/public/index.d.ts +0 -1
- package/dist/public/index.js +0 -1
- /package/dist/internal/{tokenizer/tokens.js → grammar/catalog-types.js} +0 -0
- /package/dist/internal/{tree → selectors}/types.js +0 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { ResourceGuard } from "./resources.js";
|
|
2
|
+
const CARRIAGE_RETURN = 0x0d;
|
|
3
|
+
const FORM_FEED = 0x0c;
|
|
4
|
+
const LINE_FEED = 0x0a;
|
|
5
|
+
const NULL = 0x00;
|
|
6
|
+
const REPLACEMENT_CHARACTER = 0xfffd;
|
|
7
|
+
export function utf8ByteLength(input) {
|
|
8
|
+
let bytes = 0;
|
|
9
|
+
for (let offset = 0; offset < input.length; offset += 1) {
|
|
10
|
+
const first = input.charCodeAt(offset);
|
|
11
|
+
if (first <= 0x7f) {
|
|
12
|
+
bytes += 1;
|
|
13
|
+
}
|
|
14
|
+
else if (first <= 0x7ff) {
|
|
15
|
+
bytes += 2;
|
|
16
|
+
}
|
|
17
|
+
else if (first >= 0xd800 && first <= 0xdbff) {
|
|
18
|
+
const second = input.charCodeAt(offset + 1);
|
|
19
|
+
if (second >= 0xdc00 && second <= 0xdfff) {
|
|
20
|
+
bytes += 4;
|
|
21
|
+
offset += 1;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
bytes += 3;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
bytes += 3;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return bytes;
|
|
32
|
+
}
|
|
33
|
+
function decodeAt(input, offset) {
|
|
34
|
+
if (offset >= input.length) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const first = input.charCodeAt(offset);
|
|
38
|
+
if (first === CARRIAGE_RETURN) {
|
|
39
|
+
const following = input.charCodeAt(offset + 1);
|
|
40
|
+
return {
|
|
41
|
+
value: LINE_FEED,
|
|
42
|
+
endOffset: following === LINE_FEED ? offset + 2 : offset + 1
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
if (first === FORM_FEED) {
|
|
46
|
+
return { value: LINE_FEED, endOffset: offset + 1 };
|
|
47
|
+
}
|
|
48
|
+
if (first === NULL) {
|
|
49
|
+
return { value: REPLACEMENT_CHARACTER, endOffset: offset + 1 };
|
|
50
|
+
}
|
|
51
|
+
if (first >= 0xd800 && first <= 0xdbff) {
|
|
52
|
+
const second = input.charCodeAt(offset + 1);
|
|
53
|
+
if (second >= 0xdc00 && second <= 0xdfff) {
|
|
54
|
+
return {
|
|
55
|
+
value: ((first - 0xd800) * 0x400) + (second - 0xdc00) + 0x10000,
|
|
56
|
+
endOffset: offset + 2
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return { value: REPLACEMENT_CHARACTER, endOffset: offset + 1 };
|
|
60
|
+
}
|
|
61
|
+
if (first >= 0xdc00 && first <= 0xdfff) {
|
|
62
|
+
return { value: REPLACEMENT_CHARACTER, endOffset: offset + 1 };
|
|
63
|
+
}
|
|
64
|
+
return { value: first, endOffset: offset + 1 };
|
|
65
|
+
}
|
|
66
|
+
export class InputCursor {
|
|
67
|
+
#input;
|
|
68
|
+
#guard;
|
|
69
|
+
#offset = 0;
|
|
70
|
+
#line = 1;
|
|
71
|
+
#column = 1;
|
|
72
|
+
#lastMark = null;
|
|
73
|
+
constructor(input, guard = new ResourceGuard()) {
|
|
74
|
+
this.#input = input;
|
|
75
|
+
this.#guard = guard;
|
|
76
|
+
}
|
|
77
|
+
get sourceLength() {
|
|
78
|
+
return this.#input.length;
|
|
79
|
+
}
|
|
80
|
+
get offset() {
|
|
81
|
+
return this.#offset;
|
|
82
|
+
}
|
|
83
|
+
get eof() {
|
|
84
|
+
return this.#offset >= this.#input.length;
|
|
85
|
+
}
|
|
86
|
+
position() {
|
|
87
|
+
return Object.freeze({
|
|
88
|
+
offset: this.#offset,
|
|
89
|
+
line: this.#line,
|
|
90
|
+
column: this.#column
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
mark() {
|
|
94
|
+
return Object.freeze({
|
|
95
|
+
offset: this.#offset,
|
|
96
|
+
line: this.#line,
|
|
97
|
+
column: this.#column
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
restore(mark) {
|
|
101
|
+
if (!Number.isSafeInteger(mark.offset) ||
|
|
102
|
+
mark.offset < 0 ||
|
|
103
|
+
mark.offset > this.#input.length ||
|
|
104
|
+
!Number.isSafeInteger(mark.line) ||
|
|
105
|
+
mark.line < 1 ||
|
|
106
|
+
!Number.isSafeInteger(mark.column) ||
|
|
107
|
+
mark.column < 1) {
|
|
108
|
+
throw new RangeError("invalid input cursor mark");
|
|
109
|
+
}
|
|
110
|
+
this.#offset = mark.offset;
|
|
111
|
+
this.#line = mark.line;
|
|
112
|
+
this.#column = mark.column;
|
|
113
|
+
this.#lastMark = null;
|
|
114
|
+
}
|
|
115
|
+
peek(distance = 0) {
|
|
116
|
+
if (!Number.isSafeInteger(distance) || distance < 0) {
|
|
117
|
+
throw new RangeError("peek distance must be a non-negative safe integer");
|
|
118
|
+
}
|
|
119
|
+
this.#guard.step();
|
|
120
|
+
let offset = this.#offset;
|
|
121
|
+
let decoded = null;
|
|
122
|
+
for (let index = 0; index <= distance; index += 1) {
|
|
123
|
+
decoded = decodeAt(this.#input, offset);
|
|
124
|
+
if (decoded === null) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
offset = decoded.endOffset;
|
|
128
|
+
}
|
|
129
|
+
return decoded?.value ?? null;
|
|
130
|
+
}
|
|
131
|
+
consume() {
|
|
132
|
+
this.#guard.step();
|
|
133
|
+
const decoded = decodeAt(this.#input, this.#offset);
|
|
134
|
+
if (decoded === null) {
|
|
135
|
+
this.#lastMark = null;
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
const start = this.position();
|
|
139
|
+
this.#lastMark = this.mark();
|
|
140
|
+
this.#offset = decoded.endOffset;
|
|
141
|
+
if (decoded.value === LINE_FEED) {
|
|
142
|
+
this.#line += 1;
|
|
143
|
+
this.#column = 1;
|
|
144
|
+
}
|
|
145
|
+
else {
|
|
146
|
+
this.#column += 1;
|
|
147
|
+
}
|
|
148
|
+
return Object.freeze({
|
|
149
|
+
value: decoded.value,
|
|
150
|
+
span: Object.freeze({
|
|
151
|
+
start,
|
|
152
|
+
end: this.position()
|
|
153
|
+
})
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
reconsume() {
|
|
157
|
+
if (this.#lastMark === null) {
|
|
158
|
+
throw new Error("no consumed code point is available to reconsume");
|
|
159
|
+
}
|
|
160
|
+
const mark = this.#lastMark;
|
|
161
|
+
this.restore(mark);
|
|
162
|
+
}
|
|
163
|
+
slice(start, end) {
|
|
164
|
+
if (!Number.isSafeInteger(start) ||
|
|
165
|
+
!Number.isSafeInteger(end) ||
|
|
166
|
+
start < 0 ||
|
|
167
|
+
end < start ||
|
|
168
|
+
end > this.#input.length) {
|
|
169
|
+
throw new RangeError("invalid source slice");
|
|
170
|
+
}
|
|
171
|
+
return this.#input.slice(start, end);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export function preprocessCssInput(input, guard = new ResourceGuard()) {
|
|
175
|
+
const cursor = new InputCursor(input, guard);
|
|
176
|
+
let result = "";
|
|
177
|
+
for (;;) {
|
|
178
|
+
const point = cursor.consume();
|
|
179
|
+
if (point === null) {
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
result += String.fromCodePoint(point.value);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { CommaSeparatedComponentValuesResult, ComponentValue, ComponentValuesResult, CssBlockItem, CssDeclaration, CssRule, CssStylesheet, SyntaxResult } from "./ast.js";
|
|
2
|
+
import type { ParserResourceLimits } from "./types.js";
|
|
3
|
+
export interface SyntaxParserOptions {
|
|
4
|
+
readonly limits?: ParserResourceLimits;
|
|
5
|
+
readonly signal?: AbortSignal;
|
|
6
|
+
}
|
|
7
|
+
export declare class CssSyntaxParser {
|
|
8
|
+
#private;
|
|
9
|
+
constructor(input: string, options?: SyntaxParserOptions);
|
|
10
|
+
parseStylesheet(): SyntaxResult<CssStylesheet>;
|
|
11
|
+
parseStylesheetContents(): SyntaxResult<readonly CssRule[]>;
|
|
12
|
+
parseBlockContents(): SyntaxResult<readonly CssBlockItem[]>;
|
|
13
|
+
parseRule(): SyntaxResult<CssRule>;
|
|
14
|
+
parseDeclaration(): SyntaxResult<CssDeclaration>;
|
|
15
|
+
parseComponentValue(): SyntaxResult<ComponentValue>;
|
|
16
|
+
parseComponentValues(): ComponentValuesResult;
|
|
17
|
+
parseCommaSeparatedComponentValues(): CommaSeparatedComponentValuesResult;
|
|
18
|
+
}
|
|
19
|
+
export declare function parseCssStylesheet(input: string, options?: SyntaxParserOptions): SyntaxResult<CssStylesheet>;
|
|
20
|
+
export declare function parseCssStylesheetContents(input: string, options?: SyntaxParserOptions): SyntaxResult<readonly CssRule[]>;
|
|
21
|
+
export declare function parseCssBlockContents(input: string, options?: SyntaxParserOptions): SyntaxResult<readonly CssBlockItem[]>;
|
|
22
|
+
export declare function parseCssRule(input: string, options?: SyntaxParserOptions): SyntaxResult<CssRule>;
|
|
23
|
+
export declare function parseCssDeclaration(input: string, options?: SyntaxParserOptions): SyntaxResult<CssDeclaration>;
|
|
24
|
+
export declare function parseCssComponentValue(input: string, options?: SyntaxParserOptions): SyntaxResult<ComponentValue>;
|
|
25
|
+
export declare function parseCssComponentValues(input: string, options?: SyntaxParserOptions): ComponentValuesResult;
|
|
26
|
+
export declare function parseCssCommaSeparatedComponentValues(input: string, options?: SyntaxParserOptions): CommaSeparatedComponentValuesResult;
|