Skip to content

Commit 0d5d3f3

Browse files
xuanruliclaude
andauthored
feat(lint): flag video/img src pointing at an audio file (#3741)
* feat(lint): flag video/img src pointing at an audio file Co-Authored-By: Claude Opus 5 <[email protected]> * refactor(lint): drop m4a from the audio src kinds Co-Authored-By: Claude Opus 5 <[email protected]> --------- Co-authored-by: Claude Opus 5 <[email protected]>
1 parent b0ac581 commit 0d5d3f3

2 files changed

Lines changed: 74 additions & 7 deletions

File tree

‎packages/lint/src/rules/media.test.ts‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,62 @@ describe("media_src_kind_mismatch", () => {
853853
expect(result.findings.find((f) => f.code === "media_src_kind_mismatch")).toBeUndefined();
854854
});
855855

856+
it("errors when <img> src is an audio file", async () => {
857+
const html = `
858+
<html><body>
859+
<div id="root" data-composition-id="c1" data-width="1920" data-height="1080">
860+
<img id="logo" src="https://cdn.example.com/brand-assets/music/theme.mp3?sig=abc">
861+
</div>
862+
<script>window.__timelines = {};</script>
863+
</body></html>`;
864+
const result = await lintHyperframeHtml(html);
865+
const finding = result.findings.find((f) => f.code === "media_src_kind_mismatch");
866+
expect(finding?.severity).toBe("error");
867+
expect(finding?.elementId).toBe("logo");
868+
expect(finding?.message).toContain("an audio file");
869+
});
870+
871+
it("errors when <video> src is an audio file", async () => {
872+
const html = `
873+
<html><body>
874+
<div id="root" data-composition-id="c1" data-width="1920" data-height="1080">
875+
<video id="bg" src="theme.wav" data-start="0" data-duration="5" muted></video>
876+
</div>
877+
<script>window.__timelines = {};</script>
878+
</body></html>`;
879+
const result = await lintHyperframeHtml(html);
880+
const finding = result.findings.find((f) => f.code === "media_src_kind_mismatch");
881+
expect(finding?.severity).toBe("error");
882+
expect(finding?.elementId).toBe("bg");
883+
});
884+
885+
it("does not flag <audio> pointing at a video container", async () => {
886+
const html = `
887+
<html><body>
888+
<div id="root" data-composition-id="c1" data-width="1920" data-height="1080">
889+
<audio id="sfx" src="whoosh.mp4" data-start="0"></audio>
890+
</div>
891+
<script>window.__timelines = {};</script>
892+
</body></html>`;
893+
const result = await lintHyperframeHtml(html);
894+
expect(result.findings.find((f) => f.code === "media_src_kind_mismatch")).toBeUndefined();
895+
});
896+
897+
it("does not flag .ogg or .m4a, whose containers can carry video too", async () => {
898+
const html = `
899+
<html><body>
900+
<div id="root" data-composition-id="c1" data-width="1920" data-height="1080">
901+
<video id="v1" src="clip.ogg" data-start="0" data-duration="5" muted></video>
902+
<video id="v2" src="clip.m4a" data-start="0" data-duration="5" muted></video>
903+
<img id="i1" src="clip.ogg" data-start="0" data-duration="5" />
904+
<img id="i2" src="clip.m4a" data-start="0" data-duration="5" />
905+
</div>
906+
<script>window.__timelines = {};</script>
907+
</body></html>`;
908+
const result = await lintHyperframeHtml(html);
909+
expect(result.findings.find((f) => f.code === "media_src_kind_mismatch")).toBeUndefined();
910+
});
911+
856912
it("does not flag extensionless or audio src", async () => {
857913
const html = `
858914
<html><body>

‎packages/lint/src/rules/media.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,17 @@ const VIDEO_SRC_EXT = new Set([
6767
"mpeg",
6868
]);
6969

70-
function srcKind(src: string): "image" | "video" | null {
70+
const AUDIO_SRC_EXT = new Set(["mp3", "wav", "aac", "flac", "opus", "aiff", "wma"]);
71+
72+
type SrcKind = "image" | "video" | "audio";
73+
74+
const SRC_KIND_NOUN: Record<SrcKind, string> = {
75+
image: "an image",
76+
video: "a video",
77+
audio: "an audio file",
78+
};
79+
80+
function srcKind(src: string): SrcKind | null {
7181
const stripped = src.trim();
7282
if (!stripped) return null;
7383
const lower = stripped.toLowerCase();
@@ -76,6 +86,7 @@ function srcKind(src: string): "image" | "video" | null {
7686
if (!mime) return null;
7787
if (mime.startsWith("image/")) return "image";
7888
if (mime.startsWith("video/")) return "video";
89+
if (mime.startsWith("audio/")) return "audio";
7990
return null;
8091
}
8192
if (lower.startsWith("blob:")) return null;
@@ -95,6 +106,7 @@ function srcKind(src: string): "image" | "video" | null {
95106
const ext = base.slice(dot + 1).toLowerCase();
96107
if (IMAGE_SRC_EXT.has(ext)) return "image";
97108
if (VIDEO_SRC_EXT.has(ext)) return "video";
109+
if (AUDIO_SRC_EXT.has(ext)) return "audio";
98110
return null;
99111
}
100112

@@ -106,19 +118,18 @@ function findMediaSrcKindMismatchFindings(ctx: LintContext): HyperframeLintFindi
106118
if (!src) continue;
107119
const kind = srcKind(src);
108120
if (kind === null) continue;
109-
if (tag.name === "video" && kind !== "image") continue;
110-
if (tag.name === "img" && kind !== "video") continue;
111-
const elementId = readAttr(tag.raw, "id") || undefined;
112121
const expected = tag.name === "video" ? "video" : "image";
122+
if (kind === expected) continue;
123+
const elementId = readAttr(tag.raw, "id") || undefined;
113124
findings.push({
114125
code: "media_src_kind_mismatch",
115126
severity: "error",
116-
message: `<${tag.name}${elementId ? ` id="${elementId}"` : ""}> src is a ${kind}, not a ${expected}. The producer fail-closes when the tag and file kind disagree.`,
127+
message: `<${tag.name}${elementId ? ` id="${elementId}"` : ""}> src is ${SRC_KIND_NOUN[kind]}, not ${SRC_KIND_NOUN[expected]}. The producer fail-closes when the tag and file kind disagree.`,
117128
elementId,
118129
fixHint:
119130
tag.name === "video"
120-
? "Use <img> for a still, or point <video> at a video URL (mp4/webm/mov/…)."
121-
: "Use <video> for a video URL, or point <img> at a still (png/jpg/webp/…).",
131+
? "Use <img> for a still, <audio> for sound, or point <video> at a video URL (mp4/webm/mov/…)."
132+
: "Use <video> for a video URL, <audio> for sound, or point <img> at a still (png/jpg/webp/…).",
122133
snippet: truncateSnippet(tag.raw),
123134
});
124135
}

0 commit comments

Comments
 (0)