@hyperframes/producer 0.9.64 → 0.9.66
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/dist/distributed.js +190 -79
- package/dist/hyperframe.manifest.json +1 -1
- package/dist/hyperframe.runtime.iife.js +72 -71
- package/dist/index.js +190 -79
- package/dist/public-server.js +190 -79
- package/dist/services/compositionSizeLimit.d.ts +5 -1
- package/dist/services/htmlCompiler.d.ts +1 -6
- package/package.json +7 -7
package/dist/index.js
CHANGED
|
@@ -6944,9 +6944,35 @@ var fileTooLarge = (file, bytes) => new CompositionTooLargeError(
|
|
|
6944
6944
|
`Composition file "${file}" is ${megabytes(bytes)}, over the ${megabytes(MAX_COMPOSITION_HTML_BYTES)} limit for one composition HTML file. Keep large data such as images, fonts and JSON in separate asset files and reference them by path instead of inlining them.`,
|
|
6945
6945
|
{ bytes, limitBytes: MAX_COMPOSITION_HTML_BYTES }
|
|
6946
6946
|
);
|
|
6947
|
+
var limitText = `One render generates at most ${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)}`;
|
|
6948
|
+
function generatedTooLarge(file, bytes) {
|
|
6949
|
+
if (file === "composition variables") {
|
|
6950
|
+
return `Composition variable styles add up to ${megabytes(bytes)}. ${limitText}. Shorten the variable values or declare them on fewer elements.`;
|
|
6951
|
+
}
|
|
6952
|
+
if (file === "variable media sources") {
|
|
6953
|
+
return `Variable-bound media sources add up to ${megabytes(bytes)}. ${limitText}. Use shorter URLs or bind fewer elements.`;
|
|
6954
|
+
}
|
|
6955
|
+
return `Scripts, styles and rebased asset paths generated for compositions, ending at "${file}", add up to ${megabytes(bytes)}. One render generates at most ${megabytes(MAX_EXPANDED_COMPOSITION_BYTES)} for its mounts. Merge small inline scripts and style rules, keep data-composition-id values short, or mount scenes fewer times.`;
|
|
6956
|
+
}
|
|
6947
6957
|
var CompositionExpansionBudget = class {
|
|
6948
6958
|
bytes = 0;
|
|
6949
6959
|
mounts = 0;
|
|
6960
|
+
generated = 0;
|
|
6961
|
+
/** `bytes` may be negative when a reserved upper bound is settled to the real size. */
|
|
6962
|
+
chargeGenerated(file, bytes) {
|
|
6963
|
+
this.generated += bytes;
|
|
6964
|
+
if (this.generated <= MAX_EXPANDED_COMPOSITION_BYTES) return;
|
|
6965
|
+
throw new CompositionTooLargeError(generatedTooLarge(file, this.generated), {
|
|
6966
|
+
bytes: this.generated,
|
|
6967
|
+
limitBytes: MAX_EXPANDED_COMPOSITION_BYTES,
|
|
6968
|
+
exceeded: "generated"
|
|
6969
|
+
});
|
|
6970
|
+
}
|
|
6971
|
+
chargeGeneratedIfItFits(bytes) {
|
|
6972
|
+
if (this.generated + bytes > MAX_EXPANDED_COMPOSITION_BYTES) return false;
|
|
6973
|
+
this.generated += bytes;
|
|
6974
|
+
return true;
|
|
6975
|
+
}
|
|
6950
6976
|
charge(file, bytes) {
|
|
6951
6977
|
this.bytes += bytes;
|
|
6952
6978
|
this.mounts += 1;
|
|
@@ -8657,7 +8683,7 @@ function coalesceHeadStylesAndBodyScripts(html) {
|
|
|
8657
8683
|
}
|
|
8658
8684
|
return document2.toString();
|
|
8659
8685
|
}
|
|
8660
|
-
function inlineSubCompositions(html, subCompositions, projectDir, variableOverrides, assetRoot, outsideFiles) {
|
|
8686
|
+
function inlineSubCompositions(html, subCompositions, projectDir, variableOverrides, assetRoot, outsideFiles, budget) {
|
|
8661
8687
|
const { document: document2 } = parseHTML5(html);
|
|
8662
8688
|
const head = document2.querySelector("head");
|
|
8663
8689
|
const body = document2.querySelector("body");
|
|
@@ -8666,11 +8692,11 @@ function inlineSubCompositions(html, subCompositions, projectDir, variableOverri
|
|
|
8666
8692
|
const emitted = emitRootCompositionVariableStyles(
|
|
8667
8693
|
document2,
|
|
8668
8694
|
{},
|
|
8669
|
-
variableOverrides
|
|
8695
|
+
variableOverrides,
|
|
8696
|
+
{ chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) }
|
|
8670
8697
|
);
|
|
8671
8698
|
return { html: emitted ? document2.toString() : html, variablesByComp: {} };
|
|
8672
8699
|
}
|
|
8673
|
-
const budget = new CompositionExpansionBudget();
|
|
8674
8700
|
const readOnce = /* @__PURE__ */ new Map();
|
|
8675
8701
|
const readMountHtml = (srcPath) => {
|
|
8676
8702
|
const filePath = resolveCompositionFile(projectDir, srcPath, assetRoot);
|
|
@@ -8705,11 +8731,16 @@ function inlineSubCompositions(html, subCompositions, projectDir, variableOverri
|
|
|
8705
8731
|
hostIdentityMap: hostIdentityByElement,
|
|
8706
8732
|
readVariableDefaults: readDeclaredDefaults,
|
|
8707
8733
|
parseHostVariables: parseHostVariableValues,
|
|
8734
|
+
resolveFilePath: (srcPath) => {
|
|
8735
|
+
const file = resolveCompositionFile(projectDir, srcPath, assetRoot);
|
|
8736
|
+
return file && relative2(projectDir, file).split(sep3).join("/");
|
|
8737
|
+
},
|
|
8708
8738
|
resolveHtml: (srcPath) => {
|
|
8709
8739
|
const compHtml = resolveMountHtml(srcPath);
|
|
8710
8740
|
if (compHtml) budget.charge(srcPath, Buffer.byteLength(compHtml));
|
|
8711
8741
|
return compHtml;
|
|
8712
8742
|
},
|
|
8743
|
+
chargeEmitted: (srcPath, bytes) => budget.chargeGenerated(srcPath, bytes),
|
|
8713
8744
|
parseHtml: (htmlStr) => parseHTML5(htmlStr).document,
|
|
8714
8745
|
// Mirrors the preview bundler: a sub-composition's SIBLING assets resolve
|
|
8715
8746
|
// against its own directory, project-root refs stay as authored.
|
|
@@ -8794,7 +8825,8 @@ function inlineSubCompositions(html, subCompositions, projectDir, variableOverri
|
|
|
8794
8825
|
emitRootCompositionVariableStyles(
|
|
8795
8826
|
document2,
|
|
8796
8827
|
result.variablesByComp,
|
|
8797
|
-
variableOverrides
|
|
8828
|
+
variableOverrides,
|
|
8829
|
+
{ chargeBytes: (bytes) => budget.chargeGenerated("composition variables", bytes) }
|
|
8798
8830
|
);
|
|
8799
8831
|
assignMediaRenderIds(document2);
|
|
8800
8832
|
return { html: document2.toString(), variablesByComp: result.variablesByComp };
|
|
@@ -9219,10 +9251,6 @@ function localizeFontFaceUrls(html, downloadDir) {
|
|
|
9219
9251
|
(h, url, relPath) => h.replaceAll(`url(${url})`, `url("${relPath}")`)
|
|
9220
9252
|
);
|
|
9221
9253
|
}
|
|
9222
|
-
function urlOccurrenceRe(localPath) {
|
|
9223
|
-
const escaped = localPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
9224
|
-
return new RegExp(`url\\((["']?)${escaped}\\1\\)`, "g");
|
|
9225
|
-
}
|
|
9226
9254
|
var MAX_LOCAL_FONT_DATA_URI_BYTES = 5 * 1024 * 1024;
|
|
9227
9255
|
async function readLocalFont(absPath) {
|
|
9228
9256
|
const chunks = [];
|
|
@@ -9237,59 +9265,83 @@ async function readLocalFont(absPath) {
|
|
|
9237
9265
|
}
|
|
9238
9266
|
return { kind: "inline", buffer: Buffer.concat(chunks, totalBytes) };
|
|
9239
9267
|
}
|
|
9240
|
-
async function
|
|
9241
|
-
const
|
|
9242
|
-
|
|
9243
|
-
|
|
9268
|
+
async function localFontDataUri(localPath, projectDir, assetRoot, dataUriByAbsolutePath, log) {
|
|
9269
|
+
const absPath = localPath.startsWith("/") ? localPath : resolve7(projectDir, localPath);
|
|
9270
|
+
if (!isPathInside(absPath, projectDir) || escapesAssetRoot(absPath, assetRoot)) return void 0;
|
|
9271
|
+
if (refusesOutsideLink(projectDir, absPath, (line) => log.warn(`[Compiler] ${line}`))) {
|
|
9272
|
+
return void 0;
|
|
9273
|
+
}
|
|
9274
|
+
if (isPrivateCompositionFile(projectDir, absPath)) return void 0;
|
|
9275
|
+
if (dataUriByAbsolutePath.has(absPath)) return dataUriByAbsolutePath.get(absPath);
|
|
9276
|
+
const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf";
|
|
9277
|
+
try {
|
|
9278
|
+
const font = await readLocalFont(absPath);
|
|
9279
|
+
if (font.kind === "file-backed") {
|
|
9280
|
+
dataUriByAbsolutePath.set(absPath, void 0);
|
|
9281
|
+
defaultLogger.debug(
|
|
9282
|
+
`[Compiler] Kept large local font file-backed: ${localPath} (> ${(MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024).toFixed(1)} MB)`
|
|
9283
|
+
);
|
|
9284
|
+
return void 0;
|
|
9285
|
+
}
|
|
9286
|
+
const { fontToDataUri } = await import("@hyperframes/core/fonts/embed");
|
|
9287
|
+
const dataUri = await fontToDataUri(font.buffer, ext, { compressToWoff2: false });
|
|
9288
|
+
dataUriByAbsolutePath.set(absPath, dataUri);
|
|
9289
|
+
defaultLogger.debug(
|
|
9290
|
+
`[Compiler] Embedded local font file: ${localPath} (${(font.buffer.length / 1024).toFixed(0)} KB \u2192 data URI)`
|
|
9291
|
+
);
|
|
9292
|
+
return dataUri;
|
|
9293
|
+
} catch (error) {
|
|
9294
|
+
defaultLogger.warn(
|
|
9295
|
+
`[Compiler] Could not embed local font ${localPath}: ${error instanceof Error ? error.message : String(error)}`
|
|
9296
|
+
);
|
|
9297
|
+
return void 0;
|
|
9298
|
+
}
|
|
9299
|
+
}
|
|
9300
|
+
var replaceInner = (whole, inner, tail, next) => whole.slice(0, whole.length - tail - inner.length) + next + whole.slice(whole.length - tail);
|
|
9301
|
+
async function embedLocalFontFaces(html, projectDir, assetRoot, budget, log = defaultLogger) {
|
|
9302
|
+
const dataUriByPath = /* @__PURE__ */ new Map();
|
|
9244
9303
|
const dataUriByAbsolutePath = /* @__PURE__ */ new Map();
|
|
9245
|
-
const fileBackedAbsolutePaths = /* @__PURE__ */ new Set();
|
|
9246
9304
|
for (const { parts: style } of styleBlockMatches(html)) {
|
|
9247
9305
|
for (const { parts: face } of fontFaceBlockMatches(style[1])) {
|
|
9248
|
-
const
|
|
9249
|
-
for (const { parts } of localFontFaceUrlMatches(block)) {
|
|
9306
|
+
for (const { parts } of localFontFaceUrlMatches(face[1])) {
|
|
9250
9307
|
const localPath = parts[1];
|
|
9251
|
-
if (!localPath ||
|
|
9252
|
-
|
|
9253
|
-
|
|
9254
|
-
|
|
9255
|
-
|
|
9256
|
-
continue;
|
|
9257
|
-
}
|
|
9258
|
-
if (isPrivateCompositionFile(projectDir, absPath)) continue;
|
|
9259
|
-
const ext = absPath.match(/\.(woff2?|ttf|otf|ttc)$/i)?.[1]?.toLowerCase() ?? "ttf";
|
|
9260
|
-
try {
|
|
9261
|
-
if (fileBackedAbsolutePaths.has(absPath)) {
|
|
9262
|
-
embeddedPaths.add(localPath);
|
|
9263
|
-
continue;
|
|
9264
|
-
}
|
|
9265
|
-
let dataUri = dataUriByAbsolutePath.get(absPath);
|
|
9266
|
-
if (!dataUri) {
|
|
9267
|
-
const font = await readLocalFont(absPath);
|
|
9268
|
-
if (font.kind === "file-backed") {
|
|
9269
|
-
fileBackedAbsolutePaths.add(absPath);
|
|
9270
|
-
defaultLogger.debug(
|
|
9271
|
-
`[Compiler] Kept large local font file-backed: ${localPath} (> ${(MAX_LOCAL_FONT_DATA_URI_BYTES / 1024 / 1024).toFixed(1)} MB)`
|
|
9272
|
-
);
|
|
9273
|
-
embeddedPaths.add(localPath);
|
|
9274
|
-
continue;
|
|
9275
|
-
}
|
|
9276
|
-
dataUri = await toDataUri(font.buffer, ext, { compressToWoff2: false });
|
|
9277
|
-
dataUriByAbsolutePath.set(absPath, dataUri);
|
|
9278
|
-
defaultLogger.debug(
|
|
9279
|
-
`[Compiler] Embedded local font file: ${localPath} (${(font.buffer.length / 1024).toFixed(0)} KB \u2192 data URI)`
|
|
9280
|
-
);
|
|
9281
|
-
}
|
|
9282
|
-
result = result.replace(urlOccurrenceRe(localPath), `url("${dataUri}")`);
|
|
9283
|
-
embeddedPaths.add(localPath);
|
|
9284
|
-
} catch (error) {
|
|
9285
|
-
defaultLogger.warn(
|
|
9286
|
-
`[Compiler] Could not embed local font ${localPath}: ${error instanceof Error ? error.message : String(error)}`
|
|
9287
|
-
);
|
|
9288
|
-
}
|
|
9308
|
+
if (!localPath || dataUriByPath.has(localPath)) continue;
|
|
9309
|
+
dataUriByPath.set(
|
|
9310
|
+
localPath,
|
|
9311
|
+
await localFontDataUri(localPath, projectDir, assetRoot, dataUriByAbsolutePath, log)
|
|
9312
|
+
);
|
|
9289
9313
|
}
|
|
9290
9314
|
}
|
|
9291
9315
|
}
|
|
9292
|
-
return
|
|
9316
|
+
if (![...dataUriByPath.values()].some(Boolean)) return html;
|
|
9317
|
+
let warned = false;
|
|
9318
|
+
const embed = (source, localPath) => {
|
|
9319
|
+
const dataUri = dataUriByPath.get(localPath);
|
|
9320
|
+
if (!dataUri) return source;
|
|
9321
|
+
const inlined = `url("${dataUri}")`;
|
|
9322
|
+
if (budget.chargeGeneratedIfItFits(inlined.length - source.length)) return inlined;
|
|
9323
|
+
if (!warned) {
|
|
9324
|
+
warned = true;
|
|
9325
|
+
log.warn(
|
|
9326
|
+
`[Compiler] Kept local font ${localPath} file-backed: the page reached its inline size limit`
|
|
9327
|
+
);
|
|
9328
|
+
}
|
|
9329
|
+
return source;
|
|
9330
|
+
};
|
|
9331
|
+
return replaceMatches(
|
|
9332
|
+
html,
|
|
9333
|
+
styleBlockMatches(html),
|
|
9334
|
+
(style, css) => replaceInner(
|
|
9335
|
+
style,
|
|
9336
|
+
css,
|
|
9337
|
+
"</style>".length,
|
|
9338
|
+
replaceMatches(
|
|
9339
|
+
css,
|
|
9340
|
+
fontFaceBlockMatches(css),
|
|
9341
|
+
(face, body) => replaceInner(face, body, 1, replaceMatches(body, localFontFaceUrlMatches(body), embed))
|
|
9342
|
+
)
|
|
9343
|
+
)
|
|
9344
|
+
);
|
|
9293
9345
|
}
|
|
9294
9346
|
function renderGsapDist() {
|
|
9295
9347
|
try {
|
|
@@ -9315,32 +9367,42 @@ function rewriteUnresolvableGsapToCdn(html, projectDir, compSrcPath) {
|
|
|
9315
9367
|
return `${prefix}${gsapDist}${file}${suffix}`;
|
|
9316
9368
|
});
|
|
9317
9369
|
}
|
|
9318
|
-
function rebaseDirectEntryAssetPaths(html, projectDir, htmlPath) {
|
|
9370
|
+
function rebaseDirectEntryAssetPaths(html, projectDir, htmlPath, budget) {
|
|
9319
9371
|
if (!isPathInside(htmlPath, projectDir)) return html;
|
|
9320
9372
|
const entryPath = relative2(projectDir, htmlPath).replace(/\\/g, "/");
|
|
9321
9373
|
if (!entryPath.includes("/")) return html;
|
|
9322
9374
|
const { document: document2 } = parseHTML5(html);
|
|
9323
9375
|
const assetExists = projectAssetExists(projectDir);
|
|
9376
|
+
const charge = (bytes) => budget.chargeGenerated(entryPath, bytes);
|
|
9324
9377
|
rewriteAssetPaths(
|
|
9325
9378
|
document2.querySelectorAll("[src], [href], [srcset]"),
|
|
9326
9379
|
entryPath,
|
|
9327
9380
|
(el, attr) => el.getAttribute(attr),
|
|
9328
9381
|
(el, attr, value) => el.setAttribute(attr, value),
|
|
9329
|
-
assetExists
|
|
9382
|
+
assetExists,
|
|
9383
|
+
charge
|
|
9330
9384
|
);
|
|
9331
9385
|
rewriteInlineStyleAssetUrls(
|
|
9332
9386
|
document2.querySelectorAll("[style]"),
|
|
9333
9387
|
entryPath,
|
|
9334
9388
|
(el) => el.getAttribute("style"),
|
|
9335
9389
|
(el, value) => el.setAttribute("style", value),
|
|
9336
|
-
assetExists
|
|
9390
|
+
assetExists,
|
|
9391
|
+
charge
|
|
9337
9392
|
);
|
|
9338
9393
|
for (const style of document2.querySelectorAll("style")) {
|
|
9339
|
-
style.textContent = rewriteCssAssetUrls(
|
|
9394
|
+
style.textContent = rewriteCssAssetUrls(
|
|
9395
|
+
style.textContent || "",
|
|
9396
|
+
entryPath,
|
|
9397
|
+
assetExists,
|
|
9398
|
+
charge
|
|
9399
|
+
);
|
|
9340
9400
|
}
|
|
9341
9401
|
return document2.toString();
|
|
9342
9402
|
}
|
|
9343
|
-
|
|
9403
|
+
var MAX_VARIABLE_MEDIA_SRC_BYTES = 8 * 1024;
|
|
9404
|
+
var MEDIA_SRC_CONTROL_CHARS = /[\u0000-\u001f\u007f]/;
|
|
9405
|
+
function bindVariableMediaSources(html, variables, budget, log) {
|
|
9344
9406
|
if (!html.includes("data-var-src")) return html;
|
|
9345
9407
|
const { document: document2 } = parseHTML5(html);
|
|
9346
9408
|
const declared = {};
|
|
@@ -9351,8 +9413,17 @@ function bindVariableMediaSources(html, variables) {
|
|
|
9351
9413
|
let changed = false;
|
|
9352
9414
|
for (const el of document2.querySelectorAll("video[data-var-src], audio[data-var-src]")) {
|
|
9353
9415
|
if (el.closest("[data-variable-values]")) continue;
|
|
9354
|
-
const
|
|
9355
|
-
if (
|
|
9416
|
+
const bound = boundMediaSrc(el, values);
|
|
9417
|
+
if (bound === null) continue;
|
|
9418
|
+
const src = bound.trim();
|
|
9419
|
+
if (src.length > MAX_VARIABLE_MEDIA_SRC_BYTES || MEDIA_SRC_CONTROL_CHARS.test(src)) {
|
|
9420
|
+
log?.warn("[Compiler] Refused a variable media source that cannot be a URL", {
|
|
9421
|
+
length: src.length
|
|
9422
|
+
});
|
|
9423
|
+
continue;
|
|
9424
|
+
}
|
|
9425
|
+
if (el.getAttribute("src") === src) continue;
|
|
9426
|
+
budget.chargeGenerated("variable media sources", src.length);
|
|
9356
9427
|
el.setAttribute("src", src);
|
|
9357
9428
|
changed = true;
|
|
9358
9429
|
}
|
|
@@ -9372,10 +9443,18 @@ async function compileForRender(projectDir, htmlPath, downloadDir, options = {})
|
|
|
9372
9443
|
throw new Error(`Entry file ${htmlPath} is not an .html file, so it is refused`);
|
|
9373
9444
|
}
|
|
9374
9445
|
assertCompositionFileSize(htmlPath, basename5(htmlPath));
|
|
9375
|
-
const
|
|
9446
|
+
const budget = new CompositionExpansionBudget();
|
|
9447
|
+
const entryHtml = rebaseDirectEntryAssetPaths(
|
|
9448
|
+
readTextFile(htmlPath),
|
|
9449
|
+
projectDir,
|
|
9450
|
+
htmlPath,
|
|
9451
|
+
budget
|
|
9452
|
+
);
|
|
9376
9453
|
const rawHtml = bindVariableMediaSources(
|
|
9377
9454
|
normalizeCompositionSrcs(rewriteUnresolvableGsapToCdn(entryHtml, projectDir), projectDir),
|
|
9378
|
-
options.variables ?? {}
|
|
9455
|
+
options.variables ?? {},
|
|
9456
|
+
budget,
|
|
9457
|
+
options.log
|
|
9379
9458
|
);
|
|
9380
9459
|
assertSubCompositionsUsable(rawHtml, projectDir, assetRoot, outsideFiles);
|
|
9381
9460
|
const probes = /* @__PURE__ */ new Map();
|
|
@@ -9406,7 +9485,8 @@ async function compileForRender(projectDir, htmlPath, downloadDir, options = {})
|
|
|
9406
9485
|
projectDir,
|
|
9407
9486
|
options.variables ?? {},
|
|
9408
9487
|
assetRoot,
|
|
9409
|
-
outsideFiles
|
|
9488
|
+
outsideFiles,
|
|
9489
|
+
budget
|
|
9410
9490
|
);
|
|
9411
9491
|
const sanitizedHtml = stripPreloadNone(inlinedHtml);
|
|
9412
9492
|
const renderModeHints = detectRenderModeHints(sanitizedHtml, options.pageMotionBlur);
|
|
@@ -9485,6 +9565,7 @@ async function compileForRender(projectDir, htmlPath, downloadDir, options = {})
|
|
|
9485
9565
|
htmlWithPreparedGifs,
|
|
9486
9566
|
projectDir,
|
|
9487
9567
|
assetRoot,
|
|
9568
|
+
budget,
|
|
9488
9569
|
options.log
|
|
9489
9570
|
);
|
|
9490
9571
|
const { html, externalAssets } = options.allowOutsideProjectAssets ? collectExternalAssets(
|
|
@@ -10287,21 +10368,50 @@ function reconcileBrowserMediaEnd(existingEnd, projectedEnd, sourceChanged, dura
|
|
|
10287
10368
|
return existingEnd <= 0 ? projectedEnd : Math.min(existingEnd, projectedEnd);
|
|
10288
10369
|
}
|
|
10289
10370
|
var MAX_MEDIA_SELECTOR_WORK = 1e6;
|
|
10290
|
-
var
|
|
10291
|
-
|
|
10371
|
+
var ATTRIBUTE_SELECTOR = /\[\s*[\w-]+\s*(?:[~|^$*]?=\s*(?:"[^"\\]*"|'[^'\\]*'|[\w-]+)\s*(?:[is]\s*)?)?\]/g;
|
|
10372
|
+
var COMPOUND_SELECTORS = /^[\w\s#.*>,@-]+$/;
|
|
10373
|
+
function cheapSelectorShape(selector) {
|
|
10374
|
+
const shape = selector.replace(ATTRIBUTE_SELECTOR, "@").trim();
|
|
10375
|
+
if (!COMPOUND_SELECTORS.test(shape)) return null;
|
|
10376
|
+
const collapsed = shape.replace(/\s+/g, " ").replace(/ ?([>,]) ?/g, "$1");
|
|
10377
|
+
const parts = collapsed.split(",");
|
|
10378
|
+
return parts.every((part) => part !== "" && part.split(" ").length <= 2) ? collapsed : null;
|
|
10379
|
+
}
|
|
10380
|
+
var attributeChars = (element) => Array.from(element.attributes).reduce((sum2, a) => sum2 + a.name.length + a.value.length + 1, 0);
|
|
10381
|
+
function managedMediaElements(document2, measure) {
|
|
10292
10382
|
const managed = [];
|
|
10293
|
-
const pending =
|
|
10294
|
-
|
|
10383
|
+
const pending = Array.from(document2.children, (element) => ({
|
|
10384
|
+
element,
|
|
10385
|
+
inMedia: false,
|
|
10386
|
+
depth: 0,
|
|
10387
|
+
above: 0
|
|
10388
|
+
}));
|
|
10295
10389
|
for (let next = pending.pop(); next; next = pending.pop()) {
|
|
10296
|
-
const
|
|
10390
|
+
const { element, depth } = next;
|
|
10391
|
+
const chainAttributeChars = measure ? next.above + attributeChars(element) : 0;
|
|
10392
|
+
const name = element.tagName.toLowerCase();
|
|
10297
10393
|
const isMedia = name === "video" || name === "audio";
|
|
10298
|
-
if (isMedia || next.inMedia && name === "source")
|
|
10299
|
-
|
|
10300
|
-
|
|
10394
|
+
if (isMedia || next.inMedia && name === "source") {
|
|
10395
|
+
managed.push({ element, depth, chainAttributeChars });
|
|
10396
|
+
}
|
|
10397
|
+
for (const child3 of Array.from(element.children)) {
|
|
10398
|
+
const inMedia = next.inMedia || isMedia;
|
|
10399
|
+
pending.push({ element: child3, inMedia, depth: depth + 1, above: chainAttributeChars });
|
|
10301
10400
|
}
|
|
10302
10401
|
}
|
|
10303
10402
|
return managed;
|
|
10304
10403
|
}
|
|
10404
|
+
function selectorCheckCost(selector, measured) {
|
|
10405
|
+
const shape = cheapSelectorShape(selector);
|
|
10406
|
+
if (shape === null) return Infinity;
|
|
10407
|
+
const tests = (shape.match(/[.#@]/g)?.length ?? 0) + shape.split(/[ >,]/).length;
|
|
10408
|
+
let cost = 0;
|
|
10409
|
+
for (const media of measured) {
|
|
10410
|
+
cost += selector.length * (media.depth + 1) + tests * media.chainAttributeChars;
|
|
10411
|
+
if (cost > MAX_MEDIA_SELECTOR_WORK) return cost;
|
|
10412
|
+
}
|
|
10413
|
+
return cost;
|
|
10414
|
+
}
|
|
10305
10415
|
function hasRuntimeMediaChanges(html) {
|
|
10306
10416
|
const { document: document2 } = parseHTML6(html);
|
|
10307
10417
|
const scriptBodies = [...document2.querySelectorAll("script")].map(
|
|
@@ -10311,11 +10421,12 @@ function hasRuntimeMediaChanges(html) {
|
|
|
10311
10421
|
(script) => /\bcreateElement\s*\(\s*["'`](?:video|audio)["'`]\s*\)/i.test(script) || /\bnew\s+(?:Audio|Video)\s*\(/i.test(script) || hasClosedMediaTag(script)
|
|
10312
10422
|
);
|
|
10313
10423
|
if (insertedMedia) return true;
|
|
10314
|
-
const managed = managedMediaElements(document2);
|
|
10424
|
+
const managed = managedMediaElements(document2, false);
|
|
10315
10425
|
if (managed.length === 0) return false;
|
|
10316
|
-
const managedIds = new Set(managed.map((
|
|
10426
|
+
const managedIds = new Set(managed.map((m) => m.element.getAttribute("id")).filter(Boolean));
|
|
10317
10427
|
const selectors = /* @__PURE__ */ new Set();
|
|
10318
|
-
let
|
|
10428
|
+
let work = 0;
|
|
10429
|
+
let measured;
|
|
10319
10430
|
for (const script of scriptBodies) {
|
|
10320
10431
|
for (const mutation of extractMediaSrcMutations(script)) {
|
|
10321
10432
|
const id = /^#[A-Za-z_][\w-]*$/.test(mutation.selector) ? mutation.selector.slice(1) : null;
|
|
@@ -10324,15 +10435,15 @@ function hasRuntimeMediaChanges(html) {
|
|
|
10324
10435
|
continue;
|
|
10325
10436
|
}
|
|
10326
10437
|
if (selectors.has(mutation.selector)) continue;
|
|
10327
|
-
if (SIBLING_COMBINATOR.test(mutation.selector)) return true;
|
|
10328
10438
|
selectors.add(mutation.selector);
|
|
10329
|
-
|
|
10330
|
-
|
|
10439
|
+
measured ??= managedMediaElements(document2, true);
|
|
10440
|
+
work += selectorCheckCost(mutation.selector, measured);
|
|
10441
|
+
if (work > MAX_MEDIA_SELECTOR_WORK) return true;
|
|
10331
10442
|
}
|
|
10332
10443
|
}
|
|
10333
10444
|
for (const selector of selectors) {
|
|
10334
10445
|
try {
|
|
10335
|
-
if (managed.some((
|
|
10446
|
+
if (managed.some((media) => media.element.matches(selector))) return true;
|
|
10336
10447
|
} catch {
|
|
10337
10448
|
}
|
|
10338
10449
|
}
|