@teacss/classes 0.10.1 → 0.10.3

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.
@@ -0,0 +1,306 @@
1
+ // Ported from teacss-ts `packages-engine/core/src/group.ts` (0.10.1). Only `expandClassGroups` and what it reaches are kept.
2
+ /**
3
+ * Grouping `{decl;decl;…}@cond`.
4
+ *
5
+ * A pure, textual expansion run before the whitespace split: a `{ }` group is
6
+ * local DRY sugar that expands to individual atomic tokens and itself emits no
7
+ * CSS. Declarations inside are `;`-separated; any `@cond` immediately following
8
+ * the closing `}` is distributed to every declaration.
9
+ *
10
+ * {bg:red;c:white;p:4}@hover → bg:red@hover c:white@hover p:4@hover
11
+ * {bg:red;c:white} → bg:red c:white
12
+ *
13
+ * `()` and `[]` inside the group are literal regions: `;`, `}`, `@` inside
14
+ * them do not terminate the group or a declaration. Text outside groups is kept
15
+ * verbatim, so this is safe to run over whole source strings.
16
+ */
17
+ import { defineArrayAppend as appendOwn, attachedBracketRanges } from "./literal.js";
18
+ /** Consume CSS escapes, or delimiter/string characters protected inside `()` / `[]`. */
19
+ function consumeLiteral(code, index, state) {
20
+ const ch = code[index];
21
+ if (state.blockComment) {
22
+ if (ch === "*" && code[index + 1] === "/") {
23
+ state.blockComment = false;
24
+ return index + 1;
25
+ }
26
+ return index;
27
+ }
28
+ if (state.quote) {
29
+ if (ch === "\\")
30
+ return index + 1;
31
+ if (ch === state.quote)
32
+ state.quote = undefined;
33
+ return index;
34
+ }
35
+ if (state.closers.length > 0 && ch === "/" && code[index + 1] === "*") {
36
+ state.blockComment = true;
37
+ return index + 1;
38
+ }
39
+ if (state.closers.length > 0 && (ch === '"' || ch === "'")) {
40
+ state.quote = ch;
41
+ return index;
42
+ }
43
+ if (ch === "\\")
44
+ return index + 1;
45
+ if (ch === "(" || ch === "[") {
46
+ appendOwn(state.closers, ch === "(" ? ")" : "]");
47
+ return index;
48
+ }
49
+ if (ch === state.closers.at(-1)) {
50
+ state.closers.pop();
51
+ return index;
52
+ }
53
+ }
54
+ /** Find the next group boundary, respecting `()`/`[]` literal regions. */
55
+ function findClassGroupBoundary(code, open, limit = code.length) {
56
+ const state = { closers: [] };
57
+ for (let i = open + 1; i < limit; i++) {
58
+ const ch = code[i];
59
+ const consumed = consumeLiteral(code, i, state);
60
+ if (consumed !== undefined) {
61
+ i = consumed;
62
+ continue;
63
+ }
64
+ if (state.closers.length === 0) {
65
+ if (ch === "}")
66
+ return { index: i, kind: "close" };
67
+ if (ch === "{")
68
+ return { index: i, kind: "open" };
69
+ }
70
+ }
71
+ return { index: limit, kind: "limit" };
72
+ }
73
+ const trailingConditionBoundaryRE = /[\s'"`;{},\])]/;
74
+ function isTrailingConditionBoundary(code, index) {
75
+ const ch = code[index];
76
+ return trailingConditionBoundaryRE.test(ch) || (ch === "/" && (code[index + 1] === "/" || code[index + 1] === "*"));
77
+ }
78
+ /** A `!` right after a group's `}` and before its conditions makes every member important. */
79
+ function isGroupImportance(code, from) {
80
+ return code[from] === "!" && (from + 1 === code.length || code[from + 1] === "@" || isTrailingConditionBoundary(code, from + 1));
81
+ }
82
+ /** The member with `!` before its own conditions, as `p:4@md` becomes `p:4!@md`. */
83
+ function importantMember(member) {
84
+ const state = { closers: [] };
85
+ let index = 0;
86
+ for (; index < member.length; index++) {
87
+ const consumed = consumeLiteral(member, index, state);
88
+ if (consumed !== undefined) {
89
+ index = consumed;
90
+ continue;
91
+ }
92
+ if (state.closers.length === 0 && member[index] === "@")
93
+ break;
94
+ }
95
+ const declarationEnd = Math.min(index, member.length);
96
+ const declaration = member.slice(0, declarationEnd);
97
+ return `${declaration}${declaration.endsWith("!") ? "" : "!"}${member.slice(declarationEnd)}`;
98
+ }
99
+ /** Read the `@cond` run immediately after `}`, stopping at a host boundary. */
100
+ function readTrailingConditions(code, from, limit = code.length) {
101
+ if (code[from] !== "@")
102
+ return { conds: "", end: from };
103
+ const state = { closers: [] };
104
+ let i = from;
105
+ for (; i < limit; i++) {
106
+ const ch = code[i];
107
+ const consumed = consumeLiteral(code, i, state);
108
+ if (consumed !== undefined) {
109
+ i = consumed;
110
+ continue;
111
+ }
112
+ if (ch === "/" && (code[i + 1] === "/" || code[i + 1] === "*") && state.closers.length === 0)
113
+ break;
114
+ if (trailingConditionBoundaryRE.test(ch) && state.closers.length === 0)
115
+ break;
116
+ }
117
+ return { conds: code.slice(from, i), end: i };
118
+ }
119
+ /** Keep host classification semicolon-only; expansion also splits settled inner members. */
120
+ function splitDeclarations(body, includeWhitespace = false) {
121
+ const out = [];
122
+ const state = { closers: [] };
123
+ let start = 0;
124
+ for (let i = 0; i < body.length; i++) {
125
+ const ch = body[i];
126
+ const consumed = consumeLiteral(body, i, state);
127
+ if (consumed !== undefined) {
128
+ i = consumed;
129
+ continue;
130
+ }
131
+ if ((ch === ";" || (includeWhitespace && /\s/.test(ch))) && state.closers.length === 0) {
132
+ appendOwn(out, body.slice(start, i));
133
+ start = i + 1;
134
+ }
135
+ }
136
+ appendOwn(out, body.slice(start));
137
+ return out.map((s) => s.trim()).filter(Boolean);
138
+ }
139
+ /**
140
+ * A group body may hold another group. One pass expands the inner one and
141
+ * leaves the outer `{` in place, because a `{` inside a body ends the candidate
142
+ * — that guard is what keeps host object literals out. Repeating settles the
143
+ * outer group too; without it the leftover braces reach the whitespace split,
144
+ * which eats them and detaches the outer `@cond`.
145
+ */
146
+ export const MAX_GROUP_NESTING = 5;
147
+ const attachedClassPropertyRE = /^(?:--?[\w-]+|[A-Z_$][\w$-]*|@teacss-skip-bracket-[\da-z]{6})$/i;
148
+ function isAttachedClassFunctionStart(code, start) {
149
+ let functionStart = start;
150
+ while (functionStart > 0 && /[\w-]/.test(code[functionStart - 1]))
151
+ functionStart--;
152
+ if (functionStart === start || code[functionStart - 1] !== ":")
153
+ return false;
154
+ const propertyEnd = functionStart - 1;
155
+ let propertyStart = propertyEnd;
156
+ while (propertyStart > 0 && /[\w$@-]/.test(code[propertyStart - 1]))
157
+ propertyStart--;
158
+ if (!attachedClassPropertyRE.test(code.slice(propertyStart, propertyEnd)))
159
+ return false;
160
+ let previous = propertyStart - 1;
161
+ while (previous >= 0 && /\s/.test(code[previous]))
162
+ previous--;
163
+ return previous < 0 || !"{,?".includes(code[previous]);
164
+ }
165
+ function attachedClassFunctionRanges(code) {
166
+ const candidates = [];
167
+ const frames = [];
168
+ const bracketClosers = [];
169
+ let blockComment = false;
170
+ let quote;
171
+ for (let index = 0; index < code.length; index++) {
172
+ const char = code[index];
173
+ const next = code[index + 1];
174
+ if (frames.length === 0) {
175
+ if (char === "(" && isAttachedClassFunctionStart(code, index))
176
+ appendOwn(frames, { attached: true, start: index });
177
+ continue;
178
+ }
179
+ if (blockComment) {
180
+ if (char === "*" && next === "/") {
181
+ blockComment = false;
182
+ index++;
183
+ }
184
+ }
185
+ else if (quote) {
186
+ if (char === "\\")
187
+ index++;
188
+ else if (char === quote)
189
+ quote = undefined;
190
+ }
191
+ else if (char === "\\") {
192
+ index++;
193
+ }
194
+ else if (char === "/" && next === "*") {
195
+ blockComment = true;
196
+ index++;
197
+ }
198
+ else if (char === "'" || char === '"' || char === "`") {
199
+ quote = char;
200
+ }
201
+ else if (bracketClosers.length > 0) {
202
+ if (char === "[" || char === "(")
203
+ appendOwn(bracketClosers, char === "[" ? "]" : ")");
204
+ else if (char === bracketClosers.at(-1))
205
+ bracketClosers.pop();
206
+ }
207
+ else if (char === "[") {
208
+ appendOwn(bracketClosers, "]");
209
+ }
210
+ else if (char === "(") {
211
+ appendOwn(frames, { attached: isAttachedClassFunctionStart(code, index), start: index });
212
+ }
213
+ else if (char === ")") {
214
+ const frame = frames.pop();
215
+ if (frame.attached)
216
+ appendOwn(candidates, { end: index + 1, start: frame.start });
217
+ }
218
+ }
219
+ const ranges = [];
220
+ for (const candidate of candidates.sort((left, right) => left.start - right.start || right.end - left.end)) {
221
+ if (ranges.length === 0 || candidate.start >= ranges.at(-1).end)
222
+ appendOwn(ranges, candidate);
223
+ }
224
+ return ranges;
225
+ }
226
+ /** Expand groups in a class list while preserving braces inside attached literals. */
227
+ export function expandClassGroups(code) {
228
+ const nesting = { seen: false };
229
+ let expanded = expandClassGroupPass(code, nesting);
230
+ // Each pass re-reads the attached literals, so a nested group settles here.
231
+ for (let depth = 1; depth < MAX_GROUP_NESTING && nesting.seen; depth++) {
232
+ nesting.seen = false;
233
+ const next = expandClassGroupPass(expanded, nesting);
234
+ if (next === expanded)
235
+ break;
236
+ expanded = next;
237
+ }
238
+ return expanded;
239
+ }
240
+ function expandClassGroupPass(code, nesting) {
241
+ if (!code.includes("{"))
242
+ return code;
243
+ let out = "";
244
+ let index = 0;
245
+ let previousExpansionEnd = -1;
246
+ let bracketRangeIndex = 0;
247
+ let functionRangeIndex = 0;
248
+ const bracketRanges = code.includes("[") ? attachedBracketRanges(code) : [];
249
+ const functionRanges = code.includes("(") ? attachedClassFunctionRanges(code) : [];
250
+ const literal = { closers: [] };
251
+ while (index < code.length) {
252
+ while (bracketRangeIndex < bracketRanges.length && bracketRanges[bracketRangeIndex].end <= index)
253
+ bracketRangeIndex++;
254
+ while (functionRangeIndex < functionRanges.length && functionRanges[functionRangeIndex].end <= index)
255
+ functionRangeIndex++;
256
+ const bracketRange = bracketRangeIndex < bracketRanges.length ? bracketRanges[bracketRangeIndex] : undefined;
257
+ const functionRange = functionRangeIndex < functionRanges.length ? functionRanges[functionRangeIndex] : undefined;
258
+ const protectedRange = bracketRange?.start === index ? bracketRange : functionRange?.start === index ? functionRange : undefined;
259
+ if (protectedRange && literal.blockComment !== true && literal.quote === undefined) {
260
+ out += code.slice(index, protectedRange.end);
261
+ index = protectedRange.end;
262
+ continue;
263
+ }
264
+ if (literal.closers.length > 0 && /\s/.test(code[index])) {
265
+ literal.blockComment = false;
266
+ literal.closers.length = 0;
267
+ literal.quote = undefined;
268
+ }
269
+ const consumed = consumeLiteral(code, index, literal);
270
+ if (consumed !== undefined) {
271
+ out += code.slice(index, consumed + 1);
272
+ index = consumed + 1;
273
+ continue;
274
+ }
275
+ const ch = code[index];
276
+ if (literal.closers.length > 0 || ch !== "{") {
277
+ out += ch;
278
+ index++;
279
+ continue;
280
+ }
281
+ const boundary = findClassGroupBoundary(code, index);
282
+ if (boundary.kind !== "close") {
283
+ if (boundary.kind === "open" && nesting)
284
+ nesting.seen = true;
285
+ out += code.slice(index, boundary.index);
286
+ index = boundary.index;
287
+ continue;
288
+ }
289
+ const end = boundary.index;
290
+ const body = code.slice(index + 1, end);
291
+ const important = isGroupImportance(code, end + 1);
292
+ const { conds, end: after } = readTrailingConditions(code, end + 1 + Number(important));
293
+ const declarations = splitDeclarations(body, true);
294
+ if (declarations.length === 0) {
295
+ out += ch;
296
+ index++;
297
+ continue;
298
+ }
299
+ if (previousExpansionEnd === index && out.length > 0 && !/\s/.test(out.at(-1)))
300
+ out += " ";
301
+ out += declarations.map((declaration) => `${important ? importantMember(declaration) : declaration}${conds}`).join(" ");
302
+ previousExpansionEnd = after;
303
+ index = after;
304
+ }
305
+ return out;
306
+ }
@@ -0,0 +1,17 @@
1
+ export interface BracketRange {
2
+ end: number;
3
+ start: number;
4
+ }
5
+ export declare function appendOwnArrayValue<Value>(target: Value[], value: Value): void;
6
+ export declare function attachedBracketRanges(input: string): BracketRange[];
7
+ /** Append an own data property without consulting inherited numeric properties. */
8
+ export declare function defineArrayAppend<T>(target: T[], value: T): void;
9
+ /**
10
+ * Split a class list on whitespace outside attached `[]` literal regions.
11
+ *
12
+ * Unlike the source extractor, this does not treat quotes, semicolons, braces,
13
+ * or backticks as delimiters. It is for already-isolated class-list surfaces
14
+ * such as shortcuts, `cn()`, and `@apply`.
15
+ */
16
+ export declare function splitClassTokens(input: string): string[];
17
+ //# sourceMappingURL=literal.d.ts.map
@@ -0,0 +1,152 @@
1
+ // Ported from teacss-ts `packages-engine/core/src/literal.ts`, `literal-internal.ts` and `array-internal.ts` (0.10.1).
2
+ // Only `splitClassTokens` and the helpers `token.ts` and `group.ts` reach are kept.
3
+ export function appendOwnArrayValue(target, value) {
4
+ const index = target.length;
5
+ // Own index properties always sit below `length`, so `in` here can only see the
6
+ // prototype chain. A clean chain lets plain assignment create the identical own
7
+ // data property; only a polluted one needs the define path, which never consults
8
+ // prototype accessors.
9
+ if (index in target) {
10
+ Object.defineProperty(target, index, {
11
+ configurable: true,
12
+ enumerable: true,
13
+ value,
14
+ writable: true,
15
+ });
16
+ }
17
+ else {
18
+ target[index] = value;
19
+ }
20
+ }
21
+ export function attachedBracketRanges(input) {
22
+ const ranges = [];
23
+ const stack = [];
24
+ const resumes = new Map();
25
+ let blockComment = false;
26
+ let outerQuote;
27
+ let quote;
28
+ for (let index = 0; index < input.length; index++) {
29
+ const char = input[index];
30
+ if (stack.length === 0) {
31
+ if (char === "\\") {
32
+ if (/\s/.test(input[index + 1] ?? ""))
33
+ outerQuote = undefined;
34
+ index++;
35
+ }
36
+ else if (/\s/.test(char)) {
37
+ outerQuote = undefined;
38
+ }
39
+ else if (char === '"' || char === "'") {
40
+ // Quotes remain ordinary token characters. Track them only to avoid
41
+ // treating bracket-shaped text inside one whitespace token as a
42
+ // literal range.
43
+ outerQuote = outerQuote === char ? undefined : (outerQuote ?? char);
44
+ }
45
+ else if (!outerQuote && char === "[") {
46
+ appendOwnArrayValue(stack, index);
47
+ }
48
+ continue;
49
+ }
50
+ if ((quote || blockComment) && char === "[" && /[:@\s]/.test(input[index - 1])) {
51
+ appendOwnArrayValue(stack, index);
52
+ resumes.set(index, quote ?? blockComment);
53
+ blockComment = false;
54
+ quote = undefined;
55
+ continue;
56
+ }
57
+ if (blockComment) {
58
+ if (char === "*" && input[index + 1] === "/") {
59
+ blockComment = false;
60
+ index++;
61
+ }
62
+ continue;
63
+ }
64
+ if (quote) {
65
+ if (char === "\\")
66
+ index++;
67
+ else if (char === quote)
68
+ quote = undefined;
69
+ continue;
70
+ }
71
+ if (char === "/" && input[index + 1] === "*") {
72
+ blockComment = true;
73
+ index++;
74
+ }
75
+ else if (char === '"' || char === "'") {
76
+ quote = char;
77
+ }
78
+ else if (char === "\\") {
79
+ index++;
80
+ }
81
+ else if (char === "[") {
82
+ appendOwnArrayValue(stack, index);
83
+ }
84
+ else if (char === "]") {
85
+ const start = stack.pop();
86
+ const resume = resumes.get(start);
87
+ if ((start > 0 && !/\s/.test(input[start - 1])) || input[index + 1] === ":") {
88
+ appendOwnArrayValue(ranges, { end: index + 1, start });
89
+ }
90
+ if (resume === true)
91
+ blockComment = true;
92
+ else if (resume)
93
+ quote = resume;
94
+ }
95
+ }
96
+ const outermost = [];
97
+ for (const range of ranges.sort((left, right) => left.start - right.start || right.end - left.end)) {
98
+ if (!outermost.length || range.start >= outermost.at(-1).end)
99
+ appendOwnArrayValue(outermost, range);
100
+ }
101
+ return outermost;
102
+ }
103
+ /** Append an own data property without consulting inherited numeric properties. */
104
+ export function defineArrayAppend(target, value) {
105
+ Object.defineProperty(target, target.length, { configurable: true, enumerable: true, value, writable: true });
106
+ }
107
+ function splitOnWhitespace(input) {
108
+ const tokens = input.split(/\s+/);
109
+ if (tokens[0] === "")
110
+ tokens.shift();
111
+ if (tokens.at(-1) === "")
112
+ tokens.pop();
113
+ return tokens;
114
+ }
115
+ /**
116
+ * Split a class list on whitespace outside attached `[]` literal regions.
117
+ *
118
+ * Unlike the source extractor, this does not treat quotes, semicolons, braces,
119
+ * or backticks as delimiters. It is for already-isolated class-list surfaces
120
+ * such as shortcuts, `cn()`, and `@apply`.
121
+ */
122
+ export function splitClassTokens(input) {
123
+ if (!input.includes("["))
124
+ return splitOnWhitespace(input);
125
+ const ranges = attachedBracketRanges(input);
126
+ if (ranges.length === 0)
127
+ return splitOnWhitespace(input);
128
+ const tokens = [];
129
+ let rangeIndex = 0;
130
+ let start = -1;
131
+ for (let index = 0; index < input.length; index++) {
132
+ const range = ranges[rangeIndex];
133
+ if (range && index === range.start) {
134
+ if (start < 0)
135
+ start = index;
136
+ index = range.end - 1;
137
+ rangeIndex++;
138
+ continue;
139
+ }
140
+ if (/\s/.test(input[index])) {
141
+ if (start >= 0)
142
+ appendOwnArrayValue(tokens, input.slice(start, index));
143
+ start = -1;
144
+ }
145
+ else if (start < 0) {
146
+ start = index;
147
+ }
148
+ }
149
+ if (start >= 0)
150
+ appendOwnArrayValue(tokens, input.slice(start));
151
+ return tokens;
152
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Token parsing primitives.
3
+ *
4
+ * Splits a single TeaCSS class token into its stable structural parts so every
5
+ * later stage (the `@` condition axis, escaping, rules) can build on one source
6
+ * of truth. This module is pure structure: it does not resolve rules, variants,
7
+ * or CSS. The current token shape is documented in
8
+ * `notes/proposals/v00-teacss.md`.
9
+ *
10
+ * Token shape:
11
+ *
12
+ * ```txt
13
+ * (property:value | keyword)[!](@cond)*
14
+ * ```
15
+ *
16
+ * - A declaration is either `property:value` (split on the first depth-0 `:`) or
17
+ * a bare keyword (no depth-0 `:`, e.g. `relative`, `sticky`, `text-truncate`).
18
+ * - `()` and `[]` are literal regions; operators inside them (`:` `!` `@` …) do
19
+ * not participate in parsing.
20
+ * - `!` immediately after the value (before any `@`) is `!important`.
21
+ * - The declaration ends at the first depth-0 `@` or the token's end. Only a
22
+ * trailing depth-0 `!` is stripped; other bangs remain for later validation.
23
+ * - Zero or more `@cond` segments follow; each is captured verbatim (without its
24
+ * leading `@`). Interpreting a condition (pseudo / target / media / NOT `!…` …)
25
+ * belongs to the condition pipeline.
26
+ */
27
+ export interface ParsedClassToken {
28
+ /** The full original token, unchanged. */
29
+ raw: string;
30
+ /** Declaration body without the trailing `!` or any `@cond` (e.g. `bg:red`, `d:flex`, `content:[""]`). */
31
+ body: string;
32
+ /** Property side, present only when the declaration has a depth-0 `:`. */
33
+ property?: string;
34
+ /** Value side, present only when the declaration has a depth-0 `:`. */
35
+ value?: string;
36
+ /** True when the declaration is a bare keyword (no depth-0 `:`). */
37
+ keyword: boolean;
38
+ /** True when a depth-0 `!` immediately follows the value (`!important`). */
39
+ important: boolean;
40
+ /** Ordered `@cond` segments, each without its leading `@` (e.g. `["hover"]`, `[">li::after"]`). */
41
+ conditions: string[];
42
+ }
43
+ /**
44
+ * Parse a single class token into its structural parts.
45
+ *
46
+ * Pure and total: any non-empty string yields a result. Malformed tokens (e.g.
47
+ * unbalanced brackets) are parsed leniently rather than throwing — validation is
48
+ * left to the rule layer.
49
+ */
50
+ export declare function parseClassToken(raw: string): ParsedClassToken;
51
+ //# sourceMappingURL=token.d.ts.map
@@ -0,0 +1,132 @@
1
+ // Ported from teacss-ts `packages-engine/core/src/token.ts` (0.10.1).
2
+ /**
3
+ * Token parsing primitives.
4
+ *
5
+ * Splits a single TeaCSS class token into its stable structural parts so every
6
+ * later stage (the `@` condition axis, escaping, rules) can build on one source
7
+ * of truth. This module is pure structure: it does not resolve rules, variants,
8
+ * or CSS. The current token shape is documented in
9
+ * `notes/proposals/v00-teacss.md`.
10
+ *
11
+ * Token shape:
12
+ *
13
+ * ```txt
14
+ * (property:value | keyword)[!](@cond)*
15
+ * ```
16
+ *
17
+ * - A declaration is either `property:value` (split on the first depth-0 `:`) or
18
+ * a bare keyword (no depth-0 `:`, e.g. `relative`, `sticky`, `text-truncate`).
19
+ * - `()` and `[]` are literal regions; operators inside them (`:` `!` `@` …) do
20
+ * not participate in parsing.
21
+ * - `!` immediately after the value (before any `@`) is `!important`.
22
+ * - The declaration ends at the first depth-0 `@` or the token's end. Only a
23
+ * trailing depth-0 `!` is stripped; other bangs remain for later validation.
24
+ * - Zero or more `@cond` segments follow; each is captured verbatim (without its
25
+ * leading `@`). Interpreting a condition (pseudo / target / media / NOT `!…` …)
26
+ * belongs to the condition pipeline.
27
+ */
28
+ import { appendOwnArrayValue } from "./literal.js";
29
+ /**
30
+ * Parse a single class token into its structural parts.
31
+ *
32
+ * Pure and total: any non-empty string yields a result. Malformed tokens (e.g.
33
+ * unbalanced brackets) are parsed leniently rather than throwing — validation is
34
+ * left to the rule layer.
35
+ */
36
+ export function parseClassToken(raw) {
37
+ // Single left-to-right scan tracking literal-region depth, recording the first
38
+ // depth-0 `:` (declaration split) and every depth-0 `@` (condition boundaries).
39
+ let closers = "";
40
+ let quote;
41
+ let firstColon = -1;
42
+ let firstAt = -1;
43
+ let lastDepthZeroBang = -1;
44
+ const atPositions = [];
45
+ for (let i = 0; i < raw.length; i++) {
46
+ const ch = raw[i];
47
+ if (quote) {
48
+ if (ch === "\\")
49
+ i++;
50
+ else if (ch === quote)
51
+ quote = undefined;
52
+ continue;
53
+ }
54
+ // Conditions carry CSS selectors, whose escapes/comments also apply outside brackets.
55
+ if ((closers.length > 0 || firstAt !== -1) && ch === "\\") {
56
+ i++;
57
+ continue;
58
+ }
59
+ if (closers.length > 0 && (ch === '"' || ch === "'")) {
60
+ quote = ch;
61
+ continue;
62
+ }
63
+ if ((closers.length > 0 || firstAt !== -1) && ch === "/" && raw[i + 1] === "*") {
64
+ const close = raw.indexOf("*/", i + 2);
65
+ if (close < 0)
66
+ break;
67
+ i = close + 1;
68
+ continue;
69
+ }
70
+ if (ch === "(" || ch === "[") {
71
+ closers += ch === "(" ? ")" : "]";
72
+ }
73
+ else if (ch === closers.at(-1)) {
74
+ closers = closers.slice(0, -1);
75
+ }
76
+ else if (closers.length === 0) {
77
+ if (ch === "@") {
78
+ if (firstAt === -1)
79
+ firstAt = i;
80
+ appendOwnArrayValue(atPositions, i);
81
+ }
82
+ else if (firstAt === -1) {
83
+ if (ch === ":" && firstColon === -1) {
84
+ // Only the first depth-0 `:` that appears before any `@` splits the declaration.
85
+ firstColon = i;
86
+ }
87
+ else if (ch === "!") {
88
+ lastDepthZeroBang = i;
89
+ }
90
+ }
91
+ }
92
+ }
93
+ // The declaration segment ends at the first depth-0 `@` (or the token's end).
94
+ const declEnd = firstAt === -1 ? raw.length : firstAt;
95
+ let declaration = raw.slice(0, declEnd);
96
+ // Trailing depth-0 `!` on the declaration segment = `!important`.
97
+ // (A literal `!` in the value must be inside `[]`, so it stays at depth > 0.)
98
+ // `declEnd > 0` keeps an `@`-leading token (empty declaration) from
99
+ // matching the `-1` "no bang" sentinel.
100
+ let important = false;
101
+ if (declEnd > 0 && lastDepthZeroBang === declEnd - 1) {
102
+ important = true;
103
+ declaration = declaration.slice(0, -1);
104
+ }
105
+ // Split conditions on each depth-0 `@`, stripping the leading `@` from each.
106
+ const conditions = [];
107
+ for (let k = 0; k < atPositions.length; k++) {
108
+ const start = atPositions[k] + 1;
109
+ const end = k + 1 < atPositions.length ? atPositions[k + 1] : raw.length;
110
+ appendOwnArrayValue(conditions, raw.slice(start, end));
111
+ }
112
+ // Declaration form: `property:value` (depth-0 `:` exists) or bare keyword.
113
+ const hasColon = firstColon !== -1 && firstColon < declEnd;
114
+ if (hasColon) {
115
+ return {
116
+ raw,
117
+ body: declaration,
118
+ property: raw.slice(0, firstColon),
119
+ value: declaration.slice(firstColon + 1),
120
+ keyword: false,
121
+ important,
122
+ conditions,
123
+ };
124
+ }
125
+ return {
126
+ raw,
127
+ body: declaration,
128
+ keyword: true,
129
+ important,
130
+ conditions,
131
+ };
132
+ }