commit 375014099dda1d1f00a4e6b49b293a9c9fc90d50
parent 82dcb117f51e30fa337a8081e43057b237d07981
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Thu, 12 Nov 2026 08:55:40 -0500
vtt: a short-form cue line never takes a line the old parser read -- its body stops at the next cue line of either form, it must not end before it starts, its hours are two to four digits, and a stray \r before its end is allowed
Review 2 of lane/local-media, four should-fixes, a test each:
- A short-form cue's body ran to the blank line, so a hh:mm:ss.mmm cue line
under it became text ("01:00.000 --> 01:02.000\n00:00:05.000 --> ...\nkept?"
lost the cue the old parser read). The body now stops at a line
CUE_TIMING_RE matches, or at another short-form cue line; a short cue left
with no body is dropped, as a bodiless hh:mm:ss.mmm cue always was. A long
cue's body is unchanged.
- "01:00.000 --> 00:01.000" (end before start) is not a cue line.
- Hours are \d{2,4}: "99999999999999999999:00:01.000" is not a cue line,
"100:00:01.000" and "1000:00:01.000" still are (replaces the finite check).
- A line ending in a stray \r (a \r\r\n file) parses as the long form does.
The reviewer's long-form equivalence fuzz (200,000 documents) against this
parser: 0 cue differences on documents with no short-form cue line.
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat:
2 files changed, 60 insertions(+), 10 deletions(-)
diff --git a/common/lib/vtt.test.ts b/common/lib/vtt.test.ts
@@ -215,3 +215,39 @@ test("a short-form cue line in a body is that cue's text, and one on its own is
{ start: 5, end: 6, text: "Second" },
]);
});
+
+// Review 2 should-fixes: a short-form cue line never takes a line the old
+// parser read, and is held to the shape of a real cue line.
+test("a short-form cue's body stops at a hh:mm:ss.mmm cue line, which is read as the old parser read it", () => {
+ const src = "WEBVTT\n\n01:00.000 --> 01:02.000\n00:00:05.000 --> 00:00:06.000\nkept?\n";
+ // The short cue is left with no body and dropped, as a bodiless long cue is.
+ assert.deepEqual(parseVtt(src), [{ start: 5, end: 6, text: "kept?" }]);
+ assert.deepEqual(parseVtt("WEBVTT\n\n00:00:01.000 --> 00:00:02.000\n\nA\n"), []);
+});
+test("a short-form cue's body stops at another short-form cue line", () => {
+ assert.deepEqual(parseVtt("WEBVTT\n\n00:01.000 --> 00:02.000\nOne\n00:03.000 --> 00:04.000\nTwo\n"), [
+ { start: 1, end: 2, text: "One" },
+ { start: 3, end: 4, text: "Two" },
+ ]);
+});
+test("a short-form line that ends before it starts is not a cue line", () => {
+ assert.deepEqual(parseVtt("WEBVTT\n\n01:00.000 --> 00:01.000\nBackwards\n"), []);
+ // Nor does it end a short cue's body: it is that cue's text.
+ assert.deepEqual(parseVtt("WEBVTT\n\n00:01.000 --> 00:02.000\nOne\n01:00.000 --> 00:01.000\n"),
+ [{ start: 1, end: 2, text: "One 01:00.000 --> 00:01.000" }]);
+});
+test("short-form hours are two to four digits", () => {
+ assert.deepEqual(parseVtt("WEBVTT\n\n99999999999999999999:00:01.000 --> 99999999999999999999:00:02.000\nToo long\n"), []);
+ assert.deepEqual(parseVtt("WEBVTT\n\n12345:00:01.000 --> 12345:00:02.000\nFive\n"), []);
+ assert.deepEqual(parseVtt("WEBVTT\n\n1000:00:01.000 --> 1000:00:02.000\nFour\n"),
+ [{ start: 3600000 + 1, end: 3600000 + 2, text: "Four" }]);
+ assert.deepEqual(parseVtt("WEBVTT\n\n100:00:01.000 --> 100:00:02.000\nThree\n"),
+ [{ start: 360001, end: 360002, text: "Three" }]);
+});
+test("a short-form line ending in a stray \\r (a \\r\\r\\n file) reads like a hh:mm:ss.mmm one", () => {
+ const short = "WEBVTT\r\r\n\r\r\n00:01.000 --> 00:02.000\r\r\nHi\r\r\n";
+ const long = "WEBVTT\r\r\n\r\r\n00:00:01.000 --> 00:00:02.000\r\r\nHi\r\r\n";
+ assert.deepEqual(parseVtt(short), [{ start: 1, end: 2, text: "Hi" }]);
+ assert.deepEqual(parseVtt(short), parseVtt(long));
+ assert.deepEqual(parseVtt("WEBVTT\n\n00:01.000 --> 00:02.000 align:start\r\nHi\n"), [{ start: 1, end: 2, text: "Hi" }]);
+});
diff --git a/common/lib/vtt.ts b/common/lib/vtt.ts
@@ -12,12 +12,14 @@ const CUE_TIMING_RE =
// files write). Tried only on a line CUE_TIMING_RE did not match, so it can add
// cues to a document and never change one: it is strict where the old line is
// lenient (anchored, minutes and seconds 00–59, exactly three millisecond
-// digits), either side may carry hours, and hours may exceed two digits.
-const SHORT_TIMESTAMP = "(?:\\d{2,}:)?[0-5]\\d:[0-5]\\d\\.\\d{3}";
+// digits, end not before start), either side may carry hours, of two to four
+// digits. A stray \r before the line's end (a \r\r\n file) is allowed, as
+// the old line, unanchored, always allowed it.
+const SHORT_TIMESTAMP = "(?:\\d{2,4}:)?[0-5]\\d:[0-5]\\d\\.\\d{3}";
const SHORT_TIMING_TAG_RE = new RegExp("<" + SHORT_TIMESTAMP + ">");
const SHORT_TIMING_TAG_RE_G = new RegExp("<" + SHORT_TIMESTAMP + ">", "g");
const SHORT_CUE_TIMING_RE = new RegExp(
- "^[ \\t]*(" + SHORT_TIMESTAMP + ")[ \\t]+-->[ \\t]+(" + SHORT_TIMESTAMP + ")(?:[ \\t].*)?$",
+ "^[ \\t]*(" + SHORT_TIMESTAMP + ")[ \\t]+-->[ \\t]+(" + SHORT_TIMESTAMP + ")(?:[ \\t].*)?[ \\t\\r]*$",
);
// A rolling track written with short-form word tags (<mm:ss.mmm>). It counts
@@ -51,6 +53,16 @@ function parseShortTimestamp(ts: string): number {
return +h * 3600 + +mm * 60 + +s + +ms / 1000;
}
+// A short-form cue line's times, or null when the line is not one: it must
+// match SHORT_CUE_TIMING_RE and not end before it starts.
+function shortCueTiming(line: string): { start: number; end: number } | null {
+ const m = line.match(SHORT_CUE_TIMING_RE);
+ if (!m) return null;
+ const start = parseShortTimestamp(m[1]);
+ const end = parseShortTimestamp(m[2]);
+ return Number.isFinite(start) && Number.isFinite(end) && end >= start ? { start, end } : null;
+}
+
function decodeEntities(s: string): string {
return s
.replace(/ /g, " ")
@@ -102,19 +114,21 @@ export function parseVtt(src: string): Cue[] {
while (i < lines.length) {
const line = lines[i];
const m = line.match(CUE_TIMING_RE);
- const short = m ? null : line.match(SHORT_CUE_TIMING_RE);
+ const short = m ? null : shortCueTiming(line);
if (!m && !short) {
i++;
continue;
}
- const start = m ? parseTimestamp(m[1]) : parseShortTimestamp(short![1]);
- const end = m ? parseTimestamp(m[2]) : parseShortTimestamp(short![2]);
+ const start = m ? parseTimestamp(m[1]) : short!.start;
+ const end = m ? parseTimestamp(m[2]) : short!.end;
i++;
- // A short-form stamp's hours are unbounded digits: one too long to be a
- // number is not a cue line, so no Infinity reaches cue JSON or the index.
- if (!m && !(Number.isFinite(start) && Number.isFinite(end))) continue;
+ // A hh:mm:ss.mmm cue's body runs to the blank line, as it always did. A
+ // short-form cue's body also stops at the next cue line of either form,
+ // so a line the old parser read as a cue is still read as one. A short
+ // cue left with no body is dropped, as a bodiless hh:mm:ss.mmm cue is.
const body: string[] = [];
- while (i < lines.length && lines[i].trim() !== "") {
+ while (i < lines.length && lines[i].trim() !== ""
+ && (m || !(CUE_TIMING_RE.test(lines[i]) || shortCueTiming(lines[i])))) {
body.push(lines[i]);
i++;
}