@hyperframes/core 0.9.54 → 0.9.56

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.
@@ -44,14 +44,37 @@ export function parseHTMLContent(html) {
44
44
  function lowerAscii(text) {
45
45
  return text.replace(/[A-Z][\s\S]{0,65535}/g, (chunk) => chunk.replace(/[A-Z]+/g, (letters) => letters.toLowerCase()));
46
46
  }
47
+ function startsWithLower(text, token, at) {
48
+ if (at < 0 || at + token.length > text.length)
49
+ return false;
50
+ for (let i = 0; i < token.length; i += 1) {
51
+ const code = text.charCodeAt(at + i);
52
+ if ((code >= 65 && code <= 90 ? code + 32 : code) !== token.charCodeAt(i))
53
+ return false;
54
+ }
55
+ return true;
56
+ }
57
+ function indexOfLower(text, token, from) {
58
+ for (let at = text.indexOf(token.charAt(0), from); at !== -1; at = text.indexOf(token.charAt(0), at + 1)) {
59
+ if (startsWithLower(text, token, at))
60
+ return at;
61
+ }
62
+ return -1;
63
+ }
64
+ function lastIndexOfLower(text, token) {
65
+ for (let at = text.lastIndexOf(token.charAt(0)); at !== -1; at = at === 0 ? -1 : text.lastIndexOf(token.charAt(0), at - 1)) {
66
+ if (startsWithLower(text, token, at))
67
+ return at;
68
+ }
69
+ return -1;
70
+ }
47
71
  export function stripEmbeddedRuntimeScripts(html) {
48
72
  if (!html)
49
73
  return html;
50
- const loweredHtml = lowerAscii(html);
51
74
  let output = "";
52
75
  let cursor = 0;
53
76
  while (cursor < html.length) {
54
- const scriptStart = findScriptStart(loweredHtml, cursor);
77
+ const scriptStart = findScriptStart(html, cursor);
55
78
  if (scriptStart === -1) {
56
79
  output += html.slice(cursor);
57
80
  break;
@@ -62,7 +85,7 @@ export function stripEmbeddedRuntimeScripts(html) {
62
85
  output += html.slice(scriptStart);
63
86
  break;
64
87
  }
65
- const closeTagEnd = findScriptCloseTagEnd(loweredHtml, startTagEnd + 1);
88
+ const closeTagEnd = findScriptCloseTagEnd(html, startTagEnd + 1);
66
89
  const scriptEnd = closeTagEnd === -1 ? html.length : closeTagEnd;
67
90
  const block = html.slice(scriptStart, scriptEnd);
68
91
  if (!shouldStripRuntimeScriptBlock(block, html.slice(scriptStart, startTagEnd + 1))) {
@@ -72,13 +95,13 @@ export function stripEmbeddedRuntimeScripts(html) {
72
95
  }
73
96
  return output;
74
97
  }
75
- function findScriptStart(loweredHtml, from) {
76
- let index = loweredHtml.indexOf("<script", from);
98
+ function findScriptStart(html, from) {
99
+ let index = indexOfLower(html, "<script", from);
77
100
  while (index !== -1) {
78
- const next = loweredHtml[index + "<script".length] ?? "";
101
+ const next = html[index + "<script".length] ?? "";
79
102
  if (isTagBoundary(next))
80
103
  return index;
81
- index = loweredHtml.indexOf("<script", index + 1);
104
+ index = indexOfLower(html, "<script", index + 1);
82
105
  }
83
106
  return -1;
84
107
  }
@@ -119,22 +142,22 @@ function stateAfterWhitespace(state) {
119
142
  function stateAfterSlash(state) {
120
143
  return state === "equals" || state === "value" ? "value" : "between";
121
144
  }
122
- function findScriptCloseTagEnd(loweredHtml, from) {
123
- let index = loweredHtml.indexOf("</script", from);
145
+ function findScriptCloseTagEnd(html, from) {
146
+ let index = indexOfLower(html, "</script", from);
124
147
  while (index !== -1) {
125
- const closeTagEnd = findScriptCloseTagBoundary(loweredHtml, index + "</script".length);
148
+ const closeTagEnd = findScriptCloseTagBoundary(html, index + "</script".length);
126
149
  if (closeTagEnd !== -1)
127
150
  return closeTagEnd;
128
- index = loweredHtml.indexOf("</script", index + 1);
151
+ index = indexOfLower(html, "</script", index + 1);
129
152
  }
130
153
  return -1;
131
154
  }
132
- function findScriptCloseTagBoundary(loweredHtml, from) {
155
+ function findScriptCloseTagBoundary(html, from) {
133
156
  let cursor = from;
134
- while (cursor < loweredHtml.length && isHtmlWhitespace(loweredHtml[cursor] ?? "")) {
157
+ while (cursor < html.length && isHtmlWhitespace(html[cursor] ?? "")) {
135
158
  cursor += 1;
136
159
  }
137
- return loweredHtml[cursor] === ">" ? cursor + 1 : -1;
160
+ return html[cursor] === ">" ? cursor + 1 : -1;
138
161
  }
139
162
  function shouldStripRuntimeScriptBlock(block, startTag) {
140
163
  const script = parseHTML(`${startTag}</script>`).document.querySelector("script");
@@ -153,8 +176,7 @@ function getScriptSource(block) {
153
176
  const startTagEnd = findTagEnd(block, 1);
154
177
  if (startTagEnd === -1)
155
178
  return "";
156
- const loweredBlock = lowerAscii(block);
157
- const closeTagStart = loweredBlock.lastIndexOf("</script");
179
+ const closeTagStart = lastIndexOfLower(block, "</script");
158
180
  const end = closeTagStart === -1 ? block.length : closeTagStart;
159
181
  return block.slice(startTagEnd + 1, end);
160
182
  }
@@ -168,12 +190,11 @@ export function escapeInlineScriptSource(source) {
168
190
  return escapeCaseInsensitiveToken(escapeCaseInsensitiveToken(source, "</script", (match) => `<\\/${match.slice(2)}`), "<!--", () => "\\x3C!--");
169
191
  }
170
192
  function escapeCaseInsensitiveToken(source, token, replacement) {
171
- const loweredSource = lowerAscii(source);
172
193
  const loweredToken = lowerAscii(token);
173
194
  let output = "";
174
195
  let cursor = 0;
175
196
  while (cursor < source.length) {
176
- const tokenStart = loweredSource.indexOf(loweredToken, cursor);
197
+ const tokenStart = indexOfLower(source, loweredToken, cursor);
177
198
  if (tokenStart === -1) {
178
199
  output += source.slice(cursor);
179
200
  break;
@@ -193,40 +214,38 @@ function inlineScriptTags(scripts) {
193
214
  }
194
215
  const RAW_TEXT_TAGS = ["script", "style", "title", "textarea"];
195
216
  const COMMENT_END = /--!?>/g;
196
- function* markupStarts(lowered) {
217
+ function* markupStarts(html) {
197
218
  const unclosedRawText = new Set();
198
219
  let cursor = 0;
199
220
  while (cursor !== -1) {
200
- const open = lowered.indexOf("<", cursor);
221
+ const open = html.indexOf("<", cursor);
201
222
  if (open === -1)
202
223
  return;
203
224
  yield open;
204
- cursor = skipMarkup(lowered, open, unclosedRawText);
225
+ cursor = skipMarkup(html, open, unclosedRawText);
205
226
  }
206
227
  }
207
228
  function findDocumentTag(html, tag) {
208
- const lowered = lowerAscii(html);
209
- for (const open of markupStarts(lowered)) {
210
- if (isTagAt(lowered, open, tag))
229
+ for (const open of markupStarts(html)) {
230
+ if (isTagAt(html, open, tag))
211
231
  return open;
212
232
  }
213
233
  return -1;
214
234
  }
215
235
  export function mapStartTagsAndStyles(html, map) {
216
- const lowered = lowerAscii(html);
217
236
  let out = "";
218
237
  let cursor = 0;
219
- for (const open of markupStarts(lowered)) {
220
- if (open < cursor || !/[a-z]/.test(lowered.charAt(open + 1)))
238
+ for (const open of markupStarts(html)) {
239
+ if (open < cursor || !/[a-z]/i.test(html.charAt(open + 1)))
221
240
  continue;
222
- const end = findTagEnd(lowered, open + 1);
241
+ const end = findTagEnd(html, open + 1);
223
242
  if (end === -1)
224
243
  break;
225
244
  out += html.slice(cursor, open) + map.tag(html.slice(open, end + 1));
226
245
  cursor = end + 1;
227
- if (!isTagAt(lowered, open, "<style"))
246
+ if (!isTagAt(html, open, "<style"))
228
247
  continue;
229
- const close = findRawTextClose(lowered, "style", cursor);
248
+ const close = findRawTextClose(html, "style", cursor);
230
249
  if (close === -1)
231
250
  continue;
232
251
  out += map.style(html.slice(cursor, close));
@@ -235,97 +254,94 @@ export function mapStartTagsAndStyles(html, map) {
235
254
  return out + html.slice(cursor);
236
255
  }
237
256
  export function findStartTags(html, name) {
238
- const lowered = lowerAscii(html);
239
257
  const token = `<${lowerAscii(name)}`;
240
258
  const starts = [];
241
259
  let templateDepth = 0;
242
- for (const open of markupStarts(lowered)) {
243
- if (templateDepth === 0 && isTagAt(lowered, open, token))
260
+ for (const open of markupStarts(html)) {
261
+ if (templateDepth === 0 && isTagAt(html, open, token))
244
262
  starts.push(open);
245
- if (isTagAt(lowered, open, "<template"))
263
+ if (isTagAt(html, open, "<template"))
246
264
  templateDepth++;
247
- else if (templateDepth > 0 && isTagAt(lowered, open, "</template"))
265
+ else if (templateDepth > 0 && isTagAt(html, open, "</template"))
248
266
  templateDepth--;
249
267
  }
250
268
  return starts;
251
269
  }
252
- function isTagAt(lowered, at, token) {
253
- return lowered.startsWith(token, at) && isTagBoundary(lowered.charAt(at + token.length));
270
+ function isTagAt(html, at, token) {
271
+ return startsWithLower(html, token, at) && isTagBoundary(html.charAt(at + token.length));
254
272
  }
255
273
  /** Cursor just past the markup that starts at `open`, or -1 when the document ends inside it. */
256
- function skipMarkup(lowered, open, unclosedRawText) {
257
- if (lowered.startsWith("<!--", open))
258
- return skipComment(lowered, open);
259
- const next = lowered.charAt(open + 1);
260
- if (next === "/" && !/[a-z]/.test(lowered.charAt(open + 2))) {
261
- const bogusEnd = lowered.indexOf(">", open);
274
+ function skipMarkup(html, open, unclosedRawText) {
275
+ if (html.startsWith("<!--", open))
276
+ return skipComment(html, open);
277
+ const next = html.charAt(open + 1);
278
+ if (next === "/" && !/[a-z]/i.test(html.charAt(open + 2))) {
279
+ const bogusEnd = html.indexOf(">", open);
262
280
  return bogusEnd === -1 ? -1 : bogusEnd + 1;
263
281
  }
264
- if (!/[a-z/]/.test(next))
282
+ if (!/[a-z/]/i.test(next))
265
283
  return open + 1;
266
- const tagEnd = findTagEnd(lowered, open + 1);
267
- return tagEnd === -1 ? -1 : skipRawText(lowered, open, tagEnd, unclosedRawText);
284
+ const tagEnd = findTagEnd(html, open + 1);
285
+ return tagEnd === -1 ? -1 : skipRawText(html, open, tagEnd, unclosedRawText);
268
286
  }
269
- function skipComment(lowered, open) {
270
- if (lowered.startsWith("<!-->", open) || lowered.startsWith("<!--->", open)) {
271
- return lowered.indexOf(">", open) + 1;
287
+ function skipComment(html, open) {
288
+ if (html.startsWith("<!-->", open) || html.startsWith("<!--->", open)) {
289
+ return html.indexOf(">", open) + 1;
272
290
  }
273
291
  COMMENT_END.lastIndex = open + 4;
274
- const end = COMMENT_END.exec(lowered);
292
+ const end = COMMENT_END.exec(html);
275
293
  return end ? end.index + end[0].length : -1;
276
294
  }
277
- function skipRawText(lowered, open, tagEnd, unclosedRawText) {
278
- const rawText = RAW_TEXT_TAGS.find((name) => isTagAt(lowered, open, `<${name}`));
295
+ function skipRawText(html, open, tagEnd, unclosedRawText) {
296
+ const rawText = RAW_TEXT_TAGS.find((name) => isTagAt(html, open, `<${name}`));
279
297
  if (!rawText)
280
298
  return tagEnd + 1;
281
- const close = unclosedRawText.has(rawText) ? -1 : findRawTextClose(lowered, rawText, tagEnd + 1);
299
+ const close = unclosedRawText.has(rawText) ? -1 : findRawTextClose(html, rawText, tagEnd + 1);
282
300
  if (close !== -1)
283
301
  return close;
284
302
  unclosedRawText.add(rawText);
285
- return lowered.charAt(tagEnd - 1) === "/" ? tagEnd + 1 : -1;
303
+ return html.charAt(tagEnd - 1) === "/" ? tagEnd + 1 : -1;
286
304
  }
287
- function findRawTextClose(lowered, name, from) {
305
+ function findRawTextClose(html, name, from) {
288
306
  const close = `</${name}`;
289
- for (let at = lowered.indexOf(close, from); at !== -1; at = lowered.indexOf(close, at + 1)) {
290
- if (isTagAt(lowered, at, close))
307
+ for (let at = indexOfLower(html, close, from); at !== -1; at = indexOfLower(html, close, at + 1)) {
308
+ if (isTagAt(html, at, close))
291
309
  return at;
292
310
  }
293
311
  return -1;
294
312
  }
295
313
  function findOuterTemplateClose(html) {
296
- const lowered = lowerAscii(html);
297
314
  let depth = 0;
298
- for (const open of markupStarts(lowered)) {
299
- if (isTagAt(lowered, open, "<template"))
315
+ for (const open of markupStarts(html)) {
316
+ if (isTagAt(html, open, "<template"))
300
317
  depth++;
301
- else if (isTagAt(lowered, open, "</template") && --depth === 0)
318
+ else if (isTagAt(html, open, "</template") && --depth === 0)
302
319
  return open;
303
320
  }
304
321
  return -1;
305
322
  }
306
- const COMPOSITION_ID_ATTR = /\sdata-composition-id\s*=/;
307
- const isDocumentWrapper = (lowered, open) => isTagAt(lowered, open, "<html") || isTagAt(lowered, open, "<body");
308
- function carriesCompositionId(lowered, open) {
309
- const end = findTagEnd(lowered, open + 1);
310
- return end !== -1 && COMPOSITION_ID_ATTR.test(lowered.slice(open, end));
323
+ const COMPOSITION_ID_ATTR = /\sdata-composition-id\s*=/i;
324
+ const isDocumentWrapper = (html, open) => isTagAt(html, open, "<html") || isTagAt(html, open, "<body");
325
+ function carriesCompositionId(html, open) {
326
+ const end = findTagEnd(html, open + 1);
327
+ return end !== -1 && COMPOSITION_ID_ATTR.test(html.slice(open, end));
311
328
  }
312
329
  export function hasCompositionOutsideTemplates(html) {
313
- const lowered = lowerAscii(html);
314
330
  let depth = 0;
315
331
  let idOnWrapper = false;
316
332
  let idInTemplate = false;
317
- for (const open of markupStarts(lowered)) {
318
- if (isTagAt(lowered, open, "<template")) {
333
+ for (const open of markupStarts(html)) {
334
+ if (isTagAt(html, open, "<template")) {
319
335
  depth++;
320
- if (carriesCompositionId(lowered, open))
336
+ if (carriesCompositionId(html, open))
321
337
  idInTemplate = true;
322
338
  }
323
- else if (isTagAt(lowered, open, "</template"))
339
+ else if (isTagAt(html, open, "</template"))
324
340
  depth = Math.max(0, depth - 1);
325
- else if (/[a-z]/.test(lowered.charAt(open + 1)) && carriesCompositionId(lowered, open)) {
341
+ else if (/[a-z]/i.test(html.charAt(open + 1)) && carriesCompositionId(html, open)) {
326
342
  if (depth > 0)
327
343
  idInTemplate = true;
328
- else if (isDocumentWrapper(lowered, open))
344
+ else if (isDocumentWrapper(html, open))
329
345
  idOnWrapper = true;
330
346
  else
331
347
  return true;
@@ -350,7 +366,7 @@ function insertTagInHead(html, tag) {
350
366
  const insertPos = htmlOpenMatch.index + htmlOpenMatch[0].length;
351
367
  return `${html.slice(0, insertPos)}<head>${tag}</head>${html.slice(insertPos)}`;
352
368
  }
353
- const doctypeIdx = html.toLowerCase().indexOf("<!doctype");
369
+ const doctypeIdx = indexOfLower(html, "<!doctype", 0);
354
370
  if (doctypeIdx >= 0) {
355
371
  const insertPos = html.indexOf(">", doctypeIdx) + 1;
356
372
  return html.slice(0, insertPos) + tag + html.slice(insertPos);