fix(cli): media-treatment says which media the file has when the selector matches none (#6958)

This commit is contained in:
Miguel Ángel
2026-11-01 22:22:18 +00:00
committed by GitHub
parent 5c5794287b
commit 465128d002
2 changed files with 33 additions and 1 deletions
@@ -222,6 +222,24 @@ describe("applyMediaTreatmentToHtml", () => {
expect(result.html).toContain("data-color-grading=");
});
it("says what media the file has when the selector matches none", () => {
expect(() => applyMediaTreatmentToHtml(VIDEO, { selector: "#prepared", grading: {} })).toThrow(
"Selector did not match: #prepared. Media in the file: video#hero.",
);
expect(() =>
applyMediaTreatmentToHtml(SEGMENTS, { selector: "#prepared", grading: {} }),
).toThrow('video[src="a.mp4"], video[src="b.mp4"].');
});
it("says the file has no media yet when preparation added none", () => {
expect(() =>
applyMediaTreatmentToHtml("<!doctype html><html><body></body></html>", {
selector: "#prepared",
grading: {},
}),
).toThrow("Selector did not match: #prepared. The file has no <video>, <img> or <canvas> yet.");
});
it("treats every video a selector matches alike", () => {
const result = applyMediaTreatmentToHtml(SEGMENTS, {
selector: "video",
+15 -1
View File
@@ -460,6 +460,18 @@ function queryIncludingTemplates(root: Document | Element, selector: string): El
return [];
}
function describeMediaInDocument(document: Document): string {
const media = queryIncludingTemplates(document, "video, img, canvas");
if (media.length === 0) return "The file has no <video>, <img> or <canvas> yet.";
const shown = media.slice(0, 5).map((element) => {
const tag = element.tagName.toLowerCase();
const id = element.getAttribute("id");
return id ? `${tag}#${id}` : `${tag}[src="${element.getAttribute("src") ?? ""}"]`;
});
const more = media.length > shown.length ? ` and ${media.length - shown.length} more` : "";
return `Media in the file: ${shown.join(", ")}${more}.`;
}
type MediaTarget = { element: Element; selectorIndex: number; tag: GradingSourceTag };
function selectMediaElements(
@@ -473,7 +485,9 @@ function selectMediaElements(
} catch {
throw new Error(`Invalid selector: ${selector}`);
}
if (matches.length === 0) throw new Error(`Selector did not match: ${selector}`);
if (matches.length === 0) {
throw new Error(`Selector did not match: ${selector}. ${describeMediaInDocument(document)}`);
}
if (selectorIndex !== undefined && !matches[selectorIndex]) {
throw new Error(`--selector-index ${selectorIndex} is outside ${matches.length} matches`);
}