forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsources.js
More file actions
166 lines (145 loc) · 5.38 KB
/
sources.js
File metadata and controls
166 lines (145 loc) · 5.38 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
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {ampMediaElementFor} from './utils';
import {removeElement} from '../../../src/dom';
import {toArray} from '../../../src/types';
/**
* Class handling HTMLMediaElements sources.
*/
export class Sources {
/**
* @param {?string=} srcAttr The 'src' attribute of the media element.
* @param {!Array<!Element>=} srcEls Any child <source> tags of the
* media element.
* @param {!Array<!Element>=} trackEls Any child <track> tags of the
* media element.
*/
constructor(srcAttr = null, srcEls = [], trackEls = []) {
/** @private @const {?string} */
this.srcAttr_ = srcAttr;
/** @private @const {!Array<!Element>} */
this.srcEls_ = srcEls;
/** @private @const {!Array<!Element>} */
this.trackEls_ = trackEls;
}
/**
* Applies track tags to a specified element. This is done in a separate
* method from the source tags, because we must wait for "loadedmetadata"
* video event before doing this.
* @param {!HTMLMediaElement} element The element to adopt the text tracks
* represented by this object.
* @private
*/
applyTracksToElement_(element) {
Array.prototype.forEach.call(this.trackEls_, (trackEl) => {
const track = document.createElement('track');
track.id = trackEl.id;
track.kind = trackEl.kind;
track.label = trackEl.label;
track.srclang = trackEl.srclang;
track.default = trackEl.default;
track.src = trackEl.src;
track.addEventListener('load', () => {
track.mode = 'showing';
element.textTracks[0].mode = 'showing';
});
element.appendChild(track);
});
}
/**
* Applies the src attribute and source tags to a specified element.
* @param {!Window} win
* @param {!HTMLMediaElement} element The element to adopt the sources
* represented by this object.
*/
applyToElement(win, element) {
Sources.removeFrom(win, element);
if (!this.srcAttr_) {
element.removeAttribute('src');
} else {
element.setAttribute('src', this.srcAttr_);
}
Array.prototype.forEach.call(this.srcEls_, (srcEl) =>
element.appendChild(srcEl)
);
if (element.changedSources) {
element.changedSources();
}
if (this.trackEls_.length > 0) {
// Wait for "loadedmetadata" before adding tracks.
// Firefox adds tracks, but does not toggle them on unless video metadata
// is loaded first.
if (element.readyState >= 1 /* HAVE_METADATA */) {
this.applyTracksToElement_(element);
} else {
const addTracksHandler = () => {
element.removeEventListener('loadedmetadata', addTracksHandler);
this.applyTracksToElement_(element);
};
element.addEventListener('loadedmetadata', addTracksHandler);
}
}
}
/**
* Removes and returns the sources from a specified element.
* @param {!Window} win
* @param {!Element} element The element whose sources should be removed and
* returned.
* @return {!Sources} An object representing the sources of the specified
* element.
*/
static removeFrom(win, element) {
const elementToUse = ampMediaElementFor(element) || element;
let srcEl = null;
// If the src attribute is specified, create a source element from it as it
// prevents race conditions between amp-story and amp-video propagating or
// removing attributes from amp-video/video elements.
if (elementToUse.hasAttribute('src')) {
srcEl = Sources.createSourceElement(win, elementToUse);
elementToUse.removeAttribute('src');
}
const srcEls = toArray(elementToUse.querySelectorAll('source'));
srcEls.forEach((srcEl) => removeElement(srcEl));
const trackEls = toArray(elementToUse.querySelectorAll('track'));
trackEls.forEach((trackEl) => removeElement(trackEl));
// If the src attribute is present, browsers will follow it and ignore the
// HTMLSourceElements. To ensure this behavior, drop the sources if the src
// was specified.
// cf: https://html.spec.whatwg.org/#concept-media-load-algorithm
const sourcesToUse = srcEl ? [srcEl] : srcEls;
return new Sources(null /** srcAttr */, sourcesToUse, trackEls);
}
/**
* Creates a HTMLSourceElement from the element src attribute.
* @param {!Window} win
* @param {!Element} element
* @return {!Element}
*/
static createSourceElement(win, element) {
const srcEl = win.document.createElement('source');
const srcAttr = element.getAttribute('src');
srcEl.setAttribute('src', srcAttr);
const origSrcAttr = element.getAttribute('amp-orig-src');
if (origSrcAttr) {
srcEl.setAttribute('amp-orig-src', origSrcAttr);
}
const typeAttr = element.getAttribute('type');
if (typeAttr) {
srcEl.setAttribute('type', typeAttr);
}
return srcEl;
}
}