-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathaudio_video.zig
More file actions
671 lines (619 loc) · 22.1 KB
/
Copy pathaudio_video.zig
File metadata and controls
671 lines (619 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const root = @import("../../root.zig");
const Span = root.Span;
const Ast = @import("../Ast.zig");
const Element = @import("../Element.zig");
const Model = Element.Model;
const Categories = Element.Categories;
const Attribute = @import("../Attribute.zig");
const ValidatingIterator = Attribute.ValidatingIterator;
const AttributeSet = Attribute.AttributeSet;
pub const video: Element = .{
.tag = .video,
.model = audio.model,
.meta = audio.meta,
.reasons = audio.reasons,
.attributes = audio.attributes,
.content = audio.content,
.desc =
\\The `<video>` HTML element embeds a media player which supports video
\\playback into the document. You can use `<video>` for audio content
\\as well, but the `<audio>` element may provide a more appropriate
\\user experience.
\\
\\ - [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/video)
\\ - [HTML Spec](https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-video-element)
,
};
pub const audio: Element = .{
.tag = .audio,
.model = .{
.categories = .{
.flow = true,
.phrasing = true,
.embedded = true,
},
.content = .transparent,
},
.meta = .{
.categories_superset = .{
.flow = true,
.phrasing = true,
.embedded = true,
.interactive = true,
},
},
.reasons = .{
.categories = .{
.interactive = .{
.reject = "presence of [control]",
.accept = "missing [control]",
},
},
},
.attributes = .{ .dynamic = validateAttrs },
.content = .{
.custom = .{
.validate = validateContent,
.completions = completionsContent,
},
},
.desc =
\\The `<audio>` HTML element is used to embed sound content in
\\documents. It may contain one or more audio sources, represented
\\using the `src` attribute or the `<source>` element: the browser will
\\choose the most suitable one. It can also be the destination for
\\streamed media, using a `MediaStream`.
\\
\\ - [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/audio)
\\ - [HTML Spec](https://html.spec.whatwg.org/multipage/text-level-semantics.html#the-audio-element)
,
};
pub const attributes: AttributeSet = .init(&.{
.{
.name = "src",
.model = .{
.rule = .{ .url = .not_empty },
.desc =
\\The URL of the media to embed. This is subject to HTTP
\\access controls. This is optional; you may instead use the
\\`<source>` element within the element to specify the media
\\to embed.
,
},
},
.{
.name = "autoplay",
.model = .{
.rule = .bool,
.desc =
\\If specified, the media will automatically begin playback as
\\soon as it can do so, without waiting for the entire media
\\file to finish downloading.
,
},
},
.{
.name = "loop",
.model = .{
.rule = .bool,
.desc =
\\If specified, the media player will automatically seek back
\\to the start upon reaching the end of the media.
,
},
},
.{
.name = "muted",
.model = .{
.rule = .bool,
.desc =
\\If specified, the audio will be initially muted.
,
},
},
.{
.name = "controls",
.model = .{
.rule = .bool,
.desc =
\\If specified, the browser will offer controls to allow the
\\user to control media playback, including volume, seeking,
\\and pause/resume playback.
,
},
},
.{
.name = "preload",
.model = .{
.rule = .{
.list = .init(.missing_or_empty, .one, &.{
.{
.label = "none",
.desc =
\\Indicates that the media should not be preloaded.
,
},
.{
.label = "auto",
.desc =
\\Indicates that the whole media file can be downloaded, even if the user is not expected to use it.
,
},
.{
.label = "metadata",
.desc =
\\Indicates that only media metadata (e.g., length) is fetched.
,
},
}),
},
.desc =
\\Provides a hint to the browser about what the author thinks
\\will lead to the best user experience.
\\
\\- The autoplay attribute has precedence over preload. If autoplay
\\ is specified, the browser would obviously need to start
\\ downloading the media for playback.
\\
\\- The browser is not forced by the specification to follow the
\\ value of this attribute; it is a mere hint.
,
},
},
.{
.name = "crossorigin",
.model = .{
.rule = .cors,
.desc =
\\Indicates whether to use CORS to fetch the related media file.
\\CORS-enabled resources can be reused in the `<canvas>` element
\\without being tainted.
\\
\\When not present, the resource is fetched without a CORS request
\\(i.e., without sending the `Origin:` HTTP header), preventing its
\\non-tainted use in `<canvas>` elements.
,
},
},
});
pub fn validateAttrs(
gpa: Allocator,
errors: *std.ArrayListUnmanaged(Ast.Error),
src: []const u8,
nodes: []const Ast.Node,
parent_idx: u32,
node_idx: u32,
vait: *Attribute.ValidatingIterator,
) !Model {
var has_controls = false;
while (try vait.next(gpa, src)) |attr| {
const name = attr.name.slice(src);
const attr_model = blk: {
if (attributes.index(name)) |idx| {
if (attributes.comptimeIndex("controls") == idx) has_controls = true;
break :blk attributes.list[idx].model;
}
break :blk Attribute.global.get(name) orelse {
if (Attribute.isData(name)) continue;
try errors.append(gpa, .{
.tag = .invalid_attr,
.main_location = attr.name,
.node_idx = node_idx,
});
continue;
};
};
try attr_model.rule.validate(gpa, errors, src, node_idx, attr);
}
const categories: Categories = .{
.flow = true,
.phrasing = true,
// .embedded = true,
.interactive = has_controls,
};
const parent = nodes[parent_idx];
return .{
.categories = categories,
.content = categories.intersect(parent.model.content),
};
}
pub fn validateContent(
gpa: Allocator,
nodes: []const Ast.Node,
seen_attrs: *std.StringHashMapUnmanaged(Span),
seen_ids: *std.StringHashMapUnmanaged(Span),
errors: *std.ArrayListUnmanaged(Ast.Error),
src: []const u8,
parent_idx: u32,
) !void {
const parent = nodes[parent_idx];
const parent_span, const has_src = blk: {
var it = parent.startTagIterator(src, .html);
while (it.next(src)) |attr| {
if (attributes.index(attr.name.slice(src))) |idx| {
if (attributes.comptimeIndex("src") == idx) {
break :blk .{ it.name_span, true };
}
}
}
break :blk .{ it.name_span, false };
};
var state: enum { source, track, rest } = if (has_src) .track else .source;
var first_default: ?Span = null;
var child_idx = parent.first_child_idx;
while (child_idx != 0) {
const child = nodes[child_idx];
defer child_idx = child.next_idx;
const child_name = child.span(src);
state: switch (state) {
.source => {
if (child.kind != .source) {
state = .track;
continue :state .track;
}
try validateSource(
gpa,
errors,
seen_attrs,
seen_ids,
src,
parent.kind,
child.open,
child_idx,
);
},
.track => {
if (child.kind == .source) {
try errors.append(gpa, .{
.tag = .{
.invalid_nesting = .{
.span = parent_span,
.reason = if (has_src)
\\with [src] present, no <source> elements are allowed
else
\\<source> elements must go before all other
,
},
},
.main_location = child_name,
.node_idx = child_idx,
});
continue;
}
if (child.kind != .track) {
state = .rest;
continue :state .rest;
}
try validateTrack(
gpa,
errors,
seen_attrs,
seen_ids,
src,
parent.kind,
child.open,
child_idx,
&first_default,
);
},
.rest => {
if (child.kind == .source) {
try errors.append(gpa, .{
.tag = .{
.invalid_nesting = .{
.span = parent_span,
.reason = if (has_src)
\\with [src] present, no <source> elements are allowed
else
\\<source> elements must go before all other
,
},
},
.main_location = child_name,
.node_idx = child_idx,
});
continue;
}
if (child.kind == .track) {
try errors.append(gpa, .{
.tag = .{
.invalid_nesting = .{
.span = parent_span,
.reason =
\\<track> elements must go before all non-<source> elements
,
},
},
.main_location = child_name,
.node_idx = child_idx,
});
continue;
}
if (child.kind == .audio or child.kind == .video) {
try errors.append(gpa, .{
.tag = .{
.invalid_nesting = .{ .span = parent_span },
},
.main_location = child_name,
.node_idx = child_idx,
});
continue;
}
},
}
}
}
fn validateSource(
gpa: Allocator,
errors: *std.ArrayList(Ast.Error),
seen_attrs: *std.StringHashMapUnmanaged(Span),
seen_ids: *std.StringHashMapUnmanaged(Span),
src: []const u8,
parent_kind: Ast.Kind,
node_span: Span,
node_idx: u32,
) !void {
assert(parent_kind == .audio or parent_kind == .video);
const source_attrs = comptime Attribute.element_attrs.get(.source);
var vait: ValidatingIterator = .init(
errors,
seen_attrs,
seen_ids,
.html,
node_span,
src,
node_idx,
);
var seen_src = false;
while (try vait.next(gpa, src)) |attr| {
const name = attr.name.slice(src);
const model = if (source_attrs.index(name)) |idx| blk: {
switch (idx) {
source_attrs.comptimeIndex("src") => {
seen_src = true;
},
source_attrs.comptimeIndex("type"),
source_attrs.comptimeIndex("media"),
=> {},
else => {
try errors.append(gpa, .{
.tag = .{
.invalid_attr_nesting = .{
.kind = parent_kind,
},
},
.main_location = attr.name,
.node_idx = node_idx,
});
continue;
},
}
break :blk source_attrs.list[idx].model;
} else Attribute.global.get(name) orelse {
if (Attribute.isData(name)) continue;
try errors.append(gpa, .{
.tag = .invalid_attr,
.main_location = attr.name,
.node_idx = node_idx,
});
continue;
};
try model.rule.validate(gpa, errors, src, node_idx, attr);
}
if (!seen_src) return errors.append(gpa, .{
.tag = .{
.missing_required_attr = "src",
},
.main_location = vait.name,
.node_idx = node_idx,
});
}
fn validateTrack(
gpa: Allocator,
errors: *std.ArrayList(Ast.Error),
seen_attrs: *std.StringHashMapUnmanaged(Span),
seen_ids: *std.StringHashMapUnmanaged(Span),
src: []const u8,
parent_kind: Ast.Kind,
node_span: Span,
node_idx: u32,
first_default: *?Span,
) !void {
assert(parent_kind == .audio or parent_kind == .video);
const track_attrs = comptime Attribute.element_attrs.get(.track);
// The src attribute gives the URL of the text track data. The value must be a valid non-empty URL potentially surrounded by spaces. This attribute must be present.
// The srclang attribute gives the language of the text track data. The value must be a valid BCP 47 language tag. This attribute must be present if the element's kind attribute is in the subtitles state.
// The default attribute is a boolean attribute, which, if specified, indicates that the track is to be enabled if the user's preferences do not indicate that another track would be more appropriate.
// Each media element must have no more than one track element child whose kind attribute is in the subtitles or captions state and whose default attribute is specified.
// Each media element must have no more than one track element child whose kind attribute is in the description state and whose default attribute is specified.
// Each media element must have no more than one track element child whose kind attribute is in the chapters metadata state and whose default attribute is specified.
// There is no limit on the number of track elements whose kind attribute is in the metadata state and whose default attribute is specified.
// TODO
// The value of the label attribute, if the attribute is present, must not be the empty string. Furthermore, there must not be two track element children of the same media element whose kind attributes are in the same state, whose srclang attributes are both missing or have values that represent the same language, and whose label attributes are again both missing or both have the same value.
var vait: ValidatingIterator = .init(
errors,
seen_attrs,
seen_ids,
.html,
node_span,
src,
node_idx,
);
var seen_src = false;
var seen_srclang = false;
var seen_default: ?Span = null;
var kind_state: enum {
missing,
subtitles,
captions,
descriptions,
chapters,
metadata,
} = .missing;
while (try vait.next(gpa, src)) |attr| {
const name = attr.name.slice(src);
const model = if (track_attrs.index(name)) |idx| blk: {
switch (idx) {
else => {},
track_attrs.comptimeIndex("src") => {
seen_src = true;
},
track_attrs.comptimeIndex("srclang") => {
seen_srclang = true;
},
track_attrs.comptimeIndex("default") => {
seen_default = attr.name;
},
track_attrs.comptimeIndex("kind") => {
const rule = comptime track_attrs.get("kind").?.rule;
const value = attr.value orelse {
try errors.append(gpa, .{
.tag = .missing_attr_value,
.main_location = attr.name,
.node_idx = node_idx,
});
continue;
};
switch (try rule.list.match(
gpa,
errors,
node_idx,
value.span.start,
value.span.slice(src),
)) {
else => {},
.list => |lidx| kind_state = switch (lidx) {
else => unreachable,
rule.list.comptimeIndex("subtitles") => .subtitles,
rule.list.comptimeIndex("captions") => .captions,
rule.list.comptimeIndex("descriptions") => .descriptions,
rule.list.comptimeIndex("chapters") => .chapters,
rule.list.comptimeIndex("metadata") => .metadata,
},
}
continue;
},
}
break :blk track_attrs.list[idx].model;
} else Attribute.global.get(name) orelse {
if (Attribute.isData(name)) continue;
try errors.append(gpa, .{
.tag = .invalid_attr,
.main_location = attr.name,
.node_idx = node_idx,
});
continue;
};
try model.rule.validate(gpa, errors, src, node_idx, attr);
}
if (!seen_src) try errors.append(gpa, .{
.tag = .{
.missing_required_attr = "[src]",
},
.main_location = vait.name,
.node_idx = node_idx,
});
if ((kind_state == .missing or kind_state == .subtitles) and
!seen_srclang) return errors.append(gpa, .{
.tag = .{
.missing_required_attr = if (kind_state == .missing)
"[srclang] (mandatory when [kind] is not defined)"
else
"[srclang] (mandatory when [kind] is 'subtitles')",
},
.main_location = vait.name,
.node_idx = node_idx,
});
if (kind_state != .metadata) {
if (first_default.*) |fd| {
if (seen_default) |sd| {
try errors.append(gpa, .{
.tag = .{
.duplicate_sibling_attr = fd,
},
.main_location = sd,
.node_idx = node_idx,
});
}
} else if (seen_default) |sd| {
first_default.* = sd;
}
}
}
fn completionsContent(
arena: Allocator,
ast: Ast,
src: []const u8,
parent_idx: u32,
offset: u32,
) error{OutOfMemory}![]const Ast.Completion {
const parent = ast.nodes[parent_idx];
const has_src = blk: {
var it = parent.startTagIterator(src, .html);
while (it.next(src)) |attr| {
if (attributes.index(attr.name.slice(src))) |idx| {
if (attributes.comptimeIndex("src") == idx) {
break :blk true;
}
}
}
break :blk false;
};
var state: enum { source, track, rest } = if (has_src) .track else .source;
var kind_after_cursor: Ast.Kind = .root;
var child_idx = parent.first_child_idx;
while (child_idx != 0) {
const child = ast.nodes[child_idx];
defer child_idx = child.next_idx;
if (child.open.start > offset) {
kind_after_cursor = child.kind;
break;
}
state: switch (state) {
.source => if (child.kind != .source) {
state = .track;
continue :state .track;
},
.track => if (child.kind != .track and child.kind != .source) {
state = .rest;
continue :state .rest;
},
.rest => break,
}
}
const source = comptime Element.all_completions.get(.source);
const track = comptime Element.all_completions.get(.track);
switch (state) {
.source => switch (kind_after_cursor) {
.source => return &.{source},
.track => return &.{ source, track },
else => return Element.simpleCompletions(
arena,
&.{ .source, .track },
parent.model.content,
audio.meta.content_reject,
.{},
),
},
.track => switch (kind_after_cursor) {
.track => return &.{track},
else => return Element.simpleCompletions(
arena,
&.{.track},
parent.model.content,
audio.meta.content_reject,
.{ .forbidden_children = &.{.source} },
),
},
.rest => return Element.simpleCompletions(
arena,
&.{},
parent.model.content,
audio.meta.content_reject,
.{ .forbidden_children = &.{ .source, .track } },
),
}
}