Skip to content

Lightpanda: the headless browser designed for AI and automation

webapi: media / webvtt improvments.

Karl Seguin4af7bf630592
diff --git a/src/browser/Frame.zig b/src/browser/Frame.zig
index f8c7308bc..c8f7fe271 100644
--- a/src/browser/Frame.zig
+++ b/src/browser/Frame.zig
@@ -3299,8 +3299,10 @@ fn _insertNodeRelative(self: *Frame, comptime from_parser: bool, parent: *Node,
if (child.is(Element.Html.Image)) |img| {
// noop if it didn't actually change
try img.sourceChanged(self);
- } else {
- try Element.Html.Picture.childInserted(parent, child, self);
+ } else if (child.is(Element.Html.Source)) |source| {
+ try Element.Html.Picture.sourceInserted(parent, source, self);
+ } else if (child.is(Element.Html.Track)) |track| {
+ try track.insertedInto(parent, self);
}
// Update live ranges for insertion (DOM spec insert step 6).
diff --git a/src/browser/frame/node_factory.zig b/src/browser/frame/node_factory.zig
index 4b1fe08cc..b0c45aa75 100644
--- a/src/browser/frame/node_factory.zig
+++ b/src/browser/frame/node_factory.zig
@@ -548,7 +548,7 @@ pub fn createElementNS(document: *const Node.Document, namespace: Element.Namesp
Element.Html.Track,
namespace,
attribute_iterator,
- .{ ._kind = comptime .wrap("subtitles"), ._ready_state = .none },
+ .{},
),
else => {},
},
diff --git a/src/browser/js/bridge.zig b/src/browser/js/bridge.zig
index 7690230e2..27fc7054d 100644
--- a/src/browser/js/bridge.zig
+++ b/src/browser/js/bridge.zig
@@ -1256,8 +1256,12 @@ pub const PageJsApis = flattenTypes(&.{
@import("../webapi/ServiceWorkerContainer.zig"),
@import("../webapi/ServiceWorkerRegistration.zig"),
@import("../webapi/media/MediaError.zig"),
+ @import("../webapi/media/TextTrack.zig"),
@import("../webapi/media/TextTrackCue.zig"),
+ @import("../webapi/media/TextTrackCueList.zig"),
+ @import("../webapi/media/TextTrackList.zig"),
@import("../webapi/media/VTTCue.zig"),
+ @import("../webapi/media/VTTRegion.zig"),
@import("../webapi/animation/Animation.zig"),
@import("../webapi/EventTarget.zig"),
@import("../webapi/Location.zig"),
diff --git a/src/browser/parser/html5ever.zig b/src/browser/parser/html5ever.zig
index 73e23449c..d83dc0a68 100644
--- a/src/browser/parser/html5ever.zig
+++ b/src/browser/parser/html5ever.zig
@@ -224,6 +224,10 @@ pub extern "c" fn xml5ever_parse_document(
allow_declarative_shadow: bool,
) void;
+// 0: no reference starts with `name`, 1: `name` is a prefix of one,
+// 2: `name` is one, its codepoints are in `out` (out[1] == 0 for a single one).
+pub extern "c" fn html5ever_named_character_reference(name: [*]const u8, len: usize, out: *[2]u32) u8;
+
// General encoding api
pub const EncodingInfo = extern struct {
found: u8,
diff --git a/src/browser/tests/media/texttrack.html b/src/browser/tests/media/texttrack.html
new file mode 100644
index 000000000..560ed6ec5
--- /dev/null
+++ b/src/browser/tests/media/texttrack.html
@@ -0,0 +1,221 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+<body></body>
+
+<script id=addTextTrack>
+{
+ const video = document.createElement("video");
+ testing.expectEqual(video.textTracks, video.textTracks);
+ testing.expectEqual(0, video.textTracks.length);
+
+ const track = video.addTextTrack("captions", "English", "en");
+ testing.expectEqual(true, track instanceof TextTrack);
+ testing.expectEqual("captions", track.kind);
+ testing.expectEqual("English", track.label);
+ testing.expectEqual("en", track.language);
+ testing.expectEqual("", track.id);
+ testing.expectEqual("hidden", track.mode);
+ testing.expectEqual(1, video.textTracks.length);
+ testing.expectEqual(track, video.textTracks[0]);
+ testing.expectError("TypeError", () => video.addTextTrack("bogus"));
+
+ track.mode = "nope";
+ testing.expectEqual("hidden", track.mode);
+ track.mode = "disabled";
+ testing.expectEqual(null, track.cues);
+ testing.expectEqual(null, track.activeCues);
+ track.mode = "showing";
+ testing.expectEqual(track.cues, track.cues);
+}
+</script>
+
+<script id=cues>
+{
+ const video = document.createElement("video");
+ const track = video.addTextTrack("subtitles");
+
+ const late = new VTTCue(5, 6, "late");
+ const early = new VTTCue(1, 2, "early");
+ const longer = new VTTCue(1, 4, "longer");
+ early.id = "e";
+ track.addCue(late);
+ track.addCue(early);
+ track.addCue(longer);
+
+ // start time, then end time descending
+ const cues = track.cues;
+ testing.expectEqual(3, cues.length);
+ testing.expectEqual([longer, early, late], [cues[0], cues[1], cues[2]]);
+ testing.expectEqual(undefined, cues[3]);
+ testing.expectEqual(early, cues.getCueById("e"));
+ testing.expectEqual(null, cues.getCueById(""));
+ testing.expectEqual(track, early.track);
+
+ // the list is live, and kept sorted when times change
+ late.startTime = 0;
+ testing.expectEqual(late, cues[0]);
+
+ // cues only become active once playback runs "time marches on"
+ testing.expectEqual(0, track.activeCues.length);
+ testing.expectEqual(undefined, track.activeCues[0]);
+
+ track.removeCue(late);
+ testing.expectEqual(2, cues.length);
+ testing.expectEqual(null, late.track);
+ testing.expectError("NotFoundError", () => track.removeCue(late));
+
+ // moving a cue to another track removes it from the first
+ const other = video.addTextTrack("metadata");
+ other.addCue(early);
+ testing.expectEqual(1, cues.length);
+ testing.expectEqual(other, early.track);
+}
+</script>
+
+<script id=track_element>
+{
+ const video = document.createElement("video");
+ const el = document.createElement("track");
+ el.id = "t1";
+ el.kind = "chapters";
+ el.label = "Chapters";
+ el.srclang = "fr";
+
+ const track = el.track;
+ testing.expectEqual(track, el.track);
+ testing.expectEqual("chapters", track.kind);
+ testing.expectEqual("Chapters", track.label);
+ testing.expectEqual("fr", track.language);
+ testing.expectEqual("t1", track.id);
+ testing.expectEqual("disabled", track.mode);
+ testing.expectEqual(0, el.readyState);
+
+ const added = video.addTextTrack("subtitles");
+ video.appendChild(el);
+ // <track> children first, then addTextTrack() ones
+ testing.expectEqual(2, video.textTracks.length);
+ testing.expectEqual(track, video.textTracks[0]);
+ testing.expectEqual(added, video.textTracks[1]);
+ testing.expectEqual(track, video.textTracks.getTrackById("t1"));
+
+ el.remove();
+ testing.expectEqual(1, video.textTracks.length);
+ testing.expectEqual(0, el.readyState);
+}
+</script>
+
+<script id=load type=module>
+ const state = await testing.async();
+
+ const vtt = "WEBVTT\n\nintro\n00:00.000 --> 00:01.500 align:start line:10%\nHello <b>world</b>\n\n00:00:02.000 --> 00:00:03.000 position:20%,line-left size:50%\nSecond\r\nline\n\nNOTE comment\n\n00:01.000 --> 00:00.500\nbackwards";
+ const url = URL.createObjectURL(new Blob([vtt], { type: "text/vtt" }));
+
+ const video = document.createElement("video");
+ const el = document.createElement("track");
+ el.src = url;
+ el.default = true;
+ video.appendChild(el);
+ document.body.appendChild(video);
+
+ const evt = await new Promise(resolve => {
+ el.onload = () => resolve("load");
+ el.onerror = () => resolve("error");
+ });
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual("load", evt);
+ testing.expectEqual(2, el.readyState);
+ // the default subtitles track is selected
+ testing.expectEqual("showing", el.track.mode);
+
+ const cues = el.track.cues;
+ testing.expectEqual(3, cues.length);
+
+ testing.expectEqual("intro", cues[0].id);
+ testing.expectEqual(0, cues[0].startTime);
+ testing.expectEqual(1.5, cues[0].endTime);
+ testing.expectEqual("start", cues[0].align);
+ testing.expectEqual(10, cues[0].line);
+ testing.expectEqual(false, cues[0].snapToLines);
+ testing.expectEqual("Hello <b>world</b>", cues[0].text);
+
+ testing.expectEqual(1, cues[1].startTime);
+ testing.expectEqual(0.5, cues[1].endTime);
+ testing.expectEqual("backwards", cues[1].text);
+
+ testing.expectEqual(20, cues[2].position);
+ testing.expectEqual("line-left", cues[2].positionAlign);
+ testing.expectEqual(50, cues[2].size);
+ testing.expectEqual("Second\nline", cues[2].text);
+ });
+</script>
+
+<script id=not_webvtt type=module>
+ const state = await testing.async();
+
+ const url = URL.createObjectURL(new Blob(["WEBVTTX\n\n00:00.000 --> 00:01.000\nx"]));
+ const video = document.createElement("video");
+ const el = document.createElement("track");
+ el.src = url;
+ el.track.mode = "hidden";
+ video.appendChild(el);
+
+ const evt = await new Promise(resolve => {
+ el.onload = () => resolve("load");
+ el.onerror = () => resolve("error");
+ });
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual("error", evt);
+ testing.expectEqual(3, el.readyState);
+ });
+</script>
+
+<script id=no_src type=module>
+ const state = await testing.async();
+
+ const video = document.createElement("video");
+ const el = document.createElement("track");
+ el.track.mode = "hidden";
+ video.appendChild(el);
+
+ const evt = await new Promise(resolve => {
+ el.onload = () => resolve("load");
+ el.onerror = () => resolve("error");
+ });
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual("error", evt);
+ });
+</script>
+
+<!-- Regions are in the spec, WPT and Firefox; Chrome doesn't expose them. -->
+<script id=load_regions type=module>
+ const state = await testing.async();
+
+ const vtt = "WEBVTT\n\nREGION\nid:r1\nlines:2\n\nREGION\nid:r1\nlines:4\n\n00:00.000 --> 00:01.000 region:r1\na\n\n00:00.000 --> 00:01.000 region:nope\nb";
+ const url = URL.createObjectURL(new Blob([vtt]));
+ const video = document.createElement("video");
+ const el = document.createElement("track");
+ el.src = url;
+ el.track.mode = "hidden";
+ video.appendChild(el);
+
+ const evt = await new Promise(resolve => {
+ el.onload = () => resolve("load");
+ el.onerror = () => resolve("error");
+ });
+
+ state.resolve();
+ await state.done(() => {
+ testing.expectEqual("load", evt);
+ const cues = el.track.cues;
+ // a later region with the same id replaces the earlier one
+ testing.expectEqual(4, cues[0].region.lines);
+ testing.expectEqual("r1", cues[0].region.id);
+ testing.expectEqual(null, cues[1].region);
+ });
+</script>
diff --git a/src/browser/tests/media/vttcue.html b/src/browser/tests/media/vttcue.html
index ad1d2cd40..b139f69a1 100644
--- a/src/browser/tests/media/vttcue.html
+++ b/src/browser/tests/media/vttcue.html
@@ -69,3 +69,120 @@
testing.expectEqual(true, cue.pauseOnExit);
}
</script>
+
+<script id=enums>
+ {
+ const cue = new VTTCue(0, 1, "test");
+
+ // invalid enum values are ignored
+ for (const invalid of ["start\u0000", "centre", "middle", "auto"]) {
+ cue.align = invalid;
+ cue.vertical = invalid;
+ }
+ testing.expectEqual("center", cue.align);
+ testing.expectEqual("", cue.vertical);
+
+ cue.vertical = "lr";
+ testing.expectEqual("lr", cue.vertical);
+ cue.vertical = "";
+ testing.expectEqual("", cue.vertical);
+ }
+</script>
+
+<!-- lineAlign and positionAlign are in the spec, WPT and Firefox; Chrome doesn't have them. -->
+<script id=line_and_position_align>
+ {
+ const cue = new VTTCue(0, 1, "test");
+ testing.expectEqual("start", cue.lineAlign);
+ testing.expectEqual("auto", cue.positionAlign);
+
+ cue.lineAlign = "end";
+ testing.expectEqual("end", cue.lineAlign);
+ cue.positionAlign = "line-left";
+ testing.expectEqual("line-left", cue.positionAlign);
+
+ for (const invalid of ["start\u0000", "centre", "middle", "auto"]) {
+ cue.lineAlign = invalid;
+ }
+ testing.expectEqual("end", cue.lineAlign);
+ }
+</script>
+
+<script id=ranges>
+ {
+ const cue = new VTTCue(0, 1, "test");
+ testing.expectError("IndexSizeError", () => { cue.position = 101; });
+ testing.expectError("IndexSizeError", () => { cue.size = -1; });
+ testing.expectError("TypeError", () => { cue.line = "middle"; });
+ testing.expectEqual("auto", cue.position);
+ testing.expectEqual(100, cue.size);
+ testing.expectEqual("auto", cue.line);
+
+ cue.line = -5;
+ testing.expectEqual(-5, cue.line);
+ cue.line = "auto";
+ testing.expectEqual("auto", cue.line);
+
+ testing.expectError("TypeError", () => new VTTCue(NaN, 1, "x"));
+ testing.expectError("TypeError", () => new VTTCue(0, NaN, "x"));
+ testing.expectError("TypeError", () => new VTTCue(0, -Infinity, "x"));
+ testing.expectError("TypeError", () => { cue.startTime = Infinity; });
+ }
+</script>
+
+<!-- endTime is an unrestricted double, so +Infinity is allowed (spec, WPT
+ VTTCue/constructor.html, Firefox). Chrome still treats it as a double and throws. -->
+<script id=infinite_end_time>
+ {
+ testing.expectEqual(Infinity, new VTTCue(0, Infinity, "x").endTime);
+ }
+</script>
+
+<!-- Regions are in the spec, WPT and Firefox; Chrome doesn't expose them. -->
+<script id=region>
+ {
+ const cue = new VTTCue(0, 1, "test");
+ testing.expectEqual(null, cue.region);
+ const region = new VTTRegion();
+ cue.region = region;
+ testing.expectEqual(region, cue.region);
+ cue.region = null;
+ testing.expectEqual(null, cue.region);
+ testing.expectError("TypeError", () => { cue.region = {}; });
+ }
+</script>
+
+<script id=getCueAsHTML>
+ {
+ const cue = new VTTCue(0, 1, '<c></c><c.a.b></c><i></i><b></b><u></u><ruby>x<rt></rt></ruby><v></v><v a b></v><v Foo&amp;Bar>text</v><1:00:00.500>&lt;&notit;&#x20;<lang.k en>x</lang><rt>y');
+ const frag = cue.getCueAsHTML();
+ testing.expectEqual(true, frag instanceof DocumentFragment);
+
+ const nodes = Array.from(frag.childNodes);
+ testing.expectEqual(["SPAN", "SPAN", "I", "B", "U", "RUBY", "SPAN", "SPAN", "SPAN", "timestamp", "#text", "SPAN", "#text"],
+ nodes.map(n => n.nodeName));
+
+ testing.expectEqual(0, nodes[0].attributes.length);
+ testing.expectEqual("a b", nodes[1].getAttribute("class"));
+ testing.expectEqual("RT", nodes[5].lastChild.nodeName);
+ testing.expectEqual("", nodes[6].getAttribute("title"));
+ testing.expectEqual("a b", nodes[7].getAttribute("title"));
+ testing.expectEqual("Foo&Bar", nodes[8].getAttribute("title"));
+ testing.expectEqual("text", nodes[8].textContent);
+ testing.expectEqual("01:00:00.500", nodes[9].data);
+ testing.expectEqual("<¬it; ", nodes[10].data);
+ testing.expectEqual("k", nodes[11].getAttribute("class"));
+ testing.expectEqual("en", nodes[11].getAttribute("lang"));
+ // <rt> outside of a <ruby> is ignored
+ testing.expectEqual("y", nodes[12].data);
+ }
+</script>
+
+<!-- The cue text tokenizer collapses whitespace in a start tag's annotation
+ (spec, Firefox); Chrome keeps it as-is. -->
+<script id=annotation_whitespace>
+ {
+ const frag = new VTTCue(0, 1, "<v a \t b >x</v>").getCueAsHTML();
+ testing.expectEqual("a b", frag.firstChild.getAttribute("title"));
+ }
+</script>
diff --git a/src/browser/tests/media/vttregion.html b/src/browser/tests/media/vttregion.html
new file mode 100644
index 000000000..08edb380b
--- /dev/null
+++ b/src/browser/tests/media/vttregion.html
@@ -0,0 +1,60 @@
+<!DOCTYPE html>
+<script src="../testing.js"></script>
+<!-- VTTRegion is in the spec, WPT and Firefox; Chrome doesn't expose it. -->
+
+<script id=defaults>
+{
+ const region = new VTTRegion();
+ testing.expectEqual(true, region instanceof VTTRegion);
+ testing.expectEqual("", region.id);
+ testing.expectEqual(100, region.width);
+ testing.expectEqual(3, region.lines);
+ testing.expectEqual(0, region.regionAnchorX);
+ testing.expectEqual(100, region.regionAnchorY);
+ testing.expectEqual(0, region.viewportAnchorX);
+ testing.expectEqual(100, region.viewportAnchorY);
+ testing.expectEqual("", region.scroll);
+}
+</script>
+
+<script id=percentages>
+{
+ const region = new VTTRegion();
+ region.width = 42.5;
+ testing.expectEqual(42.5, region.width);
+
+ testing.expectError("IndexSizeError", () => { region.width = 101; });
+ testing.expectError("IndexSizeError", () => { region.regionAnchorX = -1; });
+ testing.expectError("TypeError", () => { region.viewportAnchorY = NaN; });
+ testing.expectError("TypeError", () => { region.viewportAnchorX = Infinity; });
+ testing.expectEqual(42.5, region.width);
+ testing.expectEqual(0, region.regionAnchorX);
+ testing.expectEqual(100, region.viewportAnchorY);
+}
+</script>
+
+<!-- Per the current IDL (unsigned long) and WPT VTTRegion/lines.html; Firefox
+ still throws IndexSizeError for negative values, from an older spec. -->
+<script id=lines>
+{
+ const region = new VTTRegion();
+ region.lines = 130;
+ testing.expectEqual(130, region.lines);
+ region.lines = -1;
+ testing.expectEqual(4294967295, region.lines);
+ region.lines = NaN;
+ testing.expectEqual(0, region.lines);
+}
+</script>
+
+<script id=scroll>
+{
+ const region = new VTTRegion();
+ region.scroll = "up";
+ testing.expectEqual("up", region.scroll);
+ region.scroll = "down";
+ testing.expectEqual("up", region.scroll);
+ region.scroll = "";
+ testing.expectEqual("", region.scroll);
+}
+</script>
diff --git a/src/browser/webapi/EventTarget.zig b/src/browser/webapi/EventTarget.zig
index 84640b9af..d773688fb 100644
--- a/src/browser/webapi/EventTarget.zig
+++ b/src/browser/webapi/EventTarget.zig
@@ -53,7 +53,9 @@ const IDBTransaction = @import("storage/idb/IDBTransaction.zig");
const FontFaceSet = @import("css/FontFaceSet.zig");
const MediaQueryList = @import("css/MediaQueryList.zig");
+const TextTrack = @import("media/TextTrack.zig");
const TextTrackCue = @import("media/TextTrackCue.zig");
+const TextTrackList = @import("media/TextTrackList.zig");
const Animation = @import("animation/Animation.zig");
@@ -94,7 +96,9 @@ pub const Type = enum(u8) {
service_worker_container,
service_worker_registration,
shared_worker,
+ text_track,
text_track_cue,
+ text_track_list,
visual_viewport,
websocket,
window,
@@ -131,7 +135,9 @@ pub fn Subtype(comptime tag: Type) type {
.service_worker_container => ServiceWorkerContainer,
.service_worker_registration => ServiceWorkerRegistration,
.shared_worker => SharedWorker,
+ .text_track => TextTrack,
.text_track_cue => TextTrackCue,
+ .text_track_list => TextTrackList,
.visual_viewport => VisualViewport,
.websocket => WebSocket,
.window => Window,
@@ -369,7 +375,9 @@ pub fn format(self: *EventTarget, writer: *std.Io.Writer) !void {
.media_query_list => writer.writeAll("<MediaQueryList>"),
.message_port => writer.writeAll("<MessagePort>"),
.broadcast_channel => writer.writeAll("<BroadcastChannel>"),
+ .text_track => writer.writeAll("<TextTrack>"),
.text_track_cue => writer.writeAll("<TextTrackCue>"),
+ .text_track_list => writer.writeAll("<TextTrackList>"),
.navigation => writer.writeAll("<Navigation>"),
.screen => writer.writeAll("<Screen>"),
.screen_orientation => writer.writeAll("<ScreenOrientation>"),
@@ -413,7 +421,9 @@ pub fn toString(self: *EventTarget) []const u8 {
.service_worker_container => return "[object ServiceWorkerContainer]",
.service_worker_registration => return "[object ServiceWorkerRegistration]",
.shared_worker => return "[object SharedWorker]",
+ .text_track => return "[object TextTrack]",
.text_track_cue => return "[object TextTrackCue]",
+ .text_track_list => return "[object TextTrackList]",
.visual_viewport => return "[object VisualViewport]",
.websocket => return "[object WebSocket]",
.window => return "[object Window]",
diff --git a/src/browser/webapi/element/html/Media.zig b/src/browser/webapi/element/html/Media.zig
index 31acaad81..ab5ef9c74 100644
--- a/src/browser/webapi/element/html/Media.zig
+++ b/src/browser/webapi/element/html/Media.zig
@@ -29,6 +29,8 @@ const Event = @import("../../Event.zig");
const HtmlElement = @import("../Html.zig");
const MediaError = @import("../../media/MediaError.zig");
+const TextTrack = @import("../../media/TextTrack.zig");
+const TextTrackList = @import("../../media/TextTrackList.zig");
pub const Audio = @import("Audio.zig");
pub const Video = @import("Video.zig");
@@ -90,6 +92,10 @@ _playback_rate: f64 = 1.0,
_ready_state: ReadyState = .HAVE_NOTHING,
_network_state: NetworkState = .NETWORK_EMPTY,
_error: ?*MediaError = null,
+_text_track_list: ?*TextTrackList = null,
+// added via addTextTrack. <track> children are found by a normal tree walk
+_added_text_tracks: std.ArrayList(*TextTrack) = .empty,
+_track_selection: enum { none, pending, done } = .none,
pub fn asElement(self: *Media) *Element {
return Factory.protoOf(self).asElement();
@@ -251,6 +257,84 @@ fn setCurrentTime(self: *Media, value: f64) void {
self._current_time = value;
}
+fn getTextTracks(self: *Media, frame: *Frame) !*TextTrackList {
+ if (self._text_track_list == null) {
+ self._text_track_list = try TextTrackList.init(self, frame);
+ }
+ return self._text_track_list.?;
+}
+
+fn addTextTrack(self: *Media, kind_: []const u8, label_: ?[]const u8, language_: ?[]const u8, frame: *Frame) !*TextTrack {
+ const kind = std.meta.stringToEnum(TextTrack.Kind, kind_) orelse return error.TypeError;
+ const track = try TextTrack.initForScript(kind, label_ orelse "", language_ orelse "", frame);
+ try self._added_text_tracks.append(frame.page_arena, track);
+ return track;
+}
+
+pub fn queueAutomaticTrackSelection(self: *Media, frame: *Frame) !void {
+ if (self._track_selection != .none) {
+ return;
+ }
+ const task = try frame._factory.create(TrackSelectionTask{ .media = self, .frame = frame });
+ errdefer frame._factory.destroy(task);
+ try frame.js.scheduler.add(task, TrackSelectionTask.run, 0, .{
+ .name = "Media.automaticTrackSelection",
+ .finalizer = TrackSelectionTask.finalizer,
+ });
+ self._track_selection = .pending;
+}
+
+const TrackSelectionTask = struct {
+ media: *Media,
+ frame: *Frame,
+
+ fn run(ctx: *anyopaque) !?u32 {
+ const self: *TrackSelectionTask = @ptrCast(@alignCast(ctx));
+ const media = self.media;
+ const frame = self.frame;
+ frame._factory.destroy(self);
+ try media.performAutomaticTrackSelection(frame);
+ return null;
+ }
+
+ fn finalizer(ctx: *anyopaque) void {
+ const self: *TrackSelectionTask = @ptrCast(@alignCast(ctx));
+ self.frame._factory.destroy(self);
+ }
+};
+
+fn performAutomaticTrackSelection(self: *Media, frame: *Frame) !void {
+ self._track_selection = .done;
+
+ var shown_subtitles = false;
+ var shown_chapters = false;
+ var it = self.asNode().childrenIterator();
+ while (it.next()) |child| {
+ const track_element = child.is(Element.Html.Track) orelse continue;
+ if (track_element.asElement().hasAttributeSafe(comptime .wrap("default")) == false) {
+ continue;
+ }
+ const track = try track_element.getTrack(frame);
+ if (track.getMode() != .disabled) {
+ continue;
+ }
+ switch (track.getKind()) {
+ .subtitles, .captions => {
+ if (shown_subtitles) continue;
+ shown_subtitles = true;
+ try track.setMode(.showing, frame);
+ },
+ .chapters => {
+ if (shown_chapters) continue;
+ shown_chapters = true;
+ try track.setMode(.showing, frame);
+ },
+ .metadata => try track.setMode(.hidden, frame),
+ .descriptions => {},
+ }
+ }
+}
+
pub fn getSrc(self: *const Media, frame: *Frame) ![]const u8 {
const element = self.asConstElement();
const src = element.getAttributeInterned("src") orelse return "";
@@ -306,6 +390,9 @@ pub const JsApi = struct {
pub const networkState = bridge.accessor(Media.getNetworkState, null, .{});
pub const @"error" = bridge.accessor(Media.getError, null, .{});
+ pub const textTracks = bridge.accessor(Media.getTextTracks, null, .{});
+ pub const addTextTrack = bridge.function(Media.addTextTrack, .{});
+
pub const canPlayType = bridge.function(Media.canPlayType, .{});
pub const play = bridge.function(Media.play, .{});
pub const pause = bridge.function(Media.pause, .{});
diff --git a/src/browser/webapi/element/html/Picture.zig b/src/browser/webapi/element/html/Picture.zig
index e15f666ac..7c460092e 100644
--- a/src/browser/webapi/element/html/Picture.zig
+++ b/src/browser/webapi/element/html/Picture.zig
@@ -45,14 +45,12 @@ pub fn asNode(self: *Picture) *Node {
return self.asElement().asNode();
}
-// A <source> entering the picture. Frame handles an inserted <img> itself.
-pub fn childInserted(parent: *Node, child: *Node, frame: *Frame) !void {
+// A <source> inserted into `parent`. Frame handles an inserted <img> itself.
+pub fn sourceInserted(parent: *Node, source: *Source, frame: *Frame) !void {
if (parent.is(Picture) == null) {
return;
}
- if (child.is(Source) != null) {
- return imagesFrom(child.nextSibling(), frame);
- }
+ return imagesFrom(source.asNode().nextSibling(), frame);
}
// `next_sibling` is the child's sibling from before it was unlinked.
diff --git a/src/browser/webapi/element/html/Track.zig b/src/browser/webapi/element/html/Track.zig
index 5913132e7..720e5088e 100644
--- a/src/browser/webapi/element/html/Track.zig
+++ b/src/browser/webapi/element/html/Track.zig
@@ -16,34 +16,296 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+const std = @import("std");
const lp = @import("lightpanda");
const js = @import("../../../js/js.zig");
+const URL = @import("../../../URL.zig");
+const Frame = @import("../../../Frame.zig");
const Factory = @import("../../../Factory.zig");
+const HttpClient = @import("../../../../network/HttpClient.zig");
const Node = @import("../../Node.zig");
const Element = @import("../../Element.zig");
+const TextTrack = @import("../../media/TextTrack.zig");
+const webvtt = @import("../../media/webvtt.zig");
+
const HtmlElement = @import("../Html.zig");
+const Media = @import("Media.zig");
const String = lp.String;
+const log = lp.log;
const Track = @This();
pub const Proto = HtmlElement;
_proto_canary: if (lp.IS_DEBUG) *HtmlElement else void = undefined,
-_kind: String,
-_ready_state: ReadyState,
+_ready_state: ReadyState = .none,
+_track: ?*TextTrack = null,
+_generation: u32 = 0, // bumped when src changes. Drop inflight loads for old URLs
const ReadyState = enum(u8) { none, loading, loaded, @"error" };
pub fn asElement(self: *Track) *Element {
return Factory.protoOf(self).asElement();
}
+pub fn asConstElement(self: *const Track) *const Element {
+ return Factory.protoOf(@constCast(self)).asElement();
+}
pub fn asNode(self: *Track) *Node {
return self.asElement().asNode();
}
+pub fn getTrack(self: *Track, frame: *Frame) !*TextTrack {
+ if (self._track == null) {
+ self._track = try TextTrack.initForElement(self, frame);
+ }
+ return self._track.?;
+}
+
+// The kind attribute, as reflected: missing is subtitles, invalid is metadata.
+pub fn getKind(self: *const Track) TextTrack.Kind {
+ const value = self.asConstElement().getAttributeSafe(comptime .wrap("kind")) orelse return .subtitles;
+ const info = @typeInfo(TextTrack.Kind).@"enum";
+ inline for (info.field_names, info.field_values) |field_name, field_value| {
+ if (std.ascii.eqlIgnoreCase(value, field_name)) {
+ return @fromBackingInt(@intCast(field_value));
+ }
+ }
+ return .metadata;
+}
+
+pub fn parentMedia(self: *Track) ?*Media {
+ const parent = self.asNode()._parent orelse return null;
+ return parent.is(Media);
+}
+
+fn getReadyState(self: *const Track) u16 {
+ return @backingInt(self._ready_state);
+}
+
+// The track was inserted into `parent`
+pub fn insertedInto(self: *Track, parent: *Node, frame: *Frame) !void {
+ const media = parent.is(Media) orelse return;
+ try media.queueAutomaticTrackSelection(frame);
+ try self.startTrackProcessing(frame);
+}
+
+pub fn startTrackProcessing(self: *Track, frame: *Frame) !void {
+ const track = self._track orelse return;
+ if (track.getMode() == .disabled) {
+ return;
+ }
+ if (self._ready_state != .none) {
+ // already loading, or done until src changes
+ return;
+ }
+ if (self.parentMedia() == null) {
+ return;
+ }
+ if (frame.isGoingAway()) {
+ return;
+ }
+ // A document without a browsing context (DOMParser et al.) loads nothing.
+ if (self.asElement().getDocument(frame)._frame == null) {
+ return;
+ }
+
+ self._ready_state = .loading;
+ try TrackLoad.schedule(self, frame);
+}
+
+// A changed (or removed) src empties the track's cues and, if the track was
+// being processed, restarts it with the new URL.
+fn srcChanged(self: *Track, frame: *Frame) !void {
+ self._generation +%= 1;
+ const track = self._track orelse return;
+ track.clearCues();
+ if (self._ready_state == .none) {
+ return;
+ }
+ self._ready_state = .none;
+ try self.startTrackProcessing(frame);
+}
+
+// One fetch of the track's src.
+const TrackLoad = struct {
+ frame: *Frame,
+ track: *Track,
+ generation: u32,
+ arena: *lp.Arena,
+ status: u16 = 0,
+ body: std.ArrayList(u8) = .empty,
+
+ fn schedule(track: *Track, frame: *Frame) !void {
+ const arena = try frame._session.getArena(.medium, "Track.load");
+ errdefer arena.release();
+
+ const self = try arena.allocator().create(TrackLoad);
+ self.* = .{
+ .frame = frame,
+ .track = track,
+ .arena = arena,
+ .generation = track._generation,
+ };
+
+ try frame.js.scheduler.add(self, TrackLoad.start, 0, .{
+ .name = "Track.load",
+ .finalizer = TrackLoad.finalizer,
+ });
+ frame._pending_loads += 1;
+ }
+
+ fn finalizer(ctx: *anyopaque) void {
+ shutdownCallback(ctx);
+ }
+
+ fn start(ctx: *anyopaque) !?u32 {
+ const self: *TrackLoad = @ptrCast(@alignCast(ctx));
+ self.fetch() catch |err| {
+ log.info(.http, "track fetch", .{ .err = err });
+ self.settle(false);
+ };
+ return null;
+ }
+
+ fn fetch(self: *TrackLoad) !void {
+ const frame = self.frame;
+ const track = self.track;
+ if (self.generation != track._generation or frame.isGoingAway()) {
+ return self.settle(false);
+ }
+
+ const src = track.asElement().getAttributeInterned("src") orelse "";
+ if (src.len == 0) {
+ return self.settle(false);
+ }
+ const url = try URL.resolve(self.arena.allocator(), frame.base(), src, .{ .encoding = frame.charset });
+
+ // A potential-CORS request with the same-origin fallback flag: without
+ // a crossorigin attribute on the media element, only same-origin
+ // tracks load.
+ const media = track.parentMedia() orelse return self.settle(false);
+ const cross_origin = media.asElement().getAttributeInterned("crossorigin");
+ const request_mode: HttpClient.Request.RequestMode = if (cross_origin == null) .same_origin else .cors;
+ const credentials_mode: HttpClient.Request.CredentialsMode = blk: {
+ const value = cross_origin orelse break :blk .include;
+ break :blk if (std.ascii.eqlIgnoreCase(value, "use-credentials")) .include else .same_origin;
+ };
+
+ const transfer = try frame._session.browser.http_client.newRequest(.{
+ .ctx = self,
+ .url = url,
+ .method = .GET,
+ .origin = frame.origin,
+ .request_mode = request_mode,
+ .credentials_mode = credentials_mode,
+ .resource_type = .text_track,
+ .header_callback = TrackLoad.headerCallback,
+ .data_callback = TrackLoad.dataCallback,
+ .done_callback = TrackLoad.doneCallback,
+ .error_callback = TrackLoad.errorCallback,
+ .shutdown_callback = TrackLoad.shutdownCallback,
+ }, &frame._http_owner);
+ {
+ // `deinit` fires no callbacks: our caller settles on this path.
+ errdefer transfer.deinit();
+ try frame.headersForRequest(transfer, .{});
+ }
+
+ // From here the transfer owns us; a failed submit has already gone
+ // through errorCallback.
+ transfer.submit() catch |err| {
+ log.debug(.http, "track fetch", .{ .err = err, .url = url });
+ };
+ }
+
+ fn headerCallback(transfer: *HttpClient.Transfer) !HttpClient.Transfer.HeaderResult {
+ const self: *TrackLoad = @ptrCast(@alignCast(transfer.req.ctx));
+ self.status = transfer.responseStatus() orelse 0;
+ return .proceed;
+ }
+
+ fn dataCallback(transfer: *HttpClient.Transfer, data: []const u8) !void {
+ const self: *TrackLoad = @ptrCast(@alignCast(transfer.req.ctx));
+ try self.body.appendSlice(self.arena.allocator(), data);
+ }
+
+ fn doneCallback(ctx: *anyopaque) !void {
+ const self: *TrackLoad = @ptrCast(@alignCast(ctx));
+ self.settle(self.status >= 200 and self.status < 300);
+ }
+
+ fn errorCallback(ctx: *anyopaque, err: anyerror) void {
+ const self: *TrackLoad = @ptrCast(@alignCast(ctx));
+ log.info(.http, "track fetch", .{ .err = err, .status = self.status });
+ self.settle(false);
+ }
+
+ // Teardown or a superseding navigation: give the slot back without
+ // running anything (see ImageLoad.shutdownCallback).
+ fn shutdownCallback(ctx: *anyopaque) void {
+ const self: *TrackLoad = @ptrCast(@alignCast(ctx));
+ const frame = self.frame;
+ self.arena.release();
+ frame._pending_loads -|= 1;
+ }
+
+ fn settle(self: *TrackLoad, fetched: bool) void {
+ const frame = self.frame;
+ const track = self.track;
+ const arena = self.arena;
+ defer arena.release();
+
+ if (frame.isGoingAway()) {
+ frame._pending_loads -|= 1;
+ return;
+ }
+
+ if (self.generation == track._generation) {
+ const loaded = fetched and track.addParsedCues(arena.allocator(), self.body.items, frame);
+ track._ready_state = if (loaded) .loaded else .@"error";
+ frame.queueElementEvent(Factory.protoOf(track), if (loaded) .load else .@"error") catch |err| {
+ log.warn(.frame, "track queue event", .{ .err = err });
+ };
+ }
+
+ frame.pendingLoadCompleted();
+ }
+};
+
+// False when the body isn't a WebVTT file.
+fn addParsedCues(self: *Track, arena: std.mem.Allocator, body: []const u8, frame: *Frame) bool {
+ const cues = webvtt.parse(arena, body, frame) catch |err| {
+ log.warn(.frame, "track parse", .{ .err = err });
+ return false;
+ } orelse return false;
+
+ const track = self.getTrack(frame) catch return false;
+ for (cues) |cue| {
+ track.addCue(cue.asCue(), frame) catch |err| {
+ log.warn(.frame, "track add cue", .{ .err = err });
+ return false;
+ };
+ }
+ return true;
+}
+
+pub const Build = struct {
+ pub fn attributeChange(element: *Element, name: String, _: String, frame: *Frame) !void {
+ if (name.eql(comptime .wrap("src"))) {
+ return element.as(Track).srcChanged(frame);
+ }
+ }
+
+ pub fn attributeRemove(element: *Element, name: String, frame: *Frame) !void {
+ if (name.eql(comptime .wrap("src"))) {
+ return element.as(Track).srcChanged(frame);
+ }
+ }
+};
+
pub const JsApi = struct {
pub const bridge = js.Bridge(Track);
@@ -65,6 +327,9 @@ pub const JsApi = struct {
pub const LOADING = bridge.property(@as(u16, @backingInt(ReadyState.loading)), .{ .template = true });
pub const LOADED = bridge.property(@as(u16, @backingInt(ReadyState.loaded)), .{ .template = true });
pub const ERROR = bridge.property(@as(u16, @backingInt(ReadyState.@"error")), .{ .template = true });
+
+ pub const readyState = bridge.accessor(Track.getReadyState, null, .{});
+ pub const track = bridge.accessor(Track.getTrack, null, .{});
};
const testing = @import("../../../../testing.zig");
diff --git a/src/browser/webapi/media/TextTrack.zig b/src/browser/webapi/media/TextTrack.zig
new file mode 100644
index 000000000..537500697
--- /dev/null
+++ b/src/browser/webapi/media/TextTrack.zig
@@ -0,0 +1,235 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const std = @import("std");
+
+const js = @import("../../js/js.zig");
+const Frame = @import("../../Frame.zig");
+
+const EventTarget = @import("../EventTarget.zig");
+const Track = @import("../element/html/Track.zig");
+
+const TextTrackCue = @import("TextTrackCue.zig");
+const TextTrackCueList = @import("TextTrackCueList.zig");
+
+const TextTrack = @This();
+
+pub const Proto = EventTarget;
+
+_proto: *EventTarget,
+_source: Source,
+_mode: Mode = .disabled,
+_cues: std.ArrayList(*TextTrackCue) = .empty,
+_cue_list: ?*TextTrackCueList = null,
+_active_cue_list: ?*TextTrackCueList = null,
+
+pub const Kind = enum { subtitles, captions, descriptions, chapters, metadata };
+pub const Mode = enum { disabled, hidden, showing };
+
+const Source = union(enum) {
+ element: *Track,
+ // created by HTMLMediaElement.addTextTrack()
+ script: struct {
+ kind: Kind,
+ label: []const u8,
+ language: []const u8,
+ },
+};
+
+pub fn initForElement(track: *Track, frame: *Frame) !*TextTrack {
+ return frame._factory.eventTarget(TextTrack{
+ ._proto = undefined,
+ ._source = .{ .element = track },
+ });
+}
+
+pub fn initForScript(kind: Kind, label: []const u8, language: []const u8, frame: *Frame) !*TextTrack {
+ return frame._factory.eventTarget(TextTrack{
+ ._proto = undefined,
+ // per spec, a script-created track starts out hidden
+ ._mode = .hidden,
+ ._source = .{ .script = .{
+ .kind = kind,
+ .label = try frame.dupeString(label),
+ .language = try frame.dupeString(language),
+ } },
+ });
+}
+
+pub fn asEventTarget(self: *TextTrack) *EventTarget {
+ return self._proto;
+}
+
+pub fn getKind(self: *const TextTrack) Kind {
+ return switch (self._source) {
+ .element => |track| track.getKind(),
+ .script => |s| s.kind,
+ };
+}
+
+fn getKindString(self: *const TextTrack) []const u8 {
+ return @tagName(self.getKind());
+}
+
+fn getLabel(self: *const TextTrack) []const u8 {
+ return switch (self._source) {
+ .element => |track| track.asConstElement().getAttributeSafe(comptime .wrap("label")) orelse "",
+ .script => |s| s.label,
+ };
+}
+
+fn getLanguage(self: *const TextTrack) []const u8 {
+ return switch (self._source) {
+ .element => |track| track.asConstElement().getAttributeSafe(comptime .wrap("srclang")) orelse "",
+ .script => |s| s.language,
+ };
+}
+
+pub fn getId(self: *const TextTrack) []const u8 {
+ return switch (self._source) {
+ .element => |track| track.asConstElement().getAttributeInterned("id") orelse "",
+ .script => "",
+ };
+}
+
+fn getInBandMetadataTrackDispatchType(_: *const TextTrack) []const u8 {
+ return "";
+}
+
+pub fn getMode(self: *const TextTrack) Mode {
+ return self._mode;
+}
+
+fn getModeString(self: *const TextTrack) []const u8 {
+ return @tagName(self._mode);
+}
+
+fn setModeString(self: *TextTrack, value: []const u8, frame: *Frame) !void {
+ // invalid enum values are ignored
+ const mode = std.meta.stringToEnum(Mode, value) orelse return;
+ return self.setMode(mode, frame);
+}
+
+pub fn setMode(self: *TextTrack, mode: Mode, frame: *Frame) !void {
+ if (self._mode == mode) {
+ return;
+ }
+ self._mode = mode;
+ if (mode != .disabled) {
+ switch (self._source) {
+ .element => |track| try track.startTrackProcessing(frame),
+ .script => {},
+ }
+ }
+}
+
+fn getCues(self: *TextTrack, frame: *Frame) !?*TextTrackCueList {
+ if (self._mode == .disabled) {
+ return null;
+ }
+ if (self._cue_list == null) {
+ self._cue_list = try frame._factory.create(TextTrackCueList{ ._track = self });
+ }
+ return self._cue_list;
+}
+
+fn getActiveCues(self: *TextTrack, frame: *Frame) !?*TextTrackCueList {
+ if (self._mode == .disabled) {
+ return null;
+ }
+ if (self._active_cue_list == null) {
+ self._active_cue_list = try frame._factory.create(TextTrackCueList{ ._track = null });
+ }
+ return self._active_cue_list;
+}
+
+pub fn cues(self: *const TextTrack) []const *TextTrackCue {
+ return self._cues.items;
+}
+
+pub fn addCue(self: *TextTrack, cue: *TextTrackCue, frame: *Frame) !void {
+ if (cue._track) |previous| {
+ previous.remove(cue);
+ }
+ try self.insert(cue, frame);
+ cue._track = self;
+}
+
+fn removeCue(self: *TextTrack, cue: *TextTrackCue) !void {
+ if (cue._track != self) {
+ return error.NotFoundError;
+ }
+ self.remove(cue);
+ cue._track = null;
+}
+
+pub fn clearCues(self: *TextTrack) void {
+ for (self._cues.items) |cue| {
+ cue._track = null;
+ }
+ self._cues.clearRetainingCapacity();
+}
+
+pub fn reorder(self: *TextTrack, cue: *TextTrackCue, frame: *Frame) !void {
+ self.remove(cue);
+ try self.insert(cue, frame);
+}
+
+fn insert(self: *TextTrack, cue: *TextTrackCue, frame: *Frame) !void {
+ // ordered by start time, then end time descending, then insert order
+ var index: usize = self._cues.items.len;
+ for (self._cues.items, 0..) |existing, i| {
+ if (existing._start_time > cue._start_time or (existing._start_time == cue._start_time and existing._end_time < cue._end_time)) {
+ index = i;
+ break;
+ }
+ }
+ try self._cues.insert(frame.page_arena, index, cue);
+}
+
+fn remove(self: *TextTrack, cue: *TextTrackCue) void {
+ const index = std.mem.findScalar(*TextTrackCue, self._cues.items, cue) orelse return;
+ _ = self._cues.orderedRemove(index);
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(TextTrack);
+
+ pub const Meta = struct {
+ pub const name = "TextTrack";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const kind = bridge.accessor(TextTrack.getKindString, null, .{});
+ pub const label = bridge.accessor(TextTrack.getLabel, null, .{});
+ pub const language = bridge.accessor(TextTrack.getLanguage, null, .{});
+ pub const id = bridge.accessor(TextTrack.getId, null, .{});
+ pub const inBandMetadataTrackDispatchType = bridge.accessor(TextTrack.getInBandMetadataTrackDispatchType, null, .{});
+ pub const mode = bridge.accessor(TextTrack.getModeString, TextTrack.setModeString, .{});
+ pub const cues = bridge.accessor(TextTrack.getCues, null, .{});
+ pub const activeCues = bridge.accessor(TextTrack.getActiveCues, null, .{});
+ pub const addCue = bridge.function(TextTrack.addCue, .{});
+ pub const removeCue = bridge.function(TextTrack.removeCue, .{});
+ pub const oncuechange = EventTarget.handlerAccessor(TextTrack, "cuechange");
+};
+
+const testing = @import("../../../testing.zig");
+test "WebApi: TextTrack" {
+ try testing.htmlRunner("media/texttrack.html", .{});
+}
diff --git a/src/browser/webapi/media/TextTrackCue.zig b/src/browser/webapi/media/TextTrackCue.zig
index 4f31d4458..01f3cfdcc 100644
--- a/src/browser/webapi/media/TextTrackCue.zig
+++ b/src/browser/webapi/media/TextTrackCue.zig
@@ -16,10 +16,13 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
+const std = @import("std");
+
const js = @import("../../js/js.zig");
const Frame = @import("../../Frame.zig");
const EventTarget = @import("../EventTarget.zig");
+const TextTrack = @import("TextTrack.zig");
const TextTrackCue = @This();
@@ -31,6 +34,7 @@ _id: []const u8 = "",
_start_time: f64 = 0,
_end_time: f64 = 0,
_pause_on_exit: bool = false,
+_track: ?*TextTrack = null, // The track whose list of cues this cue is in.
pub const Type = union(enum) {
vtt: *@import("VTTCue.zig"),
@@ -40,6 +44,10 @@ pub fn asEventTarget(self: *TextTrackCue) *EventTarget {
return self._proto;
}
+fn getTrack(self: *const TextTrackCue) ?*TextTrack {
+ return self._track;
+}
+
pub fn getId(self: *const TextTrackCue) []const u8 {
return self._id;
}
@@ -52,16 +60,25 @@ pub fn getStartTime(self: *const TextTrackCue) f64 {
return self._start_time;
}
-fn setStartTime(self: *TextTrackCue, value: f64) void {
+fn setStartTime(self: *TextTrackCue, value: f64, frame: *Frame) !void {
+ if (std.math.isFinite(value) == false) {
+ return error.TypeError;
+ }
self._start_time = value;
+ if (self._track) |track| {
+ try track.reorder(self, frame);
+ }
}
fn getEndTime(self: *const TextTrackCue) f64 {
return self._end_time;
}
-fn setEndTime(self: *TextTrackCue, value: f64) void {
+fn setEndTime(self: *TextTrackCue, value: f64, frame: *Frame) !void {
self._end_time = value;
+ if (self._track) |track| {
+ try track.reorder(self, frame);
+ }
}
fn getPauseOnExit(self: *const TextTrackCue) bool {
@@ -84,6 +101,7 @@ pub const JsApi = struct {
pub const Prototype = EventTarget;
+ pub const track = bridge.accessor(TextTrackCue.getTrack, null, .{});
pub const id = bridge.accessor(TextTrackCue.getId, TextTrackCue.setId, .{});
pub const startTime = bridge.accessor(TextTrackCue.getStartTime, TextTrackCue.setStartTime, .{});
pub const endTime = bridge.accessor(TextTrackCue.getEndTime, TextTrackCue.setEndTime, .{});
diff --git a/src/browser/webapi/media/TextTrackCueList.zig b/src/browser/webapi/media/TextTrackCueList.zig
new file mode 100644
index 000000000..faaf9eb73
--- /dev/null
+++ b/src/browser/webapi/media/TextTrackCueList.zig
@@ -0,0 +1,78 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const std = @import("std");
+
+const js = @import("../../js/js.zig");
+
+const TextTrack = @import("TextTrack.zig");
+const TextTrackCue = @import("TextTrackCue.zig");
+
+const TextTrackCueList = @This();
+
+_track: ?*TextTrack,
+
+pub fn getLength(self: *const TextTrackCueList) u32 {
+ const track = self._track orelse return 0;
+ return @intCast(track.cues().len);
+}
+
+pub fn item(self: *const TextTrackCueList, index: usize) ?*TextTrackCue {
+ const track = self._track orelse return null;
+ const cues = track.cues();
+ if (index >= cues.len) {
+ return null;
+ }
+ return cues[index];
+}
+
+fn getCueById(self: *const TextTrackCueList, id: []const u8) ?*TextTrackCue {
+ if (id.len == 0) {
+ return null;
+ }
+ const track = self._track orelse return null;
+ for (track.cues()) |cue| {
+ if (std.mem.eql(u8, cue._id, id)) {
+ return cue;
+ }
+ }
+ return null;
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(TextTrackCueList);
+
+ pub const Meta = struct {
+ pub const name = "TextTrackCueList";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const length = bridge.accessor(TextTrackCueList.getLength, null, .{});
+ pub const @"[]" = bridge.indexed(TextTrackCueList.item, getIndexes, .{ .null_as_undefined = true });
+ pub const getCueById = bridge.function(TextTrackCueList.getCueById, .{});
+
+ fn getIndexes(self: *TextTrackCueList, exec: *const js.Execution) !js.Array {
+ const len = self.getLength();
+ var arr = exec.js.local.?.newArray(len);
+ for (0..len) |i| {
+ _ = try arr.set(@intCast(i), i, .{});
+ }
+ return arr;
+ }
+};
diff --git a/src/browser/webapi/media/TextTrackList.zig b/src/browser/webapi/media/TextTrackList.zig
new file mode 100644
index 000000000..ccb3b372d
--- /dev/null
+++ b/src/browser/webapi/media/TextTrackList.zig
@@ -0,0 +1,136 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const std = @import("std");
+
+const js = @import("../../js/js.zig");
+const Frame = @import("../../Frame.zig");
+
+const Node = @import("../Node.zig");
+const EventTarget = @import("../EventTarget.zig");
+const Media = @import("../element/html/Media.zig");
+const Track = @import("../element/html/Track.zig");
+
+const TextTrack = @import("TextTrack.zig");
+
+const TextTrackList = @This();
+
+pub const Proto = EventTarget;
+
+_proto: *EventTarget,
+_media: *Media,
+
+pub fn init(media: *Media, frame: *Frame) !*TextTrackList {
+ return frame._factory.eventTarget(TextTrackList{
+ ._proto = undefined,
+ ._media = media,
+ });
+}
+
+pub fn asEventTarget(self: *TextTrackList) *EventTarget {
+ return self._proto;
+}
+
+fn getLength(self: *const TextTrackList, frame: *Frame) !u32 {
+ var it = self.iterator(frame);
+ var count: u32 = 0;
+ while (try it.next()) |_| {
+ count += 1;
+ }
+ return count + @as(u32, @intCast(self._media._added_text_tracks.items.len));
+}
+
+fn item(self: *const TextTrackList, index: usize, frame: *Frame) !?*TextTrack {
+ var it = self.iterator(frame);
+ var child_index: usize = 0;
+ while (try it.next()) |track| : (child_index += 1) {
+ if (child_index == index) {
+ return track;
+ }
+ }
+ const added_index = index - child_index;
+ const added = self._media._added_text_tracks.items;
+ if (added_index < added.len) {
+ return added[added_index];
+ }
+ return null;
+}
+
+fn getTrackById(self: *const TextTrackList, id: []const u8, frame: *Frame) !?*TextTrack {
+ var it = self.iterator(frame);
+ while (try it.next()) |track| {
+ if (std.mem.eql(u8, track.getId(), id)) {
+ return track;
+ }
+ }
+ for (self._media._added_text_tracks.items) |track| {
+ if (std.mem.eql(u8, track.getId(), id)) {
+ return track;
+ }
+ }
+ return null;
+}
+
+// only used internally
+const Iterator = struct {
+ child: ?*Node,
+ frame: *Frame,
+
+ fn next(self: *Iterator) !?*TextTrack {
+ while (self.child) |child| {
+ self.child = child.nextSibling();
+ if (child.is(Track)) |track| {
+ return try track.getTrack(self.frame);
+ }
+ }
+ return null;
+ }
+};
+
+fn iterator(self: *const TextTrackList, frame: *Frame) Iterator {
+ return .{
+ .frame = frame,
+ .child = self._media.asNode().firstChild(),
+ };
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(TextTrackList);
+
+ pub const Meta = struct {
+ pub const name = "TextTrackList";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const length = bridge.accessor(TextTrackList.getLength, null, .{});
+ pub const @"[]" = bridge.indexed(TextTrackList.item, getIndexes, .{ .null_as_undefined = true });
+ pub const getTrackById = bridge.function(TextTrackList.getTrackById, .{});
+ pub const onchange = EventTarget.handlerAccessor(TextTrackList, "change");
+ pub const onaddtrack = EventTarget.handlerAccessor(TextTrackList, "addtrack");
+ pub const onremovetrack = EventTarget.handlerAccessor(TextTrackList, "removetrack");
+
+ fn getIndexes(self: *TextTrackList, frame: *Frame) !js.Array {
+ const len = try self.getLength(frame);
+ var arr = frame.js.local.?.newArray(len);
+ for (0..len) |i| {
+ _ = try arr.set(@intCast(i), i, .{});
+ }
+ return arr;
+ }
+};
diff --git a/src/browser/webapi/media/VTTCue.zig b/src/browser/webapi/media/VTTCue.zig
index cffa56831..1a0b45375 100644
--- a/src/browser/webapi/media/VTTCue.zig
+++ b/src/browser/webapi/media/VTTCue.zig
@@ -16,9 +16,15 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
-const js = @import("../../js/js.zig");
+const std = @import("std");
+const js = @import("../../js/js.zig");
const Frame = @import("../../Frame.zig");
+
+const DocumentFragment = @import("../DocumentFragment.zig");
+
+const cue_text = @import("cue_text.zig");
+const VTTRegion = @import("VTTRegion.zig");
const TextTrackCue = @import("TextTrackCue.zig");
const VTTCue = @This();
@@ -27,25 +33,48 @@ pub const Proto = TextTrackCue;
_proto: *TextTrackCue,
_text: []const u8 = "",
-_region: ?js.Object.Global = null,
-_vertical: []const u8 = "",
+_region: ?*VTTRegion = null,
+_vertical: Direction = .horizontal,
_snap_to_lines: bool = true,
_line: ?f64 = null, // null represents "auto"
+_line_align: LineAlign = .start,
_position: ?f64 = null, // null represents "auto"
+_position_align: PositionAlign = .auto,
_size: f64 = 100,
-_align: []const u8 = "center",
+_align: Align = .center,
+
+pub const Direction = enum {
+ horizontal,
+ rl,
+ lr,
+
+ pub fn fromString(value: []const u8) ?Direction {
+ if (value.len == 0) {
+ return .horizontal;
+ }
+ return std.meta.stringToEnum(Direction, value) orelse return null;
+ }
+};
+
+pub const LineAlign = enum { start, center, end };
+pub const PositionAlign = enum { @"line-left", center, @"line-right", auto };
+pub const Align = enum { start, center, end, left, right };
pub fn constructor(start_time: f64, end_time: f64, text: []const u8, frame: *Frame) !*VTTCue {
+ if (std.math.isFinite(start_time) == false) {
+ return error.TypeError;
+ }
+ if (std.math.isNan(end_time) or end_time == -std.math.inf(f64)) {
+ return error.TypeError;
+ }
+ return init(start_time, end_time, try frame.dupeString(text), frame);
+}
+
+// `text` must already be owned by the page.
+pub fn init(start_time: f64, end_time: f64, text: []const u8, frame: *Frame) !*VTTCue {
const cue = try frame._factory.textTrackCue(VTTCue{
._proto = undefined,
- ._text = try frame.dupeString(text),
- ._region = null,
- ._vertical = "",
- ._snap_to_lines = true,
- ._line = null, // "auto"
- ._position = null, // "auto"
- ._size = 100,
- ._align = "center",
+ ._text = text,
});
cue._proto._start_time = start_time;
@@ -54,6 +83,10 @@ pub fn constructor(start_time: f64, end_time: f64, text: []const u8, frame: *Fra
return cue;
}
+pub fn asCue(self: *VTTCue) *TextTrackCue {
+ return self._proto;
+}
+
pub fn getText(self: *const VTTCue) []const u8 {
return self._text;
}
@@ -62,21 +95,24 @@ fn setText(self: *VTTCue, value: []const u8, frame: *Frame) !void {
self._text = try frame.dupeString(value);
}
-fn getRegion(self: *const VTTCue) ?js.Object.Global {
+fn getRegion(self: *const VTTCue) ?*VTTRegion {
return self._region;
}
-fn setRegion(self: *VTTCue, value: ?js.Object.Global) !void {
+fn setRegion(self: *VTTCue, value: ?*VTTRegion) void {
self._region = value;
}
fn getVertical(self: *const VTTCue) []const u8 {
- return self._vertical;
+ return switch (self._vertical) {
+ .horizontal => "",
+ else => |v| @tagName(v),
+ };
}
-fn setVertical(self: *VTTCue, value: []const u8, frame: *Frame) !void {
- // Valid values: "", "rl", "lr"
- self._vertical = try frame.dupeString(value);
+fn setVertical(self: *VTTCue, value: []const u8) void {
+ // Invalid enum values are ignored
+ self._vertical = Direction.fromString(value) orelse return;
}
fn getSnapToLines(self: *const VTTCue) bool {
@@ -99,13 +135,31 @@ fn getLine(self: *const VTTCue) LineAndPositionSetting {
return .{ .auto = "auto" };
}
-fn setLine(self: *VTTCue, value: LineAndPositionSetting) void {
+fn setLine(self: *VTTCue, value: LineAndPositionSetting) !void {
switch (value) {
- .number => |num| self._line = num,
- .auto => self._line = null,
+ .number => |num| {
+ if (std.math.isFinite(num) == false) {
+ return error.TypeError;
+ }
+ self._line = num;
+ },
+ .auto => |str| {
+ if (std.mem.eql(u8, str, "auto") == false) {
+ return error.TypeError;
+ }
+ self._line = null;
+ },
}
}
+fn getLineAlign(self: *const VTTCue) []const u8 {
+ return @tagName(self._line_align);
+}
+
+fn setLineAlign(self: *VTTCue, value: []const u8) void {
+ self._line_align = std.meta.stringToEnum(LineAlign, value) orelse return;
+}
+
fn getPosition(self: *const VTTCue) LineAndPositionSetting {
if (self._position) |num| {
return .{ .number = num };
@@ -113,35 +167,58 @@ fn getPosition(self: *const VTTCue) LineAndPositionSetting {
return .{ .auto = "auto" };
}
-fn setPosition(self: *VTTCue, value: LineAndPositionSetting) void {
+fn setPosition(self: *VTTCue, value: LineAndPositionSetting) !void {
switch (value) {
- .number => |num| self._position = num,
- .auto => self._position = null,
+ .number => |num| {
+ if (std.math.isFinite(num) == false) {
+ return error.TypeError;
+ }
+ if (num < 0 or num > 100) {
+ return error.IndexSizeError;
+ }
+ self._position = num;
+ },
+ .auto => |str| {
+ if (std.mem.eql(u8, str, "auto") == false) {
+ return error.TypeError;
+ }
+ self._position = null;
+ },
}
}
+fn getPositionAlign(self: *const VTTCue) []const u8 {
+ return @tagName(self._position_align);
+}
+
+fn setPositionAlign(self: *VTTCue, value: []const u8) void {
+ self._position_align = std.meta.stringToEnum(PositionAlign, value) orelse return;
+}
+
pub fn getSize(self: *const VTTCue) f64 {
return self._size;
}
-fn setSize(self: *VTTCue, value: f64) void {
+fn setSize(self: *VTTCue, value: f64) !void {
+ if (std.math.isFinite(value) == false) {
+ return error.TypeError;
+ }
+ if (value < 0 or value > 100) {
+ return error.IndexSizeError;
+ }
self._size = value;
}
fn getAlign(self: *const VTTCue) []const u8 {
- return self._align;
+ return @tagName(self._align);
}
-fn setAlign(self: *VTTCue, value: []const u8, frame: *Frame) !void {
- // Valid values: "start", "center", "end", "left", "right"
- self._align = try frame.dupeString(value);
+fn setAlign(self: *VTTCue, value: []const u8) void {
+ self._align = std.meta.stringToEnum(Align, value) orelse return;
}
-fn getCueAsHTML(self: *const VTTCue) !js.Object {
- // Minimal implementation: return a document fragment
- // In a full implementation, this would parse the VTT text into HTML nodes
- _ = self;
- return error.NotImplemented;
+fn getCueAsHTML(self: *const VTTCue, frame: *Frame) !*DocumentFragment {
+ return cue_text.toFragment(self._text, frame);
}
pub const JsApi = struct {
@@ -162,7 +239,9 @@ pub const JsApi = struct {
pub const vertical = bridge.accessor(VTTCue.getVertical, VTTCue.setVertical, .{});
pub const snapToLines = bridge.accessor(VTTCue.getSnapToLines, VTTCue.setSnapToLines, .{});
pub const line = bridge.accessor(VTTCue.getLine, VTTCue.setLine, .{});
+ pub const lineAlign = bridge.accessor(VTTCue.getLineAlign, VTTCue.setLineAlign, .{});
pub const position = bridge.accessor(VTTCue.getPosition, VTTCue.setPosition, .{});
+ pub const positionAlign = bridge.accessor(VTTCue.getPositionAlign, VTTCue.setPositionAlign, .{});
pub const size = bridge.accessor(VTTCue.getSize, VTTCue.setSize, .{});
pub const @"align" = bridge.accessor(VTTCue.getAlign, VTTCue.setAlign, .{});
pub const getCueAsHTML = bridge.function(VTTCue.getCueAsHTML, .{});
diff --git a/src/browser/webapi/media/VTTRegion.zig b/src/browser/webapi/media/VTTRegion.zig
new file mode 100644
index 000000000..5e19f02ea
--- /dev/null
+++ b/src/browser/webapi/media/VTTRegion.zig
@@ -0,0 +1,155 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+const std = @import("std");
+
+const js = @import("../../js/js.zig");
+const Frame = @import("../../Frame.zig");
+
+const VTTRegion = @This();
+
+_id: []const u8 = "",
+_width: f64 = 100,
+_lines: u32 = 3,
+_region_anchor_x: f64 = 0,
+_region_anchor_y: f64 = 100,
+_viewport_anchor_x: f64 = 0,
+_viewport_anchor_y: f64 = 100,
+_scroll: Scroll = .none,
+
+pub const Scroll = enum {
+ none,
+ up,
+
+ pub fn fromString(value: []const u8) ?Scroll {
+ if (value.len == 0) {
+ return .none;
+ }
+ if (std.mem.eql(u8, value, "up")) {
+ return .up;
+ }
+ return null;
+ }
+};
+
+pub fn init(frame: *Frame) !*VTTRegion {
+ return frame._factory.create(VTTRegion{});
+}
+
+fn percentage(value: f64) !f64 {
+ if (std.math.isFinite(value) == false) {
+ return error.TypeError;
+ }
+ if (value < 0 or value > 100) {
+ return error.IndexSizeError;
+ }
+ return value;
+}
+
+pub fn getId(self: *const VTTRegion) []const u8 {
+ return self._id;
+}
+
+pub fn setId(self: *VTTRegion, value: []const u8, frame: *Frame) !void {
+ self._id = try frame.dupeString(value);
+}
+
+fn getWidth(self: *const VTTRegion) f64 {
+ return self._width;
+}
+
+pub fn setWidth(self: *VTTRegion, value: f64) !void {
+ self._width = try percentage(value);
+}
+
+fn getLines(self: *const VTTRegion) u32 {
+ return self._lines;
+}
+
+pub fn setLines(self: *VTTRegion, value: u32) void {
+ self._lines = value;
+}
+
+fn getRegionAnchorX(self: *const VTTRegion) f64 {
+ return self._region_anchor_x;
+}
+
+pub fn setRegionAnchorX(self: *VTTRegion, value: f64) !void {
+ self._region_anchor_x = try percentage(value);
+}
+
+fn getRegionAnchorY(self: *const VTTRegion) f64 {
+ return self._region_anchor_y;
+}
+
+pub fn setRegionAnchorY(self: *VTTRegion, value: f64) !void {
+ self._region_anchor_y = try percentage(value);
+}
+
+fn getViewportAnchorX(self: *const VTTRegion) f64 {
+ return self._viewport_anchor_x;
+}
+
+pub fn setViewportAnchorX(self: *VTTRegion, value: f64) !void {
+ self._viewport_anchor_x = try percentage(value);
+}
+
+fn getViewportAnchorY(self: *const VTTRegion) f64 {
+ return self._viewport_anchor_y;
+}
+
+pub fn setViewportAnchorY(self: *VTTRegion, value: f64) !void {
+ self._viewport_anchor_y = try percentage(value);
+}
+
+fn getScroll(self: *const VTTRegion) []const u8 {
+ return switch (self._scroll) {
+ .none => "",
+ .up => "up",
+ };
+}
+
+fn setScroll(self: *VTTRegion, value: []const u8) void {
+ // invalid enum values are ignored
+ self._scroll = Scroll.fromString(value) orelse return;
+}
+
+pub const JsApi = struct {
+ pub const bridge = js.Bridge(VTTRegion);
+
+ pub const Meta = struct {
+ pub const name = "VTTRegion";
+ pub const prototype_chain = bridge.prototypeChain();
+ pub var class_id: bridge.ClassId = undefined;
+ };
+
+ pub const constructor = bridge.constructor(VTTRegion.init, .{});
+ pub const id = bridge.accessor(VTTRegion.getId, VTTRegion.setId, .{});
+ pub const width = bridge.accessor(VTTRegion.getWidth, VTTRegion.setWidth, .{});
+ pub const lines = bridge.accessor(VTTRegion.getLines, VTTRegion.setLines, .{});
+ pub const regionAnchorX = bridge.accessor(VTTRegion.getRegionAnchorX, VTTRegion.setRegionAnchorX, .{});
+ pub const regionAnchorY = bridge.accessor(VTTRegion.getRegionAnchorY, VTTRegion.setRegionAnchorY, .{});
+ pub const viewportAnchorX = bridge.accessor(VTTRegion.getViewportAnchorX, VTTRegion.setViewportAnchorX, .{});
+ pub const viewportAnchorY = bridge.accessor(VTTRegion.getViewportAnchorY, VTTRegion.setViewportAnchorY, .{});
+ pub const scroll = bridge.accessor(VTTRegion.getScroll, VTTRegion.setScroll, .{});
+};
+
+const testing = @import("../../../testing.zig");
+test "WebApi: VTTRegion" {
+ try testing.htmlRunner("media/vttregion.html", .{});
+}
diff --git a/src/browser/webapi/media/cue_text.zig b/src/browser/webapi/media/cue_text.zig
new file mode 100644
index 000000000..048577c66
--- /dev/null
+++ b/src/browser/webapi/media/cue_text.zig
@@ -0,0 +1,465 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// WebVTT cue text parsing rules and the DOM construction rules behind
+// VTTCue.getCueAsHTML(): https://w3c.github.io/webvtt/#cue-text-parsing-rules
+
+const std = @import("std");
+
+const Frame = @import("../../Frame.zig");
+const html5ever = @import("../../parser/html5ever.zig");
+
+const Node = @import("../Node.zig");
+const Element = @import("../Element.zig");
+const DocumentFragment = @import("../DocumentFragment.zig");
+
+const Allocator = std.mem.Allocator;
+
+pub fn toFragment(text: []const u8, frame: *Frame) !*DocumentFragment {
+ const document = frame.document;
+ const fragment = try DocumentFragment.init(document, frame);
+
+ const Kind = enum { root, c, i, b, u, ruby, rt, v, lang };
+ const Open = struct { node: *Node, kind: Kind };
+
+ const arena = frame.call_arena;
+ var stack: std.ArrayList(Open) = .empty;
+ try stack.append(arena, .{ .node = fragment.asNode(), .kind = .root });
+
+ var tokenizer: Tokenizer = .{ .input = text, .arena = arena };
+ while (try tokenizer.next()) |token| {
+ const current = stack.items[stack.items.len - 1];
+ switch (token) {
+ .string => |value| {
+ const node = try Frame.node_factory.createTextNode(document, value);
+ try frame.appendNode(current.node, node, .{});
+ },
+ .start_tag => |tag| {
+ const kind = std.meta.stringToEnum(Kind, tag.name) orelse continue;
+ const local_name, const annotation_attribute: ?[]const u8 = switch (kind) {
+ .root => continue,
+ .c => .{ "span", null },
+ .v => .{ "span", "title" },
+ .lang => .{ "span", "lang" },
+ .rt => blk: {
+ // only valid directly inside a <ruby>
+ if (current.kind != .ruby) continue;
+ break :blk .{ "rt", null };
+ },
+ .i, .b, .u, .ruby => .{ @tagName(kind), null },
+ };
+
+ const node = try Frame.node_factory.createElementNS(document, .html, local_name, null);
+ const element = node.as(Element);
+ if (tag.classes.len > 0) {
+ try element.setAttributeSafe(comptime .wrap("class"), .wrap(tag.classes), frame);
+ }
+ if (annotation_attribute) |name| {
+ try element.setAttributeSafe(.wrap(name), .wrap(tag.annotation), frame);
+ }
+ try frame.appendNode(current.node, node, .{});
+ try stack.append(arena, .{ .node = node, .kind = kind });
+ },
+ .end_tag => |name| {
+ if (current.kind != .root and std.mem.eql(u8, name, @tagName(current.kind))) {
+ stack.items.len -= 1;
+ } else if (current.kind == .rt and std.mem.eql(u8, name, "ruby")) {
+ // an rt's parent is always a ruby, so this never pops the root
+ stack.items.len -= 2;
+ }
+ },
+ .timestamp => |value| {
+ const seconds = parseTimestamp(value) orelse continue;
+ var buf: [64]u8 = undefined;
+ const data = try formatTimestamp(&buf, seconds);
+ const node = try Frame.node_factory.createProcessingInstruction(document, "timestamp", data);
+ try frame.appendNode(current.node, node, .{});
+ },
+ }
+ }
+
+ return fragment;
+}
+
+const Token = union(enum) {
+ string: []const u8,
+ start_tag: struct {
+ name: []const u8,
+ // already joined with spaces
+ classes: []const u8,
+ annotation: []const u8,
+ },
+ end_tag: []const u8,
+ timestamp: []const u8,
+};
+
+const Tokenizer = struct {
+ input: []const u8,
+ pos: usize = 0,
+ arena: Allocator,
+
+ const State = enum { data, tag, start_tag, start_tag_class, start_tag_annotation, end_tag, timestamp_tag };
+
+ fn next(self: *Tokenizer) !?Token {
+ const input = self.input;
+ if (self.pos >= input.len) {
+ return null;
+ }
+
+ var state: State = .data;
+ var result: std.ArrayList(u8) = .empty;
+ var buffer: std.ArrayList(u8) = .empty;
+ var classes: std.ArrayList(u8) = .empty;
+
+ const arena = self.arena;
+ while (true) : (self.pos += 1) {
+ const c: ?u8 = if (self.pos < input.len) input[self.pos] else null;
+ switch (state) {
+ .data => {
+ const char = c orelse return .{ .string = result.items };
+ switch (char) {
+ '&' => {
+ if (try consumeCharacterReference(arena, &result, input, self.pos + 1, null)) |end| {
+ // the loop's increment moves past the reference
+ self.pos = end - 1;
+ } else {
+ try result.append(arena, '&');
+ }
+ },
+ '<' => {
+ if (result.items.len == 0) {
+ state = .tag;
+ } else {
+ return .{ .string = result.items };
+ }
+ },
+ else => try result.append(arena, char),
+ }
+ },
+ .tag => {
+ const char = c orelse return startTag(&result, &classes, "");
+ switch (char) {
+ '\t', '\n', 0x0C, ' ' => state = .start_tag_annotation,
+ '.' => state = .start_tag_class,
+ '/' => state = .end_tag,
+ '0'...'9' => {
+ try result.append(arena, char);
+ state = .timestamp_tag;
+ },
+ '>' => {
+ self.pos += 1;
+ return startTag(&result, &classes, "");
+ },
+ else => {
+ try result.append(arena, char);
+ state = .start_tag;
+ },
+ }
+ },
+ .start_tag => {
+ const char = c orelse return startTag(&result, &classes, "");
+ switch (char) {
+ '\t', 0x0C, ' ' => state = .start_tag_annotation,
+ '\n' => {
+ try buffer.append(arena, char);
+ state = .start_tag_annotation;
+ },
+ '.' => state = .start_tag_class,
+ '>' => {
+ self.pos += 1;
+ return startTag(&result, &classes, "");
+ },
+ else => try result.append(arena, char),
+ }
+ },
+ .start_tag_class => {
+ const char = c orelse {
+ try appendClass(arena, &classes, &buffer);
+ return startTag(&result, &classes, "");
+ };
+ switch (char) {
+ '\t', 0x0C, ' ' => {
+ try appendClass(arena, &classes, &buffer);
+ state = .start_tag_annotation;
+ },
+ '\n' => {
+ try appendClass(arena, &classes, &buffer);
+ try buffer.append(arena, char);
+ state = .start_tag_annotation;
+ },
+ '.' => try appendClass(arena, &classes, &buffer),
+ '>' => {
+ self.pos += 1;
+ try appendClass(arena, &classes, &buffer);
+ return startTag(&result, &classes, "");
+ },
+ else => try buffer.append(arena, char),
+ }
+ },
+ .start_tag_annotation => {
+ const char = c orelse return startTag(&result, &classes, try collapseWhitespace(arena, buffer.items));
+ switch (char) {
+ '&' => {
+ if (try consumeCharacterReference(arena, &buffer, input, self.pos + 1, '>')) |end| {
+ self.pos = end - 1;
+ } else {
+ try buffer.append(arena, '&');
+ }
+ },
+ '>' => {
+ self.pos += 1;
+ return startTag(&result, &classes, try collapseWhitespace(arena, buffer.items));
+ },
+ else => try buffer.append(arena, char),
+ }
+ },
+ .end_tag => {
+ const char = c orelse return .{ .end_tag = result.items };
+ if (char == '>') {
+ self.pos += 1;
+ return .{ .end_tag = result.items };
+ }
+ try result.append(arena, char);
+ },
+ .timestamp_tag => {
+ const char = c orelse return .{ .timestamp = result.items };
+ if (char == '>') {
+ self.pos += 1;
+ return .{ .timestamp = result.items };
+ }
+ try result.append(arena, char);
+ },
+ }
+ }
+ }
+
+ fn startTag(result: *const std.ArrayList(u8), classes: *const std.ArrayList(u8), annotation: []const u8) Token {
+ return .{ .start_tag = .{
+ .name = result.items,
+ .classes = classes.items,
+ .annotation = annotation,
+ } };
+ }
+
+ // The empty class from "<c..a>" still counts: spec appends `buffer` to the
+ // class list unconditionally. We keep the joined form, which is all the
+ // DOM construction needs, so an empty class only adds a separator.
+ fn appendClass(arena: Allocator, classes: *std.ArrayList(u8), buffer: *std.ArrayList(u8)) !void {
+ if (classes.items.len > 0) {
+ try classes.append(arena, ' ');
+ }
+ try classes.appendSlice(arena, buffer.items);
+ buffer.clearRetainingCapacity();
+ }
+};
+
+fn collapseWhitespace(arena: Allocator, value: []const u8) ![]const u8 {
+ const whitespace = "\t\n\x0C\r ";
+ var out: std.ArrayList(u8) = .empty;
+ var it = std.mem.tokenizeAny(u8, value, whitespace);
+ while (it.next()) |word| {
+ if (out.items.len > 0) {
+ try out.append(arena, ' ');
+ }
+ try out.appendSlice(arena, word);
+ }
+ return out.items;
+}
+
+// HTML "consume a character reference" (the version with an additional
+// allowed character that WebVTT references). `start` is the index right after
+// the '&'. Returns the index after the reference, having appended its value to
+// `out`, or null if there's no reference there (nothing appended).
+fn consumeCharacterReference(arena: Allocator, out: *std.ArrayList(u8), input: []const u8, start: usize, additional_allowed: ?u8) !?usize {
+ if (start >= input.len) {
+ return null;
+ }
+ const first = input[start];
+ switch (first) {
+ '\t', '\n', 0x0C, ' ', '<', '&' => return null,
+ '#' => return consumeNumericReference(arena, out, input, start + 1),
+ else => {
+ if (additional_allowed) |c| {
+ if (first == c) return null;
+ }
+ },
+ }
+
+ // Longest match: html5ever's table also holds every prefix of every name,
+ // so we can stop as soon as nothing starts with what we've read.
+ var matched_end: ?usize = null;
+ var codepoints: [2]u32 = undefined;
+ var end = start + 1;
+ while (end <= input.len) : (end += 1) {
+ const name = input[start..end];
+ var found: [2]u32 = undefined;
+ switch (html5ever.html5ever_named_character_reference(name.ptr, name.len, &found)) {
+ 0 => break,
+ 1 => {},
+ else => {
+ matched_end = end;
+ codepoints = found;
+ },
+ }
+ }
+
+ const match = matched_end orelse return null;
+ try appendCodepoint(arena, out, codepoints[0]);
+ if (codepoints[1] != 0) {
+ try appendCodepoint(arena, out, codepoints[1]);
+ }
+ return match;
+}
+
+fn consumeNumericReference(arena: Allocator, out: *std.ArrayList(u8), input: []const u8, start: usize) !?usize {
+ var pos = start;
+ var base: u8 = 10;
+ if (pos < input.len and (input[pos] == 'x' or input[pos] == 'X')) {
+ base = 16;
+ pos += 1;
+ }
+
+ const digits_start = pos;
+ var value: u32 = 0;
+ while (pos < input.len) : (pos += 1) {
+ const digit = std.fmt.charToDigit(input[pos], base) catch break;
+ // anything past the Unicode range is replaced anyway; just don't overflow
+ value = @min(value *| base +| digit, 0x110000);
+ }
+ if (pos == digits_start) {
+ // "&#" or "&#x" without digits: no characters are consumed
+ return null;
+ }
+ if (pos < input.len and input[pos] == ';') {
+ pos += 1;
+ }
+
+ const codepoint: u32 = switch (value) {
+ 0, 0xD800...0xDFFF, 0x110000...std.math.maxInt(u32) => 0xFFFD,
+ 0x80...0x9F => c1_replacements[value - 0x80] orelse value,
+ else => value,
+ };
+ try appendCodepoint(arena, out, codepoint);
+ return pos;
+}
+
+// Windows-1252 interpretation of the C1 range, per the HTML tokenizer.
+const c1_replacements = [32]?u32{
+ 0x20AC, null, 0x201A, 0x0192, 0x201E, 0x2026, 0x2020, 0x2021,
+ 0x02C6, 0x2030, 0x0160, 0x2039, 0x0152, null, 0x017D, null,
+ null, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014,
+ 0x02DC, 0x2122, 0x0161, 0x203A, 0x0153, null, 0x017E, 0x0178,
+};
+
+fn appendCodepoint(arena: Allocator, out: *std.ArrayList(u8), codepoint: u32) !void {
+ var buf: [4]u8 = undefined;
+ const cp: u21 = std.math.cast(u21, codepoint) orelse 0xFFFD;
+ const len = std.unicode.utf8Encode(cp, &buf) catch std.unicode.utf8Encode(0xFFFD, &buf) catch unreachable;
+ try out.appendSlice(arena, buf[0..len]);
+}
+
+// "collect a WebVTT timestamp", requiring the whole input to be consumed.
+// Returns the time in milliseconds.
+pub fn parseTimestamp(input: []const u8) ?u64 {
+ var pos: usize = 0;
+ const value = collectTimestamp(input, &pos) orelse return null;
+ if (pos != input.len) {
+ return null;
+ }
+ return value;
+}
+
+// "collect a WebVTT timestamp" starting at `pos.*`, which is advanced past
+// what was consumed. Returns null on failure, otherwise milliseconds.
+pub fn collectTimestamp(input: []const u8, pos: *usize) ?u64 {
+ var hours_units = false;
+
+ const v1 = collectDigits(input, pos) orelse return null;
+ var value1 = v1.value;
+ if (v1.len != 2 or value1 > 59) {
+ hours_units = true;
+ }
+
+ if (pos.* >= input.len or input[pos.*] != ':') return null;
+ pos.* += 1;
+
+ const v2 = collectDigits(input, pos) orelse return null;
+ if (v2.len != 2) return null;
+ var value2 = v2.value;
+
+ var value3: u64 = undefined;
+ if (hours_units or (pos.* < input.len and input[pos.*] == ':')) {
+ if (pos.* >= input.len or input[pos.*] != ':') return null;
+ pos.* += 1;
+ const v3 = collectDigits(input, pos) orelse return null;
+ if (v3.len != 2) return null;
+ value3 = v3.value;
+ } else {
+ value3 = value2;
+ value2 = value1;
+ value1 = 0;
+ }
+
+ if (pos.* >= input.len or input[pos.*] != '.') return null;
+ pos.* += 1;
+
+ const v4 = collectDigits(input, pos) orelse return null;
+ if (v4.len != 3) return null;
+
+ if (value2 > 59 or value3 > 59) return null;
+
+ return ((value1 *| 60 +| value2) *| 60 +| value3) *| 1000 +| v4.value;
+}
+
+const Digits = struct { value: u64, len: usize };
+
+// Collects a sequence of ASCII digits; null if there are none.
+fn collectDigits(input: []const u8, pos: *usize) ?Digits {
+ const start = pos.*;
+ var value: u64 = 0;
+ while (pos.* < input.len and std.ascii.isDigit(input[pos.*])) : (pos.* += 1) {
+ value = value *| 10 +| (input[pos.*] - '0');
+ }
+ const len = pos.* - start;
+ if (len == 0) return null;
+ return .{ .value = value, .len = len };
+}
+
+// Hours are at least two digits; "100:00:00.000" is valid.
+pub fn formatTimestamp(buf: []u8, ms: u64) ![]const u8 {
+ const hours = ms / 3_600_000;
+ const minutes = (ms / 60_000) % 60;
+ const seconds = (ms / 1000) % 60;
+ const millis = ms % 1000;
+ return std.fmt.bufPrint(buf, "{d:0>2}:{d:0>2}:{d:0>2}.{d:0>3}", .{ hours, minutes, seconds, millis });
+}
+
+const testing = std.testing;
+test "cue_text: timestamps" {
+ try testing.expectEqual(@as(?u64, 500), parseTimestamp("00:00.500"));
+ try testing.expectEqual(@as(?u64, 3_600_500), parseTimestamp("1:00:00.500"));
+ try testing.expectEqual(@as(?u64, 360_000_000), parseTimestamp("100:00:00.000"));
+ try testing.expectEqual(@as(?u64, null), parseTimestamp("0"));
+ try testing.expectEqual(@as(?u64, null), parseTimestamp("0.500"));
+ try testing.expectEqual(@as(?u64, null), parseTimestamp("0:00.500"));
+ try testing.expectEqual(@as(?u64, null), parseTimestamp("00:60.000"));
+
+ var buf: [64]u8 = undefined;
+ try testing.expectEqualStrings("01:00:00.500", try formatTimestamp(&buf, 3_600_500));
+ try testing.expectEqualStrings("100:00:00.000", try formatTimestamp(&buf, 360_000_000));
+}
diff --git a/src/browser/webapi/media/webvtt.zig b/src/browser/webapi/media/webvtt.zig
new file mode 100644
index 000000000..9cdef2313
--- /dev/null
+++ b/src/browser/webapi/media/webvtt.zig
@@ -0,0 +1,495 @@
+// Copyright (C) 2023-2026 Lightpanda (Selecy SAS)
+//
+// Francis Bouvier <[email protected]>
+// Pierre Tachoire <[email protected]>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as
+// published by the Free Software Foundation, either version 3 of the
+// License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+// The WebVTT parser algorithm: https://w3c.github.io/webvtt/#file-parsing
+// Turns a decoded .vtt file into VTTCues (and the VTTRegions they reference).
+
+const std = @import("std");
+
+const Frame = @import("../../Frame.zig");
+const VTTCue = @import("VTTCue.zig");
+const VTTRegion = @import("VTTRegion.zig");
+const cue_text = @import("cue_text.zig");
+
+const Allocator = std.mem.Allocator;
+
+// space, tab, LF, FF, CR
+const whitespace = " \t\n\x0C\r";
+
+// Returns null when the file isn't WebVTT (no signature): the track fails to
+// load. A valid file can have no cues.
+pub fn parse(arena: Allocator, raw: []const u8, frame: *Frame) !?[]*VTTCue {
+ const input = try preprocess(arena, raw);
+
+ if (std.mem.startsWith(u8, input, "WEBVTT") == false) {
+ return null;
+ }
+ if (input.len > 6) {
+ switch (input[6]) {
+ ' ', '\t', '\n' => {},
+ else => return null,
+ }
+ }
+
+ var parser: Parser = .{ .input = input, .arena = arena, .frame = frame };
+
+ // the rest of the signature line
+ _ = parser.collectLine();
+ if (parser.atEnd()) {
+ return parser.cues.items;
+ }
+ parser.pos += 1;
+ if (parser.atEnd()) {
+ return parser.cues.items;
+ }
+
+ // the header: anything up to the first blank line
+ if (input[parser.pos] != '\n') {
+ _ = try parser.collectBlock(true);
+ } else {
+ parser.pos += 1;
+ }
+ parser.skipNewlines();
+
+ while (parser.atEnd() == false) {
+ switch (try parser.collectBlock(false)) {
+ .none => {},
+ .cue => |cue| try parser.cues.append(arena, cue),
+ .region => |region| {
+ // a later definition replaces an earlier one with the same id
+ var i: usize = 0;
+ while (i < parser.regions.items.len) {
+ if (std.mem.eql(u8, parser.regions.items[i]._id, region._id)) {
+ _ = parser.regions.orderedRemove(i);
+ } else {
+ i += 1;
+ }
+ }
+ try parser.regions.append(arena, region);
+ },
+ }
+ parser.skipNewlines();
+ }
+
+ return parser.cues.items;
+}
+
+// UTF-8 decode (BOM stripped, invalid sequences replaced) and then the
+// WebVTT preprocessing: NUL becomes U+FFFD, CRLF and CR become LF.
+fn preprocess(arena: Allocator, raw: []const u8) ![]const u8 {
+ const replacement = "\u{FFFD}";
+
+ var input = raw;
+ if (std.mem.startsWith(u8, input, "\xEF\xBB\xBF")) {
+ input = input[3..];
+ }
+
+ var out: std.ArrayList(u8) = try .initCapacity(arena, input.len);
+ var i: usize = 0;
+ while (i < input.len) {
+ const c = input[i];
+ switch (c) {
+ 0 => {
+ try out.appendSlice(arena, replacement);
+ i += 1;
+ },
+ '\r' => {
+ try out.append(arena, '\n');
+ i += 1;
+ if (i < input.len and input[i] == '\n') {
+ i += 1;
+ }
+ },
+ 0x01...0x0C, 0x0E...0x7F => {
+ try out.append(arena, c);
+ i += 1;
+ },
+ else => {
+ const len = std.unicode.utf8ByteSequenceLength(c) catch 0;
+ if (len == 0 or i + len > input.len or std.unicode.utf8ValidateSlice(input[i .. i + len]) == false) {
+ try out.appendSlice(arena, replacement);
+ i += 1;
+ } else {
+ try out.appendSlice(arena, input[i .. i + len]);
+ i += len;
+ }
+ },
+ }
+ }
+ return out.items;
+}
+
+const Block = union(enum) {
+ none,
+ cue: *VTTCue,
+ region: *VTTRegion,
+};
+
+const Parser = struct {
+ input: []const u8,
+ pos: usize = 0,
+ arena: Allocator,
+ frame: *Frame,
+ seen_cue: bool = false,
+ cues: std.ArrayList(*VTTCue) = .empty,
+ regions: std.ArrayList(*VTTRegion) = .empty,
+
+ fn atEnd(self: *const Parser) bool {
+ return self.pos >= self.input.len;
+ }
+
+ fn collectLine(self: *Parser) []const u8 {
+ const start = self.pos;
+ const end = std.mem.findScalarPos(u8, self.input, start, '\n') orelse self.input.len;
+ self.pos = end;
+ return self.input[start..end];
+ }
+
+ fn skipNewlines(self: *Parser) void {
+ while (self.pos < self.input.len and self.input[self.pos] == '\n') {
+ self.pos += 1;
+ }
+ }
+
+ fn collectBlock(self: *Parser, in_header: bool) !Block {
+ const arena = self.arena;
+ const frame = self.frame;
+
+ var line_count: usize = 0;
+ var previous_position = self.pos;
+ var buffer: std.ArrayList(u8) = .empty;
+ var seen_arrow = false;
+ var cue: ?*VTTCue = null;
+ var is_stylesheet = false;
+ var is_region = false;
+
+ while (true) {
+ const line = self.collectLine();
+ line_count += 1;
+
+ const seen_eof = self.atEnd();
+ if (seen_eof == false) {
+ self.pos += 1;
+ }
+
+ if (std.mem.find(u8, line, "-->") != null) {
+ if (in_header == false and (line_count == 1 or (line_count == 2 and seen_arrow == false))) {
+ seen_arrow = true;
+ previous_position = self.pos;
+ cue = try self.parseCueTimingsAndSettings(line, buffer.items);
+ if (cue != null) {
+ buffer.clearRetainingCapacity();
+ self.seen_cue = true;
+ }
+ } else {
+ self.pos = previous_position;
+ break;
+ }
+ } else if (line.len == 0) {
+ break;
+ } else {
+ if (in_header == false and line_count == 2 and self.seen_cue == false) {
+ if (isBlockHeader(buffer.items, "STYLE")) {
+ is_stylesheet = true;
+ buffer.clearRetainingCapacity();
+ } else if (isBlockHeader(buffer.items, "REGION")) {
+ is_region = true;
+ buffer.clearRetainingCapacity();
+ }
+ }
+ if (buffer.items.len > 0) {
+ try buffer.append(arena, '\n');
+ }
+ try buffer.appendSlice(arena, line);
+ previous_position = self.pos;
+ }
+
+ if (seen_eof) {
+ break;
+ }
+ }
+
+ if (cue) |c| {
+ c._text = try frame.dupeString(buffer.items);
+ return .{ .cue = c };
+ }
+ if (is_stylesheet) {
+ // nothing renders cues, so their stylesheets have no use
+ return .none;
+ }
+ if (is_region) {
+ return .{ .region = try parseRegion(buffer.items, frame) };
+ }
+ return .none;
+ }
+
+ // "collect WebVTT cue timings and settings"
+ fn parseCueTimingsAndSettings(self: *Parser, line: []const u8, id: []const u8) !?*VTTCue {
+ var pos: usize = 0;
+ skipWhitespace(line, &pos);
+ const start = cue_text.collectTimestamp(line, &pos) orelse return null;
+
+ skipWhitespace(line, &pos);
+ if (std.mem.startsWith(u8, line[pos..], "-->") == false) {
+ return null;
+ }
+ pos += 3;
+
+ skipWhitespace(line, &pos);
+ const end = cue_text.collectTimestamp(line, &pos) orelse return null;
+
+ const frame = self.frame;
+ const cue = try VTTCue.init(toSeconds(start), toSeconds(end), "", frame);
+ cue.asCue()._id = try frame.dupeString(id);
+ self.parseCueSettings(cue, line[pos..]);
+ return cue;
+ }
+
+ fn parseCueSettings(self: *const Parser, cue: *VTTCue, settings: []const u8) void {
+ var it = std.mem.tokenizeAny(u8, settings, whitespace);
+ while (it.next()) |setting| {
+ const colon = std.mem.findScalar(u8, setting, ':') orelse continue;
+ if (colon == 0 or colon == setting.len - 1) {
+ continue;
+ }
+ const name = setting[0..colon];
+ const value = setting[colon + 1 ..];
+
+ if (std.mem.eql(u8, name, "region")) {
+ cue._region = null;
+ var i = self.regions.items.len;
+ while (i > 0) {
+ i -= 1;
+ const region = self.regions.items[i];
+ if (std.mem.eql(u8, region._id, value)) {
+ cue._region = region;
+ break;
+ }
+ }
+ } else if (std.mem.eql(u8, name, "vertical")) {
+ if (std.mem.eql(u8, value, "rl")) {
+ cue._vertical = .rl;
+ } else if (std.mem.eql(u8, value, "lr")) {
+ cue._vertical = .lr;
+ }
+ } else if (std.mem.eql(u8, name, "line")) {
+ parseLineSetting(cue, value);
+ } else if (std.mem.eql(u8, name, "position")) {
+ parsePositionSetting(cue, value);
+ } else if (std.mem.eql(u8, name, "size")) {
+ if (parsePercentage(value)) |size| {
+ cue._size = size;
+ }
+ } else if (std.mem.eql(u8, name, "align")) {
+ if (std.meta.stringToEnum(VTTCue.Align, value)) |a| {
+ cue._align = a;
+ }
+ }
+ }
+ }
+};
+
+fn toSeconds(ms: u64) f64 {
+ return @as(f64, @floatFromInt(ms)) / 1000;
+}
+
+fn skipWhitespace(input: []const u8, pos: *usize) void {
+ while (pos.* < input.len and std.mem.findScalar(u8, whitespace, input[pos.*]) != null) {
+ pos.* += 1;
+ }
+}
+
+// /^STYLE[ \t]*$/ and /^REGION[ \t]*$/
+fn isBlockHeader(buffer: []const u8, comptime keyword: []const u8) bool {
+ if (std.mem.startsWith(u8, buffer, keyword) == false) {
+ return false;
+ }
+ for (buffer[keyword.len..]) |c| {
+ if (c != ' ' and c != '\t') return false;
+ }
+ return true;
+}
+
+fn parseLineSetting(cue: *VTTCue, value: []const u8) void {
+ var linepos = value;
+ var linealign: ?[]const u8 = null;
+ if (std.mem.findScalar(u8, value, ',')) |comma| {
+ linepos = value[0..comma];
+ linealign = value[comma + 1 ..];
+ }
+
+ if (std.mem.indexOfAny(u8, linepos, "0123456789") == null) {
+ return;
+ }
+
+ const is_percentage = linepos[linepos.len - 1] == '%';
+ const number = if (is_percentage)
+ parsePercentage(linepos) orelse return
+ else
+ parseLineNumber(linepos) orelse return;
+
+ var line_align: VTTCue.LineAlign = .start;
+ if (linealign) |a| {
+ line_align = std.meta.stringToEnum(VTTCue.LineAlign, a) orelse return;
+ }
+
+ cue._line_align = line_align;
+ cue._line = number;
+ cue._snap_to_lines = is_percentage == false;
+}
+
+// A line number: an optional leading '-', digits, at most one '.' with a
+// digit on each side.
+fn parseLineNumber(linepos: []const u8) ?f64 {
+ var dots: usize = 0;
+ for (linepos, 0..) |c, i| {
+ switch (c) {
+ '0'...'9' => {},
+ '-' => if (i != 0) return null,
+ '.' => {
+ dots += 1;
+ if (dots > 1) return null;
+ if (i == 0 or i == linepos.len - 1) return null;
+ if (std.ascii.isDigit(linepos[i - 1]) == false or std.ascii.isDigit(linepos[i + 1]) == false) return null;
+ },
+ else => return null,
+ }
+ }
+ const number = std.fmt.parseFloat(f64, linepos) catch return null;
+ // the HTML rules for parsing floating-point numbers fail on what doesn't
+ // fit in a double, and never yield -0
+ if (std.math.isFinite(number) == false) {
+ return null;
+ }
+ return number + 0;
+}
+
+fn parsePositionSetting(cue: *VTTCue, value: []const u8) void {
+ var colpos = value;
+ var colalign: ?[]const u8 = null;
+ if (std.mem.findScalar(u8, value, ',')) |comma| {
+ colpos = value[0..comma];
+ colalign = value[comma + 1 ..];
+ }
+
+ const number = parsePercentage(colpos) orelse return;
+
+ var position_align: VTTCue.PositionAlign = .auto;
+ if (colalign) |a| {
+ position_align = std.meta.stringToEnum(VTTCue.PositionAlign, a) orelse return;
+ // "auto" isn't a valid value in the setting
+ if (position_align == .auto) return;
+ }
+
+ cue._position_align = position_align;
+ cue._position = number;
+}
+
+// "parse a percentage string": /^\d+(\.\d+)?%$/, within [0, 100]
+fn parsePercentage(input: []const u8) ?f64 {
+ if (input.len < 2 or input[input.len - 1] != '%') {
+ return null;
+ }
+ const number = input[0 .. input.len - 1];
+
+ var i: usize = 0;
+ while (i < number.len and std.ascii.isDigit(number[i])) i += 1;
+ if (i == 0) return null;
+ if (i < number.len) {
+ if (number[i] != '.') return null;
+ i += 1;
+ const fraction_start = i;
+ while (i < number.len and std.ascii.isDigit(number[i])) i += 1;
+ if (i == fraction_start or i != number.len) return null;
+ }
+
+ const value = std.fmt.parseFloat(f64, number) catch return null;
+ if (value < 0 or value > 100) {
+ return null;
+ }
+ return value;
+}
+
+// "parse the WebVTT region settings"
+fn parseRegion(settings: []const u8, frame: *Frame) !*VTTRegion {
+ const region = try VTTRegion.init(frame);
+
+ var it = std.mem.tokenizeAny(u8, settings, whitespace);
+ while (it.next()) |setting| {
+ const colon = std.mem.findScalar(u8, setting, ':') orelse continue;
+ if (colon == 0 or colon == setting.len - 1) {
+ continue;
+ }
+ const name = setting[0..colon];
+ const value = setting[colon + 1 ..];
+
+ if (std.mem.eql(u8, name, "id")) {
+ region._id = try frame.dupeString(value);
+ } else if (std.mem.eql(u8, name, "width")) {
+ if (parsePercentage(value)) |width| {
+ region._width = width;
+ }
+ } else if (std.mem.eql(u8, name, "lines")) {
+ for (value) |c| {
+ if (std.ascii.isDigit(c) == false) break;
+ } else {
+ region._lines = std.fmt.parseInt(u32, value, 10) catch continue;
+ }
+ } else if (std.mem.eql(u8, name, "regionanchor")) {
+ const x, const y = parseAnchor(value) orelse continue;
+ region._region_anchor_x = x;
+ region._region_anchor_y = y;
+ } else if (std.mem.eql(u8, name, "viewportanchor")) {
+ const x, const y = parseAnchor(value) orelse continue;
+ region._viewport_anchor_x = x;
+ region._viewport_anchor_y = y;
+ } else if (std.mem.eql(u8, name, "scroll")) {
+ if (std.mem.eql(u8, value, "up")) {
+ region._scroll = .up;
+ }
+ }
+ }
+ return region;
+}
+
+fn parseAnchor(value: []const u8) ?struct { f64, f64 } {
+ const comma = std.mem.findScalar(u8, value, ',') orelse return null;
+ const x = parsePercentage(value[0..comma]) orelse return null;
+ const y = parsePercentage(value[comma + 1 ..]) orelse return null;
+ return .{ x, y };
+}
+
+const testing = std.testing;
+test "webvtt: parsePercentage" {
+ try testing.expectEqual(@as(?f64, 50), parsePercentage("50%"));
+ try testing.expectEqual(@as(?f64, 12.5), parsePercentage("12.5%"));
+ try testing.expectEqual(@as(?f64, null), parsePercentage("101%"));
+ try testing.expectEqual(@as(?f64, null), parsePercentage("5.%"));
+ try testing.expectEqual(@as(?f64, null), parsePercentage(".5%"));
+ try testing.expectEqual(@as(?f64, null), parsePercentage("-5%"));
+ try testing.expectEqual(@as(?f64, null), parsePercentage("50"));
+}
+
+test "webvtt: parseLineNumber" {
+ try testing.expectEqual(@as(?f64, -5), parseLineNumber("-5"));
+ try testing.expect(std.math.signbit(parseLineNumber("-0").?) == false);
+ try testing.expectEqual(@as(?f64, 1.5), parseLineNumber("1.5"));
+ try testing.expectEqual(@as(?f64, null), parseLineNumber("1-5"));
+ try testing.expectEqual(@as(?f64, null), parseLineNumber("1..5"));
+ try testing.expectEqual(@as(?f64, null), parseLineNumber("-.5"));
+}
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index d362a34f0..34f2541ba 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -2088,6 +2088,7 @@ pub const Request = struct {
stylesheet,
eventsource,
image,
+ text_track,
worker,
// Allowed Values: Document, Stylesheet, Image, Media, Font, Script,
@@ -2103,6 +2104,7 @@ pub const Request = struct {
.stylesheet => "Stylesheet",
.eventsource => "EventSource",
.image => "Image",
+ .text_track => "TextTrack",
.worker => "Script",
};
}
@@ -2670,6 +2672,7 @@ pub const Transfer = struct {
.script => "script",
.stylesheet => "style",
.image => "image",
+ .text_track => "track",
.worker => "worker",
};
}
@@ -3416,6 +3419,7 @@ pub const Transfer = struct {
.stylesheet => "link",
.eventsource, .worker => "other",
.image => "img",
+ .text_track => "track",
},
.protocol = t.protocol,
.start = t.start_time,
diff --git a/src/network/adblock/Engine.zig b/src/network/adblock/Engine.zig
index 298f4e6ba..8f91eebb4 100644
--- a/src/network/adblock/Engine.zig
+++ b/src/network/adblock/Engine.zig
@@ -148,7 +148,7 @@ pub const Request = struct {
.stylesheet => .{ .stylesheet = true },
.xhr, .fetch => .{ .xmlhttprequest = true },
.image => .{ .image = true },
- .eventsource => .{ .other = true },
+ .eventsource, .text_track => .{ .other = true },
.worker => .{ .script = true },
};
diff --git a/src/rust/html5ever/lib.rs b/src/rust/html5ever/lib.rs
index bb9bbd8e2..8b0d2e272 100644
--- a/src/rust/html5ever/lib.rs
+++ b/src/rust/html5ever/lib.rs
@@ -451,6 +451,30 @@ pub extern "C" fn encoding_max_encode_buffer_length(
.unwrap_or(input_len * 10)
}
+/// Looks `name` (an HTML named character reference without its leading '&')
+/// up in html5ever's table. Returns 0 when no reference starts with `name`, 1
+/// when `name` is only a prefix of one, and 2 for a full reference, whose
+/// codepoints are written to `out` (the second is 0 for single-codepoint ones).
+#[no_mangle]
+pub extern "C" fn html5ever_named_character_reference(
+ name: *const c_uchar,
+ len: usize,
+ out: *mut [u32; 2],
+) -> u8 {
+ let bytes = unsafe { std::slice::from_raw_parts(name, len) };
+ let Ok(name) = std::str::from_utf8(bytes) else {
+ return 0;
+ };
+ match html5ever::data::NAMED_ENTITIES.get(name) {
+ None => 0,
+ Some(&(0, _)) => 1,
+ Some(&(c1, c2)) => {
+ unsafe { *out = [c1, c2] };
+ 2
+ }
+ }
+}
+
#[no_mangle]
pub extern "C" fn html5ever_parse_fragment(
html: *mut c_uchar,
diff --git a/src/server/cdp/domains/fetch.zig b/src/server/cdp/domains/fetch.zig
index b32ed5a55..e6dd5cb25 100644
--- a/src/server/cdp/domains/fetch.zig
+++ b/src/server/cdp/domains/fetch.zig
@@ -656,6 +656,7 @@ pub const InterceptState = struct {
.script, .worker => .Script,
.stylesheet => .Stylesheet,
.image => .Image,
+ .text_track => .TextTrack,
};
}
diff --git a/src/server/cdp/domains/network.zig b/src/server/cdp/domains/network.zig
index 5eb22942e..4407d3986 100644
--- a/src/server/cdp/domains/network.zig
+++ b/src/server/cdp/domains/network.zig
@@ -793,7 +793,7 @@ fn initialPriority(resource_type: HttpClient.Request.ResourceType) []const u8 {
return switch (resource_type) {
.document, .stylesheet => "VeryHigh",
.script, .worker, .xhr, .fetch, .eventsource => "High",
- .image => "Low",
+ .image, .text_track => "Low",
};
}