Skip to content

Commit ac2492b

Browse files
committed
fix(runtime): a scene swap keeps media carrying the runtime classes or reordered attributes
1 parent 06cafc5 commit ac2492b

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

‎packages/core/src/runtime/init.swapScenes.test.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,45 @@ describe("__hfSwapScenes", () => {
664664
expect(sceneHost("a").querySelector("video")).toBe(kept);
665665
});
666666

667+
it.each([
668+
["the edit flash", "__hf-flash"],
669+
["the picker's hover", "__hf-pick-highlight"],
670+
])("keeps a video that carries %s class at the swap", async (_, name) => {
671+
quietMedia();
672+
const { html } = bootWithHeadingScript(
673+
"void video",
674+
'<video class="clip" src="clip.mp4"></video>',
675+
);
676+
await tick();
677+
const video = sceneHost("a").querySelector("video")!;
678+
video.classList.add(name);
679+
await window.__hfSwapScenes!(html);
680+
expect(sceneHost("a").querySelector("video")).toBe(video);
681+
});
682+
683+
it("keeps a video that carries the edit flash as its only class at the swap", async () => {
684+
quietMedia();
685+
const { html } = bootWithHeadingScript("void video");
686+
await tick();
687+
const video = sceneHost("a").querySelector("video")!;
688+
video.classList.add("__hf-flash");
689+
await window.__hfSwapScenes!(html);
690+
expect(sceneHost("a").querySelector("video")).toBe(video);
691+
});
692+
693+
it("keeps a video whose unchanged script removes an attribute and puts it back as it was", async () => {
694+
quietMedia();
695+
const { html } = bootWithHeadingScript(
696+
"video.removeAttribute('title'), video.setAttribute('title', 'clip')",
697+
'<video title="clip" data-id="v" src="clip.mp4"></video>',
698+
);
699+
await tick();
700+
const video = sceneHost("a").querySelector("video")!;
701+
expect(video.getAttributeNames().at(-1)).toBe("title");
702+
await window.__hfSwapScenes!(html);
703+
expect(sceneHost("a").querySelector("video")).toBe(video);
704+
});
705+
667706
it("unmutes a kept video though stopping Web Audio puts back the mute it saved from the old script", async () => {
668707
quietMedia();
669708
const { html } = bootWithHeadingScript("video.muted = true");

‎packages/core/src/runtime/init.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,20 +240,28 @@ const authoredShape = (el: Element): string =>
240240
el.innerHTML,
241241
]);
242242

243-
// What the runtime writes on a video or audio: style (reset on a kept one), preload, the opacity stamp and a moved
244-
// element's original translate. A proxied src is compared as written; a variable-bound one is the binding's.
243+
// What the runtime writes on a video or audio: style (reset on a kept one), preload, the opacity stamp, a moved
244+
// element's original translate and its own __hf- classes. A proxied src is compared as written; a bound one is not.
245245
const RUNTIME_MEDIA_ATTRS = new Set([
246246
"style",
247247
"preload",
248248
"src",
249249
COLOR_GRADING_AUTHORED_OPACITY_ATTR,
250250
EDIT_ORIGINAL_TRANSLATE_ATTR,
251251
]);
252+
const ownClasses = (value: string) =>
253+
value
254+
.split(/\s+/)
255+
.filter((c) => c && !c.startsWith("__hf-"))
256+
.join(" ");
252257
const writtenShape = (el: Element): string =>
253258
JSON.stringify([
254-
Array.from(el.attributes, (a) => [a.name, a.value]).filter(
255-
([name]) => !RUNTIME_MEDIA_ATTRS.has(name!),
256-
),
259+
Array.from(el.attributes, ({ name, value }) => [
260+
name,
261+
name === "class" ? ownClasses(value) : value,
262+
])
263+
.filter(([name, value]) => !RUNTIME_MEDIA_ATTRS.has(name!) && !(name === "class" && !value))
264+
.sort(([a], [b]) => (a! < b! ? -1 : 1)),
257265
el.hasAttribute("data-var-src") ? null : unproxiedSrc(el),
258266
el.innerHTML,
259267
]);

0 commit comments

Comments
 (0)