@projectwallace/css-parser 0.13.5 → 0.13.6

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.
Files changed (41) hide show
  1. package/dist/css-node-2ejJUrIw.js +645 -0
  2. package/dist/css-node-DqyvMXBN.d.ts +313 -0
  3. package/dist/index.d.ts +150 -16
  4. package/dist/index.js +103 -13
  5. package/dist/parse-anplusb.d.ts +26 -2
  6. package/dist/parse-anplusb.js +191 -207
  7. package/dist/parse-atrule-prelude.d.ts +40 -2
  8. package/dist/parse-atrule-prelude.js +556 -652
  9. package/dist/parse-declaration.d.ts +16 -2
  10. package/dist/parse-declaration.js +140 -167
  11. package/dist/parse-dimension-CCn_XRDe.js +177 -0
  12. package/dist/parse-dimension.d.ts +6 -3
  13. package/dist/parse-dimension.js +1 -35
  14. package/dist/parse-selector.d.ts +37 -2
  15. package/dist/parse-selector.js +508 -635
  16. package/dist/parse-utils-DnsZRpfd.js +98 -0
  17. package/dist/parse-value.d.ts +23 -2
  18. package/dist/parse-value.js +176 -224
  19. package/dist/parse.d.ts +38 -8
  20. package/dist/parse.js +274 -353
  21. package/dist/tokenize-BQFB1jXg.js +540 -0
  22. package/dist/tokenize-odLrcjj2.d.ts +110 -0
  23. package/dist/tokenize.d.ts +2 -26
  24. package/dist/tokenize.js +1 -545
  25. package/package.json +20 -26
  26. package/dist/arena.d.ts +0 -60
  27. package/dist/arena.js +0 -291
  28. package/dist/char-types.d.ts +0 -14
  29. package/dist/char-types.js +0 -53
  30. package/dist/constants.d.ts +0 -44
  31. package/dist/constants.js +0 -51
  32. package/dist/css-node.d.ts +0 -203
  33. package/dist/css-node.js +0 -498
  34. package/dist/parse-utils.d.ts +0 -1
  35. package/dist/parse-utils.js +0 -60
  36. package/dist/string-utils.d.ts +0 -99
  37. package/dist/string-utils.js +0 -129
  38. package/dist/token-types.d.ts +0 -35
  39. package/dist/token-types.js +0 -29
  40. package/dist/walk.d.ts +0 -28
  41. package/dist/walk.js +0 -51
@@ -1,210 +1,194 @@
1
- import { Lexer } from './tokenize.js';
2
- import { NTH_SELECTOR, CSSDataArena } from './arena.js';
3
- import { TOKEN_IDENT, TOKEN_DELIM, TOKEN_DIMENSION, TOKEN_NUMBER } from './token-types.js';
4
- import { str_equals, CHAR_MINUS_HYPHEN, CHAR_PLUS, str_index_of } from './string-utils.js';
5
- import { CSSNode } from './css-node.js';
6
-
7
- class ANplusBParser {
8
- lexer;
9
- arena;
10
- source;
11
- expr_end;
12
- constructor(arena, source) {
13
- this.arena = arena;
14
- this.source = source;
15
- this.lexer = new Lexer(source);
16
- this.expr_end = 0;
17
- }
18
- /**
19
- * Parse An+B expression
20
- * Examples: odd, even, 3, n, -n, 2n, 2n+1, -3n-5
21
- */
22
- parse_anplusb(start, end, line = 1) {
23
- this.expr_end = end;
24
- this.lexer.seek(start, line);
25
- let b = null;
26
- let a_start = start;
27
- let a_end = start;
28
- let b_start = start;
29
- let b_end = start;
30
- const node_start = start;
31
- this.skip_whitespace();
32
- if (this.lexer.pos >= this.expr_end) {
33
- return null;
34
- }
35
- this.lexer.next_token_fast(true);
36
- if (this.lexer.token_type === TOKEN_IDENT) {
37
- const text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
38
- if (str_equals("odd", text) || str_equals("even", text)) {
39
- a_start = this.lexer.token_start;
40
- a_end = this.lexer.token_end;
41
- return this.create_anplusb_node(node_start, a_start, a_end, 0, 0);
42
- }
43
- const first_char = this.source.charCodeAt(this.lexer.token_start);
44
- const second_char = this.lexer.token_end > this.lexer.token_start + 1 ? this.source.charCodeAt(this.lexer.token_start + 1) : 0;
45
- if (first_char === CHAR_MINUS_HYPHEN && second_char === 110) {
46
- if (this.lexer.token_end > this.lexer.token_start + 2) {
47
- const third_char = this.source.charCodeAt(this.lexer.token_start + 2);
48
- if (third_char === CHAR_MINUS_HYPHEN) {
49
- a_start = this.lexer.token_start;
50
- a_end = this.lexer.token_start + 2;
51
- b = this.source.substring(this.lexer.token_start + 2, this.lexer.token_end);
52
- b_start = this.lexer.token_start + 2;
53
- b_end = this.lexer.token_end;
54
- return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
55
- }
56
- }
57
- a_start = this.lexer.token_start;
58
- a_end = this.lexer.token_start + 2;
59
- b = this.parse_b_part();
60
- if (b !== null) {
61
- b_start = this.lexer.token_start;
62
- b_end = this.lexer.token_end;
63
- }
64
- return this.create_anplusb_node(node_start, a_start, a_end, b !== null ? b_start : 0, b !== null ? b_end : 0);
65
- }
66
- if (first_char === 110) {
67
- if (this.lexer.token_end > this.lexer.token_start + 1) {
68
- const second_char2 = this.source.charCodeAt(this.lexer.token_start + 1);
69
- if (second_char2 === CHAR_MINUS_HYPHEN) {
70
- a_start = this.lexer.token_start;
71
- a_end = this.lexer.token_start + 1;
72
- b = this.source.substring(this.lexer.token_start + 1, this.lexer.token_end);
73
- b_start = this.lexer.token_start + 1;
74
- b_end = this.lexer.token_end;
75
- return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
76
- }
77
- }
78
- a_start = this.lexer.token_start;
79
- a_end = this.lexer.token_start + 1;
80
- b = this.parse_b_part();
81
- if (b !== null) {
82
- b_start = this.lexer.token_start;
83
- b_end = this.lexer.token_end;
84
- }
85
- return this.create_anplusb_node(node_start, a_start, a_end, b !== null ? b_start : 0, b !== null ? b_end : 0);
86
- }
87
- return null;
88
- }
89
- if (this.lexer.token_type === TOKEN_DELIM && this.source.charCodeAt(this.lexer.token_start) === CHAR_PLUS) {
90
- const saved = this.lexer.save_position();
91
- this.lexer.next_token_fast(true);
92
- if (this.lexer.token_type === TOKEN_IDENT) {
93
- const text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
94
- const first_char = text.charCodeAt(0);
95
- if (first_char === 110) {
96
- a_start = saved.pos - 1;
97
- a_end = this.lexer.token_start + 1;
98
- if (this.lexer.token_end > this.lexer.token_start + 1) {
99
- const second_char = this.source.charCodeAt(this.lexer.token_start + 1);
100
- if (second_char === CHAR_MINUS_HYPHEN) {
101
- b = this.source.substring(this.lexer.token_start + 1, this.lexer.token_end);
102
- b_start = this.lexer.token_start + 1;
103
- b_end = this.lexer.token_end;
104
- return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
105
- }
106
- }
107
- b = this.parse_b_part();
108
- if (b !== null) {
109
- b_start = this.lexer.token_start;
110
- b_end = this.lexer.token_end;
111
- }
112
- return this.create_anplusb_node(node_start, a_start, a_end, b !== null ? b_start : 0, b !== null ? b_end : 0);
113
- }
114
- }
115
- this.lexer.restore_position(saved);
116
- }
117
- if (this.lexer.token_type === TOKEN_DIMENSION) {
118
- const token_text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
119
- const n_index = str_index_of(token_text, "n");
120
- if (n_index !== -1) {
121
- a_start = this.lexer.token_start;
122
- a_end = this.lexer.token_start + n_index + 1;
123
- if (n_index + 1 < token_text.length) {
124
- const remainder = token_text.substring(n_index + 1);
125
- if (remainder.charCodeAt(0) === CHAR_MINUS_HYPHEN) {
126
- b = remainder;
127
- b_start = this.lexer.token_start + n_index + 1;
128
- b_end = this.lexer.token_end;
129
- return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
130
- }
131
- }
132
- b = this.parse_b_part();
133
- if (b !== null) {
134
- b_start = this.lexer.token_start;
135
- b_end = this.lexer.token_end;
136
- }
137
- return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
138
- }
139
- }
140
- if (this.lexer.token_type === TOKEN_NUMBER) {
141
- let num_text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
142
- b = num_text;
143
- b_start = this.lexer.token_start;
144
- b_end = this.lexer.token_end;
145
- return this.create_anplusb_node(node_start, 0, 0, b_start, b_end);
146
- }
147
- return null;
148
- }
149
- /**
150
- * Parse the b part after 'n'
151
- * Handles: +5, -3, whitespace variations
152
- */
153
- parse_b_part() {
154
- this.skip_whitespace();
155
- if (this.lexer.pos >= this.expr_end) {
156
- return null;
157
- }
158
- this.lexer.next_token_fast(true);
159
- if (this.lexer.token_type === TOKEN_DELIM) {
160
- const ch = this.source.charCodeAt(this.lexer.token_start);
161
- if (ch === CHAR_PLUS || ch === CHAR_MINUS_HYPHEN) {
162
- const sign = ch === CHAR_MINUS_HYPHEN ? "-" : "";
163
- this.skip_whitespace();
164
- this.lexer.next_token_fast(true);
165
- if (this.lexer.token_type === TOKEN_NUMBER) {
166
- let num_text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
167
- if (num_text.charCodeAt(0) === CHAR_PLUS) {
168
- num_text = num_text.substring(1);
169
- }
170
- return sign === "-" ? sign + num_text : num_text;
171
- }
172
- }
173
- }
174
- if (this.lexer.token_type === TOKEN_NUMBER) {
175
- let num_text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
176
- const first_char = num_text.charCodeAt(0);
177
- if (first_char === CHAR_PLUS || first_char === CHAR_MINUS_HYPHEN) {
178
- if (first_char === CHAR_PLUS) {
179
- num_text = num_text.substring(1);
180
- }
181
- return num_text;
182
- }
183
- }
184
- return null;
185
- }
186
- skip_whitespace() {
187
- this.lexer.skip_whitespace_in_range(this.expr_end);
188
- }
189
- create_anplusb_node(start, a_start, a_end, b_start, b_end) {
190
- const node = this.arena.create_node(NTH_SELECTOR, start, this.lexer.pos - start, this.lexer.line, 1);
191
- if (a_end > a_start) {
192
- this.arena.set_content_start_delta(node, a_start - start);
193
- this.arena.set_content_length(node, a_end - a_start);
194
- }
195
- if (b_end > b_start) {
196
- this.arena.set_value_start_delta(node, b_start - start);
197
- this.arena.set_value_length(node, b_end - b_start);
198
- }
199
- return node;
200
- }
201
- }
1
+ import { t as Lexer } from "./tokenize-BQFB1jXg.js";
2
+ import { C as CSSDataArena, r as CSSNode } from "./css-node-2ejJUrIw.js";
3
+ import { o as str_equals, s as str_index_of } from "./parse-dimension-CCn_XRDe.js";
4
+ //#region src/parse-anplusb.ts
5
+ /** @internal */
6
+ var ANplusBParser = class {
7
+ lexer;
8
+ arena;
9
+ source;
10
+ expr_end;
11
+ constructor(arena, source) {
12
+ this.arena = arena;
13
+ this.source = source;
14
+ this.lexer = new Lexer(source);
15
+ this.expr_end = 0;
16
+ }
17
+ /**
18
+ * Parse An+B expression
19
+ * Examples: odd, even, 3, n, -n, 2n, 2n+1, -3n-5
20
+ */
21
+ parse_anplusb(start, end, line = 1) {
22
+ this.expr_end = end;
23
+ this.lexer.seek(start, line);
24
+ let b = null;
25
+ let a_start = start;
26
+ let a_end = start;
27
+ let b_start = start;
28
+ let b_end = start;
29
+ const node_start = start;
30
+ this.skip_whitespace();
31
+ if (this.lexer.pos >= this.expr_end) return null;
32
+ this.lexer.next_token_fast(true);
33
+ if (this.lexer.token_type === 1) {
34
+ const text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
35
+ if (str_equals("odd", text) || str_equals("even", text)) {
36
+ a_start = this.lexer.token_start;
37
+ a_end = this.lexer.token_end;
38
+ return this.create_anplusb_node(node_start, a_start, a_end, 0, 0);
39
+ }
40
+ const first_char = this.source.charCodeAt(this.lexer.token_start);
41
+ const second_char = this.lexer.token_end > this.lexer.token_start + 1 ? this.source.charCodeAt(this.lexer.token_start + 1) : 0;
42
+ if (first_char === 45 && second_char === 110) {
43
+ if (this.lexer.token_end > this.lexer.token_start + 2) {
44
+ if (this.source.charCodeAt(this.lexer.token_start + 2) === 45) {
45
+ a_start = this.lexer.token_start;
46
+ a_end = this.lexer.token_start + 2;
47
+ b = this.source.substring(this.lexer.token_start + 2, this.lexer.token_end);
48
+ b_start = this.lexer.token_start + 2;
49
+ b_end = this.lexer.token_end;
50
+ return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
51
+ }
52
+ }
53
+ a_start = this.lexer.token_start;
54
+ a_end = this.lexer.token_start + 2;
55
+ b = this.parse_b_part();
56
+ if (b !== null) {
57
+ b_start = this.lexer.token_start;
58
+ b_end = this.lexer.token_end;
59
+ }
60
+ return this.create_anplusb_node(node_start, a_start, a_end, b !== null ? b_start : 0, b !== null ? b_end : 0);
61
+ }
62
+ if (first_char === 110) {
63
+ if (this.lexer.token_end > this.lexer.token_start + 1) {
64
+ if (this.source.charCodeAt(this.lexer.token_start + 1) === 45) {
65
+ a_start = this.lexer.token_start;
66
+ a_end = this.lexer.token_start + 1;
67
+ b = this.source.substring(this.lexer.token_start + 1, this.lexer.token_end);
68
+ b_start = this.lexer.token_start + 1;
69
+ b_end = this.lexer.token_end;
70
+ return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
71
+ }
72
+ }
73
+ a_start = this.lexer.token_start;
74
+ a_end = this.lexer.token_start + 1;
75
+ b = this.parse_b_part();
76
+ if (b !== null) {
77
+ b_start = this.lexer.token_start;
78
+ b_end = this.lexer.token_end;
79
+ }
80
+ return this.create_anplusb_node(node_start, a_start, a_end, b !== null ? b_start : 0, b !== null ? b_end : 0);
81
+ }
82
+ return null;
83
+ }
84
+ if (this.lexer.token_type === 9 && this.source.charCodeAt(this.lexer.token_start) === 43) {
85
+ const saved = this.lexer.save_position();
86
+ this.lexer.next_token_fast(true);
87
+ if (this.lexer.token_type === 1) {
88
+ if (this.source.substring(this.lexer.token_start, this.lexer.token_end).charCodeAt(0) === 110) {
89
+ a_start = saved.pos - 1;
90
+ a_end = this.lexer.token_start + 1;
91
+ if (this.lexer.token_end > this.lexer.token_start + 1) {
92
+ if (this.source.charCodeAt(this.lexer.token_start + 1) === 45) {
93
+ b = this.source.substring(this.lexer.token_start + 1, this.lexer.token_end);
94
+ b_start = this.lexer.token_start + 1;
95
+ b_end = this.lexer.token_end;
96
+ return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
97
+ }
98
+ }
99
+ b = this.parse_b_part();
100
+ if (b !== null) {
101
+ b_start = this.lexer.token_start;
102
+ b_end = this.lexer.token_end;
103
+ }
104
+ return this.create_anplusb_node(node_start, a_start, a_end, b !== null ? b_start : 0, b !== null ? b_end : 0);
105
+ }
106
+ }
107
+ this.lexer.restore_position(saved);
108
+ }
109
+ if (this.lexer.token_type === 12) {
110
+ const token_text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
111
+ const n_index = str_index_of(token_text, "n");
112
+ if (n_index !== -1) {
113
+ a_start = this.lexer.token_start;
114
+ a_end = this.lexer.token_start + n_index + 1;
115
+ if (n_index + 1 < token_text.length) {
116
+ const remainder = token_text.substring(n_index + 1);
117
+ if (remainder.charCodeAt(0) === 45) {
118
+ b = remainder;
119
+ b_start = this.lexer.token_start + n_index + 1;
120
+ b_end = this.lexer.token_end;
121
+ return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
122
+ }
123
+ }
124
+ b = this.parse_b_part();
125
+ if (b !== null) {
126
+ b_start = this.lexer.token_start;
127
+ b_end = this.lexer.token_end;
128
+ }
129
+ return this.create_anplusb_node(node_start, a_start, a_end, b_start, b_end);
130
+ }
131
+ }
132
+ if (this.lexer.token_type === 10) {
133
+ b = this.source.substring(this.lexer.token_start, this.lexer.token_end);
134
+ b_start = this.lexer.token_start;
135
+ b_end = this.lexer.token_end;
136
+ return this.create_anplusb_node(node_start, 0, 0, b_start, b_end);
137
+ }
138
+ return null;
139
+ }
140
+ /**
141
+ * Parse the b part after 'n'
142
+ * Handles: +5, -3, whitespace variations
143
+ */
144
+ parse_b_part() {
145
+ this.skip_whitespace();
146
+ if (this.lexer.pos >= this.expr_end) return null;
147
+ this.lexer.next_token_fast(true);
148
+ if (this.lexer.token_type === 9) {
149
+ const ch = this.source.charCodeAt(this.lexer.token_start);
150
+ if (ch === 43 || ch === 45) {
151
+ const sign = ch === 45 ? "-" : "";
152
+ this.skip_whitespace();
153
+ this.lexer.next_token_fast(true);
154
+ if (this.lexer.token_type === 10) {
155
+ let num_text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
156
+ if (num_text.charCodeAt(0) === 43) num_text = num_text.substring(1);
157
+ return sign === "-" ? sign + num_text : num_text;
158
+ }
159
+ }
160
+ }
161
+ if (this.lexer.token_type === 10) {
162
+ let num_text = this.source.substring(this.lexer.token_start, this.lexer.token_end);
163
+ const first_char = num_text.charCodeAt(0);
164
+ if (first_char === 43 || first_char === 45) {
165
+ if (first_char === 43) num_text = num_text.substring(1);
166
+ return num_text;
167
+ }
168
+ }
169
+ return null;
170
+ }
171
+ skip_whitespace() {
172
+ this.lexer.skip_whitespace_in_range(this.expr_end);
173
+ }
174
+ create_anplusb_node(start, a_start, a_end, b_start, b_end) {
175
+ const node = this.arena.create_node(30, start, this.lexer.pos - start, this.lexer.line, 1);
176
+ if (a_end > a_start) {
177
+ this.arena.set_content_start_delta(node, a_start - start);
178
+ this.arena.set_content_length(node, a_end - a_start);
179
+ }
180
+ if (b_end > b_start) {
181
+ this.arena.set_value_start_delta(node, b_start - start);
182
+ this.arena.set_value_length(node, b_end - b_start);
183
+ }
184
+ return node;
185
+ }
186
+ };
202
187
  function parse_anplusb(expr) {
203
- const arena = new CSSDataArena(64);
204
- const parser = new ANplusBParser(arena, expr);
205
- const nodeIndex = parser.parse_anplusb(0, expr.length);
206
- if (nodeIndex === null) return null;
207
- return new CSSNode(arena, expr, nodeIndex);
188
+ const arena = new CSSDataArena(64);
189
+ const nodeIndex = new ANplusBParser(arena, expr).parse_anplusb(0, expr.length);
190
+ if (nodeIndex === null) return null;
191
+ return new CSSNode(arena, expr, nodeIndex);
208
192
  }
209
-
193
+ //#endregion
210
194
  export { ANplusBParser, parse_anplusb };
@@ -1,8 +1,46 @@
1
- import { CSSNode } from './css-node';
1
+ import { E as CSSDataArena, r as CSSNode } from "./css-node-DqyvMXBN.js";
2
+
3
+ //#region src/parse-atrule-prelude.d.ts
4
+ /** @internal */
5
+ declare class AtRulePreludeParser {
6
+ private lexer;
7
+ private arena;
8
+ private source;
9
+ private prelude_end;
10
+ constructor(arena: CSSDataArena, source: string);
11
+ parse_prelude(at_rule_name: string, start: number, end: number, line?: number, column?: number): number[];
12
+ private parse_prelude_dispatch;
13
+ private parse_media_query_list;
14
+ private create_node;
15
+ private is_and_or_not;
16
+ private parse_single_media_query;
17
+ private parse_media_feature;
18
+ private parse_container_query;
19
+ private parse_supports_query;
20
+ private parse_layer_names;
21
+ private parse_function_prelude;
22
+ private parse_identifier;
23
+ private parse_charset_prelude;
24
+ private parse_import_prelude;
25
+ private parse_import_url;
26
+ private parse_import_layer;
27
+ private parse_import_supports;
28
+ private skip_whitespace;
29
+ private peek_token_type;
30
+ private next_token;
31
+ private parse_value_token;
32
+ private parse_feature_value;
33
+ private parse_namespace_prelude;
34
+ private parse_scope_prelude;
35
+ private parse_custom_media_prelude;
36
+ private parse_feature_range;
37
+ }
2
38
  /**
3
39
  * Parse an at-rule prelude string and return an array of AST nodes
4
40
  * @param at_rule_name - The name of the at-rule (e.g., "media", "supports", "layer")
5
41
  * @param prelude - The at-rule prelude to parse (e.g., "(min-width: 768px)", "utilities")
6
42
  * @returns An array of CSSNode objects representing the parsed prelude
7
43
  */
8
- export declare function parse_atrule_prelude(at_rule_name: string, prelude: string): CSSNode[];
44
+ declare function parse_atrule_prelude(at_rule_name: string, prelude: string): CSSNode[];
45
+ //#endregion
46
+ export { AtRulePreludeParser, parse_atrule_prelude };