Release Subtitle Tools 0.2.0
Verify / verify (push) Canceled after 0s

This commit is contained in:
2026-09-02 08:44:49 +02:00
parent b92793eb82
commit 88c2bcd0ff
36 changed files with 1354 additions and 182 deletions
+47
View File
@@ -2,8 +2,14 @@ import { describe, expect, it } from "vitest";
import {
compareDocuments,
convertFrameRate,
insertCue,
mergeCueWithNext,
parseSubtitle,
removeCue,
serializeSubtitle,
snapCuesToFrames,
splitCue,
transformCueText,
transformTiming,
validateDocument,
} from "../../src/subtitle/model";
@@ -85,6 +91,47 @@ describe("subtitle model", () => {
]);
});
it("imports and exports a bounded flat TTML profile", () => {
const source = `<?xml version="1.0"?><tt xmlns="http://www.w3.org/ns/ttml" xmlns:ttp="http://www.w3.org/ns/ttml#parameter" ttp:frameRate="25"><body><div><p xml:id="one" begin="00:00:01.000" end="00:00:02.500" style="caption">Hello<br/>world</p><p begin="75f" dur="1s">Frames</p></div></body></tt>`;
const document = parseSubtitle(source);
expect(document.format).toBe("ttml");
expect(document.cues).toMatchObject([
{ id: "one", startMs: 1000, endMs: 2500, text: "Hello\nworld" },
{ startMs: 3000, endMs: 4000, text: "Frames" },
]);
const exported = serializeSubtitle(document, "ttml");
expect(exported).toContain('xml:id="one"');
expect(exported).toContain("Hello<br/>world");
expect(parseSubtitle(exported).cues).toHaveLength(2);
expect(() =>
parseSubtitle(
'<!DOCTYPE tt [<!ENTITY x SYSTEM "file:///etc/passwd">]><tt/>',
"ttml",
),
).toThrow(/entities/iu);
});
it("edits cue structure, text and frame timing without mutating input", () => {
const original = parseSubtitle(SRT);
const inserted = insertCue(original, 1, {
startMs: 2600,
endMs: 3000,
text: " New ",
});
expect(original.cues).toHaveLength(2);
expect(inserted.cues).toHaveLength(3);
const cleaned = transformCueText(inserted, "trim", [1]);
expect(cleaned.cues[1]?.text).toBe("New");
const split = splitCue(cleaned, 0, 1500);
expect(split.cues).toHaveLength(4);
const merged = mergeCueWithNext(split, 0);
expect(merged.cues).toHaveLength(3);
const removed = removeCue(merged, 1);
expect(removed.cues).toHaveLength(2);
const snapped = snapCuesToFrames(removed, 25);
expect(snapped.cues.every((cue) => cue.startMs % 40 === 0)).toBe(true);
});
it("rejects pathological input bounds", () => {
expect(() => parseSubtitle("x".repeat(4_000_001))).toThrow(/4,000,000/u);
expect(() =>