lib/HTML/Object/DOM/TextTrack.pm

filter: Statementstmt 38.7% 24 / 62 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 53.3% 8 / 15 Podpod 100% 7 / 7 totaltotal 37.5% 39 / 104 CC 10 SCAR 23.7
CC
10
Cov
30%
init
20.0
addCue
20.0
removeCue
20.0
CRAP 10.7
« blib/lib/HTML/Object/DOM/Implementation.pm ↑ Summary lib/HTML/Object/DOM/XPathEvaluator.pm »
1 ##----------------------------------------------------------------------------
2 ## HTML Object - ~/lib/HTML/Object/DOM/TextTrack.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/12/27
7 ## Modified 2022/09/18
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTML::Object::DOM::TextTrack;
15 BEGIN
16 {
17 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ warnings::register_categories( 'HTML::Object' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶ use parent qw( HTML::Object::DOM::MediaTrack );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use HTML::Object::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 our $VERSION = 'v0.2.0';
24 };
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 sub init
30 {
31 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=4 SCAR=30.0
Pod documented
32 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ < 3 0 0 0
33 message => sprintf( "At least 1 argument required, but only %d passed", scalar( @_ ) ),
34 class => 'HML::Object::TypeError',
35 }) ) if( scalar( @_ ) < 3 );
36 0 my $kind = shift( @_ );
37 0 my $label = shift( @_ );
38 0 my $lang = shift( @_ );
39 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $kind =~ /^\w+$/ 0 0 0
40 message => "kind argument provided is not a string",
41 class => 'HML::Object::TypeError',
42 }) ) if( $kind !~ /^\w+$/ );
43 0 $self->{cues} = [];
44 # If set, this should be the same as the id in HTML::Object::DOM::Element::Track
45 0 $self->{id} = undef;
46 0 $self->{kind} = $kind;
47 0 $self->{label} = $label;
48 0 $self->{language} = $lang;
49 0 $self->{mode} = 'hidden';
50 0 $self->{_init_strict_use_sub} = 1;
51 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
52 0 return( $self );
53 }
54  
55 # Note: property
56 0 ▶sub activeCues { return( shift->_set_get_object( 'activecues', 'HTML::Object::DOM::TextTrackCueList', @_ ) ); }
Subroutine activeCues: not called CC=1 SCAR=6.9
Pod documented
57  
58 sub addCue
59 {
60 0 ▶ my $self = shift( @_ );
Subroutine addCue: not called CC=4 SCAR=30.0
Pod documented
61 0 ▶ my $cue = shift( @_ ) || return( $self->error({
Condition0%0/2
shift @_ || (return $self->error({'message', 'No cue object was provided.', 'class', 'HTML::Object::TypeError'}))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error({'message', 'No cue object was provided.', 'class', 'HTML::Object::TypeError'}))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
62 message => "No cue object was provided.",
63 class => 'HTML::Object::TypeError',
64 }) );
65 # HTML::Object::DOM::VTTCue inherits from HTML::Object::DOM::TextTrackCue
66 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($cue, "HTML::Object::DOM::TextTrackCue") 0 0 0
67 message => "Cue provided is not a HTML::Object::DOM::TextTrackCue object.",
68 class => 'HTML::Object::TypeError',
69 }) ) if( !$self->_is_a( $cue => 'HTML::Object::DOM::TextTrackCue' ) );
70 0 my $cues = $self->cues;
71 0 my $pos = $cues->pos( $cue );
72 0 ▶ if( !defined( $cue ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cue) 0 0 0
73 {
74 0 $cue->track( $self );
75 0 $cues->push( $cue );
76 }
77 0 return( $self );
78 }
79  
80 # Note: property
81 0 ▶sub cues { return( shift->_set_get_object_array_object( 'cues', 'HTML::Object::DOM::TextTrackCueList', @_ ) ); }
Subroutine cues: not called CC=1 SCAR=6.9
Pod documented
82  
83 # Note: property id inherited
84  
85 # Note: property
86 0 ▶sub inBandMetadataTrackDispatchType : lvalue { return( shift->_set_get_scalar_as_object( 'inbandmetadatatrackdispatchtype', @_ ) ); }
Subroutine inBandMetadataTrackDispatchType: not called CC=1 SCAR=6.9
Pod documented
87  
88 # Note: property kind inherited
89  
90 # Note: property label inherited
91  
92 # Note: property language inherited
93  
94 # Note: property
95 0 ▶sub mode : lvalue { return( shift->_set_get_scalar_as_object( 'mode', @_ ) ); }
Subroutine mode: not called CC=1 SCAR=6.9
Pod documented
96  
97 # Note: method parent is used and inherited from HTML::Object::Element via HTML::Object::EventTarget
98  
99 sub removeCue
100 {
101 0 ▶ my $self = shift( @_ );
Subroutine removeCue: not called CC=4 SCAR=30.0
Pod documented
102 0 ▶ my $cue = shift( @_ ) || return( $self->error({
Condition0%0/2
shift @_ || (return $self->error({'message', 'No cue object was provided.', 'class', 'HTML::Object::TypeError'}))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error({'message', 'No cue object was provided.', 'class', 'HTML::Object::TypeError'}))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
103 message => "No cue object was provided.",
104 class => 'HTML::Object::TypeError',
105 }) );
106 0 ▶ return( $self->error({
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($cue, "HTML::Object::DOM::TextTrackCue") 0 0 0
107 message => "Cue provided is not a HTML::Object::DOM::TextTrackCue object.",
108 class => 'HTML::Object::TypeError',
109 }) ) if( !$self->_is_a( $cue => 'HTML::Object::DOM::TextTrackCue' ) );
110 0 my $cues = $self->cues;
111 0 my $pos = $cues->pos( $cue );
112 0 ▶ if( defined( $pos ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) 0 0 0
113 {
114 0 $cues->splice( $pos, 1 );
115 0 $cue->track( undef );
116 0 return( $cue );
117 }
118 0 return;
119 }
120  
121 1 1;
122 # NOTE: POD
123 __END__
124  
125 =encoding utf-8
126  
127 =head1 NAME
128  
129 HTML::Object::DOM::TextTrack - HTML Object DOM Text Track Class
130  
131 =head1 SYNOPSIS
132  
133 use HTML::Object::DOM::TextTrack;
134 my $track = HTML::Object::DOM::TextTrack->new ||
135 die( HTML::Object::DOM::TextTrack->error, "\n" );
136  
137 <video controls>
138 <source src="https://example.org/some/where/videos/video.webm" type="video/webm" />
139 <source src="https://example.org/some/where/videos/video.mp4" type="video/mp4" />
140 <track src="video-subtitles-en.vtt" label="English captions" kind="captions" srclang="en" default />
141 <track src="video-subtitles-ja.vtt" label="日本語字幕" kind="captions" srclang="ja" />
142 <p>This browser does not support the video element.</p>
143 </video>
144  
145 =head1 VERSION
146  
147 v0.2.0
148  
149 =head1 DESCRIPTION
150  
151 The C<TextTrack> interface—part of the Web API for handling C<WebVTT> (text tracks on media presentations)—describes and controls the text track associated with a particular L<<track> element|HTML::Object::DOM::Element::Track>.
152  
153 C<TextTrack> class is only a programmatic interface with no impact on the DOM whereas L<track element|HTML::Object::DOM::Element::Track> accesses and modifies the DOM and makes use of this programmatic interface with C<TextTrack> through its method L<HTML::Object::DOM::Element::Track/track>
154  
155 This inherits from L<HTML::Object::DOM::MediaTrack>
156  
157 =head1 INHERITANCE
158  
159 +-----------------------+ +---------------------------+ +-------------------------------+ +------------------------------+
160 | HTML::Object::Element | --> | HTML::Object::EventTarget | --> | HTML::Object::DOM::MediaTrack | --> | HTML::Object::DOM::TextTrack |
161 +-----------------------+ +---------------------------+ +-------------------------------+ +------------------------------+
162  
163 =head1 CONSTRUCTOR
164  
165 =head2 new
166  
167 This takes a track C<kind>, C<label> and C<language> and this returns a new C<HTML::Object::DOM::TextTrack> object.
168  
169 Possible values for C<kind> are:
170  
171 =over 4
172  
173 =item caption
174  
175 =item chapters
176  
177 =item descriptions
178  
179 =item metadata
180  
181 =item subtitles
182  
183 =back
184  
185 C<label> is a string specifying the label for the text track. Is used to identify the text track for the users.
186  
187 C<language> in iso 639 format (e.g. C<en-US> or C<ja-JP>). Seel also L<rfc5646|https://datatracker.ietf.org/doc/html/rfc5646>
188  
189 =head1 PROPERTIES
190  
191 Inherits properties from its parent L<HTML::Object::DOM::MediaTrack>
192  
193 =head2 activeCues
194  
195 Since there is no notion of 'active cues' under perl environment, you can access the L<TextTrackCueList object|HTML::Object::DOM::TextTrackCueList> this returns and add cues to it on your own.
196  
197 Normally, under JavaScript, this returns a L<TextTrackCueList|HTML::Object::DOM::TextTrackCueList> object listing the currently active set of text track cues. Track cues are active if the current playback position of the media is between the cues' start and end times. Thus, for displayed cues such as captions or subtitles, the active cues are currently being displayed.
198  
199 Example:
200  
201 my $video = $doc->querySelector('video');
202 my $track = $video->addTextTrack("captions", "Captions", "en");
203 $track->mode = 'showing';
204 say( $track->activeCues );
205  
206 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/activeCues>
207  
208 =head2 cues
209  
210 Read-only.
211  
212 A L<TextTrackCueList|HTML::Object::DOM::TextTrackCueList> which contains all of the track's cues.
213  
214 Example:
215  
216 my $video = $doc->querySelector('video');
217 my $track = $video->addTextTrack('captions', 'Captions', 'en');
218 $track->mode = 'showing';
219 $track->addCue( HTML::Object::DOM::VTTCue->new(0, 0.9, 'Hildy!') );
220 $track->addCue( HTML::Object::DOM::VTTCue->new(1, 1.4, 'How are you?') );
221 say( $track->cues );
222  
223 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/cues>
224  
225 =head2 id
226  
227 A string which identifies the track, if it has one. If it does not have an ID, then this value is an empty string (""). If the C<TextTrack> is associated with a L<<track> element|HTML::Object::DOM::Element::Track>, then the track's ID matches the element's ID.
228  
229 Normally this should be read-only, but by design you can change it; only be careful if you do so.
230  
231 Example:
232  
233 my $video = $doc->querySelector('video');
234 my $track = $video->addTextTrack("captions", "Captions", "en");
235 $track->mode = 'showing';
236 say( $track->id );
237  
238 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/id>
239  
240 =head2 inBandMetadataTrackDispatchType
241  
242 Returns a string which indicates the track's in-band metadata track dispatch type.
243  
244 Example:
245  
246 my $video = $doc->querySelector('video');
247 my $track = $video->addTextTrack("captions", "Captions", "en");
248 $track->mode = 'showing';
249 say( $track->inBandMetadataTrackDispatchType );
250  
251 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/inBandMetadataTrackDispatchType>
252  
253 =head2 kind
254  
255 Returns a string indicating what kind of text track the C<TextTrack> describes. It must be one of the permitted values.
256  
257 Normally this should be read-only, but by design you can change it; only be careful if you do so.
258  
259 Example:
260  
261 my $video = $doc->querySelector('video');
262 my $track = $video->addTextTrack("captions", "Captions", "en");
263 $track->mode = 'showing';
264 say( $track->kind );
265  
266 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/kind>
267  
268 =head2 label
269  
270 A human-readable string which contains the text track's label, if one is present; otherwise, this is an empty string (""), in which case a custom label may need to be generated by your code using other attributes of the track, if the track's label needs to be exposed to the user.
271  
272 Normally this should be read-only, but by design you can change it; only be careful if you do so.
273  
274 Example:
275  
276 my $video = $doc->querySelector('video');
277 my $track = $video->addTextTrack("captions", "Captions", "en");
278 $track->mode = 'showing';
279 say( $track->label );
280  
281 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/label>
282  
283 =head2 language
284  
285 A string which specifies the text language in which the text track's contents is written. The value must adhere to the format specified in RFC 5646: Tags for Identifying Languages (also known as BCP 47), just like the HTML lang attribute. For example, this can be "en-US" for United States English or "pt-BR" for Brazilian Portuguese.
286  
287 Normally this should be read-only, but by design you can change it; only be careful if you do so.
288  
289 Example:
290  
291 my $video = $doc->querySelector('video');
292 my $track = $video->addTextTrack("captions", "Captions", "en-US");
293 $track->mode = 'showing';
294 say( $track->language ); # en-US
295  
296 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/language>
297  
298 =head2 mode
299  
300 A string specifying the track's current mode, which must be one of the permitted values. Changing this property's value changes the track's current mode to match. The default is C<disabled>, unless the C<<track>> element's default boolean attribute is specified, in which case the default mode is C<started>.
301  
302 Possible values are: C<disabled>, C<hidden>, C<showing>, but you are free to use whatever your want of course.
303  
304 Example:
305  
306 $doc->addEventListener( load => sub
307 {
308 my $trackElem = $doc->querySelector("track");
309 my $track = $trackElem->track;
310  
311 $track->mode = 'showing';
312  
313 for( my $index = 0; index < $track->cues->length; index++ )
314 {
315 my $cue = $track->cues->[ $index ];
316 $cue->pauseOnExit = 1; # true
317 };
318 });
319  
320 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/mode>
321  
322 =head1 METHODS
323  
324 Inherits methods from its parent L<HTML::Object::DOM::MediaTrack>
325  
326 =head2 addCue
327  
328 Adds a cue (specified as a L<TextTrackCue object|HTML::Object::DOM::TextTrackCue>) to the track's list of cues.
329  
330 When doing so, this will set the L<TextTrackCue object|HTML::Object::DOM::TextTrackCue> C<track> property to this object, and will also check it is not already added to avoid duplicates.
331  
332 It returns the current C<TextTrack> object upon success or upon error it returns C<undef> and sets an L<error|Module::Generic/error>
333  
334 Example:
335  
336 my $video = $doc->querySelector('video');
337 my $track = $video->addTextTrack("captions", "Captions", "en");
338 $track->mode = 'showing';
339 $track->addCue( HTML::Object::DOM::VTTCue->new(0, 0.9, 'Hildy!') );
340 $track->addCue( HTML::Object::DOM::VTTCue->new(1, 1.4, 'How are you?') );
341  
342 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/addCue>
343  
344 =head2 removeCue
345  
346 Removes a cue (specified as a L<TextTrackCue object|HTML::Object::DOM::TextTrackCue>) from the track's list of cues.
347  
348 When doing so, this checks the object provided does exists among our cues or it returns C<undef> if it does not exist.
349  
350 If found, it removes it from our cues, unset the cue's C<track> property.
351  
352 It returns the removed C<TextTrackCue> object upon success or upon error it returns C<undef> and sets an L<error|Module::Generic/error>
353  
354 Example:
355  
356 my $video = $doc->querySelector('video');
357 my $track = $video->addTextTrack("captions", "Captions", "en");
358 $track->mode = 'showing';
359 my $cue = HTML::Object::DOM::VTTCue->new(0, 0.9, 'Hildy!');
360 $track->addCue( $cue );
361 $track->removeCue( $cue );
362  
363 See also L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack/removeCue>
364  
365 =head1 AUTHOR
366  
367 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
368  
369 =head1 SEE ALSO
370  
371 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/API/TextTrack>, L<VideoJS documentation|https://docs.videojs.com/tutorial-text-tracks.html>, L<rfc5646 for language codes|https://datatracker.ietf.org/doc/html/rfc5646>>, L<W3C specificiations|https://html.spec.whatwg.org/multipage/media.html#texttrack>
372  
373 =head1 COPYRIGHT & LICENSE
374  
375 Copyright(c) 2021 DEGUEST Pte. Ltd.
376  
377 All rights reserved
378  
379 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
380  
381 =cut
« blib/lib/HTML/Object/DOM/Implementation.pm ↑ Summary lib/HTML/Object/DOM/XPathEvaluator.pm »