Skip to content

Commit 44e5a47

Browse files
fix(cli): media-treatment applies one treatment to every matched image or video (#5443)
* fix(cli): media-treatment applies one treatment to every matched image or video * fix(cli): media-treatment reports its action from every target and documents the per-element merge * fix(cli): media-treatment patches each match by its stable id so one write cannot redirect another * fix(cli): media-treatment sets every match in one parse instead of re-finding elements * fix(cli): media-treatment refuses media that is not an HTML element, as before * chore(skills): refresh the media-use manifest hash for the treatment reference edit
1 parent 9e9b613 commit 44e5a47

6 files changed

Lines changed: 326 additions & 58 deletions

File tree

‎docs/packages/cli.mdx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ where `u2net_human_seg` falls down, and free alternatives.
429429

430430
### `media-treatment`
431431

432-
Read or edit the colour and effect treatment stored on one image or video.
432+
Read or edit the colour and effect treatment stored on images and videos.
433433

434434
```bash
435435
# Find out what exists, then look at one family or effect
@@ -454,16 +454,16 @@ npx hyperframes media-treatment --selector '#hero' --clear
454454
| `--all` | Print the exhaustive capability catalog |
455455
| `--project <dir>` | Project directory (default: current directory) |
456456
| `--file <path>` | Composition file (default `index.html`) |
457-
| `--selector <css>` | CSS selector for one `<img>` or `<video>` |
458-
| `--selector-index <n>` | Zero-based match index when the selector is not unique |
457+
| `--selector <css>` | CSS selector; every matched `<img>` or `<video>` gets the patch |
458+
| `--selector-index <n>` | Zero-based match index to patch only one of several matches |
459459
| `--grading <json>` | Validated colour-grading patch |
460460
| `--apply` | Apply the patch |
461461
| `--analyze` | Measure the selected local media and suggest a bounded correction |
462462
| `--clear` | Remove colour grading from the target |
463463
| `--dry-run` | Validate and report without writing |
464464
| `--json` | Agent-readable JSON |
465465

466-
`--dry-run` accepts `--grading` without `--apply` and writes nothing. JSON includes `attribute`, the exact normalized `value` that apply would write (or `null` to remove it), and `lint`, the color-grading lint verdict and findings for the proposed HTML.
466+
`--dry-run` accepts `--grading` without `--apply` and writes nothing. JSON includes `count`, `attribute`, the exact normalized `value` that apply would write (or `null` to remove it), and `lint`, the color-grading lint verdict and findings for the proposed HTML. When the selector matches several elements, each gets the patch merged into its own current grading, and JSON lists them under `targets` (each with its own `value`) instead of a single `value`; run `--clear` first for an identical look. Matches come from the main document or, when it has none, from the first `<template>` that has any, the same scope rule as Studio's source edits.
467467

468468
The command writes `data-color-grading` — the same contract Studio, preview, and
469469
render all read, so the two can never disagree. It does not recognise subjects

‎packages/cli/src/commands/media-treatment.test.ts‎

Lines changed: 228 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ import {
1818
import { CliRuntimeError } from "../utils/commandResult.js";
1919

2020
const VIDEO = `<!doctype html><html><body><video id="hero" src="hero.mp4"></video></body></html>`;
21+
const SEGMENTS = `<!doctype html><html><body><video class="seg" src="a.mp4"></video><video class="seg" src="b.mp4"></video></body></html>`;
22+
23+
function gradingsOf(html: string): Array<string | null> {
24+
return Array.from(parseHTML(html).document.querySelectorAll("video"), (video) =>
25+
video.getAttribute("data-color-grading"),
26+
);
27+
}
2128

2229
describe("applyMediaTreatmentToHtml", () => {
2330
it("provides a concise first-hop overview of the complete treatment surface", () => {
@@ -176,6 +183,154 @@ describe("applyMediaTreatmentToHtml", () => {
176183
expect(result.html).toContain("data-color-grading=");
177184
});
178185

186+
it("treats every video a selector matches alike", () => {
187+
const result = applyMediaTreatmentToHtml(SEGMENTS, {
188+
selector: "video",
189+
grading: { preset: "warm-daylight" },
190+
});
191+
192+
const [first, second] = gradingsOf(result.html);
193+
expect(result.targets).toHaveLength(2);
194+
expect(result.html).toContain("data-hf-id=");
195+
expect(first).toContain('"preset":"warm-daylight"');
196+
expect(second).toBe(first);
197+
});
198+
199+
it("merges the patch into each match's own grading", () => {
200+
const warm = applyMediaTreatmentToHtml(SEGMENTS, {
201+
selector: "video",
202+
selectorIndex: 1,
203+
grading: { preset: "warm-daylight" },
204+
}).html;
205+
206+
const result = applyMediaTreatmentToHtml(warm, {
207+
selector: "video",
208+
grading: { intensity: 0.5 },
209+
});
210+
211+
expect(result.targets.map(({ before }) => before)).toEqual([
212+
null,
213+
expect.objectContaining({ preset: "warm-daylight" }),
214+
]);
215+
expect(result.targets[1]?.value).toContain('"preset":"warm-daylight"');
216+
expect(result.targets[0]?.value ?? "").not.toContain("warm-daylight");
217+
});
218+
219+
it("clears every match even when the selector depends on the cleared attribute", () => {
220+
const graded = applyMediaTreatmentToHtml(SEGMENTS, {
221+
selector: "video",
222+
grading: { preset: "warm-daylight" },
223+
}).html;
224+
225+
const result = applyMediaTreatmentToHtml(graded, {
226+
selector: "video[data-color-grading]",
227+
clear: true,
228+
});
229+
230+
expect(gradingsOf(result.html)).toEqual([null, null]);
231+
});
232+
233+
it("patches the matched element even when a write changes which elements a selector matches", () => {
234+
const four = SEGMENTS.replace(
235+
"</body>",
236+
'<video src="c.mp4"></video><video src="d.mp4"></video></body>',
237+
);
238+
const graded = applyMediaTreatmentToHtml(four, {
239+
selector: "video",
240+
grading: { preset: "warm-daylight" },
241+
}).html;
242+
243+
const result = applyMediaTreatmentToHtml(graded, {
244+
selector: "video:has(+ video[data-color-grading])",
245+
clear: true,
246+
});
247+
248+
expect(result.targets).toHaveLength(3);
249+
expect(gradingsOf(result.html).map((value) => value === null)).toEqual([
250+
true,
251+
true,
252+
true,
253+
false,
254+
]);
255+
});
256+
257+
it("refuses a video inside svg instead of writing a tag that swallows what follows it", () => {
258+
const html = `<svg><foreignObject><video src="a.mp4"></video><p>caption</p></foreignObject></svg>`;
259+
260+
expect(() =>
261+
applyMediaTreatmentToHtml(html, { selector: "video", grading: { preset: "warm-daylight" } }),
262+
).toThrow("inside <svg>");
263+
});
264+
265+
it("patches the matched element when ids in the file are duplicated", () => {
266+
const html = `<img data-hf-id="hf-x1" src="a.png"><video data-hf-id="hf-x1" src="b.mp4"></video>`;
267+
268+
const result = applyMediaTreatmentToHtml(html, {
269+
selector: "video",
270+
grading: { preset: "warm-daylight" },
271+
});
272+
273+
const { document } = parseHTML(`<!doctype html><html><body>${result.html}</body></html>`);
274+
expect(document.querySelector("img")?.getAttribute("data-color-grading")).toBeNull();
275+
expect(document.querySelector("video")?.getAttribute("data-color-grading")).toContain(
276+
"warm-daylight",
277+
);
278+
});
279+
280+
it("clears exactly the matches inside a template when a write changes the match set", () => {
281+
const videos = '<video src="a.mp4"></video>'.repeat(4);
282+
const graded = applyMediaTreatmentToHtml(`<template>${videos}</template>`, {
283+
selector: "video",
284+
grading: { preset: "warm-daylight" },
285+
}).html;
286+
287+
const result = applyMediaTreatmentToHtml(graded, {
288+
selector: "video:has(+ video[data-color-grading])",
289+
clear: true,
290+
});
291+
292+
const cleared = (result.html.match(/<video(?![^>]*data-color-grading)/g) ?? []).length;
293+
expect(result.targets).toHaveLength(3);
294+
expect(cleared).toBe(3);
295+
});
296+
297+
it("writes a later match's change when the first match is already graded", () => {
298+
const firstGraded = applyMediaTreatmentToHtml(SEGMENTS, {
299+
selector: "video",
300+
selectorIndex: 0,
301+
grading: { preset: "warm-daylight" },
302+
}).html;
303+
304+
const result = applyMediaTreatmentToHtml(firstGraded, {
305+
selector: "video",
306+
grading: { preset: "warm-daylight" },
307+
});
308+
309+
expect(result.targets.map(({ changed }) => changed)).toEqual([false, true]);
310+
expect(result.changed).toBe(true);
311+
expect(gradingsOf(result.html)[1]).toContain("warm-daylight");
312+
});
313+
314+
it("treats only the --selector-index match when one is given", () => {
315+
const result = applyMediaTreatmentToHtml(SEGMENTS, {
316+
selector: "video",
317+
selectorIndex: 1,
318+
grading: { preset: "warm-daylight" },
319+
});
320+
321+
const [first, second] = gradingsOf(result.html);
322+
expect(first).toBeNull();
323+
expect(second).toContain('"preset":"warm-daylight"');
324+
});
325+
326+
it("rejects a selector that also matches something other than an image or video", () => {
327+
const html = SEGMENTS.replace("</body>", '<div class="seg"></div></body>');
328+
329+
expect(() =>
330+
applyMediaTreatmentToHtml(html, { selector: ".seg", grading: { preset: "warm-daylight" } }),
331+
).toThrow("selector matched <div>");
332+
});
333+
179334
it("normalizes and persists advanced grading on real media", () => {
180335
const result = applyMediaTreatmentToHtml(VIDEO, {
181336
selector: "#hero",
@@ -388,11 +543,13 @@ describe("applyMediaTreatmentToHtml", () => {
388543
).toThrow(/Cannot merge.*unresolved whole-grade variable/);
389544
});
390545

391-
it("requires an unambiguous media target", () => {
546+
it("grades every matched image unless --selector-index picks one", () => {
392547
const source = `<img class="media" src="a.png"><img class="media" src="b.png">`;
393-
expect(() =>
394-
applyMediaTreatmentToHtml(source, { selector: ".media", grading: { preset: "neutral" } }),
395-
).toThrow(/matched 2 elements/);
548+
const all = applyMediaTreatmentToHtml(source, {
549+
selector: ".media",
550+
grading: { preset: "warm-daylight" },
551+
});
552+
expect((all.html.match(/data-color-grading/g) ?? []).length).toBe(2);
396553

397554
const result = applyMediaTreatmentToHtml(source, {
398555
selector: ".media",
@@ -470,6 +627,73 @@ describe("applyMediaTreatmentToHtml", () => {
470627
}
471628
});
472629

630+
it("reports how many elements an --apply treated", async () => {
631+
const project = mkdtempSync(join(tmpdir(), "hf-media-all-"));
632+
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
633+
writeFileSync(join(project, "index.html"), SEGMENTS);
634+
try {
635+
await runCommand(mediaTreatmentCommand, {
636+
rawArgs: [
637+
"--project",
638+
project,
639+
"--selector",
640+
"video",
641+
"--grading",
642+
'{"preset":"warm-daylight"}',
643+
"--apply",
644+
"--json",
645+
],
646+
});
647+
648+
const applied = JSON.parse(String(log.mock.calls.at(-1)?.[0]));
649+
expect(applied).toMatchObject({ ok: true, count: 2, changed: true });
650+
expect(
651+
applied.targets.map(({ selectorIndex }: { selectorIndex: number }) => selectorIndex),
652+
).toEqual([0, 1]);
653+
expect(gradingsOf(readFileSync(join(project, "index.html"), "utf8"))[1]).toContain(
654+
"warm-daylight",
655+
);
656+
} finally {
657+
log.mockRestore();
658+
rmSync(project, { recursive: true, force: true });
659+
}
660+
});
661+
662+
it("reports apply when any match keeps a grading after the patch", async () => {
663+
const project = mkdtempSync(join(tmpdir(), "hf-media-action-"));
664+
const html = applyMediaTreatmentToHtml(SEGMENTS, {
665+
selector: "video",
666+
selectorIndex: 1,
667+
grading: { wheels: { shadows: { hue: 205, amount: 0.08, level: 0.02 } } },
668+
}).html;
669+
const log = vi.spyOn(console, "log").mockImplementation(() => undefined);
670+
writeFileSync(join(project, "index.html"), html);
671+
try {
672+
await runCommand(mediaTreatmentCommand, {
673+
rawArgs: [
674+
"--project",
675+
project,
676+
"--selector",
677+
"video",
678+
"--grading",
679+
'{"preset":"neutral"}',
680+
"--apply",
681+
"--json",
682+
],
683+
});
684+
685+
const applied = JSON.parse(String(log.mock.calls.at(-1)?.[0]));
686+
expect(applied.targets.map(({ value }: { value: string | null }) => value === null)).toEqual([
687+
true,
688+
false,
689+
]);
690+
expect(applied.action).toBe("apply");
691+
} finally {
692+
log.mockRestore();
693+
rmSync(project, { recursive: true, force: true });
694+
}
695+
});
696+
473697
it("requires --apply for --grading while keeping --clear explicit", async () => {
474698
const project = mkdtempSync(join(tmpdir(), "hf-media-treatment-"));
475699
const file = join(project, "index.html");

0 commit comments

Comments
 (0)