2 * Copyright 2013 vtt.js Contributors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 // If we're in Node.js then require VTTCue so we can extend it, otherwise assume
18 // VTTCue is on the global.
19 if (typeof module !== "undefined" && module.exports) {
20 this.VTTCue = this.VTTCue || require("./vttcue").VTTCue;
23 // Extend VTTCue with methods to convert to JSON, from JSON, and construct a
24 // VTTCue from an options object. The primary purpose of this is for use in the
25 // vtt.js test suite (for testing only properties that we care about). It's also
26 // useful if you need to work with VTTCues in JSON format.
29 root.VTTCue.prototype.toJSON = function() {
32 // Filter out getCueAsHTML as it's a function and hasBeenReset and displayState as
33 // they're only used when running the processing model algorithm.
34 Object.keys(this).forEach(function(key) {
35 if (key !== "getCueAsHTML" && key !== "hasBeenReset" && key !== "displayState") {
42 root.VTTCue.create = function(options) {
43 if (!options.hasOwnProperty("startTime") || !options.hasOwnProperty("endTime") ||
44 !options.hasOwnProperty("text")) {
45 throw new Error("You must at least have start time, end time, and text.");
47 var cue = new root.VTTCue(options.startTime, options.endTime, options.text);
48 for (var key in options) {
49 if (cue.hasOwnProperty(key)) {
50 cue[key] = options[key];
56 root.VTTCue.fromJSON = function(json) {
57 return this.create(JSON.parse(json));