@hyperframes/core 0.9.71 → 0.9.72

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.
@@ -158,17 +158,114 @@ export function escapeCssIdentifier(value) {
158
158
  }
159
159
  return result;
160
160
  }
161
- /**
162
- * A full attribute-selector bracket (`[data-x="a"]`, quotes optional, `]`
163
- * inside a quoted value tolerated) or a bare quoted string. Either is a
164
- * region where a literal `#` is never an id-selector prefix — the CSS parser
165
- * itself never looks for one there — so `markUnguardedOffsets` below can
166
- * find every such region with one pass of `matchAll` instead of a hand-rolled
167
- * character-by-character state machine. Inside the bracket alternative, bare
168
- * characters exclude the quote marks so each position matches exactly one
169
- * branch (no ambiguous backtracking).
170
- */
171
- const GUARDED_SELECTOR_SEGMENT_RE = /\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:[0-9a-fA-F]{1,6}(?:\r\n|[ \t\r\n\f])?|[\s\S])|"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'|\[(?:\/\*(?:[^*]|\*(?!\/))*\*\/|\\[\s\S]|"(?:[^"\\]|\\[\s\S])*"|'(?:[^'\\]|\\[\s\S])*'|[^\]"'\\/]|\/(?!\*))*\]/g;
161
+ const NO_MATCH = -1;
162
+ const UNKNOWN = -2;
163
+ // One CSS escape, anchored where it is tried: at most nine characters, so each try costs a constant.
164
+ const ESCAPE_AT = /\\(?:[0-9a-fA-F]{1,6}(?:\r\n|[ \t\r\n\f])?|[\s\S])/y;
165
+ /** Regions of `selector` where `#` is never an id prefix: a closed comment, an escape, a closed quoted string,
166
+ * or a whole attribute bracket. Each opener's closer is memoized, so an unclosed opener costs one read:
167
+ * linear on any input. */
168
+ function guardedSelectorSegments(selector) {
169
+ const n = selector.length;
170
+ // commentClose[i]: where the first "*/" at or after i starts.
171
+ const commentClose = new Int32Array(n + 2).fill(NO_MATCH);
172
+ for (let i = n - 2; i >= 0; i -= 1) {
173
+ commentClose[i] = selector[i] === "*" && selector[i + 1] === "/" ? i : commentClose[i + 1];
174
+ }
175
+ const commentEnd = (start) => {
176
+ const close = commentClose[start + 2];
177
+ return close === NO_MATCH ? NO_MATCH : close + 2;
178
+ };
179
+ // Each walk reads from i to its closer, steps 1 or 2 at a time; its answer is memoized for every
180
+ // position it passed, so a later walk from any of them stops at once.
181
+ const walk = (memo, from, step) => {
182
+ const passed = [];
183
+ let i = from;
184
+ let found;
185
+ for (;;) {
186
+ if (i >= n) {
187
+ found = NO_MATCH;
188
+ break;
189
+ }
190
+ if (memo[i] !== UNKNOWN) {
191
+ found = memo[i];
192
+ break;
193
+ }
194
+ passed.push(i);
195
+ const next = step(i);
196
+ if (typeof next !== "number") {
197
+ found = next[0];
198
+ break;
199
+ }
200
+ i = next;
201
+ }
202
+ for (const at of passed)
203
+ memo[at] = found;
204
+ return found;
205
+ };
206
+ const quoteClose = { '"': new Int32Array(n).fill(UNKNOWN), "'": new Int32Array(n).fill(UNKNOWN) };
207
+ // Index of the quote that closes a string whose body starts at `from`, or NO_MATCH.
208
+ const closeQuote = (quote, from) => walk(quoteClose[quote], from, (i) => {
209
+ if (selector[i] === quote)
210
+ return [i];
211
+ if (selector[i] === "\\")
212
+ return i + 1 < n ? i + 2 : [NO_MATCH];
213
+ return i + 1;
214
+ });
215
+ const quoteEnd = (start) => {
216
+ const close = closeQuote(selector[start], start + 1);
217
+ return close === NO_MATCH ? NO_MATCH : close + 1;
218
+ };
219
+ // Where the token at `i` inside a bracket ends, or NO_MATCH when it can't close (an unclosed comment or quote).
220
+ const bracketTokenEnd = (i) => {
221
+ const char = selector[i];
222
+ if (char === "/" && selector[i + 1] === "*")
223
+ return commentEnd(i);
224
+ if (char === '"' || char === "'")
225
+ return quoteEnd(i);
226
+ if (char !== "\\")
227
+ return i + 1;
228
+ return i + 1 < n ? i + 2 : NO_MATCH;
229
+ };
230
+ const bracketClose = new Int32Array(n).fill(UNKNOWN);
231
+ // Index of the `]` that closes a bracket whose body starts at `from`, or NO_MATCH.
232
+ const closeBracket = (from) => walk(bracketClose, from, (i) => {
233
+ if (selector[i] === "]")
234
+ return [i];
235
+ const end = bracketTokenEnd(i);
236
+ return end === NO_MATCH ? [NO_MATCH] : end;
237
+ });
238
+ const escapeEnd = (start) => {
239
+ ESCAPE_AT.lastIndex = start;
240
+ return ESCAPE_AT.test(selector) ? ESCAPE_AT.lastIndex : NO_MATCH;
241
+ };
242
+ const bracketEnd = (start) => {
243
+ const close = closeBracket(start + 1);
244
+ return close === NO_MATCH ? NO_MATCH : close + 1;
245
+ };
246
+ const segmentEnd = (start) => {
247
+ const char = selector[start];
248
+ if (char === "/")
249
+ return selector[start + 1] === "*" ? commentEnd(start) : NO_MATCH;
250
+ if (char === "\\")
251
+ return escapeEnd(start);
252
+ if (char === '"' || char === "'")
253
+ return quoteEnd(start);
254
+ return char === "[" ? bracketEnd(start) : NO_MATCH;
255
+ };
256
+ const segments = [];
257
+ for (let start = 0; start < n;) {
258
+ const end = segmentEnd(start);
259
+ if (end === NO_MATCH) {
260
+ start += 1;
261
+ }
262
+ else {
263
+ segments.push([start, end]);
264
+ start = end;
265
+ }
266
+ }
267
+ return segments;
268
+ }
172
269
  /**
173
270
  * `mask[i]` is `true` when `selector[i]` sits outside both a quoted string
174
271
  * and an attribute-selector bracket — the two places a literal `#` is never
@@ -176,10 +273,8 @@ const GUARDED_SELECTOR_SEGMENT_RE = /\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:[0-9a-fA-F]
176
273
  */
177
274
  function markUnguardedOffsets(selector) {
178
275
  const mask = new Array(selector.length).fill(true);
179
- for (const match of selector.matchAll(GUARDED_SELECTOR_SEGMENT_RE)) {
180
- const start = match.index;
181
- mask.fill(false, start, start + match[0].length);
182
- }
276
+ for (const [start, end] of guardedSelectorSegments(selector))
277
+ mask.fill(false, start, end);
183
278
  return mask;
184
279
  }
185
280
  /**
@@ -342,5 +437,13 @@ export function parseAttributeSelector(predicate) {
342
437
  };
343
438
  }
344
439
  export function replaceSelectorAttributeTokens(selector, rewrite) {
345
- return selector.replace(GUARDED_SELECTOR_SEGMENT_RE, (segment) => segment.startsWith("[") ? rewrite(segment) : segment);
440
+ let result = "";
441
+ let copied = 0;
442
+ for (const [start, end] of guardedSelectorSegments(selector)) {
443
+ if (selector[start] !== "[")
444
+ continue;
445
+ result += selector.slice(copied, start) + rewrite(selector.slice(start, end));
446
+ copied = end;
447
+ }
448
+ return result + selector.slice(copied);
346
449
  }