Skip to content

Write HTML. Render video. Built for agents.

fix(studio): stop file names with %, apostrophes or $ from breaking Studio (#5156)

Miguel Ángel621ddd469343
diff --git a/bun.lock b/bun.lock
index 1a3620f306..64948f4e61 100644
--- a/bun.lock
+++ b/bun.lock
@@ -220,6 +220,7 @@
"@babel/parser": "^7.27.0",
"acorn": "^8.17.0",
"acorn-walk": "^8.3.5",
+ "entities": "^7.0.1",
"linkedom": "^0.18.12",
"magic-string": "^0.30.21",
"recast": "^0.23.11",
diff --git a/packages/cli/src/commands/validate.test.ts b/packages/cli/src/commands/validate.test.ts
index 422250a499..1af6dcab96 100644
--- a/packages/cli/src/commands/validate.test.ts
+++ b/packages/cli/src/commands/validate.test.ts
@@ -14,6 +14,7 @@ import {
auditClipDurations,
extractCompositionErrorsFromLint,
navigationTimeoutHint,
+ projectPathOfUrl,
raceMediaReady,
resolveNavigationTimeoutMs,
shouldIgnoreRequestFailure,
@@ -398,3 +399,12 @@ describe("validate command deprecation (U5)", () => {
expect(parsed._meta.deprecated).toBe(true);
});
});
+
+describe("projectPathOfUrl", () => {
+ it("names a file with a bare percent sign instead of throwing", () => {
+ expect(projectPathOfUrl("http://127.0.0.1:4000/assets/50%.png")).toBe("assets/50%.png");
+ expect(projectPathOfUrl("http://127.0.0.1:4000/assets/50%25%20off.png")).toBe(
+ "assets/50% off.png",
+ );
+ });
+});
diff --git a/packages/cli/src/commands/validate.ts b/packages/cli/src/commands/validate.ts
index d842509c37..7c2497cb0d 100644
--- a/packages/cli/src/commands/validate.ts
+++ b/packages/cli/src/commands/validate.ts
@@ -15,6 +15,7 @@ import { normalizeErrorMessage } from "../utils/errorMessage.js";
import type { ProjectLintResult } from "../utils/lintProject.js";
import { resolveCompositionViewportFromHtml } from "../utils/compositionViewport.js";
import { c } from "../ui/colors.js";
+import { decodeWellFormedEscapes } from "@hyperframes/studio-server";
import { printDeprecationNotice, withMeta } from "../utils/updateCheck.js";
import {
installPageFunctionGuard,
@@ -91,6 +92,10 @@ export function shouldIgnoreRequestFailure(
}
}
+export function projectPathOfUrl(url: string): string {
+ return decodeWellFormedEscapes(new URL(url).pathname).replace(/^\//, "");
+}
+
export function shouldIgnoreHttpError(url: string, status: number): boolean {
return status === 404 && isOptionalCaptionOverridesRequest(url);
}
@@ -477,7 +482,7 @@ async function validateInBrowser(
if (url.includes("favicon") || url.startsWith("data:")) return;
const failureText = req.failure()?.errorText;
if (shouldIgnoreRequestFailure(url, failureText, req.resourceType())) return;
- const path = decodeURIComponent(new URL(url).pathname).replace(/^\//, "");
+ const path = projectPathOfUrl(url);
errors.push({
level: "error",
text: `Failed to load ${path}: ${failureText ?? "net::ERR_FAILED"}`,
@@ -490,7 +495,7 @@ async function validateInBrowser(
const url = res.url();
if (url.includes("favicon")) return;
if (shouldIgnoreHttpError(url, res.status())) return;
- const path = decodeURIComponent(new URL(url).pathname).replace(/^\//, "");
+ const path = projectPathOfUrl(url);
errors.push({ level: "error", text: `${res.status()} loading ${path}`, url });
}
});
diff --git a/packages/cli/src/server/studioServer.previewAssets.test.ts b/packages/cli/src/server/studioServer.previewAssets.test.ts
index d74c0d6dd0..85acf27131 100644
--- a/packages/cli/src/server/studioServer.previewAssets.test.ts
+++ b/packages/cli/src/server/studioServer.previewAssets.test.ts
@@ -67,7 +67,7 @@ describe("studio preview document", () => {
expect(html).toContain('<base href="/api/projects/film/preview/">');
expect(await fetchAsset("assets/pic.png")).toEqual(PIC);
const nested = html.match(/<img [^>]*id="nested" src="([^"]+)"/)?.[1];
- expect(nested).toBe("assets/[email protected]");
+ expect(nested).toBe("assets/pic%402x.png");
expect(await fetchAsset(nested ?? "")).toEqual(PIC_2X);
});
diff --git a/packages/cli/src/utils/staticProjectServer.test.ts b/packages/cli/src/utils/staticProjectServer.test.ts
index c00871e147..1789678964 100644
--- a/packages/cli/src/utils/staticProjectServer.test.ts
+++ b/packages/cli/src/utils/staticProjectServer.test.ts
@@ -205,6 +205,18 @@ describe("serveStaticProjectHtml asset roots", () => {
expect(await res.text()).toBe("PROJECT");
});
+ it("serves a file whose name has a bare percent sign", async () => {
+ const projectDir = mk();
+ writeFileSync(join(projectDir, "100%.png"), "PERCENT");
+ writeFileSync(join(projectDir, "50% off.png"), "SALE");
+ server = await serveStaticProjectHtml(projectDir, "<html></html>", undefined, []);
+
+ const bare = await fetch(`${server.url}100%.png`);
+ expect(bare.status).toBe(200);
+ expect(await bare.text()).toBe("PERCENT");
+ expect(await (await fetch(`${server.url}50%%20off.png`)).text()).toBe("SALE");
+ });
+
it("404s a path present in no root", async () => {
const projectDir = mk();
server = await serveStaticProjectHtml(projectDir, "<html></html>", undefined, [mk()]);
diff --git a/packages/cli/src/utils/staticProjectServer.ts b/packages/cli/src/utils/staticProjectServer.ts
index 8506097abb..6be887b0d8 100644
--- a/packages/cli/src/utils/staticProjectServer.ts
+++ b/packages/cli/src/utils/staticProjectServer.ts
@@ -2,6 +2,7 @@ import { createServer, type ServerResponse } from "node:http";
import { createReadStream, existsSync, statSync } from "node:fs";
import { isAbsolute, relative, resolve } from "node:path";
import { getMimeType } from "@hyperframes/core/studio-api";
+import { decodeWellFormedEscapes } from "@hyperframes/studio-server";
import { resolveAutoProxy } from "./projectConfig.js";
import { injectMediaCodecMap } from "./compositionServer.js";
import {
@@ -186,7 +187,7 @@ export async function serveStaticProjectHtml(
const proxyRequest =
proxyParam !== null && isProxyVariantRequest(proxyParam) ? proxyParam : null;
- const requestPath = decodeURIComponent(pathOnly).replace(/^\//, "");
+ const requestPath = decodeWellFormedEscapes(pathOnly).replace(/^\//, "");
for (const root of roots) {
const filePath = resolve(root, requestPath);
const rel = relative(root, filePath);
diff --git a/packages/core/src/compiler/htmlBundler.test.ts b/packages/core/src/compiler/htmlBundler.test.ts
index f6f068148f..76588417f0 100644
--- a/packages/core/src/compiler/htmlBundler.test.ts
+++ b/packages/core/src/compiler/htmlBundler.test.ts
@@ -81,6 +81,64 @@ function makeSymlinkProject(
}
describe("bundleToSingleHtml", () => {
+ it("re-encodes physical directories when rebasing stylesheet and composition asset URLs", async () => {
+ const dir = makeTempProject({
+ "index.html":
+ '<html><head><link rel="stylesheet" href="css%2520%23/style.css"></head><body><main data-composition-id="root" data-width="320" data-height="180"><div data-composition-id="scene" data-composition-src="scenes%20#/scene.html" data-start="0" data-duration="1"></div></main></body></html>',
+ "css%20#/style.css": ".image {background:url(icon.png)}",
+ "css%20#/icon.png": "stylesheet directory witness",
+ "scenes%20#/scene.html":
+ '<template><div data-composition-id="scene" data-width="320" data-height="180"><img src="icon%28a%29.png"></div></template>',
+ "scenes%20#/icon(a).png": "composition directory witness",
+ });
+ try {
+ const bundled = await bundleToSingleHtml(dir);
+ expect(bundled).toContain(inlinedAs("image/png", "stylesheet directory witness"));
+ expect(bundled).toContain(inlinedAs("image/png", "composition directory witness"));
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ it("resolves encoded URL filenames once across asset, stylesheet and script consumers", async () => {
+ const dir = makeTempProject({
+ "index.html":
+ '<html><head><link rel="stylesheet" href="style%2520%23.css?cache=1"></head><body><main data-composition-id="root" data-width="320" data-height="180"><img src="image%2520%23.png#view"><div data-composition-id="scene" data-composition-src="scene%20.html" data-start="0" data-duration="1"></div></main><script src="app%2520%23.js?cache=1"></script></body></html>',
+ "style%20#.css": '@import "base%2520%23.css"; .image {background:url(image%2520%23.png)}',
+ "base%20#.css": ".decoded-import { color: blue; }",
+ "image%20#.png": "encoded image witness",
+ "app%20#.js": "window.encodedScriptWitness = true;",
+ "scene%20.html":
+ '<template><div data-composition-id="scene" data-width="320" data-height="180"><p>raw composition witness</p></div></template>',
+ });
+ try {
+ const bundled = await bundleToSingleHtml(dir);
+ expect(bundled).toContain(inlinedAs("image/png", "encoded image witness"));
+ expect(bundled).toContain(".decoded-import");
+ expect(bundled).toContain("window.encodedScriptWitness = true;");
+ expect(bundled).toContain("raw composition witness");
+ expect(bundled).not.toContain('src="app%2520%23.js');
+ expect(styleText(bundled)).not.toContain("url(image%2520%23.png)");
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
+ it("checks project containment after decoding an asset URL", async () => {
+ const dir = makeTempProject({
+ "index.html":
+ '<html><body><main data-composition-id="root" data-width="320" data-height="180"><img src="%2e%2e/outside.png"></main></body></html>',
+ });
+ const reads = new Set<string>();
+ try {
+ const bundled = await bundleToSingleHtml(dir, { onRead: (path) => reads.add(path) });
+ expect(bundled).toContain("%2e%2e/outside.png");
+ expect(reads).not.toContain(join(dir, "..", "outside.png"));
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
it("reports every project file it reads or looks for through onRead", async () => {
const dir = makeTempProject({
"index.html": `<!doctype html><html><head>
@@ -1776,6 +1834,97 @@ describe("bundleToSingleHtml", () => {
expect(bundled).not.toContain("url('./images/grain.png')");
});
+ it("decodes CSS escapes before rebasing linked stylesheet asset URLs", async () => {
+ const dir = makeTempProject({
+ "index.html": `<link rel="stylesheet" href="css/theme.css"><div data-composition-id="root" data-width="320" data-height="180"></div>`,
+ "css/theme.css": String.raw`.bg { background: url(icon\ 1.png); }`,
+ "css/icon 1.png": "escaped-space-image",
+ });
+
+ const linked = await bundleToSingleHtml(dir, { inlineAssets: false });
+ expect(styleText(linked)).toContain("url(css/icon%201.png)");
+ const inlined = await bundleToSingleHtml(dir);
+ expect(styleText(inlined)).toContain(inlinedAs("image/png", "escaped-space-image"));
+ });
+
+ it("keeps raw URL backslashes distinct from encoded filename backslashes in linked CSS", async () => {
+ const dir = makeTempProject({
+ "index.html": `<link rel="stylesheet" href="css/theme.css"><div data-composition-id="root" data-width="320" data-height="180"></div>`,
+ "css/theme.css": String.raw`.raw { background: url("assets\\logo.png"); } .encoded { background: url("assets%5Clogo.png"); }`,
+ "css/assets/logo.png": "separator-image",
+ ...(process.platform === "win32"
+ ? {}
+ : { "css/assets\\logo.png": "literal-backslash-image" }),
+ });
+ const linked = await bundleToSingleHtml(dir, { inlineAssets: false });
+ expect(styleText(linked)).toContain('url("css/assets/logo.png")');
+ const encodedUrl =
+ process.platform === "win32" ? 'url("css/assets/logo.png")' : 'url("css/assets%5Clogo.png")';
+ expect(styleText(linked)).toContain(encodedUrl);
+ const inlined = await bundleToSingleHtml(dir);
+ expect(styleText(inlined)).toContain(inlinedAs("image/png", "separator-image"));
+ const encodedFileContents =
+ process.platform === "win32" ? "separator-image" : "literal-backslash-image";
+ expect(styleText(inlined)).toContain(inlinedAs("image/png", encodedFileContents));
+ });
+
+ it("removes CSS line continuations before rebasing linked stylesheet URLs", async () => {
+ const dir = makeTempProject({
+ "index.html": `<link rel="stylesheet" href="css/theme.css"><div data-composition-id="root" data-width="320" data-height="180"></div>`,
+ "css/theme.css": '.bg { background: url("icon\\\n1.png"); }',
+ "css/icon1.png": "continued-image",
+ });
+
+ const linked = await bundleToSingleHtml(dir, { inlineAssets: false });
+ expect(styleText(linked)).toContain('url("css/icon1.png")');
+ });
+
+ it("preserves escaped leading spaces as part of linked CSS asset identity", async () => {
+ const dir = makeTempProject({
+ "index.html": `<link rel="stylesheet" href="css/theme.css"><div data-composition-id="root" data-width="320" data-height="180"></div>`,
+ "css/theme.css": String.raw`.bg { background: url(\ image.png); }`,
+ "css/ image.png": "leading-space-image",
+ "css/image.png": "different-image",
+ });
+
+ const linked = await bundleToSingleHtml(dir, { inlineAssets: false });
+ expect(styleText(linked)).toContain("url(css/%20image.png)");
+ const inlined = await bundleToSingleHtml(dir);
+ expect(styleText(inlined)).toContain(inlinedAs("image/png", "leading-space-image"));
+ expect(styleText(inlined)).not.toContain(inlinedAs("image/png", "different-image"));
+ });
+
+ it.each([
+ ["", String.raw`?v=\ 1`, String.raw`?v=\0000201`],
+ ['"', String.raw`?v=\22 1`, String.raw`?v=\0000221`],
+ ['"', String.raw`?v=\22`, String.raw`?v=\000022`],
+ ['"', String.raw`?v=\3c /style\3e 1`, String.raw`?v=\00003c/style\00003e1`],
+ ])("escapes decoded CSS URL suffixes with delimiter %j", async (quote, suffix, escaped) => {
+ const dir = makeTempProject({
+ "index.html": `<link rel="stylesheet" href="css/theme.css"><div data-composition-id="root" data-width="320" data-height="180"></div>`,
+ "css/theme.css": `.bg { background: url(${quote}icon.png${suffix}${quote}); }`,
+ "css/icon.png": "suffix-image",
+ });
+
+ const linked = await bundleToSingleHtml(dir, { inlineAssets: false });
+ expect(styleText(linked)).toContain(`url(${quote}css/icon.png${escaped}${quote})`);
+ const inlined = await bundleToSingleHtml(dir);
+ expect(styleText(inlined)).toContain(inlinedAs("image/png", "suffix-image"));
+ });
+
+ it("normalizes CSS nulls before resolving linked stylesheet assets", async () => {
+ const dir = makeTempProject({
+ "index.html": `<link rel="stylesheet" href="css/theme.css"><div data-composition-id="root" data-width="320" data-height="180"></div>`,
+ "css/theme.css": ".bg { background: url(\0.png); }",
+ "css/\uFFFD.png": "replacement-character-image",
+ });
+
+ const linked = await bundleToSingleHtml(dir, { inlineAssets: false });
+ expect(styleText(linked)).toContain("url(css/%EF%BF%BD.png)");
+ const inlined = await bundleToSingleHtml(dir);
+ expect(styleText(inlined)).toContain(inlinedAs("image/png", "replacement-character-image"));
+ });
+
it("rebases url() paths with ../ traversal in nested @import", async () => {
const dir = makeTempProject({
"index.html": `<!doctype html>
diff --git a/packages/core/src/compiler/htmlBundler.ts b/packages/core/src/compiler/htmlBundler.ts
index 2f465a97d3..b41893abcb 100644
--- a/packages/core/src/compiler/htmlBundler.ts
+++ b/packages/core/src/compiler/htmlBundler.ts
@@ -25,6 +25,13 @@ import { cssVariableName } from "../tokenSlug";
import { AsyncLocalStorage } from "async_hooks";
import { readFileSync, existsSync, statSync } from "fs";
import { resolve, relative, dirname, isAbsolute, sep } from "path";
+import {
+ decodeCssEscapes,
+ decodeWellFormedEscapes,
+ encodeUrlPath,
+ decodedUrlPath,
+ splitUrlSuffix,
+} from "@hyperframes/parsers/asset-paths";
import { CSS_URL_RE, isNonRelativeUrl } from "./assetPaths.js";
import { transformSync } from "esbuild";
import { compileHtml, type MediaDurationProber } from "./htmlCompiler";
@@ -133,26 +140,35 @@ function withCommentsStripped<T>(
return { result, restore };
}
+function resolveRelativeUrlPath(fromDir: string, basePath: string): string {
+ return resolve(fromDir, decodeWellFormedEscapes(basePath.replaceAll("\\", "/")));
+}
+
function rebaseCssUrls(css: string, cssFileDir: string, projectDir: string): string {
const resolvedRoot = resolve(projectDir);
const resolvedDir = resolve(cssFileDir);
if (resolvedDir === resolvedRoot) return css;
return css.replace(CSS_URL_RE, (full, quote: string, urlValue: string) => {
- if (!urlValue || !isRelativeUrl(urlValue)) return full;
- const { basePath, suffix } = splitUrlSuffix(urlValue.trim());
+ const decoded = decodeCssEscapes(urlValue);
+ if (!decoded || !isRelativeUrl(decoded)) return full;
+ const { basePath, suffix } = splitUrlSuffix(decoded);
if (!basePath) return full;
- const absolutePath = resolve(resolvedDir, basePath);
- const rebased = relative(resolvedRoot, absolutePath).split(sep).join("/");
+ const absolutePath = resolveRelativeUrlPath(resolvedDir, basePath);
+ const rebased = encodeUrlPath(relative(resolvedRoot, absolutePath).split(sep).join("/"));
if (rebased === basePath) return full;
- return `url(${quote || ""}${rebased}${suffix}${quote || ""})`;
+ const escapedSuffix = suffix.replace(
+ /[\s\p{Cc}"'()\\<>]/gu,
+ (char) => `\\${char.codePointAt(0)!.toString(16).padStart(6, "0")}`,
+ );
+ return `url(${quote || ""}${rebased}${escapedSuffix}${quote || ""})`;
});
}
function rebaseRelativePath(urlValue: string, fromDir: string, toDir: string): string {
const { basePath, suffix } = splitUrlSuffix(urlValue.trim());
if (!basePath) return urlValue;
- const absolutePath = resolve(fromDir, basePath);
- const rebased = relative(resolve(toDir), absolutePath).split(sep).join("/");
+ const absolutePath = resolveRelativeUrlPath(fromDir, basePath);
+ const rebased = encodeUrlPath(relative(resolve(toDir), absolutePath).split(sep).join("/"));
return appendSuffixToUrl(rebased, suffix);
}
@@ -244,7 +260,7 @@ function inlineCssFile(
(full, _q1, urlPath, _q2, barePath, mediaQuery) => {
const importPath = urlPath ?? barePath;
if (!importPath || !isRelativeUrl(importPath)) return full;
- const resolved = resolve(cssFileDir, importPath);
+ const resolved = resolve(cssFileDir, decodedUrlPath(importPath));
// @import is resolved relative to the CSS file, but must stay within the
// project root; isSafePath also blocks symlink escapes (content is inlined).
if (!isSafePath(projectDir, resolved)) return full;
@@ -278,14 +294,6 @@ function safeReadFileBuffer(filePath: string): Buffer | null {
}
}
-function splitUrlSuffix(urlValue: string): { basePath: string; suffix: string } {
- const queryIdx = urlValue.indexOf("?");
- const hashIdx = urlValue.indexOf("#");
- if (queryIdx < 0 && hashIdx < 0) return { basePath: urlValue, suffix: "" };
- const cutIdx = queryIdx < 0 ? hashIdx : hashIdx < 0 ? queryIdx : Math.min(queryIdx, hashIdx);
- return { basePath: urlValue.slice(0, cutIdx), suffix: urlValue.slice(cutIdx) };
-}
-
function appendSuffixToUrl(baseUrl: string, suffix: string): string {
if (!suffix) return baseUrl;
if (suffix.startsWith("#")) return `${baseUrl}${suffix}`;
@@ -366,7 +374,7 @@ function maybeInlineRelativeAssetUrl(
if (!urlValue || !isRelativeUrl(urlValue)) return null;
const { basePath, suffix } = splitUrlSuffix(urlValue.trim());
if (!basePath) return null;
- const filePath = resolveWithinProject(projectDir, basePath);
+ const filePath = resolveWithinProject(projectDir, decodeWellFormedEscapes(basePath));
if (!filePath) return null;
const ext = filePath.toLowerCase().match(/\.[^.]+$/)?.[0] ?? "";
const mimeType = INLINE_MIME[ext];
@@ -893,11 +901,13 @@ function hoistExternalScript(
if (seenSrcs.has(src)) return;
seenSrcs.add(src);
if (!isNonRelativeUrl(src) && !isAbsolute(src) && attributes.type !== "module") {
- const jsPath = resolveWithinProject(projectDir, src);
+ const jsPath = resolveWithinProject(projectDir, decodedUrlPath(src));
const js = jsPath ? safeReadFile(jsPath) : null;
if (js != null) {
chunks.push(() =>
- preserveLocalScriptIntegrity(doc, src, (value) => resolveWithinProject(projectDir, value))
+ preserveLocalScriptIntegrity(doc, src, (value) =>
+ resolveWithinProject(projectDir, decodedUrlPath(value)),
+ )
? ""
: js,
);
@@ -969,6 +979,8 @@ async function bundleProject(projectDir: string, options?: BundleOptions): Promi
return isSafePath(projectDir, resolved) ? resolved : null;
};
+ const resolveEntryUrl = (url: string): string | null => resolveEntryPath(decodedUrlPath(url));
+
const readSource = options?.stampHfIds ? ensureHfIds : (html: string) => html;
noteRead(indexPath);
const rawHtml = readSource(readFileSync(indexPath, "utf-8"));
@@ -994,7 +1006,7 @@ async function bundleProject(projectDir: string, options?: BundleOptions): Promi
const href = el.getAttribute("href");
if (!href || !isRelativeUrl(href) || cssStyleMergeKey(el) === undefined) continue;
if (el.hasAttribute("disabled")) continue;
- const cssPath = resolveEntryPath(href);
+ const cssPath = resolveEntryUrl(href);
if (!cssPath) continue;
const css = safeReadFile(cssPath);
if (css == null) continue;
@@ -1016,7 +1028,7 @@ async function bundleProject(projectDir: string, options?: BundleOptions): Promi
]),
].map((el) => {
const src = el.getAttribute("src");
- const path = src && isRelativeUrl(src) ? resolveEntryPath(src) : null;
+ const path = src && isRelativeUrl(src) ? resolveEntryUrl(src) : null;
return src ? (path && safeReadFile(path)) || "" : el.textContent || "";
})
: [];
@@ -1084,11 +1096,11 @@ async function bundleProject(projectDir: string, options?: BundleOptions): Promi
if (seenCompScriptSrcs.has(extSrc)) continue;
seenCompScriptSrcs.add(extSrc);
if (isRelativeUrl(extSrc) && scriptItem.type !== "module") {
- const jsPath = resolveEntryPath(extSrc);
+ const jsPath = resolveEntryUrl(extSrc);
const js = jsPath ? safeReadFile(jsPath) : null;
if (js != null) {
const chunk = () =>
- preserveLocalScriptIntegrity(document, extSrc, resolveEntryPath) ? "" : js;
+ preserveLocalScriptIntegrity(document, extSrc, resolveEntryUrl) ? "" : js;
pushRun(scriptRuns, scriptItem.scene, chunk);
continue;
}
@@ -1219,12 +1231,12 @@ async function bundleProject(projectDir: string, options?: BundleOptions): Promi
for (const el of [...document.querySelectorAll("script[src]")]) {
const src = el.getAttribute("src");
if (!src || !isRelativeUrl(src)) continue;
- if (preserveLocalScriptIntegrity(document, src, resolveEntryPath)) continue;
+ if (preserveLocalScriptIntegrity(document, src, resolveEntryUrl)) continue;
// Module scripts can contain static imports whose resolution is relative
// to the script URL. Folding their source into a classic inline script
// both drops module semantics and changes the import base URL.
if ((el.getAttribute("type") || "").trim().toLowerCase() === "module") continue;
- const jsPath = resolveEntryPath(src);
+ const jsPath = resolveEntryUrl(src);
const js = jsPath ? safeReadFile(jsPath) : null;
if (js == null) continue;
el.setAttribute(INLINED_FILE_ATTR, src);
diff --git a/packages/core/src/compiler/htmlCompiler.test.ts b/packages/core/src/compiler/htmlCompiler.test.ts
index c70c3caba3..7857d1b55f 100644
--- a/packages/core/src/compiler/htmlCompiler.test.ts
+++ b/packages/core/src/compiler/htmlCompiler.test.ts
@@ -1,7 +1,48 @@
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { compileHtml } from "./htmlCompiler.js";
describe("compileHtml", () => {
+ it("decodes HTML source attributes before interpreting their URL suffixes", async () => {
+ const project = mkdtempSync(join(tmpdir(), "hf-entity-media-"));
+ writeFileSync(join(project, "it's&a.wav"), "duration witness");
+ try {
+ const compiled = await compileHtml(
+ `<audio id="clip" title="src='wrong.wav' >" src="it&#39;s&amp;a.wav">`,
+ project,
+ async (path) => {
+ expect(path).toBe(join(project, "it's&a.wav"));
+ expect(readFileSync(path, "utf8")).toBe("duration witness");
+ return 2;
+ },
+ );
+ expect(compiled).toContain('data-duration="2"');
+ } finally {
+ rmSync(project, { recursive: true, force: true });
+ }
+ });
+
+ it("passes a decoded local media URL to the duration prober", async () => {
+ const project = mkdtempSync(join(tmpdir(), "hf-encoded-media-"));
+ writeFileSync(join(project, "clip%20#.mp4"), "duration witness");
+ try {
+ const compiled = await compileHtml(
+ '<video id="clip" src="clip%2520%23.mp4?cache=1" data-start="0">',
+ project,
+ async (path) => {
+ expect(path).toBe(join(project, "clip%20#.mp4"));
+ expect(readFileSync(path, "utf8")).toBe("duration witness");
+ return 2;
+ },
+ );
+ expect(compiled).toContain('data-duration="2"');
+ } finally {
+ rmSync(project, { recursive: true, force: true });
+ }
+ });
+
it("preserves explicit looped media durations that exceed source duration", async () => {
const html =
'<video id="hero" src="hero.webm" data-start="0" data-duration="4" data-end="4" loop>';
diff --git a/packages/core/src/compiler/htmlCompiler.ts b/packages/core/src/compiler/htmlCompiler.ts
index cb9320c560..359938120f 100644
--- a/packages/core/src/compiler/htmlCompiler.ts
+++ b/packages/core/src/compiler/htmlCompiler.ts
@@ -1,4 +1,5 @@
import { resolve } from "path";
+import { decodedUrlPath } from "@hyperframes/parsers/asset-paths";
import {
compileTimingAttrs,
injectDurations,
@@ -15,7 +16,9 @@ import {
export type MediaDurationProber = (src: string) => Promise<number>;
function resolveMediaSrc(src: string, projectDir: string): string {
- return src.startsWith("http://") || src.startsWith("https://") ? src : resolve(projectDir, src);
+ return src.startsWith("http://") || src.startsWith("https://")
+ ? src
+ : resolve(projectDir, decodedUrlPath(src));
}
/**
@@ -64,6 +67,14 @@ export async function compileHtml(
}
}
+ return clampProbedMedia(html, projectDir, probeMediaDuration);
+}
+
+async function clampProbedMedia(
+ html: string,
+ projectDir: string,
+ probeMediaDuration: MediaDurationProber,
+): Promise<string> {
// Phase 2: Bound authored audio to playable source. Explicit video slots may
// outlive their source and render by holding the final frame.
const preResolved = extractResolvedMedia(html);
diff --git a/packages/core/src/compiler/timingCompiler.test.ts b/packages/core/src/compiler/timingCompiler.test.ts
index 30f5bbc01c..13f73fe0c8 100644
--- a/packages/core/src/compiler/timingCompiler.test.ts
+++ b/packages/core/src/compiler/timingCompiler.test.ts
@@ -31,17 +31,32 @@ describe("inert region scanning", () => {
`<!-- <script>${media} -->`,
`<script><!-- ${media}</script>`,
`<style><script>${media}</style>`,
- ])("preserves the existing complete-region boundaries in %j", (region) => {
- const result = compileTimingAttrs(region + media);
- expect(result).toEqual({ html: region + compileTimingAttrs(media).html, unresolved: [] });
- expect(extractResolvedMedia(region + media)).toEqual(extractResolvedMedia(media));
+ ])("matches browser-visible media boundaries in %j", (region) => {
+ const html = region + media;
+ const dom = new JSDOM(html);
+ const visible = [...dom.window.document.querySelectorAll("video")].map((el) => el.id);
+ dom.window.close();
+ expect(extractResolvedMedia(html).map((el) => el.id)).toEqual(visible);
+ const compiled = new JSDOM(compileTimingAttrs(html).html);
+ expect(
+ [...compiled.window.document.querySelectorAll("video")].map((el) =>
+ el.getAttribute("data-end"),
+ ),
+ ).toEqual(visible.map(() => "3"));
+ compiled.window.close();
});
it.each(["<!--", "<script>", "<style>", "<scripture>", "<stylesheet>"])(
- "keeps media outside a complete inert region after %j visible",
+ "matches browser-visible media after an unclosed %j prefix",
(prefix) => {
- expect(compileTimingAttrs(prefix + media).html).toBe(prefix + compileTimingAttrs(media).html);
- expect(extractResolvedMedia(prefix + media)).toEqual(extractResolvedMedia(media));
+ const html = prefix + media;
+ const dom = new JSDOM(html);
+ const visible = [...dom.window.document.querySelectorAll("video")].map((el) => el.id);
+ dom.window.close();
+ expect(extractResolvedMedia(html).map((el) => el.id)).toEqual(visible);
+ expect(compileTimingAttrs(html).html).toBe(
+ visible.length ? prefix + compileTimingAttrs(media).html : html,
+ );
},
);
@@ -78,10 +93,12 @@ describe("inert region scanning", () => {
const unclosed = prefix.repeat(100_000);
const hidden = prefix === "<!--" ? `<style>${media}</style>` : `<!--${media}-->`;
expect(compileTimingAttrs(unclosed + hidden + media)).toEqual({
- html: unclosed + hidden + compileTimingAttrs(media).html,
+ html: unclosed + hidden + (prefix === "<!--" ? media : compileTimingAttrs(media).html),
unresolved: [],
});
- expect(extractResolvedMedia(unclosed + hidden + media)).toEqual(extractResolvedMedia(media));
+ expect(extractResolvedMedia(unclosed + hidden + media)).toEqual(
+ prefix === "<!--" ? [] : extractResolvedMedia(media),
+ );
},
);
@@ -105,14 +122,17 @@ describe("opening tag scanning", () => {
);
it.each(["target", "a>b", "a<b", "a.b[0]"])(
- "preserves substring-ID matches and delimiter characters for %j",
+ "matches only the actual ID while retaining delimiter characters for %j",
(id) => {
- const html = `<video data-id="${id}" data-start="1" data-duration="bad" data-end="4">`;
+ const prefixed = `<video data-id="${id}" data-start="1" data-duration="bad" data-end="4">`;
+ expect(injectDurations(prefixed, [{ id, duration: 3 }])).toBe(prefixed);
+ expect(clampDurations(prefixed, [{ id, duration: 3 }])).toBe(prefixed);
+ const html = `<video id="${id}" data-start="1" data-duration="bad" data-end="4">`;
expect(injectDurations(html, [{ id, duration: 3 }])).toBe(
- `<video data-id="${id}" data-start="1" data-duration="3" data-end="4">`,
+ `<video id="${id}" data-start="1" data-duration="3" data-end="4">`,
);
expect(clampDurations(html, [{ id, duration: 3 }])).toBe(
- `<video data-id="${id}" data-start="1" data-duration="3" data-end="4">`,
+ `<video id="${id}" data-start="1" data-duration="3" data-end="4">`,
);
},
);
@@ -166,14 +186,16 @@ describe("opening tag scanning", () => {
]);
});
- it("retains the existing first-greater-than boundary even inside a quoted value", () => {
+ it("preserves a quoted greater-than delimiter while compiling the complete tag", () => {
const html = '<video title="a>b" data-duration="2">';
const result = compileTimingAttrs(html);
expect(result.html).toBe(
- '<video title="a id="hf-video-0" data-start="0" data-hf-auto-start="" data-has-audio="true">b" data-duration="2">',
+ '<video title="a>b" data-duration="2" id="hf-video-0" data-start="0" data-hf-auto-start="" data-end="2" data-has-audio="true">',
);
- expect(result.unresolved.map((el) => el.id)).toEqual(["hf-video-0"]);
- expect(extractResolvedMedia(html)).toEqual([]);
+ expect(result.unresolved).toEqual([]);
+ expect(extractResolvedMedia(result.html).map((el) => [el.id, el.duration])).toEqual([
+ ["hf-video-0", 2],
+ ]);
});
});
diff --git a/packages/core/src/compiler/timingCompiler.ts b/packages/core/src/compiler/timingCompiler.ts
index 2874a45190..5150ec8bdf 100644
--- a/packages/core/src/compiler/timingCompiler.ts
+++ b/packages/core/src/compiler/timingCompiler.ts
@@ -2,7 +2,7 @@
* Timing Compiler
*
* Shared, pure HTML compilation that normalizes timing attributes.
- * Works in both Node.js and browser (regex-based, no DOM).
+ * Works in both Node.js and browser without a DOM.
*
* Guarantees every timed element gets:
* - id on media elements when missing
@@ -18,6 +18,7 @@
* `data-end` off so extract can resolve the id-ref later.
*/
+import { scanHtmlOpeningTags, decodeAuthoredAttribute } from "@hyperframes/parsers";
import { parseNumeric } from "@hyperframes/parsers/composition-contract";
import {
parseStrictFiniteTimingNumber,
@@ -85,113 +86,54 @@ export function shouldClampResolvedMediaDuration(
// ── Helpers ──────────────────────────────────────────────────────────────
-function getAttr(tag: string, attr: string): string | null {
- // `(?<![\w-])` anchors the attribute name to a fresh start. Without it,
- // `getAttr(tag, "id")` matches the trailing `id="…"` inside `data-hf-id="…"`
- // (and "src" inside `data-src`, etc.) and returns a phantom value. That bug
- // made compileTag believe a Studio-stamped `data-hf-id`-only element already
- // had an `id`, so it skipped its `hf-video-N` injection — leaving the element
- // with no real `el.id`, which the render pipeline keys off of (blank wash).
- const match = tag.match(new RegExp(`(?<![\\w-])${attr}=(?:"([^"]+)"|'([^']+)')`));
- return match ? (match[1] ?? match[2] ?? null) : null;
+function sourceAttribute(tag: string, name: string) {
+ return scanHtmlOpeningTags(tag)[0]?.attributes.find((attr) => attr.name === name);
+}
+
+function getAttr(tag: string, name: string): string | null {
+ return tagAttrReader(tag).getAttribute(name);
}
-/** An attribute reader over tag source that decodes the entities the DOM would, for JSON-valued attributes. */
function tagAttrReader(tag: string): Pick<Element, "getAttribute"> {
+ const attributes = scanHtmlOpeningTags(tag)[0]?.attributes ?? [];
return {
getAttribute: (name) => {
- const raw = getAttr(tag, name);
- return raw && name === "data-automation"
- ? raw
- .replace(/&quot;/g, '"')
- .replace(/&#39;/g, "'")
- .replace(/&lt;/g, "<")
- .replace(/&gt;/g, ">")
- .replace(/&amp;/g, "&")
- : raw;
+ const attr = attributes.find((attr) => attr.name === name);
+ return attr?.kind === "value" ? decodeAuthoredAttribute(attr.value) : null;
},
};
}
-function hasAttr(tag: string, attr: string): boolean {
- return new RegExp(`\\s${attr}(?:\\s|=|>|/)`).test(tag);
+function hasAttr(tag: string, name: string): boolean {
+ return sourceAttribute(tag, name) !== undefined;
}
function injectAttr(tag: string, attr: string, value: string): string {
return tag.replace(/>$/, ` ${attr}="${value}">`);
}
-function setAttr(tag: string, attr: string, value: string): string {
- if (!hasAttr(tag, attr)) return injectAttr(tag, attr, value);
- return tag.replace(new RegExp(`(${attr}=["'])[^"']*(["'])`), `$1${value}$2`);
+function setAttr(tag: string, name: string, value: string): string {
+ const attr = sourceAttribute(tag, name);
+ if (!attr) return injectAttr(tag, name, value);
+ return tag.slice(0, attr.start) + `${name}="${value}"` + tag.slice(attr.end);
}
-// Real media/timing elements never live inside comments, <script>, or <style>.
-// The tag regexes below aren't comment-aware, so a comment that merely mentions
-// `<video>`/`<audio>` gets rewritten as if it were a real element (issue #1938).
-// Mask those inert regions with placeholders (no `<`, so the tag regexes skip
-// them) before scanning, then restore them verbatim.
-// The NUL delimiters must stay as \u0000 escapes: raw 0x00 bytes make this file
-// binary to git and are corrupted by Bun's transpiler when bundled (issue #2139).
-function maskInertRegions(html: string): { masked: string; restore: (s: string) => string } {
- const stash: string[] = [];
- const parts: string[] = [];
- const opening = /<!--|<script\b|<style\b/gi;
- const closings = new Map([
- ["<!--", /--!?>/g],
- ["<script", /<\/script\s*>/gi],
- ["<style", /<\/style\s*>/gi],
- ]);
- let cursor = 0;
- let match: RegExpExecArray | null;
- while ((match = opening.exec(html)) !== null) {
- const kind = match[0].toLowerCase();
- const closing = closings.get(kind);
- if (!closing) continue;
- closing.lastIndex = opening.lastIndex;
- const end = closing.exec(html) ? closing.lastIndex : -1;
- if (end < 0) {
- // No later opener of this kind can close either. Search each unmatched
- // suffix only once, while still allowing other kinds of inert regions.
- closings.delete(kind);
- continue;
+function* iterateOpeningTags(html: string, names: readonly string[]) {
+ for (const span of scanHtmlOpeningTags(html)) {
+ if (span.closed && names.includes(span.name)) {
+ yield { tag: html.slice(span.start, span.end), index: span.start, end: span.end };
}
- const token = `\u0000HFMASK${stash.length}\u0000`;
- parts.push(html.slice(cursor, match.index), token);
- stash.push(html.slice(match.index, end));
- cursor = end;
- opening.lastIndex = cursor;
- }
- parts.push(html.slice(cursor));
- const masked = parts.join("");
- const restore = (s: string): string =>
- // oxlint-disable-next-line no-control-regex -- NUL cannot appear in HTML, which is what makes it a safe mask delimiter
- s.replace(/\u0000HFMASK(\d+)\u0000/g, (_, i) => stash[Number(i)] ?? "");
- return { masked, restore };
-}
-
-// ── Core compilation ─────────────────────────────────────────────────────
-
-function* iterateOpeningTags(html: string, prefix: RegExp) {
- let match: RegExpExecArray | null;
- while ((match = prefix.exec(html)) !== null) {
- const closing = html.indexOf(">", prefix.lastIndex);
- // Without a closer, no later prefix can form a complete tag either.
- if (closing < 0) break;
- const end = closing + 1;
- yield { tag: html.slice(match.index, end), index: match.index, end };
- prefix.lastIndex = end;
}
}
function replaceOpeningTags(
html: string,
- prefix: RegExp,
+ names: readonly string[],
replace: (tag: string) => string,
): string {
const parts: string[] = [];
let cursor = 0;
- for (const { tag, index, end } of iterateOpeningTags(html, prefix)) {
+ for (const { tag, index, end } of iterateOpeningTags(html, names)) {
parts.push(html.slice(cursor, index), replace(tag));
cursor = end;
}
@@ -200,43 +142,25 @@ function replaceOpeningTags(
}
function replaceIdTags(html: string, id: string, replace: (tag: string) => string): string {
- const idPattern = new RegExp(`id=["']${escapeRegex(id)}["']`, "gi");
- const lastClosing = html.lastIndexOf(">");
const parts: string[] = [];
let cursor = 0;
- let candidate = idPattern.exec(html);
- for (const { index, end } of iterateOpeningTags(html, /</g)) {
- if (index < cursor) continue;
- let targetEnd = -1;
- while (candidate && candidate.index < end) {
- const candidateEnd = candidate.index + candidate[0].length;
- if (candidate.index > index && candidateEnd <= lastClosing) targetEnd = candidateEnd;
- // Preserve the old greedy prefix's last matching ID, including overlaps.
- idPattern.lastIndex = candidate.index + 1;
- candidate = idPattern.exec(html);
- }
- if (targetEnd < 0) continue;
- // An authored ID can itself contain '>', so its closer may follow the
- // initial span. The lastClosing check guarantees a closing delimiter.
- const closing = html.indexOf(">", targetEnd) + 1;
- parts.push(html.slice(cursor, index), replace(html.slice(index, closing)));
- cursor = closing;
+ for (const span of scanHtmlOpeningTags(html)) {
+ if (!span.closed) continue;
+ const tag = html.slice(span.start, span.end);
+ const attr = span.attributes.find((attr) => attr.name === "id");
+ if (attr?.kind !== "value" || decodeAuthoredAttribute(attr.value) !== id) continue;
+ parts.push(html.slice(cursor, span.start), replace(tag));
+ cursor = span.end;
}
parts.push(html.slice(cursor));
return parts.join("");
}
-function compileTag(
- tag: string,
- isVideo: boolean,
- generateId: () => number,
-): { tag: string; unresolved: UnresolvedElement | null } {
+function withDefaultTimingAttrs(tag: string, tagName: string, generateId: () => number) {
let result = tag;
- let unresolved: UnresolvedElement | null = null;
-
let id = getAttr(result, "id");
if (!id) {
- id = `${isVideo ? "hf-video" : "hf-audio"}-${generateId()}`;
+ id = `hf-${tagName}-${generateId()}`;
result = injectAttr(result, "id", id);
}
let startStr = getAttr(result, "data-start");
@@ -245,7 +169,24 @@ function compileTag(
result = injectAttr(result, "data-hf-auto-start", "");
startStr = "0";
}
- const start = parseNumeric(startStr);
+ return { tag: result, id, start: parseNumeric(startStr) };
+}
+
+function withVideoAudioFlag(tag: string, isVideo: boolean): string {
+ if (!isVideo || hasAttr(tag, "data-has-audio")) return tag;
+ return injectAttr(tag, "data-has-audio", hasAttr(tag, "muted") ? "false" : "true");
+}
+
+function compileTag(
+ tag: string,
+ isVideo: boolean,
+ generateId: () => number,
+): { tag: string; unresolved: UnresolvedElement | null } {
+ const tagName = isVideo ? "video" : "audio";
+ const defaults = withDefaultTimingAttrs(tag, tagName, generateId);
+ let result = defaults.tag;
+ let unresolved: UnresolvedElement | null = null;
+ const { id, start } = defaults;
const attrReader = tagAttrReader(result);
const mediaStart = readMediaStart(attrReader);
const playbackRate = readElementRateSpec(attrReader);
@@ -258,11 +199,11 @@ function compileTag(
if (start != null) {
result = injectAttr(result, "data-end", String(start + duration));
}
- } else if (id) {
+ } else {
// No data-duration: mark as unresolved so caller can provide it
unresolved = {
id,
- tagName: isVideo ? "video" : "audio",
+ tagName,
src: getAttr(result, "src") ?? undefined,
start: start ?? 0,
mediaStart,
@@ -271,12 +212,7 @@ function compileTag(
}
}
- // 2. Add data-has-audio to <video> elements. Muted videos are visual-only by
- // contract; audible media should be represented by either an unmuted video
- // with data-has-audio="true" or a separate <audio> element.
- if (isVideo && !hasAttr(result, "data-has-audio")) {
- result = injectAttr(result, "data-has-audio", hasAttr(result, "muted") ? "false" : "true");
- }
+ result = withVideoAudioFlag(result, isVideo);
return { tag: result, unresolved };
}
@@ -296,18 +232,15 @@ export function compileTimingAttrs(html: string): CompilationResult {
let nextVideoId = 0;
let nextAudioId = 0;
- const { masked, restore } = maskInertRegions(html);
- html = masked;
-
// Process <video ...> tags
- html = replaceOpeningTags(html, /<video/gi, (match) => {
+ html = replaceOpeningTags(html, ["video"], (match) => {
const { tag, unresolved: u } = compileTag(match, true, () => nextVideoId++);
if (u) unresolved.push(u);
return tag;
});
// Process <audio ...> tags
- html = replaceOpeningTags(html, /<audio/gi, (match) => {
+ html = replaceOpeningTags(html, ["audio"], (match) => {
const { tag, unresolved: u } = compileTag(match, false, () => nextAudioId++);
if (u) unresolved.push(u);
return tag;
@@ -315,7 +248,7 @@ export function compileTimingAttrs(html: string): CompilationResult {
// Identify unresolved timed elements (divs with data-start but no data-end/data-duration)
// These are typically compositions whose duration depends on GSAP timelines
- for (const { tag: match } of iterateOpeningTags(html, /<(?:div|section)/gi)) {
+ for (const { tag: match } of iterateOpeningTags(html, ["div", "section"])) {
if (!hasAttr(match, "data-start")) continue;
if (hasAttr(match, "data-end") || hasAttr(match, "data-duration")) continue;
@@ -334,7 +267,7 @@ export function compileTimingAttrs(html: string): CompilationResult {
}
}
- return { html: restore(html), unresolved };
+ return { html, unresolved };
}
/**
@@ -370,10 +303,6 @@ export function injectDurations(html: string, resolutions: ResolvedDuration[]):
return html;
}
-function escapeRegex(str: string): string {
- return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
-}
-
/**
* Extract video/audio elements that already have data-duration set.
* Used by callers to validate declared durations against actual source durations.
@@ -381,8 +310,7 @@ function escapeRegex(str: string): string {
export function extractResolvedMedia(html: string): ResolvedMediaElement[] {
const resolved: ResolvedMediaElement[] = [];
- html = maskInertRegions(html).masked;
- for (const { tag } of iterateOpeningTags(html, /<(?:video|audio)/gi)) {
+ for (const { tag } of iterateOpeningTags(html, ["video", "audio"])) {
const id = getAttr(tag, "id");
const durationStr = getAttr(tag, "data-duration");
if (!id || durationStr === null) continue;
@@ -418,11 +346,11 @@ export function clampDurations(html: string, clamps: ResolvedDuration[]): string
for (const { id, duration } of clamps) {
html = replaceIdTags(html, id, (tag) => {
// Replace data-duration value
- tag = tag.replace(/data-duration=["'][^"']*["']/, `data-duration="${duration}"`);
+ if (hasAttr(tag, "data-duration")) tag = setAttr(tag, "data-duration", String(duration));
const start = parseNumeric(getAttr(tag, "data-start"));
- if (start != null) {
- tag = tag.replace(/data-end=["'][^"']*["']/, `data-end="${start + duration}"`);
+ if (start != null && hasAttr(tag, "data-end")) {
+ tag = setAttr(tag, "data-end", String(start + duration));
}
return tag;
diff --git a/packages/core/src/timelineAssetId.test.ts b/packages/core/src/timelineAssetId.test.ts
index 618fee5681..5239210216 100644
--- a/packages/core/src/timelineAssetId.test.ts
+++ b/packages/core/src/timelineAssetId.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { buildTimelineAssetId } from "./timelineAssetId";
+import { buildTimelineAssetId, replacementTimelineAssetId } from "./timelineAssetId";
describe("timeline asset ids", () => {
it("trims a long underscore prefix and suffix", () => {
@@ -10,4 +10,13 @@ describe("timeline asset ids", () => {
it("mints the fallback when normalization leaves no name", () => {
expect(buildTimelineAssetId("assets/____.mp4", ["asset", "asset_2"])).toBe("asset_3");
});
+
+ it("renames a dropped clip whose src is URL-encoded, from the decoded names", () => {
+ const document = new DOMParser().parseFromString(
+ '<video id="my_clip" src="assets/my%20clip.mp4"></video>',
+ "text/html",
+ );
+ const video = document.querySelector("video")!;
+ expect(replacementTimelineAssetId(document, video, "assets/new%20take.mp4")).toBe("new_take");
+ });
});
diff --git a/packages/core/src/timelineAssetId.ts b/packages/core/src/timelineAssetId.ts
index 0ba38c81ea..a620702173 100644
--- a/packages/core/src/timelineAssetId.ts
+++ b/packages/core/src/timelineAssetId.ts
@@ -1,3 +1,5 @@
+import { decodedUrlPath } from "@hyperframes/parsers";
+
function trimIdUnderscores(value: string): string {
let start = 0;
let end = value.length;
@@ -85,7 +87,7 @@ function readMediaIdentity(element: Element): MediaIdentity | null {
}
function matchesGeneratedId({ id, src }: MediaIdentity): boolean {
- const base = buildTimelineAssetId(src, []);
+ const base = buildTimelineAssetId(decodedUrlPath(src), []);
const suffix = id.slice(base.length);
return id === base || (id.startsWith(base) && /^_(?:[2-9]\d*|1\d+)$/.test(suffix));
}
@@ -110,5 +112,5 @@ export function replacementTimelineAssetId(
.filter((other) => other !== element)
.map((other) => other.getAttribute("id"))
.filter((value): value is string => value !== null);
- return buildTimelineAssetId(newSrc, ids);
+ return buildTimelineAssetId(decodedUrlPath(newSrc), ids);
}
diff --git a/packages/parsers/package.json b/packages/parsers/package.json
index 03eb1e6329..c574a6132d 100644
--- a/packages/parsers/package.json
+++ b/packages/parsers/package.json
@@ -236,6 +236,7 @@
"@babel/parser": "^7.27.0",
"acorn": "^8.17.0",
"acorn-walk": "^8.3.5",
+ "entities": "^7.0.1",
"linkedom": "^0.18.12",
"magic-string": "^0.30.21",
"recast": "^0.23.11"
diff --git a/packages/parsers/src/assetPaths.ts b/packages/parsers/src/assetPaths.ts
index f02c374464..390bc66492 100644
--- a/packages/parsers/src/assetPaths.ts
+++ b/packages/parsers/src/assetPaths.ts
@@ -43,3 +43,11 @@ export function isPathInside(childPath: string, parentPath: string): boolean {
const rel = relative(absParent, absChild);
return rel !== "" && !rel.startsWith("..") && !isAbsolute(rel);
}
+
+export {
+ decodeCssEscapes,
+ decodeWellFormedEscapes,
+ decodedUrlPath,
+ splitUrlSuffix,
+ encodeUrlPath,
+} from "./utils/urlPath.js";
diff --git a/packages/parsers/src/assetResolution.test.ts b/packages/parsers/src/assetResolution.test.ts
index 8db653eb21..8499bb0cb5 100644
--- a/packages/parsers/src/assetResolution.test.ts
+++ b/packages/parsers/src/assetResolution.test.ts
@@ -68,6 +68,22 @@ describe("isUnresolvedAssetPlaceholder", () => {
});
describe("collectSubCompositionSrcs", () => {
+ it("does not retry every unmatched raw text opener or truncate quoted tag delimiters", () => {
+ expect(
+ collectSubCompositionSrcs(
+ "<script>".repeat(10000) + '<div data-composition-src="fake.html">',
+ ),
+ ).toEqual([]);
+ expect(collectSubCompositionSrcs('<div title=">" data-composition-src="real.html">')).toEqual([
+ "real.html",
+ ]);
+ });
+
+ it("reads the actual composition attribute and preserves its raw filename identity", () => {
+ const html = `<div title="data-composition-src='fake.html'" data-composition-src="scenes/it's&amp;%20?#.html"></div>`;
+ expect(collectSubCompositionSrcs(html)).toEqual(["scenes/it's&%20?#.html"]);
+ });
+
it("finds mounts inside a template, which a DOM query cannot see", () => {
const html =
'<!doctype html><html><body><div data-composition-src="compositions/a.html"></div>' +
diff --git a/packages/parsers/src/assetResolution.ts b/packages/parsers/src/assetResolution.ts
index e5449ac75f..591cb0a5c2 100644
--- a/packages/parsers/src/assetResolution.ts
+++ b/packages/parsers/src/assetResolution.ts
@@ -1,3 +1,4 @@
+import { scanHtmlOpeningTags, decodeAuthoredAttribute } from "./htmlAttributeSpans.js";
import { closeSync, constants, existsSync, fstatSync, openSync, readFileSync } from "node:fs";
import { isAbsolute, join, posix, relative, resolve, sep } from "node:path";
import { decodeUrlPathVariants } from "./composition.js";
@@ -50,60 +51,19 @@ export function isUnresolvedAssetPlaceholder(rawSrc: string): boolean {
return /^__[A-Z_]+__$/.test(rawSrc.trim()) || hasUnresolvedTemplatingToken(rawSrc);
}
-/** `data-composition-src="..."`, matched within a single already-delimited tag. */
-const COMPOSITION_SRC_ATTR = /\bdata-composition-src\s*=\s*["']([^"']+)["']/i;
-
/**
- * Every `data-composition-src` reference in one composition file's raw text, in
- * document order, deduped. The single owner of "which sub-compositions does
- * this file mount", so lint, telemetry, and any future scanner cannot drift
- * apart on the answer.
- *
- * Text-scanning rather than DOM-walking, and that is the load-bearing choice.
- * Every sub-composition except the render entry is authored inside a
- * `<template>` (the root index.html is forbidden from using that wrapper;
- * everything else prefers it). Template content is inert: it lives under
- * `template.content`, not the live document, so `document.querySelectorAll`
- * on a raw sub-composition file finds nothing and every nested reference
- * disappears. The renderer only gets away with a DOM scan because it recurses
- * on COMPILED html, where the wrapper is already gone.
- *
- * Callers must resolve each value against the PROJECT ROOT, never the
- * referencing file's directory: `data-composition-src` is root-relative at
- * every nesting level (see `parseSubCompositions` in htmlCompiler.ts).
- *
- * Comments, `<style>`, and `<script>` bodies are masked first so a
- * commented-out mount is not counted as a real one. Build-time placeholders and
- * remote or inline URLs are dropped: neither names a file on disk, and every
- * caller resolves what comes back against the project root.
- *
- * The scan walks tag by tag with `indexOf` rather than running one regex with
- * two open-ended `[^>]*` spans across the whole file. That shape is quadratic:
- * on input full of `<` with no `>`, every `<` starts a scan to end-of-string
- * that then backtracks, measured at 41ms / 165ms / 660ms / 2640ms for 10k /
- * 20k / 40k / 80k characters. This function runs on every render (via the
- * render plan), so a truncated download or a blob full of stray `<` would hang
- * the plan step before any video is produced. Bounding each regex to one
- * already-delimited tag makes the whole scan linear.
+ * Collect active, local composition paths in document order, including template content.
+ * Paths are physical file names relative to the project root at every nesting level.
*/
export function collectSubCompositionSrcs(html: string): string[] {
- const scannable = maskNonScannableRanges(html);
const srcs: string[] = [];
const seen = new Set<string>();
- let cursor = 0;
- while (cursor < scannable.length) {
- const open = scannable.indexOf("<", cursor);
- if (open === -1) break;
- const close = scannable.indexOf(">", open + 1);
- // An unterminated final tag is not a tag. The previous whole-file regex
- // also required a closing `>`, so this drops nothing it used to find.
- if (close === -1) break;
- cursor = close + 1;
-
- const match = COMPOSITION_SRC_ATTR.exec(scannable.slice(open, cursor));
- if (!match) continue;
- const src = (match[1] ?? "").trim();
+ for (const tag of scanHtmlOpeningTags(html)) {
+ if (!tag.closed) continue;
+ const attribute = tag.attributes.find((attr) => attr.name === "data-composition-src");
+ if (attribute?.kind !== "value") continue;
+ const src = decodeAuthoredAttribute(attribute.value).trim();
if (!src || seen.has(src)) continue;
// __UPPER__ placeholder or late-bound templating token — not a real reference.
if (isUnresolvedAssetPlaceholder(src)) continue;
diff --git a/packages/parsers/src/htmlAttributeSpans.test.ts b/packages/parsers/src/htmlAttributeSpans.test.ts
new file mode 100644
index 0000000000..db0b143b94
--- /dev/null
+++ b/packages/parsers/src/htmlAttributeSpans.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from "vitest";
+import { scanHtmlOpeningTags, decodeAuthoredAttribute } from "./htmlAttributeSpans.js";
+
+describe("HTML source attribute spans", () => {
+ it("uses browser entity casing and numeric replacement semantics", () => {
+ expect(decodeAuthoredAttribute("a&aMp;b&#128;&copy;.png")).toBe("a&aMp;b۩.png");
+ });
+ it("does not scan element-looking fallback text inside raw text elements", () => {
+ expect(
+ scanHtmlOpeningTags(
+ `<iframe><video src="fake.mp4"></video></iframe><video src="real.mp4">`,
+ ).map((tag) => tag.name),
+ ).toEqual(["iframe", "video"]);
+ });
+
+ it("keeps quoted attribute-looking content separate from actual attributes", () => {
+ const html = `<video title="src='fake.mp4' >" data-src="lazy.mp4" src="it's.mp4" muted>`;
+ const [tag] = scanHtmlOpeningTags(html);
+ expect(tag?.closed).toBe(true);
+ expect(
+ tag?.attributes.map((attr) => [attr.name, attr.kind === "value" ? attr.value : true]),
+ ).toEqual([
+ ["title", "src='fake.mp4' >"],
+ ["data-src", "lazy.mp4"],
+ ["src", "it's.mp4"],
+ ["muted", true],
+ ]);
+ const source = tag?.attributes.find((attr) => attr.name === "src");
+ expect(source?.kind === "value" ? html.slice(source.valueStart, source.valueEnd) : null).toBe(
+ "it's.mp4",
+ );
+ });
+ it("skips commented and raw text tags while retaining unquoted values", () => {
+ const tags = scanHtmlOpeningTags(
+ `<!-- <img src=comment.png> --><script>"<img src=fake.png>"</script><img src=real.png>`,
+ );
+ expect(tags.map((tag) => tag.name)).toEqual(["script", "img"]);
+ expect(tags[1]?.attributes[0]).toMatchObject({ kind: "value", value: "real.png", quote: "" });
+ });
+ it("decodes the authored grammar once including numeric quote and backslash entities", () => {
+ expect(decodeAuthoredAttribute("a&amp;quot;&#39;&#92;&#x20;b")).toBe("a&quot;'\\ b");
+ });
+});
diff --git a/packages/parsers/src/htmlAttributeSpans.ts b/packages/parsers/src/htmlAttributeSpans.ts
new file mode 100644
index 0000000000..a34506618a
--- /dev/null
+++ b/packages/parsers/src/htmlAttributeSpans.ts
@@ -0,0 +1,119 @@
+import { decodeHTMLAttribute } from "entities/decode";
+
+export type HtmlAttributeSpan = {
+ name: string;
+ start: number;
+ end: number;
+} & (
+ | { kind: "boolean" }
+ | { kind: "value"; value: string; valueStart: number; valueEnd: number; quote: string }
+);
+
+export interface HtmlOpeningTagSpan {
+ name: string;
+ start: number;
+ end: number;
+ bodyEnd: number;
+ closed: boolean;
+ rawTextEnd: number | null;
+ attributes: HtmlAttributeSpan[];
+}
+
+function readAttributeValue(html: string, at: number) {
+ while (/[\t\n\f\r ]/.test(html.charAt(at))) at++;
+ const quote = ['"', "'"].includes(html.charAt(at)) ? html.charAt(at) : "";
+ if (quote) at++;
+ const valueStart = at;
+ if (quote) {
+ const closing = html.indexOf(quote, at);
+ at = closing === -1 ? html.length : closing;
+ } else {
+ while (at < html.length && !/[\t\n\f\r >]/.test(html[at]!)) at++;
+ }
+ const valueEnd = at;
+ const end = quote ? Math.min(valueEnd + 1, html.length) : valueEnd;
+ return { value: html.slice(valueStart, valueEnd), valueStart, valueEnd, quote, end };
+}
+
+function readAttribute(html: string, start: number): HtmlAttributeSpan | null {
+ let at = start;
+ while (at < html.length && !/[\t\n\f\r =/>]/.test(html[at]!)) at++;
+ if (at === start) return null;
+ const name = html.slice(start, at).toLowerCase();
+ const nameEnd = at;
+ while (/[\t\n\f\r ]/.test(html[at] ?? "")) at++;
+ if (html[at] !== "=") return { kind: "boolean", name, start, end: nameEnd };
+ return { kind: "value", name, start, ...readAttributeValue(html, at + 1) };
+}
+
+function readOpeningTag(html: string, start: number, name: string, at: number): HtmlOpeningTagSpan {
+ const attributes: HtmlAttributeSpan[] = [];
+ while (at < html.length) {
+ while (/[\t\n\f\r /]/.test(html[at] ?? "")) at++;
+ if (at >= html.length || html[at] === ">") break;
+ const attribute = readAttribute(html, at);
+ if (attribute === null) {
+ at++;
+ continue;
+ }
+ attributes.push(attribute);
+ at = attribute.end;
+ }
+ const closed = html[at] === ">";
+ return {
+ name: name.toLowerCase(),
+ start,
+ bodyEnd: at,
+ end: closed ? at + 1 : at,
+ closed,
+ rawTextEnd: null,
+ attributes,
+ };
+}
+
+export function scanHtmlOpeningTags(html: string): HtmlOpeningTagSpan[] {
+ const tags: HtmlOpeningTagSpan[] = [];
+ const opening = /<!--[\s\S]*?(?:--!?>|$)|<([a-z][^\t\n\f\r />]*)/gi;
+ let match: RegExpExecArray | null;
+ while ((match = opening.exec(html)) !== null) {
+ if (!match[1]) continue;
+ const tag = readOpeningTag(html, match.index, match[1], opening.lastIndex);
+ tags.push(tag);
+ opening.lastIndex = tag.end;
+ if (
+ [
+ "script",
+ "style",
+ "textarea",
+ "title",
+ "xmp",
+ "iframe",
+ "noembed",
+ "noframes",
+ "plaintext",
+ ].includes(tag.name)
+ ) {
+ if (tag.name === "plaintext") {
+ tag.rawTextEnd = html.length;
+ break;
+ }
+ const closing = new RegExp(`</${tag.name}(?=[\\t\\n\\f\\r />])`, "gi");
+ closing.lastIndex = tag.end;
+ const end = closing.exec(html);
+ tag.rawTextEnd = end?.index ?? html.length;
+ opening.lastIndex = tag.rawTextEnd;
+ }
+ }
+ return tags;
+}
+
+export const HTML_ATTRIBUTE_ENTITIES: Record<string, readonly string[]> = {
+ "&": ["&amp;"],
+ '"': ["&quot;"],
+ "'": ["&#39;", "&apos;"],
+ "<": ["&lt;"],
+ ">": ["&gt;"],
+};
+export function decodeAuthoredAttribute(value: string): string {
+ return decodeHTMLAttribute(value);
+}
diff --git a/packages/parsers/src/index.ts b/packages/parsers/src/index.ts
index 783d8820ed..119b3f5071 100644
--- a/packages/parsers/src/index.ts
+++ b/packages/parsers/src/index.ts
@@ -17,7 +17,7 @@ export * from "./compositionDuration.js";
// consume them without depending on @hyperframes/core). The Node-only asset
// path helpers live behind the ./asset-paths subpath to keep this entry
// browser-safe.
-export { decodeUrlPathVariants } from "./utils/urlPath.js";
+export { decodeUrlPathVariants, decodedUrlPath, encodeUrlPath } from "./utils/urlPath.js";
export { scanVariableUsage, type VariableUsageScan } from "./variableUsage.js";
export { extractMediaSrcMutations, type MediaSrcMutation } from "./mediaSrcMutation.js";
export {
@@ -26,3 +26,5 @@ export {
CANONICAL_FONT_DISPLAY_NAMES,
resolveAliasDisplayName,
} from "./fontAliases.js";
+
+export * from "./htmlAttributeSpans.js";
diff --git a/packages/parsers/src/rewriteSubCompPaths.test.ts b/packages/parsers/src/rewriteSubCompPaths.test.ts
index 2173bf2526..69c8cc53b5 100644
--- a/packages/parsers/src/rewriteSubCompPaths.test.ts
+++ b/packages/parsers/src/rewriteSubCompPaths.test.ts
@@ -6,6 +6,26 @@ import {
} from "./rewriteSubCompPaths.js";
describe("rewriteAssetPath", () => {
+ it("probes a raw filename and emits URL spelling with its suffix preserved", () => {
+ const probed: string[] = [];
+ const output = rewriteAssetPath(
+ "scenes%20?/scene.html",
+ "clip%3F%2520%23.png?v=2#frame",
+ (path) => {
+ probed.push(path);
+ return true;
+ },
+ );
+ expect(probed).toEqual(["scenes%20?/clip?%20#.png"]);
+ expect(output).toBe("scenes%2520%3F/clip%3F%2520%23.png?v=2#frame");
+ });
+
+ it("decodes traversal before rebasing and preserves URL suffixes", () => {
+ expect(rewriteAssetPath("scenes%20?/scene.html", "%2e%2e/clip%3F%2520%23.png?v=2#frame")).toBe(
+ "clip%3F%2520%23.png?v=2#frame",
+ );
+ });
+
it("rewrites `../` against the sub-composition dir", () => {
expect(rewriteAssetPath("compositions/scene.html", "../icon.svg")).toBe("icon.svg");
});
diff --git a/packages/parsers/src/rewriteSubCompPaths.ts b/packages/parsers/src/rewriteSubCompPaths.ts
index f92e349d73..614d3f3239 100644
--- a/packages/parsers/src/rewriteSubCompPaths.ts
+++ b/packages/parsers/src/rewriteSubCompPaths.ts
@@ -18,7 +18,14 @@
import { posix } from "path";
const { join, resolve, dirname } = posix;
-import { CSS_URL_RE, PATH_ATTRS, isNonRelativeUrl } from "./assetPaths.js";
+import {
+ CSS_URL_RE,
+ PATH_ATTRS,
+ isNonRelativeUrl,
+ splitUrlSuffix,
+ decodeWellFormedEscapes,
+ encodeUrlPath,
+} from "./assetPaths.js";
const isAbsoluteOrSpecial = isNonRelativeUrl;
@@ -52,12 +59,6 @@ function needsRewrite(val: string): boolean {
*/
export type AssetExists = (projectRelativePath: string) => boolean;
-/** Split `foo.png?v=2#frag` into its path and its `?`/`#` suffix. */
-function splitPathSuffix(value: string): [string, string] {
- const marker = value.search(/[?#]/);
- return marker === -1 ? [value, ""] : [value.slice(0, marker), value.slice(marker)];
-}
-
/**
* Rewrite a single relative path from a sub-composition's context to the
* project root context.
@@ -76,16 +77,12 @@ export function rewriteAssetPath(
if (isAbsoluteOrSpecial(relativePath)) return relativePath;
const compDir = dirname(compSrcPath);
if (!compDir || compDir === ".") return relativePath;
- if (!needsRewrite(relativePath)) {
- if (!assetExists) return relativePath;
- const [filePart, suffix] = splitPathSuffix(relativePath);
- if (!filePart) return relativePath;
- const sibling = resolve("/", join(compDir, filePart)).slice(1);
- return assetExists(sibling) ? sibling + suffix : relativePath;
- }
- const resolved = join(compDir, relativePath);
- const normalized = resolve("/", resolved).slice(1);
- return normalized;
+ const { basePath, suffix } = splitUrlSuffix(relativePath);
+ if (!basePath) return relativePath;
+ const filePath = decodeWellFormedEscapes(basePath);
+ const sibling = resolve("/", join(compDir, filePath)).slice(1);
+ if (!needsRewrite(filePath) && (!assetExists || !assetExists(sibling))) return relativePath;
+ return encodeUrlPath(sibling) + suffix;
}
/**
diff --git a/packages/parsers/src/utils/urlPath.test.ts b/packages/parsers/src/utils/urlPath.test.ts
new file mode 100644
index 0000000000..2f59906d7a
--- /dev/null
+++ b/packages/parsers/src/utils/urlPath.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from "vitest";
+import {
+ decodeUrlPathVariants,
+ decodedUrlPath,
+ encodeUrlPath,
+ decodeCssEscapes,
+} from "./urlPath.js";
+
+describe("URL filename identity", () => {
+ it("decodes valid escapes beside a literal percent while retaining the raw candidate", () => {
+ expect(decodeUrlPathVariants("it's%20100%.mp4")).toEqual(["it's 100%.mp4", "it's%20100%.mp4"]);
+ });
+ it("removes URL suffixes before decoding encoded filename punctuation exactly once", () => {
+ expect(decodedUrlPath("a%2520%3F%23.png?cache=1#view")).toBe("a%20?#.png");
+ });
+ it("normalizes CSS nulls and invalid escaped code points before URL encoding", () => {
+ for (const escaped of ["\0", String.raw`\0`, String.raw`\d800`, String.raw`\110000`]) {
+ expect(decodeCssEscapes(escaped)).toBe("\uFFFD");
+ }
+ });
+ it("encodes a physical path for a CSS URL without changing path separators", () => {
+ expect(encodeUrlPath("assets/it's (100%)?#.png")).toBe(
+ "assets/it%27s%20%28100%25%29%3F%23.png",
+ );
+ });
+});
diff --git a/packages/parsers/src/utils/urlPath.ts b/packages/parsers/src/utils/urlPath.ts
index dbcebec7d7..5c8c98d320 100644
--- a/packages/parsers/src/utils/urlPath.ts
+++ b/packages/parsers/src/utils/urlPath.ts
@@ -1,11 +1,54 @@
export function decodeUrlPathVariants(path: string): string[] {
- const variants = [path];
- try {
- const decoded = decodeURIComponent(path);
- if (decoded !== path) variants.unshift(decoded);
- } catch {
- // Malformed percent sequences may be literal filesystem names.
- }
-
- return variants;
+ const decoded = decodeWellFormedEscapes(path);
+ return decoded === path ? [path] : [decoded, path];
+}
+
+// A browser sends src="100%.png" as-is, so a % without two hex digits after it is the file's own.
+export function decodeWellFormedEscapes(path: string): string {
+ return path.replace(/(?:%[0-9A-Fa-f]{2})+/g, (run) => {
+ try {
+ return decodeURIComponent(run);
+ } catch {
+ return run;
+ }
+ });
+}
+
+export function splitUrlSuffix(urlValue: string): { basePath: string; suffix: string } {
+ const queryIdx = urlValue.indexOf("?");
+ const hashIdx = urlValue.indexOf("#");
+ if (queryIdx < 0 && hashIdx < 0) return { basePath: urlValue, suffix: "" };
+ let cutIdx = urlValue.length;
+ if (queryIdx >= 0) cutIdx = Math.min(cutIdx, queryIdx);
+ if (hashIdx >= 0) cutIdx = Math.min(cutIdx, hashIdx);
+ return { basePath: urlValue.slice(0, cutIdx), suffix: urlValue.slice(cutIdx) };
+}
+
+export function decodedUrlPath(url: string): string {
+ return decodeWellFormedEscapes(splitUrlSuffix(url).basePath);
+}
+
+export function encodeUrlPath(path: string): string {
+ return encodeURIComponent(path)
+ .replace(/%2F/g, "/")
+ .replace(/['()]/g, (char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`);
+}
+
+export function decodeCssEscapes(text: string): string {
+ return text
+ .replaceAll("\0", "\uFFFD")
+ .replace(
+ /\\(?:([0-9a-f]{1,6})(?:\r\n|[ \t\r\n\f])?|(\r\n|[\n\r\f])|([\s\S]))/gi,
+ (
+ _escaped,
+ hex: string | undefined,
+ lineBreak: string | undefined,
+ char: string | undefined,
+ ) => {
+ if (hex === undefined) return lineBreak === undefined ? char! : "";
+ const code = Number.parseInt(hex, 16);
+ if (code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) return "\uFFFD";
+ return String.fromCodePoint(code);
+ },
+ );
}
diff --git a/packages/producer/src/services/htmlCompiler.naturalDuration.test.ts b/packages/producer/src/services/htmlCompiler.naturalDuration.test.ts
index 2467a1d272..88ec4507ff 100644
--- a/packages/producer/src/services/htmlCompiler.naturalDuration.test.ts
+++ b/packages/producer/src/services/htmlCompiler.naturalDuration.test.ts
@@ -1,6 +1,6 @@
import { afterAll, beforeAll, describe, expect, it } from "bun:test";
import { spawnSync } from "node:child_process";
-import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { parseHTML } from "linkedom";
@@ -57,6 +57,44 @@ describe("compileForRender natural media duration parity", () => {
return { result, document: parseHTML(result.html).document };
}
+ it("probes and clamps a nested entry media URL against its decoded physical filename", async () => {
+ const entryDir = join(projectDir, "nested");
+ mkdirSync(entryDir, { recursive: true });
+ copyFileSync(audioPath, join(entryDir, "ten seconds%#.wav"));
+ const htmlPath = join(entryDir, "index.html");
+ writeFileSync(
+ htmlPath,
+ `<html><body><main data-composition-id="root" data-duration="100" data-width="16" data-height="16"><audio id="inferred" src="ten%20seconds%25%23.wav"></audio><audio id="clamped" src="ten%20seconds%25%23.wav" data-duration="12"></audio></main></body></html>`,
+ );
+ const result = await compileForRender(projectDir, htmlPath, projectDir, {
+ allowSystemFontCapture: false,
+ });
+ const document = parseHTML(result.html).document;
+ expect(Number(document.getElementById("inferred")?.getAttribute("data-duration"))).toBeCloseTo(
+ 10,
+ 3,
+ );
+ expect(Number(document.getElementById("clamped")?.getAttribute("data-duration"))).toBeCloseTo(
+ 10,
+ 3,
+ );
+ });
+
+ it("probes entity-spelled root-entry audio before URL decoding", async () => {
+ copyFileSync(audioPath, join(projectDir, "it's&a.wav"));
+ const { document } = await compile(
+ `<audio id="inferred" src="it&#39;s&amp;a.wav"></audio><audio id="clamped" src="it&#39;s&amp;a.wav" data-duration="12"></audio>`,
+ );
+ expect(Number(document.getElementById("inferred")?.getAttribute("data-duration"))).toBeCloseTo(
+ 10,
+ 3,
+ );
+ expect(Number(document.getElementById("clamped")?.getAttribute("data-duration"))).toBeCloseTo(
+ 10,
+ 3,
+ );
+ });
+
it("uses shared playback-rate parsing for duration-less video and audio", async () => {
const cases = [
["video-rate-2", "video", 'data-playback-rate="2"', 5],
diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts
index 14ce3fa936..db9762c74b 100644
--- a/packages/producer/src/services/htmlCompiler.ts
+++ b/packages/producer/src/services/htmlCompiler.ts
@@ -1,3 +1,4 @@
+import { decodedUrlPath } from "@hyperframes/parsers/asset-paths";
// fallow-ignore-file code-duplication complexity
/**
* HTML Compiler for Producer
@@ -482,8 +483,9 @@ async function resolveMediaDuration(
// The element will get duration 0 and be excluded from the render.
return { duration: null, resolvedPath: src };
}
- } else if (!filePath.startsWith("/")) {
- filePath = join(baseDir, filePath);
+ } else {
+ filePath = decodedUrlPath(src);
+ if (!filePath.startsWith("/")) filePath = join(baseDir, filePath);
}
if (!existsSync(filePath)) {
diff --git a/packages/studio-server/src/helpers/loudness.ts b/packages/studio-server/src/helpers/loudness.ts
index ba1cf1e473..bdff307b65 100644
--- a/packages/studio-server/src/helpers/loudness.ts
+++ b/packages/studio-server/src/helpers/loudness.ts
@@ -4,6 +4,7 @@ import { promisify } from "node:util";
import { isAudibleVideoElement } from "@hyperframes/core/audible-video";
import { formatAudioGain, MAX_AUDIO_GAIN_DB } from "@hyperframes/core/audio-gain";
import { findFfBinary } from "@hyperframes/parsers/ff-binaries";
+import { requestedProjectPath } from "./requestSubPath.js";
const execFileAsync = promisify(execFile);
@@ -333,13 +334,7 @@ export function audioTags(html: string): AudioTag[] {
/** Resolve only a file contained by the project. Remote and traversal sources are not measured. */
export function resolveLocalAudioPath(projectDir: string, src: string): string {
- const withoutSuffix = src.split(/[?#]/, 1)[0] ?? "";
- let decoded = "";
- try {
- decoded = decodeURIComponent(withoutSuffix);
- } catch {
- throw new Error(`Audio source must be a local project file: ${src}`);
- }
+ const decoded = requestedProjectPath(projectDir, src);
if (!decoded || isAbsolute(decoded) || /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(decoded)) {
throw new Error(`Audio source must be a local project file: ${src}`);
}
diff --git a/packages/studio-server/src/helpers/mediaCodecMap.test.ts b/packages/studio-server/src/helpers/mediaCodecMap.test.ts
index 5a17ef78b7..8898faf73a 100644
--- a/packages/studio-server/src/helpers/mediaCodecMap.test.ts
+++ b/packages/studio-server/src/helpers/mediaCodecMap.test.ts
@@ -334,6 +334,17 @@ describe("resolveProxyVariantRequest", () => {
});
describe("scanProjectMediaCodecMap", () => {
+ it("probes the actual quoted source with mixed literal and escaped percent spelling", async () => {
+ const project = tmpProject();
+ const videoPath = join(project, "it's 100%.mp4");
+ writeFileSync(videoPath, "fake video bytes");
+ const html = `<video title="src='fake.mp4'" data-src="lazy.mp4" src="it&#39;s%20100%.mp4"></video>`;
+ const map = await scanProjectMediaCodecMap(project, [{ html }], {
+ runner: makeRunner({ [videoPath]: "hevc" }),
+ });
+ expect(Object.keys(map)).toEqual(["/it's 100%.mp4"]);
+ });
+
it("omits an asset from the map (no throw) when ffprobe is unresolvable", async () => {
const project = tmpProject();
writeFileSync(join(project, "clip.mp4"), "fake video bytes");
diff --git a/packages/studio-server/src/helpers/mediaCodecMap.ts b/packages/studio-server/src/helpers/mediaCodecMap.ts
index 15649e2df9..accec7ac9e 100644
--- a/packages/studio-server/src/helpers/mediaCodecMap.ts
+++ b/packages/studio-server/src/helpers/mediaCodecMap.ts
@@ -1,13 +1,13 @@
-import { existsSync, statSync } from "node:fs";
+import { scanHtmlOpeningTags, decodeAuthoredAttribute } from "@hyperframes/parsers";
+import { statSync } from "node:fs";
import { realpath } from "./safePath.js";
-import { relative, resolve, sep } from "node:path";
+import { sep } from "node:path";
import { rewriteAssetPath } from "@hyperframes/parsers/asset-paths";
import {
cleanAssetUrl,
isRemoteOrInlineUrl,
isUnresolvedAssetPlaceholder,
- maskNonScannableRanges,
- resolveLocalAssetCandidates,
+ resolveExistingLocalAsset as resolveAsset,
} from "@hyperframes/parsers/asset-resolution";
import { pixelFormatHasAlpha, probeMediaMetadata, type FfprobeRunner } from "./mediaMetadata.js";
@@ -281,11 +281,6 @@ export interface HtmlSourceLike {
compSrcPath?: string;
}
-// --- <video src> collection: shared primitives live in
-// @hyperframes/parsers/asset-resolution; the <video>-specific regex and the
-// pinned key derivation stay here.
-const VIDEO_SRC_RE = /<video\b[^>]*\bsrc\s*=\s*["']([^"']+)["'][^>]*>/gi;
-
/**
* Resolve a `<video src>` reference to an existing local file.
*
@@ -301,13 +296,12 @@ function resolveExistingLocalAsset(
projectDir: string,
url: string,
): { resolvedPath: string; rootRelativePathname: string } | null {
- const projectRoot = resolve(projectDir);
- const resolvedPath = resolveLocalAssetCandidates(projectRoot, url).find((candidate) =>
- existsSync(candidate),
- );
- if (!resolvedPath) return null;
- const rootRelative = relative(projectRoot, resolvedPath).split(sep).join("/");
- return { resolvedPath, rootRelativePathname: `/${rootRelative}` };
+ const asset = resolveAsset(projectDir, url);
+ if (!asset) return null;
+ return {
+ resolvedPath: asset.resolved,
+ rootRelativePathname: `/${asset.rootRelativePath.split(sep).join("/")}`,
+ };
}
/**
@@ -322,11 +316,11 @@ function collectLocalVideoAssets(
const candidates = new Map<string, string>();
for (const { html, compSrcPath } of htmlSources) {
- const scannable = maskNonScannableRanges(html);
- const re = new RegExp(VIDEO_SRC_RE.source, VIDEO_SRC_RE.flags);
- let match: RegExpExecArray | null;
- while ((match = re.exec(scannable)) !== null) {
- const rawSrc = match[1] ?? "";
+ for (const tag of scanHtmlOpeningTags(html)) {
+ if (tag.name !== "video" || !tag.closed) continue;
+ const attribute = tag.attributes.find((attr) => attr.name === "src");
+ if (attribute?.kind !== "value") continue;
+ const rawSrc = decodeAuthoredAttribute(attribute.value);
// Placeholder check runs on the RAW value: cleanAssetUrl() splits on ?/# and would chop inside a ${...} token.
if (isUnresolvedAssetPlaceholder(rawSrc)) continue;
const src = cleanAssetUrl(rawSrc);
diff --git a/packages/studio-server/src/helpers/requestSubPath.ts b/packages/studio-server/src/helpers/requestSubPath.ts
index a9cc870c8c..5a3384392a 100644
--- a/packages/studio-server/src/helpers/requestSubPath.ts
+++ b/packages/studio-server/src/helpers/requestSubPath.ts
@@ -1,8 +1,20 @@
+import { existsSync } from "node:fs";
+import { decodedUrlPath, decodeWellFormedEscapes } from "@hyperframes/parsers/asset-paths";
+import { resolveWithinProject } from "./safePath.js";
+export { decodeWellFormedEscapes } from "@hyperframes/parsers/asset-paths";
+
// The decoded path after `route` ("projects/:id/preview", "composition"), cut by segment from the raw URL:
// Hono's c.req.path leaves %40 %25 %23 %26 %3F encoded, so cutting a decoded prefix out of it misses.
export function requestSubPath(url: string, route: string): string {
const routeSegments = route.split("/");
const segments = new URL(url).pathname.split("/");
const start = segments.indexOf(routeSegments[0] ?? "");
- return decodeURIComponent(segments.slice(start + routeSegments.length).join("/"));
+ return decodeWellFormedEscapes(segments.slice(start + routeSegments.length).join("/"));
+}
+
+// A project file a request names: the literal name when that file exists, otherwise the field read as a URL.
+export function requestedProjectPath(projectDir: string, field: string): string {
+ const trimmed = field.trim().replace(/^[.]\//, "");
+ const named = resolveWithinProject(projectDir, trimmed);
+ return named && existsSync(named) ? trimmed : decodedUrlPath(trimmed);
}
diff --git a/packages/studio-server/src/index.ts b/packages/studio-server/src/index.ts
index 968d2a3cc4..a12867c942 100644
--- a/packages/studio-server/src/index.ts
+++ b/packages/studio-server/src/index.ts
@@ -42,7 +42,7 @@ export type { PreviewApiAdapter } from "./helpers/mediaProxyPreview.js";
export { PREVIEW_BUNDLE_OPTIONS } from "./routes/preview.js";
export { createPreviewDocumentStore } from "./helpers/previewDocumentStore.js";
export { getMimeType, MIME_TYPES } from "./helpers/mime.js";
-export { requestSubPath } from "./helpers/requestSubPath.js";
+export { decodeWellFormedEscapes, requestSubPath } from "./helpers/requestSubPath.js";
export {
consumeFileWriteReceipt,
identifyFileWrite,
diff --git a/packages/studio-server/src/routes/files.renameReferences.test.ts b/packages/studio-server/src/routes/files.renameReferences.test.ts
index ca36ba442f..55c2a87c9a 100644
--- a/packages/studio-server/src/routes/files.renameReferences.test.ts
+++ b/packages/studio-server/src/routes/files.renameReferences.test.ts
@@ -9,7 +9,202 @@ import type { StudioApiAdapter } from "../types";
const rewrite = (text: string, from: string, to: string, folder: boolean) =>
referenceRewriter(from, to, folder)(text);
+function fileRoutesFor(project: string): Hono {
+ const adapter = {
+ resolveProject: async (id: string) => ({ id, dir: project }),
+ } as unknown as StudioApiAdapter;
+ const app = new Hono();
+ registerFileRoutes(app, adapter);
+ return app;
+}
+
describe("rename references", () => {
+ it.each([
+ ["data:logo.png", "data%3Alogo.png"],
+ ["x\\y.png", "x%5Cy.png"],
+ [" a\t\n\r .png ", "%20a%09%0A%0D%20.png%20"],
+ ])("keeps the renamed physical file %j representable in a raw HTML URL", (filename, url) => {
+ expect(rewrite('<img src="old.png">', "old.png", filename, false)).toBe(`<img src="${url}">`);
+ });
+
+ it("renames a reference spelled percent-encoded", () => {
+ expect(
+ rewrite(
+ '<video src="assets/My%20clip.mp4"></video>',
+ "assets/My clip.mp4",
+ "assets/New clip.mp4",
+ false,
+ ),
+ ).toBe('<video src="assets/New%20clip.mp4"></video>');
+ });
+
+ it("does not classify active URL fields through script-looking comments", () => {
+ expect(
+ rewrite(`<!-- <script> --><img src="a.png"><!-- </script> -->`, "a.png", "a?#.png", false),
+ ).toBe(`<!-- <script> --><img src="a%3F%23.png"><!-- </script> -->`);
+ });
+ it("keeps unquoted entity-spelled references valid when the new name contains spaces", () => {
+ expect(rewrite(`<img src=a&amp;b.png>`, "a&b.png", "new file.png", false)).toBe(
+ `<img src=new&#32;file.png>`,
+ );
+ expect(
+ rewrite(`<div data-composition-src=a&amp;b.html>`, "a&b.html", "new file.html", false),
+ ).toBe(`<div data-composition-src=new&#32;file.html>`);
+ });
+
+ it("keeps a URL suffix distinct from a longer physical filename that exists", () => {
+ const rename = referenceRewriter("a.png", "new.png", false, ["a.png?x", "a.png#x"]);
+ expect(rename(`<img src="a.png?x"><img src="a.png#x">`)).toBe(
+ `<img src="new.png?x"><img src="new.png#x">`,
+ );
+ });
+
+ it("does not borrow HTML decoding from a later URL field for a raw script", () => {
+ const text = `<script>const p = "a&amp;b.png";</script><img src="other.png">`;
+ expect(rewrite(text, "a&b.png", "new.png", false)).toBe(text);
+ });
+
+ it("encodes each srcset candidate and SVG href for its URL consumer", () => {
+ const text = `<img srcset="old.png 1x, other.png 2x"><image xlink:href="old.png"/>`;
+ expect(rewrite(text, "old.png", "new file%#.png", false)).toBe(
+ `<img srcset="new%20file%25%23.png 1x, other.png 2x"><image xlink:href="new file%25%23.png"/>`,
+ );
+ });
+
+ it("does not rename a URL query as part of the physical filename", () => {
+ const text = `<img src="a.png?x"><img src="other.png?asset=a.png">`;
+ expect(rewrite(text, "a.png?x", "new.png", false)).toBe(text);
+ expect(rewrite(`<img src="other.png?asset=a.png">`, "a.png", "new.png", false)).toBe(
+ `<img src="other.png?asset=a.png">`,
+ );
+ });
+
+ it("does not treat an attribute-looking string inside a raw HTML value as a URL field", () => {
+ const text = `<div data-note="src='old%20file.png'"></div>`;
+ expect(rewrite(text, "old file.png", "new file.png", false)).toBe(text);
+ });
+
+ it("decodes the enclosing HTML grammar when matching a CSS URL in a style attribute", () => {
+ const text = '<div style="background:url(&quot;assets/a&#39;b.png&quot;)"></div>';
+ expect(rewrite(text, "assets/a'b.png", "assets/c(d).png", false)).toBe(
+ '<div style="background:url(&quot;assets/c%28d%29.png&quot;)"></div>',
+ );
+ });
+
+ it("does not interpret percent or entity spelling in raw script file values", () => {
+ const text = '{"path":"old%20file.png","raw":"old file.png"}';
+ expect(
+ referenceRewriter("old file.png", "new file.png", false, ["old%20file.png"])(text, "script"),
+ ).toBe('{"path":"old%20file.png","raw":"new file.png"}');
+ expect(referenceRewriter("a&b.png", "new.png", false)('"a&amp;b.png"', "script")).toBe(
+ '"a&amp;b.png"',
+ );
+ });
+
+ it("keeps a raw percent-spelled composition sibling distinct from a space", () => {
+ const text = '<div data-composition-src="scenes/old%20file.html"></div>';
+ expect(referenceRewriter("scenes/old file.html", "scenes/new file.html", false)(text)).toBe(
+ text,
+ );
+ });
+
+ it("can rename an entity-quoted composition path containing literal percent text twice", () => {
+ const first = rewrite(
+ '<div data-composition-src="scenes/a.html"></div>',
+ "scenes/a.html",
+ 'scenes/a"%20.html',
+ false,
+ );
+ expect(first).toBe('<div data-composition-src="scenes/a&quot;%20.html"></div>');
+ expect(rewrite(first, 'scenes/a"%20.html', "scenes/new file.html", false)).toBe(
+ '<div data-composition-src="scenes/new file.html"></div>',
+ );
+ });
+
+ it("escapes parentheses in an unquoted CSS URL", () => {
+ expect(
+ referenceRewriter("assets/a.png", "assets/a(b).png", false)("url(assets/a.png)", "css"),
+ ).toBe("url(assets/a%28b%29.png)");
+ });
+
+ it("does not backtrack through overlapping whitespace in an unfinished CSS URL", () => {
+ const text = "url(assets/a.png)\nurl(" + " ".repeat(10000);
+ expect(referenceRewriter("assets/a.png", "assets/b.png", false)(text, "css")).toBe(
+ "url(assets/b.png)\nurl(" + " ".repeat(10000),
+ );
+ });
+
+ it("keeps the whole unquoted reference valid when renaming a folder with spaces", () => {
+ expect(rewrite("<img src=assets/a.png>", "assets", "my assets", true)).toBe(
+ "<img src=my&#32;assets/a.png>",
+ );
+ expect(referenceRewriter("assets", "my assets", true)("url(assets/a.png)", "css")).toBe(
+ "url(my%20assets/a.png)",
+ );
+ });
+
+ it("can rename a template-literal filename again after escaping interpolation", () => {
+ const original = "`assets/a.png`";
+ const first = referenceRewriter(
+ "assets/a.png",
+ "assets/${name}.png",
+ false,
+ )(original, "script");
+ expect(first).toBe("`assets/\\${name}.png`");
+ expect(
+ referenceRewriter("assets/${name}.png", "assets/final.png", false)(first, "script"),
+ ).toBe("`assets/final.png`");
+ });
+
+ it("keeps URL delimiters and literal percent signs escaped in an entity-spelled reference", () => {
+ expect(
+ rewrite('<img src="assets/a&amp;b.png">', "assets/a&b.png", "assets/c(?%20#).png", false),
+ ).toBe('<img src="assets/c(%3F%2520%23).png">');
+ });
+
+ it("leaves encoded longer existing names alone on either side of a match", () => {
+ const text =
+ '<img src="assets/my%20clip.png&amp;backup.png"><img src="other%20assets/my%20clip.png"><img src="assets/my%20clip.png">';
+ expect(
+ referenceRewriter("assets/my clip.png", "assets/new clip.png", false, [
+ "assets/my clip.png&backup.png",
+ "other assets/my clip.png",
+ "assets/my clip.png",
+ ])(text),
+ ).toBe(
+ '<img src="assets/my%20clip.png&amp;backup.png"><img src="other%20assets/my%20clip.png"><img src="assets/new%20clip.png">',
+ );
+ });
+
+ it("escapes a newly introduced quote even when the old reference used no escapes", () => {
+ expect(rewrite('<img src="assets/a.png">', "assets/a.png", 'assets/say "hi".png', false)).toBe(
+ '<img src="assets/say &quot;hi&quot;.png">',
+ );
+ });
+
+ it("keeps URL delimiters and quotes encoded when renaming a percent-spelled reference", () => {
+ expect(
+ rewrite("<img src='assets/my%20clip.png'>", "assets/my clip.png", "assets/it's?#.png", false),
+ ).toBe("<img src='assets/it%27s%3F%23.png'>");
+ });
+
+ it("can rename an apostrophe reference after it was percent-encoded", () => {
+ expect(
+ rewrite('<img src="assets/it%27s.png">', "assets/it's.png", "assets/their.png", false),
+ ).toBe('<img src="assets/their.png">');
+ });
+
+ it("keeps both quote kinds escaped when renaming an entity-spelled reference", () => {
+ expect(
+ rewrite(
+ "<img src='assets/it&apos;s.png'>",
+ "assets/it's.png",
+ `assets/that's & "ours".png`,
+ false,
+ ),
+ ).toBe("<img src='assets/that&#39;s &amp; &quot;ours&quot;.png'>");
+ });
+
it("rewrites a folder where files under it are named, in every form a project writes them", () => {
const text = [
'<img src="assets/a.png">',
@@ -42,20 +237,15 @@ describe("rename references", () => {
).toBe('url(x/b.png) srcset="x/b.png 2x"');
});
- it("keeps a root-relative lead, and reads JSON-escaped and Windows separators", () => {
- const text = [
- '<img src="/assets/a.png">',
- "url(/assets/a.png)",
- '{"src":"assets\\/a.png"}',
- '{"path":"assets\\\\a.png"}',
- ].join("\n");
+ it("keeps a root-relative lead and interprets JSON separators in their consumer grammar", () => {
+ const slash = '{"src":"assets\\/a.png"}';
+ const backslash = '{"path":"assets\\\\a.png"}';
+ expect(JSON.parse(slash).src).toBe("assets/a.png");
+ expect(JSON.parse(backslash).path).toBe("assets\\a.png");
+ const text = `<img src="/assets/a.png">\n<script>${slash}</script>\n<script>${backslash}</script>`;
+ const backslashAfter = process.platform === "win32" ? '{"path":"brand\\\\a.png"}' : backslash;
expect(rewrite(text, "assets", "brand", true)).toBe(
- [
- '<img src="/brand/a.png">',
- "url(/brand/a.png)",
- '{"src":"brand\\/a.png"}',
- '{"path":"brand\\\\a.png"}',
- ].join("\n"),
+ `<img src="/brand/a.png">\n<script>{"src":"brand\\/a.png"}</script>\n<script>${backslashAfter}</script>`,
);
});
@@ -95,11 +285,18 @@ describe("rename references", () => {
).toBe(escaped);
});
- it("compares escaped spellings by their text, not by offsets", () => {
- const text = String.raw`"dir\/other a.png&backup.png" "dir\\other a.png&backup.png"`;
- expect(referenceRewriter("a.png", "b.png", false, ["dir/other a.png&backup.png"])(text)).toBe(
- text,
- );
+ it("compares escaped script spellings against both distinct physical path identities", () => {
+ const slash = String.raw`"dir\/other a.png&backup.png"`;
+ const backslash = String.raw`"dir\\other a.png&backup.png"`;
+ expect(JSON.parse(slash)).toBe("dir/other a.png&backup.png");
+ expect(JSON.parse(backslash)).toBe("dir\\other a.png&backup.png");
+ const text = `${slash} ${backslash}`;
+ expect(
+ referenceRewriter("a.png", "b.png", false, [
+ "dir/other a.png&backup.png",
+ "dir\\other a.png&backup.png",
+ ])(text, "script"),
+ ).toBe(text);
});
it("stays fast with thousands of existing paths and references", () => {
@@ -155,6 +352,22 @@ describe("renaming a folder over the route", () => {
const dirs: string[] = [];
afterEach(() => dirs.splice(0).forEach((dir) => rmSync(dir, { recursive: true, force: true })));
+ it.each([42, null, "assets/\ud800.png", "assets/\udc00.png"])(
+ "rejects an unrepresentable rename path %j before moving the file",
+ async (newPath) => {
+ const project = mkdtempSync(join(tmpdir(), "hf-invalid-rename-"));
+ dirs.push(project);
+ mkdirSync(join(project, "assets"));
+ writeFileSync(join(project, "assets", "a.png"), "original asset");
+ const response = await fileRoutesFor(project).request("/projects/p/files/assets/a.png", {
+ method: "PATCH",
+ body: JSON.stringify({ newPath }),
+ });
+ expect(response.status).toBe(400);
+ expect(readFileSync(join(project, "assets", "a.png"), "utf8")).toBe("original asset");
+ },
+ );
+
it("rewrites references under it and leaves a sibling folder's, and prose, alone", async () => {
const project = mkdtempSync(join(tmpdir(), "hf-rename-refs-"));
dirs.push(project);
@@ -167,11 +380,7 @@ describe("renaming a folder over the route", () => {
const html =
'<img src="assets/a.png"><img src="assets-backup/a.png"><img src="other assets/a.png"><a href="empty assets/">x</a><p>The assets folder</p>';
writeFileSync(join(project, "index.html"), html);
- const adapter = {
- resolveProject: async (id: string) => ({ id, dir: project }),
- } as unknown as StudioApiAdapter;
- const app = new Hono();
- registerFileRoutes(app, adapter);
+ const app = fileRoutesFor(project);
const response = await app.request("/projects/p/files/assets", {
method: "PATCH",
@@ -184,6 +393,224 @@ describe("renaming a folder over the route", () => {
);
});
+ it("keeps raw quoted file paths usable in JSON and script strings", async () => {
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-quote-"));
+ dirs.push(project);
+ mkdirSync(join(project, "assets"));
+ writeFileSync(join(project, "assets", "a.png"), "original asset");
+ writeFileSync(join(project, "config.json"), '{"path":"assets/a.png"}');
+ writeFileSync(join(project, "script.js"), '"assets/a.png"');
+ writeFileSync(join(project, "index.html"), '<script>"assets/a.png"</script>');
+ const app = fileRoutesFor(project);
+ const response = await app.request("/projects/p/files/assets/a.png", {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: 'assets/say "hi".png' }),
+ });
+ expect(response.status).toBe(200);
+ const config = JSON.parse(readFileSync(join(project, "config.json"), "utf8")) as {
+ path: string;
+ };
+ expect(readFileSync(join(project, config.path), "utf8")).toBe("original asset");
+ expect(JSON.parse(readFileSync(join(project, "script.js"), "utf8"))).toBe(
+ 'assets/say "hi".png',
+ );
+ expect(readFileSync(join(project, "index.html"), "utf8")).toBe(
+ String.raw`<script>"assets/say \"hi\".png"</script>`,
+ );
+ const renamedAgain = await app.request(
+ `/projects/p/files/${encodeURIComponent('assets/say "hi".png')}`,
+ {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: "assets/final.png" }),
+ },
+ );
+ expect(renamedAgain.status).toBe(200);
+ const finalConfig = JSON.parse(readFileSync(join(project, "config.json"), "utf8")) as {
+ path: string;
+ };
+ expect(readFileSync(join(project, finalConfig.path), "utf8")).toBe("original asset");
+ expect(JSON.parse(readFileSync(join(project, "script.js"), "utf8"))).toBe("assets/final.png");
+ });
+
+ it("does not retarget a distinct literal-backslash file when renaming a slash path", async () => {
+ const distinctName = process.platform === "win32" ? "assets-a.png" : "assets\\a.png";
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-separator-identity-"));
+ dirs.push(project);
+ mkdirSync(join(project, "assets"));
+ writeFileSync(join(project, "assets/a.png"), "asset A");
+ writeFileSync(join(project, distinctName), "asset B");
+ const config = JSON.stringify({ path: distinctName });
+ writeFileSync(join(project, "config.json"), config);
+ writeFileSync(join(project, "script.js"), JSON.stringify(distinctName));
+ writeFileSync(join(project, "index.html"), '<img src="assets/a.png">');
+ const response = await fileRoutesFor(project).request("/projects/p/files/assets/a.png", {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: "assets/b.png" }),
+ });
+ expect(response.status).toBe(200);
+ expect(readFileSync(join(project, "config.json"), "utf8")).toBe(config);
+ expect(readFileSync(join(project, "script.js"), "utf8")).toBe(JSON.stringify(distinctName));
+ expect(readFileSync(join(project, "index.html"), "utf8")).toBe('<img src="assets/b.png">');
+ expect(readFileSync(join(project, "assets/b.png"), "utf8")).toBe("asset A");
+ expect(readFileSync(join(project, distinctName), "utf8")).toBe("asset B");
+ const folderResponse = await fileRoutesFor(project).request("/projects/p/files/assets", {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: "brand" }),
+ });
+ expect(folderResponse.status).toBe(200);
+ expect(readFileSync(join(project, "config.json"), "utf8")).toBe(config);
+ expect(readFileSync(join(project, "index.html"), "utf8")).toBe('<img src="brand/b.png">');
+ expect(readFileSync(join(project, "brand/b.png"), "utf8")).toBe("asset A");
+ expect(readFileSync(join(project, distinctName), "utf8")).toBe("asset B");
+ });
+
+ it("renames a literal backslash filename twice through the route", async () => {
+ const intermediate = process.platform === "win32" ? "x/y.png" : "x\\y.png";
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-backslash-"));
+ dirs.push(project);
+ writeFileSync(join(project, "old.png"), "image witness");
+ writeFileSync(join(project, "index.html"), '<img src="old.png">');
+ writeFileSync(join(project, "config.json"), '{"path":"old.png"}');
+ const app = fileRoutesFor(project);
+ for (const [from, to] of [
+ ["old.png", intermediate],
+ [intermediate, "final.png"],
+ ]) {
+ const response = await app.request(`/projects/p/files/${encodeURIComponent(from!)}`, {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: to }),
+ });
+ expect(response.status).toBe(200);
+ }
+ expect(readFileSync(join(project, "index.html"), "utf8")).toBe('<img src="final.png">');
+ expect(JSON.parse(readFileSync(join(project, "config.json"), "utf8")).path).toBe("final.png");
+ expect(readFileSync(join(project, "final.png"), "utf8")).toBe("image witness");
+ });
+
+ it("renames references using the full native HTML entity vocabulary through the route", async () => {
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-native-entities-"));
+ dirs.push(project);
+ writeFileSync(join(project, "a&≂̸b.png"), "image witness");
+ writeFileSync(
+ join(project, "index.html"),
+ '<img src="a&#38;&NotEqualTilde;b.png"><img src="a&#x26;&NotEqualTilde;b.png"><div style="background:url(a&#38;&NotEqualTilde;b.png)"></div>',
+ );
+ const response = await fileRoutesFor(project).request(
+ `/projects/p/files/${encodeURIComponent("a&≂̸b.png")}`,
+ {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: "new.png" }),
+ },
+ );
+ expect(response.status).toBe(200);
+ expect(readFileSync(join(project, "index.html"), "utf8")).toBe(
+ '<img src="new.png"><img src="new.png"><div style="background:url(new.png)"></div>',
+ );
+ expect(readFileSync(join(project, "new.png"), "utf8")).toBe("image witness");
+ });
+
+ it("keeps a renamed scheme-looking physical filename reachable by the bundler", async () => {
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-local-scheme-"));
+ dirs.push(project);
+ writeFileSync(join(project, "a.png"), "image witness");
+ writeFileSync(
+ join(project, "index.html"),
+ '<html><body><main data-composition-id="root" data-width="320" data-height="180"><img src="a.png"></main></body></html>',
+ );
+ const response = await fileRoutesFor(project).request("/projects/p/files/a.png", {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: "data:logo.png" }),
+ });
+ expect(response.status).toBe(200);
+ expect(readFileSync(join(project, "data:logo.png"), "utf8")).toBe("image witness");
+ expect(readFileSync(join(project, "index.html"), "utf8")).toContain('src="data%3Alogo.png"');
+ const { bundleToSingleHtml } = await import("@hyperframes/core/compiler");
+ expect(await bundleToSingleHtml(project)).toContain(
+ "data:image/png;base64,aW1hZ2Ugd2l0bmVzcw==",
+ );
+ });
+
+ it("renames URL fields to literal URL punctuation without changing raw file values", async () => {
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-url-fields-"));
+ dirs.push(project);
+ mkdirSync(join(project, "assets"));
+ writeFileSync(join(project, "assets", "a.png"), "image witness");
+ writeFileSync(join(project, "config.json"), '{"path":"assets/a.png"}');
+ writeFileSync(
+ join(project, "index.html"),
+ '<html><head><style>.image{background:url(assets/a.png)}</style></head><body><main data-composition-id="root" data-width="320" data-height="180"><img src="assets/a.png"><a href="assets/a.png">image</a></main></body></html>',
+ );
+ const response = await fileRoutesFor(project).request("/projects/p/files/assets/a.png", {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: "assets/c(?%20#).png" }),
+ });
+ expect(response.status).toBe(200);
+ expect(JSON.parse(readFileSync(join(project, "config.json"), "utf8")).path).toBe(
+ "assets/c(?%20#).png",
+ );
+ const html = readFileSync(join(project, "index.html"), "utf8");
+ expect(html).toContain('src="assets/c(%3F%2520%23).png"');
+ expect(html).toContain('href="assets/c(%3F%2520%23).png"');
+ expect(html).toContain("url(assets/c%28%3F%2520%23%29.png)");
+ const { bundleToSingleHtml } = await import("@hyperframes/core/compiler");
+ const bundled = await bundleToSingleHtml(project);
+ expect(bundled).toContain("data:image/png;base64,aW1hZ2Ugd2l0bmVzcw==");
+ expect(bundled).not.toContain("url(assets/c%28%3F%2520%23%29.png)");
+ });
+
+ it.each(["my intro.html", 'my "intro".html', "my ?%20#intro.html", "my 🙂 intro.html"])(
+ "keeps a renamed sub-composition %s reachable by the bundler",
+ async (newName) => {
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-composition-"));
+ dirs.push(project);
+ mkdirSync(join(project, "scenes"));
+ writeFileSync(
+ join(project, "index.html"),
+ '<!doctype html><html><body><main data-composition-id="root" data-width="320" data-height="180"><div data-composition-id="intro" data-composition-src="scenes/intro.html" data-start="0" data-duration="2"></div></main></body></html>',
+ );
+ writeFileSync(
+ join(project, "scenes", "intro.html"),
+ '<template><div data-composition-id="intro" data-width="320" data-height="180"><p>Renamed scene still present</p></div></template>',
+ );
+ const app = fileRoutesFor(project);
+ const response = await app.request("/projects/p/files/scenes/intro.html", {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: `scenes/${newName}` }),
+ });
+ expect(response.status).toBe(200);
+ const { bundleToSingleHtml } = await import("@hyperframes/core/compiler");
+ const bundled = await bundleToSingleHtml(project);
+ expect(bundled).toContain("Renamed scene still present");
+ },
+ );
+
+ it.each([
+ ["my clip.mp4", "next clip.mp4", "my%20clip.mp4", "next%20clip.mp4"],
+ ["a&b.png", "c&d.png", "a&amp;b.png", "c&amp;d.png"],
+ ])(
+ "renames %s references using their authored spelling",
+ async (oldName, newName, before, after) => {
+ const project = mkdtempSync(join(tmpdir(), "hf-rename-spelling-"));
+ dirs.push(project);
+ mkdirSync(join(project, "assets"));
+ writeFileSync(join(project, "assets", oldName), "x");
+ writeFileSync(join(project, "index.html"), `<img src="assets/${before}">`);
+ const app = fileRoutesFor(project);
+
+ const response = await app.request(
+ `/projects/p/files/assets/${encodeURIComponent(oldName)}`,
+ {
+ method: "PATCH",
+ body: JSON.stringify({ newPath: `assets/${newName}` }),
+ },
+ );
+
+ expect(response.status).toBe(200);
+ expect(readFileSync(join(project, "index.html"), "utf8")).toBe(`<img src="assets/${after}">`);
+ expect(readFileSync(join(project, "assets", newName), "utf8")).toBe("x");
+ },
+ );
+
it("leaves a path reached through a linked folder alone", async () => {
const project = mkdtempSync(join(tmpdir(), "hf-rename-link-"));
dirs.push(project);
@@ -196,11 +623,7 @@ describe("renaming a folder over the route", () => {
join(project, "index.html"),
'<img src="assets/a.png"><img src="other assets/a.png">',
);
- const adapter = {
- resolveProject: async (id: string) => ({ id, dir: project }),
- } as unknown as StudioApiAdapter;
- const app = new Hono();
- registerFileRoutes(app, adapter);
+ const app = fileRoutesFor(project);
await app.request("/projects/p/files/assets/a.png", {
method: "PATCH",
@@ -220,11 +643,7 @@ describe("renaming a folder over the route", () => {
writeFileSync(join(project, "shared", "logo.png"), "x");
symlinkSync("../shared/logo.png", join(project, "assets", "logo.png"), "file");
writeFileSync(join(project, "index.html"), '<img src="assets/logo.png">');
- const adapter = {
- resolveProject: async (id: string) => ({ id, dir: project }),
- } as unknown as StudioApiAdapter;
- const app = new Hono();
- registerFileRoutes(app, adapter);
+ const app = fileRoutesFor(project);
const response = await app.request("/projects/p/files/assets/logo.png", {
method: "PATCH",
diff --git a/packages/studio-server/src/routes/files.ts b/packages/studio-server/src/routes/files.ts
index 7f479bc842..6e972b3c44 100644
--- a/packages/studio-server/src/routes/files.ts
+++ b/packages/studio-server/src/routes/files.ts
@@ -3,6 +3,12 @@
// parallel — two writers, same switch-case interface. Structural duplication
// is load-bearing (both paths must remain testable in isolation).
import type { Hono } from "hono";
+import { encodeUrlPath, decodedUrlPath, decodeCssEscapes } from "@hyperframes/parsers/asset-paths";
+import {
+ scanHtmlOpeningTags,
+ decodeAuthoredAttribute,
+ HTML_ATTRIBUTE_ENTITIES,
+} from "@hyperframes/parsers";
import { bodyLimit } from "hono/body-limit";
import {
closeSync,
@@ -20,7 +26,7 @@ import {
realpathSync,
type Dirent,
} from "node:fs";
-import { resolve, dirname, join } from "node:path";
+import { resolve, dirname, join, sep } from "node:path";
import type { StudioApiAdapter } from "../types.js";
import { isAudioFile } from "../helpers/mime.js";
import { lengthIsTimeline } from "../helpers/compositionInputs.js";
@@ -107,7 +113,7 @@ import {
insertCompositionIntoSource,
} from "../helpers/compositionInsertion.js";
import { resolveGsapWriter } from "./gsapMutationCapabilities.js";
-import { requestSubPath } from "../helpers/requestSubPath.js";
+import { decodeWellFormedEscapes, requestSubPath } from "../helpers/requestSubPath.js";
import {
hasCompositionOutsideTemplates,
insertBeforeCloseTag,
@@ -701,8 +707,112 @@ const SEPARATOR = String.raw`\\{0,2}[\\/]`;
const REFERENCE_START = String.raw`(?<![\w./\\+-])`;
const FILE_END = String.raw`(?![\w-]|\.\w)`;
+const ENTITY_SPELLINGS = HTML_ATTRIBUTE_ENTITIES;
+
+function referenceSpellings(char: string): string {
+ const spellings = [escapeRegExp(char)];
+ const encoded = encodeUrlPath(char);
+ if (encoded !== char)
+ spellings.push(encoded.replace(/[A-F]/g, (hex) => `[${hex}${hex.toLowerCase()}]`));
+ if (ENTITY_SPELLINGS[char]) spellings.push(...ENTITY_SPELLINGS[char]);
+ if (["'", '"', "`", "$", " ", "(", ")"].includes(char)) {
+ spellings.push(escapeRegExp(`&#92;${char}`), escapeRegExp(`&#92;${escapeHtmlReference(char)}`));
+ }
+ const scriptSpelling = escapeQuotedReference(char, '"');
+ if (scriptSpelling !== char) spellings.push(escapeRegExp(scriptSpelling));
+ if (["'", "`", "$", " ", "(", ")"].includes(char)) spellings.push(escapeRegExp(`\\${char}`));
+ if (/[\t\n\f\r =<>`]/.test(char)) spellings.push(`&#${char.codePointAt(0)!};`);
+ if (char.charCodeAt(0) < 0x20 || char.charCodeAt(0) === 0x7f) {
+ spellings.push(escapeRegExp(`\\${char.charCodeAt(0).toString(16)} `));
+ }
+ return spellings.length === 1 ? spellings[0]! : `(?:${spellings.join("|")})`;
+}
+
+type ReferenceSyntax = "html" | "script" | "css" | "plain";
+interface ReferenceContext {
+ syntax: ReferenceSyntax;
+ url: boolean;
+ htmlAttribute: boolean;
+ encodeAll: boolean;
+ quote: string;
+}
+
+function escapeHtmlReference(path: string): string {
+ return path.replace(/[&"']/g, (char) => ENTITY_SPELLINGS[char]![0]!);
+}
+
+function escapeQuotedReference(path: string, quote: string): string {
+ const escaped = JSON.stringify(path).slice(1, -1);
+ return quote === '"'
+ ? escaped
+ : escaped.replace(new RegExp(escapeRegExp(quote), "g"), `\\${quote}`);
+}
+
+function escapeRawUrlPath(path: string): string {
+ const encodeSpaces = path.startsWith(" ") || path.endsWith(" ");
+ return path.replace(/[\s\S]/g, (char) => {
+ if (char.charCodeAt(0) < 0x20 || ":?%#\\".includes(char) || (encodeSpaces && char === " ")) {
+ return encodeUrlPath(char);
+ }
+ return char;
+ });
+}
+
+function spellLike(
+ reference: string,
+ path: string,
+ oldPath: string,
+ context: ReferenceContext,
+): string {
+ const { syntax, quote, url, encodeAll } = context;
+ if (url && (encodeAll || (reference !== oldPath && /%[0-9A-Fa-f]{2}/.test(reference))))
+ return encodeUrlPath(path);
+ switch (syntax) {
+ case "html": {
+ const escaped = escapeHtmlReference(url ? escapeRawUrlPath(path) : path);
+ return ["'", '"'].includes(quote)
+ ? escaped
+ : escaped.replace(/[\t\n\f\r =<>`]/g, (char) => `&#${char.codePointAt(0)!};`);
+ }
+ case "script": {
+ if (!["'", '"', "`"].includes(quote)) return path;
+ const escaped = escapeQuotedReference(path, quote);
+ return quote === "`" ? escaped.replace(/\$\{/g, "\\${") : escaped;
+ }
+ case "css":
+ return url ? encodeUrlPath(path) : path;
+ case "plain":
+ return path;
+ }
+}
+
+function referenceSyntax(file: string): ReferenceSyntax {
+ if (/\.html$/i.test(file)) return "html";
+ if (/\.css$/i.test(file)) return "css";
+ if (/\.(?:[cm]?js|jsx|tsx?|json)$/i.test(file)) return "script";
+ return "plain";
+}
+
+function decodeQuotedReference(text: string, syntax: ReferenceSyntax): string {
+ if (syntax === "css") return decodeCssEscapes(text);
+ if (syntax === "script") {
+ const controls: Record<string, string> = { b: "\b", f: "\f", n: "\n", r: "\r", t: "\t" };
+ return text.replace(
+ /\\(?:u([0-9a-fA-F]{4})|([bfnrt"'`$\\/]))/g,
+ (_escaped, hex: string | undefined, char: string | undefined) => {
+ if (hex !== undefined) return String.fromCharCode(Number.parseInt(hex, 16));
+ return controls[char!] ?? char!;
+ },
+ );
+ }
+ return text;
+}
+
function referencePattern(oldPath: string, isDirectory: boolean): RegExp {
- const name = oldPath.split("/").map(escapeRegExp).join(SEPARATOR);
+ const name = oldPath
+ .split("/")
+ .map((segment) => Array.from(segment, referenceSpellings).join(""))
+ .join(SEPARATOR);
const end = isDirectory ? `(?=${SEPARATOR})` : FILE_END;
return new RegExp(
String.raw`${REFERENCE_START}(?<lead>(?:\.{1,2}${SEPARATOR}|${SEPARATOR}){0,4})${name}${end}`,
@@ -710,6 +820,30 @@ function referencePattern(oldPath: string, isDirectory: boolean): RegExp {
);
}
+function rewriteEntityAttributes(text: string, rewrite: (text: string) => string): string {
+ const parts: string[] = [];
+ let cursor = 0;
+ for (const tag of scanHtmlOpeningTags(text)) {
+ for (const attr of tag.attributes) {
+ if (attr.kind !== "value") continue;
+ const decoded = decodeAuthoredAttribute(attr.value);
+ if (decoded === attr.value) continue;
+ let value = escapeHtmlReference(decoded);
+ if (!attr.quote)
+ value = value.replace(/[\t\n\f\r =<>`]/g, (char) => `&#${char.codePointAt(0)!};`);
+ const source = `<x ${attr.name}=${attr.quote}${value}${attr.quote}>`;
+ const updated = rewrite(source);
+ if (updated === source) continue;
+ const replacement = scanHtmlOpeningTags(updated)[0]!.attributes[0]!;
+ if (replacement.kind !== "value") throw new Error("Reference rewrite removed its attribute");
+ parts.push(text.slice(cursor, attr.valueStart), replacement.value);
+ cursor = attr.valueEnd;
+ }
+ }
+ parts.push(text.slice(cursor));
+ return parts.join("");
+}
+
// A match inside the longer path of a file or folder that exists (`a.png&b.png`, `other assets/`) is that path's.
// Existing paths are indexed by the text after the old path, then the text before it, so a match costs a few lookups.
export function referenceRewriter(
@@ -717,7 +851,8 @@ export function referenceRewriter(
newPath: string,
isDirectory: boolean,
existing: readonly string[] = [],
-): (text: string) => string {
+): (text: string, syntax?: ReferenceSyntax) => string {
+ const oldIdentity = sep === "\\" ? oldPath.replace(/\\/g, "/") : oldPath;
const pattern = referencePattern(oldPath, isDirectory);
const around = new Map<string, Map<number, Set<string>>>();
const afterLengths = new Set<number>();
@@ -731,17 +866,132 @@ export function referenceRewriter(
before,
);
afterLengths.add(after.length);
- window = Math.max(window, path.length * 3);
+ window = Math.max(window, path.length * 9);
}
}
- const normalized = (text: string) => text.replace(/\\{0,2}[\\/]/g, "/");
- return (text) =>
- text.replace(pattern, (...args) => {
+ const normalized = (text: string, context: ReferenceContext, full = false) => {
+ let value = text;
+ if (context.syntax === "html" || context.htmlAttribute) {
+ value = decodeAuthoredAttribute(value);
+ }
+ value = decodeQuotedReference(value, context.syntax);
+ if (context.url) {
+ value = value.replace(/\\/g, "/");
+ value = full ? decodedUrlPath(value) : decodeWellFormedEscapes(value);
+ }
+ return sep === "\\" ? value.replace(/\\/g, "/") : value;
+ };
+ return function rewriteText(text, syntax = "html", normalizeEntities = true) {
+ if (syntax === "html" && normalizeEntities) {
+ text = rewriteEntityAttributes(text, (source) => rewriteText(source, "html", false));
+ }
+ pattern.lastIndex = 0;
+ if (!pattern.test(text)) return text;
+ pattern.lastIndex = 0;
+ const tags = syntax === "html" ? scanHtmlOpeningTags(text) : [];
+ const inlineRegions = tags
+ .filter(
+ (tag) => tag.closed && ["script", "style"].includes(tag.name) && tag.rawTextEnd !== null,
+ )
+ .map((tag) => ({
+ start: tag.end,
+ end: tag.rawTextEnd!,
+ syntax: tag.name === "style" ? ("css" as const) : ("script" as const),
+ }));
+ const attributes = tags.flatMap((tag) =>
+ tag.attributes.flatMap((attr) => (attr.kind === "value" ? [attr] : [])),
+ );
+ const cssContexts =
+ syntax === "css"
+ ? [{ start: 0, end: text.length, htmlAttribute: false }]
+ : [
+ ...inlineRegions
+ .filter((region) => region.syntax === "css")
+ .map((region) => ({ ...region, htmlAttribute: false })),
+ ...attributes
+ .filter((attr) => attr.name === "style")
+ .map((attr) => ({ ...attr, htmlAttribute: true })),
+ ];
+ const cssFields = (pattern: RegExp) =>
+ cssContexts.flatMap((context) =>
+ [...text.slice(context.start, context.end).matchAll(pattern)].map((match) => {
+ const prefix = /^(?:url\(\s*|@import\s+)/i.exec(match[0])![0];
+ const rest = match[0].slice(prefix.length);
+ const delimiter = context.htmlAttribute
+ ? /^(?:["']|&quot;|&#39;|&apos;)/.exec(rest)?.[0]
+ : /^["']/.exec(rest)?.[0];
+ const start = context.start + match.index! + prefix.length + (delimiter?.length ?? 0);
+ return {
+ start,
+ end: context.start + match.index! + match[0].length,
+ syntax: "css" as const,
+ htmlAttribute: context.htmlAttribute,
+ encodeAll: true,
+ };
+ }),
+ );
+ const urlRegions = [
+ ...attributes
+ .filter((attr) => ["src", "href", "poster", "xlink:href"].includes(attr.name))
+ .map((attr) => ({
+ start: attr.valueStart + (/^[\t\n\f\r ]*/.exec(attr.value)?.[0].length ?? 0),
+ end: attr.valueEnd,
+ syntax: "html" as const,
+ htmlAttribute: true,
+ encodeAll: false,
+ })),
+ ...attributes
+ .filter((attr) => attr.name === "srcset")
+ .flatMap((attr) =>
+ [...attr.value.matchAll(/(^|,)[\t\n\f\r ]*([^,\t\n\f\r ]+)/g)].map((match) => ({
+ start: attr.valueStart + match.index! + match[0].length - match[2]!.length,
+ end: attr.valueStart + match.index! + match[0].length,
+ syntax: "html" as const,
+ htmlAttribute: true,
+ encodeAll: true,
+ })),
+ ),
+ ...cssFields(
+ /\burl\((?:\s*"(?:\\.|[^"\\])*"\s*|\s*'(?:\\.|[^'\\])*'\s*|(?:\\.|[^)\\])*)\)/gi,
+ ),
+ ...cssFields(/@import\s+(?:"[^"]*"|'[^']*')/gi),
+ ].sort((a, b) => a.start - b.start);
+ let urlIndex = 0;
+ let regionIndex = 0;
+ return text.replace(pattern, (...args) => {
const [match, offset] = [args[0] as string, args.at(-3) as number];
const { lead } = args.at(-1) as { lead: string };
+ while (inlineRegions[regionIndex] && inlineRegions[regionIndex]!.end <= offset) regionIndex++;
+ const region = inlineRegions[regionIndex];
+ const matchSyntax = region && offset >= region.start ? region.syntax : syntax;
+ while (urlRegions[urlIndex] && urlRegions[urlIndex]!.end <= offset) urlIndex++;
+ const urlRegion = urlRegions[urlIndex];
+ const url = matchSyntax !== "script" && !!urlRegion && offset >= urlRegion.start;
+ const context: ReferenceContext = {
+ syntax: url ? urlRegion!.syntax : matchSyntax,
+ url,
+ htmlAttribute: url && urlRegion!.htmlAttribute,
+ encodeAll: url && urlRegion!.encodeAll,
+ quote: text[offset - 1] ?? "",
+ };
+ if (url && offset !== urlRegion!.start) return match;
+ const reference = match.slice(lead.length);
+ if (normalized(reference, context, true) !== oldIdentity) return match;
+ if (
+ isDirectory &&
+ !normalized(
+ text.slice(offset + match.length, offset + match.length + 4),
+ context,
+ ).startsWith("/")
+ )
+ return match;
const at = offset + lead.length;
- const head = normalized(text.slice(Math.max(0, at - window), at));
- const tail = normalized(text.slice(offset + match.length, offset + match.length + window));
+ const head = normalized(text.slice(Math.max(0, at - window), at), context);
+ const tail = normalized(
+ text.slice(offset + match.length, offset + match.length + window),
+ context,
+ context.url,
+ );
const inLonger = [...afterLengths].some((after) => {
const befores = tail.length >= after && around.get(tail.slice(0, after));
return (
@@ -751,8 +1001,10 @@ export function referenceRewriter(
)
);
});
- return inLonger ? match : `${lead}${newPath}`;
+ if (inLonger) return match;
+ return `${lead}${spellLike(reference, newPath, oldPath, context)}`;
});
+ };
}
function occurrences(text: string, part: string): number[] {
@@ -807,7 +1059,7 @@ function updateReferences(
const content = readableText(file);
if (content === null) continue;
- const updated = rewrite(content);
+ const updated = rewrite(content, referenceSyntax(file));
if (updated !== content) {
replaceFileAtomically(file, updated, statSync(file).mode);
updatedCount++;
@@ -3404,12 +3656,19 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
const res = await resolveProjectFile(c, adapter, { mustExist: true });
if ("error" in res) return res.error;
- const body = (await c.req.json()) as { newPath?: string };
- if (!body.newPath || body.newPath.includes("\0")) {
+ const body: unknown = await c.req.json();
+ const newPath =
+ body !== null && typeof body === "object" && "newPath" in body ? body.newPath : undefined;
+ if (
+ typeof newPath !== "string" ||
+ !newPath ||
+ newPath.includes("\0") ||
+ /[\uD800-\uDFFF]/u.test(newPath)
+ ) {
return c.json({ error: "newPath required" }, 400);
}
- const newAbs = resolveWithinProject(res.project.dir, body.newPath);
+ const newAbs = resolveWithinProject(res.project.dir, newPath);
if (!newAbs) {
return c.json({ error: "forbidden" }, 403);
}
@@ -3422,9 +3681,9 @@ export function registerFileRoutes(api: Hono, adapter: StudioApiAdapter): void {
renameSync(res.absPath, newAbs);
// Update references to the old path across all project files
- const updatedFiles = updateReferences(res.project.dir, res.filePath, body.newPath, isDirectory);
+ const updatedFiles = updateReferences(res.project.dir, res.filePath, newPath, isDirectory);
- return c.json({ ok: true, path: body.newPath, updatedReferences: updatedFiles });
+ return c.json({ ok: true, path: newPath, updatedReferences: updatedFiles });
});
// ── Duplicate ──
diff --git a/packages/studio-server/src/routes/imageThumbnail.test.ts b/packages/studio-server/src/routes/imageThumbnail.test.ts
index e5ffca7111..71ba0f0e9f 100644
--- a/packages/studio-server/src/routes/imageThumbnail.test.ts
+++ b/packages/studio-server/src/routes/imageThumbnail.test.ts
@@ -115,7 +115,7 @@ describe("JPEG timeline thumbnail route", () => {
expect((await app.request(url)).status).toBe(422);
});
it("rejects malformed paths, corrupt images and oversized sources", async () => {
- expect((await app.request(url.replace("photo.jpg", "%FF.jpg"))).status).toBe(400);
+ expect((await app.request(url.replace("photo.jpg", "%FF.jpg"))).status).toBe(404);
await writeFile(join(dir, "photo.jpg"), "not an image");
expect((await app.request(url)).status).toBe(422);
const file = await open(join(dir, "photo.jpg"), "w");
diff --git a/packages/studio-server/src/routes/imageThumbnail.ts b/packages/studio-server/src/routes/imageThumbnail.ts
index efafab5eca..9de950b55f 100644
--- a/packages/studio-server/src/routes/imageThumbnail.ts
+++ b/packages/studio-server/src/routes/imageThumbnail.ts
@@ -55,12 +55,7 @@ async function generateThumbnail(path: string, signal: AbortSignal): Promise<Buf
async function resolveSource(adapter: StudioApiAdapter, id: string, url: string) {
const project = await adapter.resolveProject(id);
if (!project) throw new HTTPException(404);
- let relative: string;
- try {
- relative = requestSubPath(url, "projects/:id/image-thumbnail");
- } catch {
- throw new HTTPException(400);
- }
+ const relative = requestSubPath(url, "projects/:id/image-thumbnail");
if (!/\.jpe?g$/i.test(relative)) throw new HTTPException(415);
const path = pinWithinProject(project.dir, relative);
if (!path) throw new HTTPException(404);
diff --git a/packages/studio-server/src/routes/loudness.test.ts b/packages/studio-server/src/routes/loudness.test.ts
index c089c22975..465ce18503 100644
--- a/packages/studio-server/src/routes/loudness.test.ts
+++ b/packages/studio-server/src/routes/loudness.test.ts
@@ -67,6 +67,22 @@ describe("POST /projects/:id/loudness/normalize", () => {
expect(plan.limitedBy).toBe("true-peak");
});
+ it("measures a project file whose name holds %, # or ?, given raw or as a URL", async () => {
+ const measure = vi.fn<MeasureLoudness>(async () => ({ integratedLufs: -16, truePeakDbfs: -3 }));
+ const { post, projectDir } = setup(measure);
+ const name = "sale 50% off #1?.mp3";
+ writeFileSync(join(projectDir, name), "audio");
+
+ for (const src of [name, encodeURIComponent(name), "talk.mp4?v=2#t=1"]) {
+ expect((await post({ src })).status, src).toBe(200);
+ }
+ expect(measure.mock.calls.map(([file]) => file)).toEqual([
+ join(projectDir, name),
+ join(projectDir, name),
+ join(projectDir, "talk.mp4"),
+ ]);
+ });
+
it("refuses a source outside the project", async () => {
const { post } = setup(async () => ({ integratedLufs: -16, truePeakDbfs: -3 }));
expect((await post({ src: "../etc/passwd" })).status).toBe(400);
diff --git a/packages/studio-server/src/routes/media.test.ts b/packages/studio-server/src/routes/media.test.ts
index e867882c3f..5f549ab1a9 100644
--- a/packages/studio-server/src/routes/media.test.ts
+++ b/packages/studio-server/src/routes/media.test.ts
@@ -4,8 +4,8 @@ import { Hono } from "hono";
import {
mkdirSync,
mkdtempSync,
- readdirSync,
readFileSync,
+ readdirSync,
rmSync,
symlinkSync,
writeFileSync,
@@ -93,6 +93,19 @@ describe("registerMediaRoutes", () => {
expect(probe).toHaveBeenCalledWith(join(projectDir, "assets", "clip.mp4"));
});
+ it("finds a project file whose name holds %, # or ?", async () => {
+ const probe = vi.fn(async () => ({ kind: "video" as const }));
+ const { app, projectDir } = createAdapter(undefined, probe);
+ writeFileSync(join(projectDir, "assets", "sale 50% off #1?.mp4"), "");
+
+ const response = await app.request(
+ `http://localhost/projects/demo/media/metadata?path=${encodeURIComponent("assets/sale 50% off #1?.mp4")}`,
+ );
+
+ expect(response.status).toBe(200);
+ expect(probe).toHaveBeenCalledWith(join(projectDir, "assets", "sale 50% off #1?.mp4"));
+ });
+
it("rejects media metadata paths outside the project", async () => {
const { app } = createAdapter();
diff --git a/packages/studio-server/src/routes/media.ts b/packages/studio-server/src/routes/media.ts
index 58abc1cdec..ee6a463420 100644
--- a/packages/studio-server/src/routes/media.ts
+++ b/packages/studio-server/src/routes/media.ts
@@ -3,6 +3,7 @@ import { streamSSE } from "hono/streaming";
import { existsSync } from "node:fs";
import { basename, dirname, extname, join, posix } from "node:path";
import type { MediaProcessingJobState, StudioApiAdapter } from "../types.js";
+import { requestedProjectPath } from "../helpers/requestSubPath.js";
import { mkdirWithinProject, pinWithinProject, resolveWithinProject } from "../helpers/safePath.js";
import { probeMediaMetadata } from "../helpers/mediaMetadata.js";
@@ -45,13 +46,6 @@ function isImagePath(path: string): boolean {
return IMAGE_EXTENSIONS.has(extname(path).toLowerCase());
}
-function normalizeProjectAssetPath(path: string): string {
- return path
- .trim()
- .replace(/^[.]\//, "")
- .replace(/[?#].*$/, "");
-}
-
function containsNullByte(path: string): boolean {
return path.includes("\0");
}
@@ -149,7 +143,7 @@ export function registerMediaRoutes(
const project = await adapter.resolveProject(c.req.param("id"));
if (!project) return c.json({ error: "not found" }, 404);
- const assetPath = normalizeProjectAssetPath(c.req.query("path") ?? "");
+ const assetPath = requestedProjectPath(project.dir, c.req.query("path") ?? "");
if (!assetPath) return c.json({ error: "path required" }, 400);
if (containsNullByte(assetPath)) return c.json({ error: "forbidden" }, 403);
if (/^(?:https?:|data:|blob:)/i.test(assetPath)) {
@@ -177,7 +171,9 @@ export function registerMediaRoutes(
if (!project) return c.json({ error: "not found" }, 404);
const body = (await c.req.json().catch(() => ({}))) as BackgroundRemovalBody;
- const inputAssetPath = body.inputPath ? normalizeProjectAssetPath(body.inputPath) : "";
+ const inputAssetPath = body.inputPath
+ ? requestedProjectPath(project.dir, body.inputPath)
+ : "";
if (!inputAssetPath) return c.json({ error: "inputPath required" }, 400);
if (containsNullByte(inputAssetPath)) return c.json({ error: "forbidden" }, 403);
if (/^(?:https?:|data:|blob:)/i.test(inputAssetPath)) {
@@ -195,7 +191,7 @@ export function registerMediaRoutes(
}
const requestedOutput = body.outputPath
- ? posix.normalize(normalizeProjectAssetPath(body.outputPath))
+ ? posix.normalize(requestedProjectPath(project.dir, body.outputPath))
: "";
if (requestedOutput && containsNullByte(requestedOutput)) {
return c.json({ error: "forbidden" }, 403);
diff --git a/packages/studio-server/src/routes/projectIdEncoding.test.ts b/packages/studio-server/src/routes/projectIdEncoding.test.ts
index e60e38b1bc..64fdfac920 100644
--- a/packages/studio-server/src/routes/projectIdEncoding.test.ts
+++ b/packages/studio-server/src/routes/projectIdEncoding.test.ts
@@ -196,3 +196,35 @@ describe("a sub-path that decodes to a parent directory", () => {
expect(result.status).toBe(404);
});
});
+
+// A browser sends src="100%.png" with the % unescaped, since no two hex digits follow it.
+describe("a file name with a bare percent sign", () => {
+ async function servePreviewAsset(rawSubPath: string) {
+ const { dir, cleanup } = projectWithComposition();
+ writeFileSync(join(dir, "scenes", "100%.png"), "PNG BYTES");
+ writeFileSync(join(dir, "scenes", "50% off.png"), "SALE BYTES");
+ try {
+ const app = new Hono();
+ registerPreviewRoutes(app, createAdapter(dir));
+ const response = await app.request(`http://localhost/projects/film/preview/${rawSubPath}`);
+ return { status: response.status, text: await response.text() };
+ } finally {
+ cleanup();
+ }
+ }
+
+ it("serves the file when the % arrives unescaped", async () => {
+ expect(await servePreviewAsset("scenes/100%.png")).toEqual({ status: 200, text: "PNG BYTES" });
+ });
+
+ it("still decodes the escapes beside a bare %", async () => {
+ expect(await servePreviewAsset("scenes/50%%20off.png")).toEqual({
+ status: 200,
+ text: "SALE BYTES",
+ });
+ expect(await servePreviewAsset("scenes/100%25.png")).toEqual({
+ status: 200,
+ text: "PNG BYTES",
+ });
+ });
+});
diff --git a/packages/studio/src/components/MediaPreview.tsx b/packages/studio/src/components/MediaPreview.tsx
index 877a7751c6..edd4cac93e 100644
--- a/packages/studio/src/components/MediaPreview.tsx
+++ b/packages/studio/src/components/MediaPreview.tsx
@@ -1,6 +1,7 @@
import { buildProjectApiPath } from "../utils/projectRouting";
import { useState } from "react";
import { IMAGE_EXT, VIDEO_EXT, AUDIO_EXT } from "../utils/mediaTypes";
+import { encodeUrlPath } from "@hyperframes/parsers";
function MediaErrorPanel({ name, filePath }: { name: string; filePath: string }) {
return (
@@ -29,7 +30,7 @@ function MediaErrorPanel({ name, filePath }: { name: string; filePath: string })
}
export function MediaPreview({ projectId, filePath }: { projectId: string; filePath: string }) {
- const serveUrl = buildProjectApiPath(projectId, `/preview/${filePath}`);
+ const serveUrl = buildProjectApiPath(projectId, `/preview/${encodeUrlPath(filePath)}`);
const name = filePath.split("/").pop() ?? filePath;
// Keyed by path so switching to another file clears a previous failure.
const [failedPath, setFailedPath] = useState<string | null>(null);
diff --git a/packages/studio/src/components/editor/mediaAudioEdits.test.ts b/packages/studio/src/components/editor/mediaAudioEdits.test.ts
index bca8dc7449..edc5d740d7 100644
--- a/packages/studio/src/components/editor/mediaAudioEdits.test.ts
+++ b/packages/studio/src/components/editor/mediaAudioEdits.test.ts
@@ -239,6 +239,18 @@ describe("buildKeepSoundCutoutEdit", () => {
expect(doc.getElementById("clip")?.getAttribute("src")).toBe("assets/cut.webm");
});
+ it("preserves an apostrophe in the authored source of the linked audio", () => {
+ const authored = source.replace('src="assets/talk.mp4"', `src="assets/it's.mp4"`);
+ const edit = buildKeepSoundCutoutEdit({
+ video: liveVideo(),
+ videoId: "clip",
+ target: { id: "clip" },
+ cutoutSrc: "assets/cut.webm",
+ });
+ const doc = parse(applyEdit(authored, edit));
+ expect(doc.getElementById("clip-audio")?.getAttribute("src")).toBe("assets/it's.mp4");
+ });
+
it("takes the first <source> child when the video has no src attribute", () => {
const withSource = source.replace(
/<video id="clip"([^>]*?) src="assets\/talk.mp4"([^>]*)><\/video>/,
diff --git a/packages/studio/src/components/editor/mediaAudioEdits.ts b/packages/studio/src/components/editor/mediaAudioEdits.ts
index 7949291e90..e2c0b9908b 100644
--- a/packages/studio/src/components/editor/mediaAudioEdits.ts
+++ b/packages/studio/src/components/editor/mediaAudioEdits.ts
@@ -14,7 +14,7 @@ import {
findTagByTarget,
applyPatchByTarget,
readAttributeByTarget,
- unescapeHtmlAttribute,
+ readTagAttribute,
type PatchOperation,
type PatchTarget,
} from "../../utils/sourcePatcher";
@@ -147,10 +147,10 @@ export function formatAttrs(attrs: Array<[string, string]>): string {
export function readAuthoredSrc(source: string, target: PatchTarget): string {
const tag = findTagByTarget(source, target);
if (!tag) return "";
- const own = /\bsrc=(["'])([^"']*)\1/.exec(tag.tag);
- if (own?.[2]) return unescapeHtmlAttribute(own[2]);
- const firstSource = /^\s*<source\b[^>]*?\bsrc=(["'])([^"']*)\1/i.exec(source.slice(tag.end + 1));
- return firstSource?.[2] ? unescapeHtmlAttribute(firstSource[2]) : "";
+ const own = readTagAttribute(tag.tag, "src");
+ if (own) return own;
+ const firstSource = /^\s*<source\b[^>]*>/i.exec(source.slice(tag.end + 1));
+ return (firstSource && readTagAttribute(firstSource[0], "src")) || "";
}
export function insertBeforeTarget(source: string, target: PatchTarget, markup: string): string {
diff --git a/packages/studio/src/components/editor/propertyPanelFill.tsx b/packages/studio/src/components/editor/propertyPanelFill.tsx
index 248e01eb09..735411988e 100644
--- a/packages/studio/src/components/editor/propertyPanelFill.tsx
+++ b/packages/studio/src/components/editor/propertyPanelFill.tsx
@@ -20,6 +20,7 @@ import {
} from "./propertyPanelPrimitives";
import { ColorField } from "./propertyPanelColor";
import { useTrackDesignInput } from "../../contexts/DesignPanelInputContext";
+import { encodeUrlPath } from "@hyperframes/parsers";
/* ------------------------------------------------------------------ */
/* Asset path helpers */
@@ -181,7 +182,7 @@ export function ImageFillField({
{selectedAsset && (
<div className="overflow-hidden rounded-xl border border-neutral-800 bg-neutral-900/80">
<img
- src={buildProjectApiPath(projectId, `/preview/${selectedAsset}`)}
+ src={buildProjectApiPath(projectId, `/preview/${encodeUrlPath(selectedAsset)}`)}
alt={selectedAsset.split("/").pop() ?? selectedAsset}
className="h-28 w-full object-contain bg-neutral-950/80"
/>
diff --git a/packages/studio/src/components/editor/propertyPanelFlatMediaSection.tsx b/packages/studio/src/components/editor/propertyPanelFlatMediaSection.tsx
index 273728a2c8..7a33eed8e3 100644
--- a/packages/studio/src/components/editor/propertyPanelFlatMediaSection.tsx
+++ b/packages/studio/src/components/editor/propertyPanelFlatMediaSection.tsx
@@ -10,7 +10,6 @@ import {
formatTimingValue,
parseNumericValue,
readClipInPoint,
- stripQueryAndHash,
} from "./propertyPanelHelpers";
import { FlatSelectRow, FlatSlider } from "./propertyPanelFlatPrimitives";
import { FlatToggle } from "./propertyPanelFlatToggle";
@@ -37,6 +36,7 @@ import {
import { parseGainInput, parseRateInput, parseSecondsInput } from "./audioInspectorInput";
import type { CommitDomAttributeBatch } from "../../hooks/domEditCommitTypes";
import { commitCutout, commitHasAudioToggle, commitMutedToggle } from "./mediaAudioEdits";
+import { authoredSrcPath } from "../../player/components/thumbnailUtils";
// fallow-ignore-next-line complexity
export function FlatMediaSection({
@@ -126,12 +126,11 @@ export function FlatMediaSection({
const [createPlate, setCreatePlate] = useState(false);
const [quality, setQuality] = useState<"fast" | "balanced" | "best">("balanced");
- const absoluteSrc =
- projectDir && srcAttr && !srcAttr.startsWith("http") ? `${projectDir}/${srcAttr}` : srcAttr;
const projectSrc =
srcAttr && !/^(?:https?:|data:|blob:)/i.test(srcAttr)
- ? stripQueryAndHash(srcAttr.startsWith("./") ? srcAttr.slice(2) : srcAttr)
+ ? authoredSrcPath(srcAttr.startsWith("./") ? srcAttr.slice(2) : srcAttr)
: "";
+ const absoluteSrc = projectDir && projectSrc ? `${projectDir}/${projectSrc}` : srcAttr;
const canRemoveBackground = Boolean(onRemoveBackground && isVisualMedia && projectSrc);
useEffect(() => {
diff --git a/packages/studio/src/components/editor/propertyPanelMediaSection.tsx b/packages/studio/src/components/editor/propertyPanelMediaSection.tsx
index a8b57892d7..dc0a66250f 100644
--- a/packages/studio/src/components/editor/propertyPanelMediaSection.tsx
+++ b/packages/studio/src/components/editor/propertyPanelMediaSection.tsx
@@ -11,7 +11,6 @@ import {
parseNumericValue,
readClipInPoint,
RESPONSIVE_GRID,
- stripQueryAndHash,
} from "./propertyPanelHelpers";
import { Section, SegmentedControl, SelectField, SliderControl } from "./propertyPanelPrimitives";
import { useTrackDesignInput } from "../../contexts/DesignPanelInputContext";
@@ -26,6 +25,7 @@ import {
import type { CommitDomAttributeBatch } from "../../hooks/domEditCommitTypes";
import { useLinkedSpeedCommit, withLinkedPlaybackRate } from "./linkedSpeedEdits";
import { commitCutout, commitHasAudioToggle, commitMutedToggle } from "./mediaAudioEdits";
+import { authoredSrcPath } from "../../player/components/thumbnailUtils";
// fallow-ignore-next-line complexity
export function MediaSection({
@@ -98,12 +98,11 @@ export function MediaSection({
const [createPlate, setCreatePlate] = useState(false);
const [quality, setQuality] = useState<"fast" | "balanced" | "best">("balanced");
- const absoluteSrc =
- projectDir && srcAttr && !srcAttr.startsWith("http") ? `${projectDir}/${srcAttr}` : srcAttr;
const projectSrc =
srcAttr && !/^(?:https?:|data:|blob:)/i.test(srcAttr)
- ? stripQueryAndHash(srcAttr.startsWith("./") ? srcAttr.slice(2) : srcAttr)
+ ? authoredSrcPath(srcAttr.startsWith("./") ? srcAttr.slice(2) : srcAttr)
: "";
+ const absoluteSrc = projectDir && projectSrc ? `${projectDir}/${projectSrc}` : srcAttr;
const canRemoveBackground = Boolean(onRemoveBackground && isVisualMedia && projectSrc);
const panelTitle = isImage ? "Image" : isVideo ? "Video" : "Audio";
diff --git a/packages/studio/src/components/editor/useColorGradingController.ts b/packages/studio/src/components/editor/useColorGradingController.ts
index 162a10968e..132e0bffff 100644
--- a/packages/studio/src/components/editor/useColorGradingController.ts
+++ b/packages/studio/src/components/editor/useColorGradingController.ts
@@ -30,6 +30,7 @@ import {
type ColorGradingPreviewOptions,
} from "./useColorGradingPreviews";
import { studioApiFetch } from "../../utils/studioApiFetch";
+import { authoredSrcPath } from "../../player/components/thumbnailUtils";
export type { ColorGradingPresetPreviews, ColorGradingPreviewOptions };
@@ -97,7 +98,7 @@ function resolveProjectAssetPath(
const sourceDir = sourceFile.includes("/")
? sourceFile.slice(0, sourceFile.lastIndexOf("/"))
: "";
- const parts = `${sourceDir}/${trimmed}`.split("/");
+ const parts = `${sourceDir}/${authoredSrcPath(trimmed)}`.split("/");
const normalized: string[] = [];
for (const part of parts) {
if (!part || part === ".") continue;
diff --git a/packages/studio/src/hooks/useDomEditCommits.ts b/packages/studio/src/hooks/useDomEditCommits.ts
index 5051b62cde..8403ddc32b 100644
--- a/packages/studio/src/hooks/useDomEditCommits.ts
+++ b/packages/studio/src/hooks/useDomEditCommits.ts
@@ -23,6 +23,7 @@ import {
} from "./useDomEditCommitsHelpers";
import type { CutoverResult } from "../utils/sdkCutover";
import { serializeStudioFileMutations } from "../utils/studioFileMutationCoordinator";
+import { encodeUrlPath } from "@hyperframes/parsers";
export interface UseDomEditCommitsParams {
activeCompPath: string | null;
@@ -112,7 +113,7 @@ export function useDomEditCommits({
return {
family: fontFamilyFromAssetPath(asset),
path: asset,
- url: buildProjectApiPath(projectId, `/preview/${asset}`),
+ url: buildProjectApiPath(projectId, `/preview/${encodeUrlPath(asset)}`),
};
},
[fileTree, projectId, importedFontAssetsRef],
diff --git a/packages/studio/src/hooks/useFileManager.ts b/packages/studio/src/hooks/useFileManager.ts
index 23627b2277..2c867e07ee 100644
--- a/packages/studio/src/hooks/useFileManager.ts
+++ b/packages/studio/src/hooks/useFileManager.ts
@@ -9,6 +9,7 @@ import { useFileTree } from "./useFileTree";
import { useEditorSave } from "./useEditorSave";
import { useProjectFileWriter } from "./useProjectFileWriter";
import { studioApiFetch } from "../utils/studioApiFetch";
+import { encodeUrlPath } from "@hyperframes/parsers";
// ── Types ──
@@ -386,7 +387,7 @@ export function useFileManager({
.map((asset) => ({
family: fontFamilyFromAssetPath(asset),
path: asset,
- url: `/api/projects/${encodeURIComponent(pid)}/preview/${asset}`,
+ url: `/api/projects/${encodeURIComponent(pid)}/preview/${encodeUrlPath(asset)}`,
}));
importedFontAssetsRef.current = [
...imported,
diff --git a/packages/studio/src/hooks/useFileTree.ts b/packages/studio/src/hooks/useFileTree.ts
index ed3c6b6f68..bdcb64a120 100644
--- a/packages/studio/src/hooks/useFileTree.ts
+++ b/packages/studio/src/hooks/useFileTree.ts
@@ -4,6 +4,7 @@ import { FONT_EXT } from "../utils/mediaTypes";
import { fontFamilyFromAssetPath, type ImportedFontAsset } from "../components/editor/fontAssets";
import { captureProjectProvenance } from "../components/feedback/projectProvenance";
import { studioApiFetch } from "../utils/studioApiFetch";
+import { encodeUrlPath } from "@hyperframes/parsers";
interface UseFileTreeOptions {
projectId: string | null;
@@ -119,7 +120,7 @@ export function useFileTree({ projectId, projectIdRef }: UseFileTreeOptions) {
.map((asset) => ({
family: fontFamilyFromAssetPath(asset),
path: asset,
- url: projectId ? buildProjectApiPath(projectId, `/preview/${asset}`) : "",
+ url: projectId ? buildProjectApiPath(projectId, `/preview/${encodeUrlPath(asset)}`) : "",
})),
[assets, projectId],
);
diff --git a/packages/studio/src/hooks/useRenderClipContent.ts b/packages/studio/src/hooks/useRenderClipContent.ts
index 5dc25c92d3..4d9016a516 100644
--- a/packages/studio/src/hooks/useRenderClipContent.ts
+++ b/packages/studio/src/hooks/useRenderClipContent.ts
@@ -17,7 +17,11 @@ import { AudibleVideoClipContent } from "../player/components/AudibleVideoClipCo
import { ClipPeakMarks } from "../player/components/ClipPeakMarks";
import { clipPeaksUrl, clipSourceWindow } from "../player/components/clipPeakMap";
import { clipHasSound } from "../player/components/clipMenuNormalize";
-import { encodePreviewPath, resolveMediaPreviewUrl } from "../player/components/thumbnailUtils";
+import {
+ authoredSrcPath,
+ encodePreviewPath,
+ resolveMediaPreviewUrl,
+} from "../player/components/thumbnailUtils";
import { usePlayerStore } from "../player/store/playerStore";
import { thumbnailRevisionOf } from "../player/store/thumbnailSlice";
import { effectiveThumbnailMode } from "../player/lib/thumbnailPolicy";
@@ -67,7 +71,11 @@ function renderAudioClip(
elements: readonly TimelineElement[],
labelInset?: number,
): ReactNode {
- const audioUrl = resolveMediaPreviewUrl(el.src ?? "", pid, window.location.origin);
+ const audioUrl = resolveMediaPreviewUrl(
+ authoredSrcPath(el.src ?? ""),
+ pid,
+ window.location.origin,
+ );
const srcRelative = resolvePreviewRelative(audioUrl, pid, window.location.origin);
// Encode each path segment (spaces, parens, U+202F, unicode) so the URL matches
// what the assets panel loads — a raw segment 404s. resolvePreviewRelative
@@ -213,7 +221,11 @@ export function useRenderClipContent({
const htmlPreviewEligible = el.duration > 0 && effectiveTimelineDuration > 0;
if ((el.tag === "video" || el.tag === "img") && el.src) {
- const mediaSrc = resolveMediaPreviewUrl(el.src, pid, window.location.origin);
+ const mediaSrc = resolveMediaPreviewUrl(
+ authoredSrcPath(el.src),
+ pid,
+ window.location.origin,
+ );
// Still images can't be decoded by VideoThumbnail's <video> extractor
// (the error event fires and the shimmer never resolves) — render the
// image itself as the strip.
diff --git a/packages/studio/src/player/components/clipMenuNormalize.ts b/packages/studio/src/player/components/clipMenuNormalize.ts
index 83ffc02ae8..b4d0e46e14 100644
--- a/packages/studio/src/player/components/clipMenuNormalize.ts
+++ b/packages/studio/src/player/components/clipMenuNormalize.ts
@@ -1,6 +1,6 @@
import { buildProjectApiPath } from "../../utils/projectRouting";
import { resolvePreviewRelative } from "../../utils/previewRelativePath";
-import { resolveMediaPreviewUrl } from "./thumbnailUtils";
+import { authoredSrcPath, resolveMediaPreviewUrl } from "./thumbnailUtils";
import type { TimelineElement } from "../store/timelineElement";
import { parseAutomation, VOLUME_TARGET } from "@hyperframes/core/audio-automation";
import type { TimelineEditOutcome } from "../../hooks/timelineEditPermission";
@@ -79,7 +79,7 @@ export async function requestNormalizePlan(
): Promise<NormalizePlan> {
const origin = window.location.origin;
const relative = resolvePreviewRelative(
- resolveMediaPreviewUrl(el.src ?? "", projectId, origin),
+ resolveMediaPreviewUrl(authoredSrcPath(el.src ?? ""), projectId, origin),
projectId,
origin,
);
diff --git a/packages/studio/src/player/components/clipPeakMap.ts b/packages/studio/src/player/components/clipPeakMap.ts
index 01ee899fa3..5cfc163a4c 100644
--- a/packages/studio/src/player/components/clipPeakMap.ts
+++ b/packages/studio/src/player/components/clipPeakMap.ts
@@ -3,7 +3,7 @@ import { studioApiFetch } from "../../utils/studioApiFetch";
import { resolvePreviewRelative } from "../../utils/previewRelativePath";
import type { TimelineElement } from "../store/timelineElement";
import { clipSourcePeak, isPeakMap, type ClipSourceWindow, type PeakMap } from "./clipPeakRuns";
-import { encodePreviewPath, resolveMediaPreviewUrl } from "./thumbnailUtils";
+import { authoredSrcPath, encodePreviewPath, resolveMediaPreviewUrl } from "./thumbnailUtils";
const peakMapRequests = new Map<string, Promise<PeakMap | null>>();
@@ -23,7 +23,7 @@ export function loadPeakMap(url: string): Promise<PeakMap | null> {
export function clipPeaksUrl(src: string | undefined, projectId: string): string | undefined {
const origin = window.location.origin;
const relative = resolvePreviewRelative(
- resolveMediaPreviewUrl(src ?? "", projectId, origin),
+ resolveMediaPreviewUrl(authoredSrcPath(src ?? ""), projectId, origin),
projectId,
origin,
);
diff --git a/packages/studio/src/player/components/thumbnailUtils.test.ts b/packages/studio/src/player/components/thumbnailUtils.test.ts
index 3c67e344af..883454b2f7 100644
--- a/packages/studio/src/player/components/thumbnailUtils.test.ts
+++ b/packages/studio/src/player/components/thumbnailUtils.test.ts
@@ -1,5 +1,7 @@
import { describe, expect, it } from "vitest";
+import { buildTimelineAssetInsertHtml } from "../../utils/timelineAssetDrop";
import {
+ authoredSrcPath,
computeThumbnailStrip,
encodePreviewPath,
resolveMediaPreviewUrl,
@@ -187,3 +189,34 @@ describe("encodePreviewPath", () => {
expect(encodePreviewPath("assets/music.mp3")).toBe("assets/music.mp3");
});
});
+
+describe("authoredSrcPath", () => {
+ const droppedSrc = (assetPath: string) =>
+ /src="([^"]*)"/.exec(
+ buildTimelineAssetInsertHtml({
+ id: "clip",
+ hfId: "hf-clip",
+ assetPath,
+ kind: "video",
+ start: 0,
+ duration: 2,
+ track: 1,
+ zIndex: 1,
+ }),
+ )![1]!;
+
+ it.each([
+ ["assets/My clip.mp4", "/api/projects/p/preview/assets/My%20clip.mp4"],
+ ["assets/café.mp4", "/api/projects/p/preview/assets/caf%C3%A9.mp4"],
+ ["assets/50% off #1?.mp4", "/api/projects/p/preview/assets/50%25%20off%20%231%3F.mp4"],
+ ])("previews a dropped %j at its own file", (assetPath, url) => {
+ expect(resolveMediaPreviewUrl(authoredSrcPath(droppedSrc(assetPath)), "p")).toBe(url);
+ });
+
+ it("drops the query and fragment, and leaves URLs with a scheme alone", () => {
+ expect(authoredSrcPath("assets/a%20b.mp4?v=2#t=1")).toBe("assets/a b.mp4");
+ expect(authoredSrcPath("assets/100%.png")).toBe("assets/100%.png");
+ expect(authoredSrcPath("https://cdn.example/a%20b.mp4")).toBe("https://cdn.example/a%20b.mp4");
+ expect(authoredSrcPath("//cdn.example/a%20b.mp4")).toBe("//cdn.example/a%20b.mp4");
+ });
+});
diff --git a/packages/studio/src/player/components/thumbnailUtils.ts b/packages/studio/src/player/components/thumbnailUtils.ts
index 65290e44fe..bde62d4160 100644
--- a/packages/studio/src/player/components/thumbnailUtils.ts
+++ b/packages/studio/src/player/components/thumbnailUtils.ts
@@ -1,3 +1,4 @@
+import { decodedUrlPath } from "@hyperframes/parsers";
import { buildProjectApiPath } from "../../utils/projectRouting";
import { MAX_VISIBLE_THUMBNAIL_FRAMES } from "../lib/timelineViewportBudgets";
@@ -94,6 +95,10 @@ export function computeThumbnailStrip(
return { frameW, frameCount };
}
+export function authoredSrcPath(src: string): string {
+ return /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(src) ? src : decodedUrlPath(src);
+}
+
/**
* Percent-encode each segment of a composition-relative media path so filenames
* containing spaces, parentheses, a U+202F narrow no-break space (the macOS
diff --git a/packages/studio/src/player/lib/mediaProbe.test.ts b/packages/studio/src/player/lib/mediaProbe.test.ts
index 69336109e1..3f5cb83067 100644
--- a/packages/studio/src/player/lib/mediaProbe.test.ts
+++ b/packages/studio/src/player/lib/mediaProbe.test.ts
@@ -89,6 +89,18 @@ describe("media probe registry", () => {
expect(getDurationFromMetadata).toHaveBeenCalledTimes(2);
});
+ it("probes a dropped clip's URL-spelled src at its own file", async () => {
+ await probeMissingSourceDurations(
+ [{ id: "clip", tag: "video", src: "assets/My%20clip%20%231.mp4" }],
+ "project-a",
+ vi.fn(),
+ );
+
+ expect(requestedSources).toEqual([
+ `${window.location.origin}/api/projects/project-a/preview/assets/My%20clip%20%231.mp4`,
+ ]);
+ });
+
it("probes same-origin rooted media through the active project preview", async () => {
const apply = vi.fn();
await probeMissingSourceDurations(
diff --git a/packages/studio/src/player/lib/mediaProbe.ts b/packages/studio/src/player/lib/mediaProbe.ts
index 070107e4f2..ec9f015716 100644
--- a/packages/studio/src/player/lib/mediaProbe.ts
+++ b/packages/studio/src/player/lib/mediaProbe.ts
@@ -1,4 +1,4 @@
-import { resolveMediaPreviewUrl } from "../components/thumbnailUtils";
+import { authoredSrcPath, resolveMediaPreviewUrl } from "../components/thumbnailUtils";
import { TIMELINE_VIEWPORT_BUDGETS } from "./timelineViewportBudgets";
export interface MediaProbeResult {
@@ -87,7 +87,9 @@ function getCachedProbe(url: string): MediaProbeResult | undefined {
}
function resolveProbeSource(src: string, projectId: string | null): string {
- return projectId ? resolveMediaPreviewUrl(src, projectId, window.location.origin) : src;
+ return projectId
+ ? resolveMediaPreviewUrl(authoredSrcPath(src), projectId, window.location.origin)
+ : src;
}
function evictMetadataOverflow(): void {
diff --git a/packages/studio/src/utils/sourcePatcher.test.ts b/packages/studio/src/utils/sourcePatcher.test.ts
index 503124d80c..cecb72e63b 100644
--- a/packages/studio/src/utils/sourcePatcher.test.ts
+++ b/packages/studio/src/utils/sourcePatcher.test.ts
@@ -3,11 +3,43 @@ import {
applyPatch,
applyPatchByTarget,
readAttributeByTarget,
+ readTagAttribute,
readTagSnippetByTarget,
type PatchOperation,
} from "./sourcePatcher";
describe("applyPatchByTarget", () => {
+ it("removes all duplicate boolean attributes so unmuting cannot reveal another", () => {
+ expect(
+ applyPatch(`<video id="clip" muted muted></video>`, "clip", {
+ type: "html-attribute",
+ property: "muted",
+ value: null,
+ }),
+ ).toBe(`<video id="clip"></video>`);
+ });
+
+ it("locates Unicode custom tags and respects the first duplicate attribute", () => {
+ const html = `<my-élement id="real" id="other" data-duration="1"></my-élement>`;
+ const op: PatchOperation = { type: "attribute", property: "duration", value: "2" };
+ expect(applyPatch(html, "real", op)).toContain('data-duration="2"');
+ expect(applyPatch(html, "other", op)).toBe(html);
+ });
+
+ it("reads and patches only the real source outside quoted attribute content", () => {
+ const html = `<video id="hero" title="example src='other.mp4'" data-src="lazy.mp4" src="assets/it's.mp4"></video>`;
+ expect(readTagAttribute(html, "src")).toBe("assets/it's.mp4");
+ const op: PatchOperation = { type: "html-attribute", property: "src", value: "new.mp4" };
+ const expected = `<video id="hero" title="example src='other.mp4'" data-src="lazy.mp4" src="new.mp4"></video>`;
+ expect(applyPatch(html, "hero", op)).toBe(expected);
+ expect(applyPatchByTarget(html, { id: "hero" }, op)).toBe(expected);
+ });
+
+ it("does not locate a target through a prefixed or quoted id", () => {
+ const html = `<video data-id="hero" title="id='hero'" src="other.mp4"></video><video id="hero" src="real.mp4"></video>`;
+ expect(readTagSnippetByTarget(html, { id: "hero" })).toContain('src="real.mp4"');
+ });
+
it.each(['"', "'"])("patches long and multiline styles quoted with %s", (quote) => {
const otherQuote = quote === '"' ? "'" : '"';
const op: PatchOperation = { type: "inline-style", property: "opacity", value: "0.5" };
@@ -646,3 +678,106 @@ describe("T3 — hfId targeting (spec for R1)", () => {
expect(result).toContain('<h2 class="b">B</h2>');
});
});
+
+describe("file names with special characters", () => {
+ const src = (value: string): PatchOperation => ({
+ type: "html-attribute",
+ property: "src",
+ value,
+ });
+
+ it("replaces a double-quoted value that holds an apostrophe instead of adding a second one", () => {
+ expect(applyPatch(`<img id="a" src="assets/it's.png">`, "a", src("assets/b.png"))).toBe(
+ `<img id="a" src="assets/b.png">`,
+ );
+ expect(applyPatch(`<img id="a" src='assets/say "hi".png'>`, "a", src("b.png"))).toBe(
+ `<img id="a" src="b.png">`,
+ );
+ });
+
+ it("writes $ sequences in a value literally", () => {
+ expect(applyPatch(`<img id="a" src="old.png">`, "a", src("assets/$100 bill.png"))).toBe(
+ `<img id="a" src="assets/$100 bill.png">`,
+ );
+ expect(applyPatch(`<img id="a">`, "a", src("$&$1.png"))).toBe(
+ `<img id="a" src="$&amp;$1.png">`,
+ );
+ expect(
+ applyPatch(`<div id="a" data-src="old">`, "a", {
+ type: "attribute",
+ property: "src",
+ value: "$' and $`",
+ }),
+ ).toBe(`<div id="a" data-src="$' and $\`">`);
+ });
+
+ it("reads a data attribute whose value holds the other quote kind", () => {
+ expect(readAttributeByTarget(`<div id="a" data-title="it's here">`, { id: "a" }, "title")).toBe(
+ "it's here",
+ );
+ });
+});
+
+describe("readTagAttribute", () => {
+ it("reads both quote kinds and decodes entities without losing the other quote", () => {
+ expect(readTagAttribute(`<video src="assets/it's&amp;ours.mp4">`, "src")).toBe(
+ "assets/it's&ours.mp4",
+ );
+ expect(readTagAttribute(`<video src='assets/say "hi".mp4'>`, "src")).toBe(
+ 'assets/say "hi".mp4',
+ );
+ });
+
+ it("distinguishes an empty attribute from a missing attribute", () => {
+ expect(readTagAttribute('<video src="">', "src")).toBe("");
+ expect(readTagAttribute("<video>", "src")).toBeUndefined();
+ });
+});
+
+describe("whole attribute names", () => {
+ it.each(["data-src", "x-src", "xml:src", "srcset"])(
+ "reads and edits src without matching %s",
+ (other) => {
+ const tag = `<img id="a" ${other}="assets/other.png" src="assets/original.png">`;
+ expect(readTagAttribute(tag, "src")).toBe("assets/original.png");
+ for (const patch of [
+ (op: PatchOperation) => applyPatch(tag, "a", op),
+ (op: PatchOperation) => applyPatchByTarget(tag, { id: "a" }, op),
+ ]) {
+ expect(patch({ type: "html-attribute", property: "src", value: "assets/new.png" })).toBe(
+ `<img id="a" ${other}="assets/other.png" src="assets/new.png">`,
+ );
+ expect(patch({ type: "html-attribute", property: "src", value: null })).toBe(
+ `<img id="a" ${other}="assets/other.png">`,
+ );
+ }
+ expect(readTagAttribute(`<img ${other}="assets/other.png">`, "src")).toBeUndefined();
+ },
+ );
+
+ it("does not treat a longer data attribute as a boolean attribute to remove", () => {
+ const tag = '<div id="a" data-title-long="keep">';
+ expect(
+ applyPatchByTarget(tag, { id: "a" }, { type: "attribute", property: "title", value: null }),
+ ).toBe(tag);
+ });
+});
+
+it("removes a boolean attribute without truncating a longer attribute name", () => {
+ const tag = '<video id="a" muted-long="keep" muted>';
+ expect(
+ applyPatchByTarget(
+ tag,
+ { id: "a" },
+ { type: "html-attribute", property: "muted", value: null },
+ ),
+ ).toBe('<video id="a" muted-long="keep">');
+});
+
+it("decodes an authored apostrophe entity once", () => {
+ expect(readTagAttribute("<video src='assets/it&apos;s.mp4'>", "src")).toBe("assets/it's.mp4");
+ expect(readTagAttribute("<video src='assets/it&#39;s.mp4'>", "src")).toBe("assets/it's.mp4");
+ expect(readTagAttribute('<video src="assets/it&amp;apos;s.mp4">', "src")).toBe(
+ "assets/it&apos;s.mp4",
+ );
+});
diff --git a/packages/studio/src/utils/sourcePatcher.ts b/packages/studio/src/utils/sourcePatcher.ts
index 57a250e859..6bf2668f2e 100644
--- a/packages/studio/src/utils/sourcePatcher.ts
+++ b/packages/studio/src/utils/sourcePatcher.ts
@@ -1,3 +1,9 @@
+import {
+ scanHtmlOpeningTags,
+ decodeAuthoredAttribute,
+ HTML_ATTRIBUTE_ENTITIES,
+} from "@hyperframes/parsers";
+
/**
* Source Patcher — Maps visual property edits back to source HTML files.
* Handles inline style updates, attribute changes, and text content.
@@ -7,26 +13,44 @@ export function escapeRegex(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
+function tagAttribute(tag: string, name: string) {
+ return scanHtmlOpeningTags(tag)[0]?.attributes.find((attr) => attr.name === name.toLowerCase());
+}
+
+type AttributeEdit = { kind: "remove" } | { kind: "boolean" } | { kind: "value"; value: string };
+
+function patchTagAttribute(tag: string, name: string, edit: AttributeEdit): string {
+ if (edit.kind === "remove") {
+ const attributes =
+ scanHtmlOpeningTags(tag)[0]?.attributes.filter((attr) => attr.name === name.toLowerCase()) ??
+ [];
+ const parts: string[] = [];
+ let cursor = 0;
+ for (const attribute of attributes) {
+ let start = attribute.start;
+ while (start > cursor && /[\t\n\f\r ]/.test(tag[start - 1]!)) start--;
+ parts.push(tag.slice(cursor, start));
+ cursor = attribute.end;
+ }
+ parts.push(tag.slice(cursor));
+ return parts.join("");
+ }
+ const attr = tagAttribute(tag, name);
+ const replacement =
+ edit.kind === "boolean" ? name : `${name}="${escapeHtmlAttribute(edit.value)}"`;
+ if (attr) return tag.slice(0, attr.start) + replacement + tag.slice(attr.end);
+ return tag.endsWith("/")
+ ? `${tag.slice(0, -1).trimEnd()} ${replacement} /`
+ : `${tag} ${replacement}`;
+}
+
function escapeStyleAttributeValue(value: string, quote: string): string {
return quote === '"' ? value.replace(/"/g, "&quot;") : value.replace(/'/g, "&#39;");
}
/** Escape a string for safe use inside a double-quoted HTML attribute. */
export function escapeHtmlAttribute(value: string): string {
- return value
- .replace(/&/g, "&amp;")
- .replace(/"/g, "&quot;")
- .replace(/</g, "&lt;")
- .replace(/>/g, "&gt;");
-}
-
-/** Reverse escapeHtmlAttribute so callers get the original value. */
-export function unescapeHtmlAttribute(value: string): string {
- return value
- .replace(/&quot;/g, '"')
- .replace(/&lt;/g, "<")
- .replace(/&gt;/g, ">")
- .replace(/&amp;/g, "&");
+ return value.replace(/[&"<>]/g, (char) => HTML_ATTRIBUTE_ENTITIES[char]![0]!);
}
function splitInlineStyleDeclarations(style: string): string[] {
@@ -98,8 +122,6 @@ export interface PatchOperation {
childIndex?: number;
}
-// Runtime validation for hfId lives in findTagByTarget → execDataAttrPattern (CSS attr-value
-// escape). This type is documentation only; the server's MutationTarget mirrors this shape.
export interface PatchTarget {
id?: string | null;
hfId?: string;
@@ -120,7 +142,7 @@ export function resolveSourceFile(
// Strategy 1: Search by id attribute
if (elementId) {
for (const [path, content] of Object.entries(files)) {
- if (content.includes(`id="${elementId}"`) || content.includes(`id='${elementId}'`)) {
+ if (findTagByAttribute(content, "id", elementId)) {
return path;
}
}
@@ -131,7 +153,7 @@ export function resolveSourceFile(
if (compIdMatch) {
const compId = compIdMatch[1];
for (const [path, content] of Object.entries(files)) {
- if (content.includes(`data-composition-id="${compId}"`)) {
+ if (findTagByAttribute(content, "data-composition-id", compId!)) {
return path;
}
}
@@ -142,11 +164,7 @@ export function resolveSourceFile(
if (classMatch) {
const cls = classMatch[1];
for (const [path, content] of Object.entries(files)) {
- if (
- content.includes(`class="${cls}"`) ||
- content.includes(`class="${cls} `) ||
- content.includes(` ${cls}"`)
- ) {
+ if (findTagByClass(content, { selector: `.${cls}` })) {
return path;
}
}
@@ -166,28 +184,17 @@ function patchInlineStyle(
prop: string,
value: string | null,
): string {
- // Find the element tag with this id
- const idPattern = new RegExp(`(<[^>]*\\bid=(["'])${escapeRegex(elementId)}\\2[^>]*)>`, "i");
- const match = idPattern.exec(html);
- if (!match) return html;
-
- const tag = match[1];
- return patchInlineStyleInTag(html, tag, prop, value);
+ return patchInlineStyleByTarget(html, { id: elementId }, prop, value);
}
-function patchInlineStyleInTag(
- html: string,
- tag: string,
- prop: string,
- value: string | null,
-): string {
- if (!tag) return html;
+function patchInlineStyleInTag(tag: string, prop: string, value: string | null): string {
+ if (!tag) return tag;
// Check if there's an existing style attribute
- const styleMatch = /\bstyle=(")([^"]*)"|\bstyle=(')([^']*)'/.exec(tag);
- if (styleMatch) {
- const existingStyle = styleMatch[2] ?? styleMatch[4];
- const quote = styleMatch[1] ?? styleMatch[3];
+ const styleMatch = tagAttribute(tag, "style");
+ if (styleMatch?.kind === "value") {
+ const existingStyle = styleMatch.value;
+ const quote = styleMatch.quote || '"';
// Parse existing properties
const props = new Map<string, string>();
for (const part of splitInlineStyleDeclarations(existingStyle)) {
@@ -207,15 +214,18 @@ function patchInlineStyleInTag(
const newStyle = Array.from(props.entries())
.map(([k, v]) => `${k}: ${escapeStyleAttributeValue(v, quote)}`)
.join("; ");
- const newTag = tag.replace(styleMatch[0], `style=${quote}${newStyle}${quote}`);
- return html.replace(tag, newTag);
+ const newTag =
+ tag.slice(0, styleMatch.start) +
+ `style=${quote}${newStyle}${quote}` +
+ tag.slice(styleMatch.end);
+ return newTag;
} else {
// No existing style attribute
- if (value === null) return html; // nothing to remove
+ if (value === null) return tag; // nothing to remove
const selfClosing = tag.endsWith("/");
const base = selfClosing ? tag.slice(0, -1).trimEnd() : tag;
const newTag = `${base} style="${prop}: ${escapeStyleAttributeValue(value, '"')}"${selfClosing ? " /" : ""}`;
- return html.replace(tag, newTag);
+ return newTag;
}
}
@@ -227,7 +237,7 @@ function patchInlineStyleByTarget(
): string {
const match = findTagByTarget(html, target);
if (!match) return html;
- const newTag = patchInlineStyleInTag(match.tag, match.tag, prop, value);
+ const newTag = patchInlineStyleInTag(match.tag, prop, value);
return replaceTagAtMatch(html, match, newTag);
}
@@ -241,45 +251,40 @@ function replaceTagAtMatch(html: string, match: TagMatch, newTag: string): strin
return `${html.slice(0, match.start)}${newTag}${html.slice(match.end)}`;
}
-function execDataAttrPattern(html: string, attr: string, value: string): TagMatch | null {
- const pattern = new RegExp(`(<[^>]*\\b${attr}=(["'])${escapeRegex(value)}\\2[^>]*)>`, "i");
- const match = pattern.exec(html);
- if (match?.index == null) return null;
- return { tag: match[1], start: match.index, end: match.index + match[1].length };
+function findTagByAttribute(html: string, name: string, value: string): TagMatch | null {
+ const found = scanHtmlOpeningTags(html).find((tag) => {
+ const attr = tag.attributes.find((attr) => attr.name === name);
+ return tag.closed && attr?.kind === "value" && decodeAuthoredAttribute(attr.value) === value;
+ });
+ return found
+ ? { tag: html.slice(found.start, found.bodyEnd), start: found.start, end: found.bodyEnd }
+ : null;
}
function findTagByClass(html: string, target: PatchTarget): TagMatch | null {
const classMatch = target.selector?.match(/^\.([a-zA-Z0-9_-]+)$/);
if (!classMatch) return null;
- const cls = classMatch[1];
- const pattern = new RegExp(
- `(<[^>]*\\bclass=(["'])[^"']*\\b${escapeRegex(cls)}\\b[^"']*\\2[^>]*)>`,
- "gi",
- );
- const selectorIndex = target.selectorIndex ?? 0;
- let match: RegExpExecArray | null;
- let currentIndex = 0;
- while ((match = pattern.exec(html)) !== null) {
- if (currentIndex === selectorIndex && match.index != null) {
- return {
- tag: match[1],
- start: match.index,
- end: match.index + match[1].length,
- };
- }
- currentIndex += 1;
- }
- return null;
+ const found = scanHtmlOpeningTags(html).filter((tag) => {
+ const attr = tag.attributes.find((attr) => attr.name === "class");
+ return (
+ tag.closed &&
+ attr?.kind === "value" &&
+ decodeAuthoredAttribute(attr.value).split(/\s+/).includes(classMatch[1]!)
+ );
+ })[target.selectorIndex ?? 0];
+ return found
+ ? { tag: html.slice(found.start, found.bodyEnd), start: found.start, end: found.bodyEnd }
+ : null;
}
export function findTagByTarget(html: string, target: PatchTarget): TagMatch | null {
if (target.hfId) {
- const result = execDataAttrPattern(html, "data-hf-id", target.hfId);
+ const result = findTagByAttribute(html, "data-hf-id", target.hfId);
if (result) return result;
}
if (target.id) {
- const result = execDataAttrPattern(html, "id", target.id);
+ const result = findTagByAttribute(html, "id", target.id);
if (result) return result;
}
@@ -287,7 +292,7 @@ export function findTagByTarget(html: string, target: PatchTarget): TagMatch | n
const compositionIdMatch = target.selector.match(/^\[data-composition-id="([^"]+)"\]$/);
if (compositionIdMatch) {
- const result = execDataAttrPattern(html, "data-composition-id", compositionIdMatch[1]);
+ const result = findTagByAttribute(html, "data-composition-id", compositionIdMatch[1]);
if (result) return result;
}
@@ -302,9 +307,12 @@ export function readAttributeByTarget(
const match = findTagByTarget(html, target);
if (!match) return undefined;
- const fullAttr = attr.startsWith("data-") ? attr : `data-${attr}`;
- const valueMatch = new RegExp(`\\b${fullAttr}=(["'])([^"']*)\\1`).exec(match.tag);
- return valueMatch?.[2] != null ? unescapeHtmlAttribute(valueMatch[2]) : undefined;
+ return readTagAttribute(match.tag, attr.startsWith("data-") ? attr : `data-${attr}`);
+}
+
+export function readTagAttribute(tag: string, attr: string): string | undefined {
+ const value = tagAttribute(tag, attr);
+ return value?.kind === "value" ? decodeAuthoredAttribute(value.value) : undefined;
}
export function readTagSnippetByTarget(html: string, target: PatchTarget): string | undefined {
@@ -320,85 +328,25 @@ function patchAttributeByTarget(
): string {
const match = findTagByTarget(html, target);
if (!match) return html;
-
- const fullAttr = attr.startsWith("data-") ? attr : `data-${attr}`;
- const attrPattern = new RegExp(`\\b${escapeRegex(fullAttr)}=(["'])([^"']*)\\1`);
- const tag = match.tag;
-
- if (value === null) {
- // Remove the attribute if present
- const boolAttrPattern = new RegExp(`\\b${escapeRegex(fullAttr)}(?:=(["'])[^"']*\\1)?`);
- if (!boolAttrPattern.test(tag)) return html;
- const removePattern = new RegExp(`\\s+${escapeRegex(fullAttr)}(?:=(["'])[^"']*\\1)?`);
- const newTag = tag.replace(removePattern, "");
- return replaceTagAtMatch(html, match, newTag);
- }
-
- const escaped = escapeHtmlAttribute(value);
- if (attrPattern.test(tag)) {
- const newTag = tag.replace(attrPattern, `${fullAttr}="${escaped}"`);
- return replaceTagAtMatch(html, match, newTag);
- }
-
- const newTag = tag + ` ${fullAttr}="${escaped}"`;
- return replaceTagAtMatch(html, match, newTag);
+ const name = attr.startsWith("data-") ? attr : `data-${attr}`;
+ const edit: AttributeEdit = value === null ? { kind: "remove" } : { kind: "value", value };
+ return replaceTagAtMatch(html, match, patchTagAttribute(match.tag, name, edit));
}
-/**
- * Apply an attribute change to an element in the HTML source.
- */
function patchAttribute(
html: string,
elementId: string,
attr: string,
value: string | null,
): string {
- const idPattern = new RegExp(`(<[^>]*\\bid=(["'])${escapeRegex(elementId)}\\2[^>]*)>`, "i");
- const match = idPattern.exec(html);
- if (!match) return html;
-
- const tag = match[1];
- const fullAttr = attr.startsWith("data-") ? attr : `data-${attr}`;
- const attrPattern = new RegExp(`\\b${escapeRegex(fullAttr)}=(["'])([^"']*)\\1`);
-
- if (value === null) {
- const boolAttrPattern = new RegExp(`\\b${escapeRegex(fullAttr)}(?:=(["'])[^"']*\\1)?`);
- if (!boolAttrPattern.test(tag)) return html;
- const removePattern = new RegExp(`\\s+${escapeRegex(fullAttr)}(?:=(["'])[^"']*\\1)?`);
- const newTag = tag.replace(removePattern, "");
- return html.replace(tag, newTag);
- }
-
- const escaped = escapeHtmlAttribute(value);
- if (attrPattern.test(tag)) {
- // Update existing attribute
- const newTag = tag.replace(attrPattern, `${fullAttr}="${escaped}"`);
- return html.replace(tag, newTag);
- } else {
- // Add new attribute
- const newTag = tag + ` ${fullAttr}="${escaped}"`;
- return html.replace(tag, newTag);
- }
+ return patchAttributeByTarget(html, { id: elementId }, attr, value);
}
/**
* Apply a text content change to an element.
*/
function patchTextContent(html: string, elementId: string, value: string): string {
- const openTagPattern = new RegExp(
- `(<([a-z0-9-]+)[^>]*\\bid=(["'])${escapeRegex(elementId)}\\3[^>]*>)`,
- "i",
- );
- const match = openTagPattern.exec(html);
- if (!match || match.index == null) return html;
-
- const openingTag = match[1];
- const tagName = match[2];
- const contentStart = match.index + openingTag.length;
- const closingIndex = findMatchingClosingTagIndex(html, tagName, contentStart);
- if (closingIndex < 0) return html;
-
- return `${html.slice(0, contentStart)}${value}${html.slice(closingIndex)}`;
+ return patchTextContentByTarget(html, { id: elementId }, value);
}
function findMatchingClosingTagIndex(html: string, tagName: string, contentStart: number): number {
@@ -438,47 +386,18 @@ export const HTML_BOOLEAN_ATTRIBUTES = new Set([
"selected",
]);
-function patchHtmlAttributeInTag(
- html: string,
- tag: string,
- attr: string,
- value: string | null,
-): string {
- if (!tag) return html;
-
- const isBoolean = HTML_BOOLEAN_ATTRIBUTES.has(attr);
-
- if (isBoolean) {
- const escapedAttr = escapeRegex(attr);
- const hasBoolAttr = new RegExp(`(?:^|\\s)${escapedAttr}(?:\\s|=|$)`).test(tag);
-
- if (value === null || value === "" || value === "false") {
- if (!hasBoolAttr) return html;
- const removePattern = new RegExp(`\\s+${escapedAttr}(?:=(["'])[^"']*\\1)?`);
- const newTag = tag.replace(removePattern, "");
- return html.replace(tag, newTag);
+function patchHtmlAttributeInTag(tag: string, attr: string, value: string | null): string {
+ if (!tag) return tag;
+ let edit: AttributeEdit;
+ if (HTML_BOOLEAN_ATTRIBUTES.has(attr)) {
+ if (value === null || value === "" || value === "false") edit = { kind: "remove" };
+ else {
+ if (tagAttribute(tag, attr)) return tag;
+ edit = { kind: "boolean" };
}
- if (hasBoolAttr) return html;
- const newTag = tag + ` ${attr}`;
- return html.replace(tag, newTag);
- }
-
- const attrPattern = new RegExp(`\\b${escapeRegex(attr)}=(["'])([^"']*)\\1`);
- if (value === null) {
- if (!attrPattern.test(tag)) return html;
- const removePattern = new RegExp(`\\s+${escapeRegex(attr)}=(["'])[^"']*\\1`);
- const newTag = tag.replace(removePattern, "");
- return html.replace(tag, newTag);
- }
-
- const escaped = escapeHtmlAttribute(value);
- if (attrPattern.test(tag)) {
- const newTag = tag.replace(attrPattern, `${attr}="${escaped}"`);
- return html.replace(tag, newTag);
- }
-
- const newTag = tag + ` ${attr}="${escaped}"`;
- return html.replace(tag, newTag);
+ } else edit = value === null ? { kind: "remove" } : { kind: "value", value };
+ const newTag = patchTagAttribute(tag, attr, edit);
+ return newTag;
}
function patchHtmlAttribute(
@@ -487,10 +406,7 @@ function patchHtmlAttribute(
attr: string,
value: string | null,
): string {
- const idPattern = new RegExp(`(<[^>]*\\bid=(["'])${escapeRegex(elementId)}\\2[^>]*)>`, "i");
- const match = idPattern.exec(html);
- if (!match) return html;
- return patchHtmlAttributeInTag(html, match[1], attr, value);
+ return patchHtmlAttributeByTarget(html, { id: elementId }, attr, value);
}
function patchHtmlAttributeByTarget(
@@ -501,7 +417,7 @@ function patchHtmlAttributeByTarget(
): string {
const match = findTagByTarget(html, target);
if (!match) return html;
- const newTag = patchHtmlAttributeInTag(match.tag, match.tag, attr, value);
+ const newTag = patchHtmlAttributeInTag(match.tag, attr, value);
return replaceTagAtMatch(html, match, newTag);
}
diff --git a/packages/studio/src/utils/studioHelpers.test.ts b/packages/studio/src/utils/studioHelpers.test.ts
index 3581415dab..17984db3b1 100644
--- a/packages/studio/src/utils/studioHelpers.test.ts
+++ b/packages/studio/src/utils/studioHelpers.test.ts
@@ -6,6 +6,7 @@ import {
findMatchingTimelineElementId,
findTimelineIdByAncestor,
resolveAssetHasAudio,
+ resolveDroppedAssetDuration,
resolveDroppedAssetDimensions,
resolveElementTrack,
resolveTimelineIdForSelection,
@@ -210,6 +211,28 @@ describe("resolveDroppedAssetDimensions", () => {
});
});
+describe("dropped asset probes", () => {
+ const NAME = "assets/50% off #1?'s take.mp4";
+ const URL = "/api/projects/demo/preview/assets/50%25%20off%20%231%3F%27s%20take.mp4";
+
+ it("probe a dropped file's length and size at its own URL", async () => {
+ vi.useFakeTimers();
+ const probes: HTMLVideoElement[] = [];
+ const createElement = document.createElement.bind(document);
+ vi.spyOn(document, "createElement").mockImplementation((tagName, options) => {
+ const el = createElement(tagName, options);
+ if (tagName === "video") probes.push(el as HTMLVideoElement);
+ return el;
+ });
+
+ const duration = resolveDroppedAssetDuration("demo", NAME, "video");
+ const size = resolveDroppedAssetDimensions("demo", NAME, "video");
+ expect(probes.map((probe) => probe.getAttribute("src"))).toEqual([URL, URL]);
+ await vi.advanceTimersByTimeAsync(3000);
+ await Promise.all([duration, size]);
+ });
+});
+
describe("resolveElementTrack", () => {
it("rounds an authored track", () => {
expect(resolveElementTrack({ authoredTrack: 2.4, track: 0 })).toBe(2);
diff --git a/packages/studio/src/utils/studioHelpers.ts b/packages/studio/src/utils/studioHelpers.ts
index bc644d0339..2a5c7be16e 100644
--- a/packages/studio/src/utils/studioHelpers.ts
+++ b/packages/studio/src/utils/studioHelpers.ts
@@ -5,6 +5,7 @@ import type { DomEditSelection } from "../components/editor/domEditing";
import type { TimelineAssetKind } from "./timelineAssetDrop";
import { roundToCenti } from "./rounding";
import { studioApiFetch } from "./studioApiFetch";
+import { encodeUrlPath } from "@hyperframes/parsers";
export interface EditingFile {
path: string;
@@ -352,7 +353,7 @@ export async function resolveDroppedAssetDuration(
const media = document.createElement(kind === "video" ? "video" : "audio");
media.preload = "metadata";
- media.src = buildProjectApiPath(projectId, `/preview/${assetPath}`);
+ media.src = buildProjectApiPath(projectId, `/preview/${encodeUrlPath(assetPath)}`);
const duration = await new Promise<number>((resolve) => {
const timeout = window.setTimeout(() => resolve(DEFAULT_TIMELINE_ASSET_DURATION[kind]), 3000);
@@ -424,7 +425,7 @@ export async function resolveDroppedAssetDimensions(
kind: TimelineAssetKind,
): Promise<{ width: number; height: number } | null> {
if (kind === "audio") return null;
- const src = buildProjectApiPath(projectId, `/preview/${assetPath}`);
+ const src = buildProjectApiPath(projectId, `/preview/${encodeUrlPath(assetPath)}`);
if (kind === "image") {
return new Promise((resolve) => {
diff --git a/packages/studio/src/utils/studioSelectionSnapshot.test.ts b/packages/studio/src/utils/studioSelectionSnapshot.test.ts
index fb645ba83a..28be1da4ac 100644
--- a/packages/studio/src/utils/studioSelectionSnapshot.test.ts
+++ b/packages/studio/src/utils/studioSelectionSnapshot.test.ts
@@ -44,4 +44,35 @@ describe("buildStudioSelectionSnapshot", () => {
expect(JSON.stringify(snapshot)).not.toContain("HTMLElement");
expect(snapshot).not.toHaveProperty("element");
});
+
+ it("links a selection inside a sub-composition to its own thumbnail", () => {
+ const snapshot = buildStudioSelectionSnapshot({
+ projectId: "demo",
+ selection: {
+ element: { tagName: "H1" } as HTMLElement,
+ id: null,
+ hfId: "intro-title",
+ selector: ".title",
+ selectorIndex: 0,
+ label: "Intro title",
+ tagName: "h1",
+ sourceFile: "compositions/intro #1.html",
+ compositionPath: "compositions/intro #1.html",
+ isCompositionHost: false,
+ isInsideLockedComposition: false,
+ boundingBox: { x: 0, y: 0, width: 10, height: 10 },
+ textContent: "Intro",
+ dataAttributes: {},
+ inlineStyles: {},
+ computedStyles: {},
+ textFields: [],
+ capabilities: { canSelect: true, canEditStyles: true },
+ } as DomEditSelection,
+ currentTime: 0,
+ });
+
+ expect(snapshot.thumbnailUrl).toMatch(
+ /^\/api\/projects\/demo\/thumbnail\/compositions%2Fintro%20%231\.html\?/,
+ );
+ });
});
diff --git a/packages/studio/src/utils/timelineAssetDrop.test.ts b/packages/studio/src/utils/timelineAssetDrop.test.ts
index b7750fd994..246a5b51db 100644
--- a/packages/studio/src/utils/timelineAssetDrop.test.ts
+++ b/packages/studio/src/utils/timelineAssetDrop.test.ts
@@ -292,3 +292,17 @@ describe("fitTimelineAssetGeometry", () => {
});
});
});
+
+it("writes a dropped file's name as a URL, so %, #, ? and apostrophes load", () => {
+ const html = buildTimelineAssetInsertHtml({
+ id: "clip",
+ hfId: "hf-clip",
+ assetPath: "../assets/50% off #1?'s take.mp4",
+ kind: "video",
+ start: 0,
+ duration: 2,
+ track: 1,
+ zIndex: 1,
+ });
+ expect(html).toContain('src="../assets/50%25%20off%20%231%3F%27s%20take.mp4"');
+});
diff --git a/packages/studio/src/utils/timelineAssetDrop.ts b/packages/studio/src/utils/timelineAssetDrop.ts
index 48b88e262b..14186263a5 100644
--- a/packages/studio/src/utils/timelineAssetDrop.ts
+++ b/packages/studio/src/utils/timelineAssetDrop.ts
@@ -1,3 +1,4 @@
+import { encodeUrlPath } from "@hyperframes/parsers";
export { buildTimelineAssetId } from "@hyperframes/core/timeline-asset-id";
import { AUDIO_EXT, IMAGE_EXT, VIDEO_EXT } from "./mediaTypes";
import { roundToCenti } from "./rounding";
@@ -105,7 +106,7 @@ export function buildTimelineAssetInsertHtml(input: {
/** Video only: true inserts `data-has-audio="true"` with no `muted`. Unknown or false stays muted. */
hasAudio?: boolean;
}): string {
- const sharedAttrs = `id="${input.id}" data-hf-id="${input.hfId}" class="clip" src="${input.assetPath}" data-start="${input.start}" data-duration="${input.duration}" data-track-index="${input.track}"`;
+ const sharedAttrs = `id="${input.id}" data-hf-id="${input.hfId}" class="clip" src="${encodeUrlPath(input.assetPath)}" data-start="${input.start}" data-duration="${input.duration}" data-track-index="${input.track}"`;
const geometry = input.geometry ?? { left: 0, top: 0, width: 640, height: 360 };
const visualStyles = `position: absolute; left: ${geometry.left}px; top: ${geometry.top}px; width: ${geometry.width}px; height: ${geometry.height}px; object-fit: contain; z-index: ${input.zIndex}`;