/
mediafragments.js
449 lines (442 loc) · 14.1 KB
/
mediafragments.js
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
'use strict';
var MediaFragments = (function(window) {
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fun /*, thisp */) {
if (this === void 0 || this === null) {
throw new TypeError();
}
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== 'function') {
throw new TypeError();
}
var thisp = arguments[1];
for (var i = 0; i < len; i++) {
if (i in t) {
fun.call(thisp, t[i], i, t);
}
}
};
}
// '&' is the only primary separator for key-value pairs
var SEPARATOR = '&';
// report errors?
var VERBOSE = true;
var logWarning = function(message) {
if (VERBOSE) {
console.log('Media Fragments URI Parsing Warning: ' + message);
}
};
// the currently supported media fragments dimensions are: t, xywh, track, id
// allows for O(1) checks for existence of valid keys
var dimensions = {
t: function(value) {
var components = value.split(',');
if (components.length > 2) {
return false;
}
var start = components[0]? components[0] : '';
var end = components[1]? components[1] : '';
if ((start === '' && end === '') ||
(start && !end && value.indexOf(',') !== -1)) {
return false;
}
// hours:minutes:seconds.milliseconds
var npt =
/^((npt\:)?((\d+\:(\d\d)\:(\d\d))|((\d\d)\:(\d\d))|(\d+))(\.\d*)?)?$/;
if ((npt.test(start)) &&
(npt.test(end))) {
start = start.replace(/^npt\:/, '');
// replace a sole trailing dot, which is legal:
// npt-sec = 1*DIGIT [ "." *DIGIT ]
start = start.replace(/\.$/, '');
end = end.replace(/\.$/, '');
var convertToSeconds = function(time) {
if (time === '') {
return false;
}
// possible cases:
// 12:34:56.789
// 34:56.789
// 56.789
// 56
var hours;
var minutes;
var seconds;
time = time.split(':');
var length = time.length;
if (length === 3) {
hours = parseInt(time[0], 10);
minutes = parseInt(time[1], 10);
seconds = parseFloat(time[2]);
} else if (length === 2) {
hours = 0;
minutes = parseInt(time[0], 10);
seconds = parseFloat(time[1]);
} else if (length === 1) {
hours = 0;
minutes = 0;
seconds = parseFloat(time[0]);
} else {
return false;
}
if (hours > 23) {
logWarning('Please ensure that hours <= 23.');
return false;
}
if (minutes > 59) {
logWarning('Please ensure that minutes <= 59.');
return false;
}
if (length > 1 && seconds >= 60) {
// this constraint must not be applied if you specify only seconds
logWarning('Please ensure that seconds < 60.');
return false;
}
return hours * 3600 + minutes * 60 + seconds;
};
var startNormalized = convertToSeconds(start);
var endNormalized = convertToSeconds(end);
if (start && end) {
if (startNormalized < endNormalized) {
return {
value: value,
unit: 'npt',
start: start,
end: end,
startNormalized: startNormalized,
endNormalized: endNormalized
};
} else {
logWarning('Please ensure that start < end.');
return false;
}
} else {
if ((convertToSeconds(start) !== false) ||
(convertToSeconds(end) !== false)) {
return {
value: value,
unit: 'npt',
start: start,
end: end,
startNormalized: startNormalized === false ? '' : startNormalized,
endNormalized: endNormalized === false ? '' : endNormalized
};
} else {
logWarning('Please ensure that start or end are legal.');
return false;
}
}
}
// hours:minutes:seconds:frames.further-subdivison-of-frames
var smpte = /^(\d+\:\d\d\:\d\d(\:\d\d(\.\d\d)?)?)?$/;
var prefix = start.replace(/^(smpte(-25|-30|-30-drop)?).*/, '$1');
start = start.replace(/^smpte(-25|-30|-30-drop)?\:/, '');
if ((smpte.test(start)) && (smpte.test(end))) {
// we interpret frames as milliseconds, and further-subdivison-of-frames
// as microseconds. this allows for relatively easy comparison.
var convertToSecondsWithFrames = function(time) {
if (time === '') {
return false;
}
// possible cases:
// 12:34:56
// 12:34:56:78
// 12:34:56:78.90
var hours;
var minutes;
var seconds;
var frames;
var subframes;
time = time.split(':');
var length = time.length;
if (length === 3) {
hours = parseInt(time[0], 10);
minutes = parseInt(time[1], 10);
seconds = parseInt(time[2], 10);
frames = 0;
subframes = 0;
} else if (length === 4) {
hours = parseInt(time[0], 10);
minutes = parseInt(time[1], 10);
seconds = parseInt(time[2], 10);
if (time[3].indexOf('.') === -1) {
frames = parseInt(time[3], 10);
subframes = 0;
} else {
var frameSubFrame = time[3].split('.');
frames = parseInt(frameSubFrame[0], 10);
subframes = parseInt(frameSubFrame[1], 10);
}
} else {
return false;
}
if (hours > 23) {
logWarning('Please ensure that hours <= 23.');
return false;
}
if (minutes > 59) {
logWarning('Please ensure that minutes <= 59.');
return false;
}
if (seconds > 59) {
logWarning('Please ensure that seconds <= 59.');
return false;
}
return hours * 3600 + minutes * 60 + seconds +
frames * 0.001 + subframes * 0.000001;
};
if (start && end) {
if (convertToSecondsWithFrames(start) <
convertToSecondsWithFrames(end)) {
return {
value: value,
unit: prefix,
start: start,
end: end
};
} else {
logWarning('Please ensure that start < end.');
return false;
}
} else {
if ((convertToSecondsWithFrames(start) !== false) ||
(convertToSecondsWithFrames(end) !== false)) {
return {
value: value,
unit: prefix,
start: start,
end: end
};
} else {
logWarning('Please ensure that start or end are legal.');
return false;
}
}
}
// regexp adapted from http://delete.me.uk/2005/03/iso8601.html
var wallClock = /^((\d{4})(-(\d{2})(-(\d{2})(T(\d{2})\:(\d{2})(\:(\d{2})(\.(\d+))?)?(Z|(([-\+])(\d{2})\:(\d{2})))?)?)?)?)?$/;
start = start.replace('clock:', '');
if ((wallClock.test(start)) && (wallClock.test(end))) {
// the last condition is to ensure ISO 8601 date conformance.
// not all browsers parse ISO 8601, so we can only use date parsing
// when it's there.
if (start && end && !isNaN(Date.parse('2009-07-26T11:19:01Z'))) {
// if both start and end are given, then the start must be before
// the end
if (Date.parse(start) <= Date.parse(end)) {
return {
value: value,
unit: 'clock',
start: start,
end: end
};
} else {
logWarning('Please ensure that start < end.');
return false;
}
} else {
return {
value: value,
unit: 'clock',
start: start,
end: end
};
}
}
logWarning('Invalid time dimension.');
return false;
},
xywh: function(value) {
// "pixel:" is optional
var pixelCoordinates = /^(pixel\:)?\d+,\d+,\d+,\d+$/;
// "percent:" is obligatory
var percentSelection = /^percent\:\d+,\d+,\d+,\d+$/;
var values = value.replace(/(pixel|percent)\:/, '').split(',');
var x = parseInt(values[0], 10);
var y = parseInt(values[1], 10);
var w = parseInt(values[2], 10);
var h = parseInt(values[3], 10);
if (pixelCoordinates.test(value)) {
if (w > 0 && h > 0) {
return {
value: value,
unit: 'pixel',
x: x,
y: y,
w: w,
h: h
};
} else {
logWarning('Please ensure that w > 0 and h > 0');
return false;
}
} else if (percentSelection.test(value)) {
/**
* checks for valid percent selections
*/
var checkPercentSelection = (function checkPercentSelection(
x, y, w, h) {
if (!((0 <= x) && (x <= 100))) {
logWarning('Please ensure that 0 <= x <= 100.');
return false;
}
if (!((0 <= y) && (y <= 100))) {
logWarning('Please ensure that 0 <= y <= 100.');
return false;
}
if (!((0 <= w) && (w <= 100))) {
logWarning('Please ensure that 0 <= w <= 100.');
return false;
}
if (!((0 <= h) && (h <= 100))) {
logWarning('Please ensure that 0 <= h <= 100.');
return false;
}
if (x + w > 100) {
logWarning('Please ensure that x + w <= 100.');
return false;
}
if (y + h > 100) {
logWarning('Please ensure that y + h <= 100.');
return false;
}
return true;
});
if (checkPercentSelection(x, y, w, h)) {
return {
value: value,
unit: 'percent',
x: x,
y: y,
w: w,
h: h
};
}
logWarning('Invalid percent selection.');
return false;
} else {
logWarning('Invalid spatial dimension.');
return false;
}
},
track: function(value) {
return {
value: value,
name: value
};
},
id: function(value) {
return {
value: value,
name: value
};
},
chapter: function(value) {
return {
value: value,
chapter: value
};
}
};
/**
* splits an octet string into allowed key-value pairs
*/
var splitKeyValuePairs = function(octetString) {
var keyValues = {};
var keyValuePairs = octetString.split(SEPARATOR);
keyValuePairs.forEach(function(keyValuePair) {
// the key part is up to the first(!) occurrence of '=', further '='-s
// form part of the value
var position = keyValuePair.indexOf('=');
if (position < 1) {
return;
}
var components = [
keyValuePair.substring(0, position),
keyValuePair.substring(position + 1)];
// we require a value for each key
if (!components[1]) {
return;
}
// the key name needs to be decoded
var key = decodeURIComponent(components[0]);
// only allow keys that are currently supported media fragments dimensions
var dimensionChecker = dimensions[key];
// the value needs to be decoded
var value = decodeURIComponent(components[1]);
if (dimensionChecker) {
value = dimensionChecker(value);
} else {
// we had a key that is not part of media fragments
return;
}
if (!value) {
return;
}
// keys may appear more than once, thus store all values in an array,
// the exception being &t
if (!keyValues[key]) {
keyValues[key] = [];
}
if (key !== 't') {
keyValues[key].push(value);
} else {
keyValues[key][0] = value;
}
});
return keyValues;
};
return {
parse: function(opt_uri) {
return MediaFragments.parseMediaFragmentsUri(opt_uri);
},
parseMediaFragmentsUri: function(opt_uri) {
var uri = opt_uri? opt_uri : window.location.href;
// retrieve the query part of the URI
var indexOfHash = uri.indexOf('#');
var indexOfQuestionMark = uri.indexOf('?');
var end = (indexOfHash !== -1? indexOfHash : uri.length);
var query = indexOfQuestionMark !== -1?
uri.substring(indexOfQuestionMark + 1, end) : '';
// retrieve the hash part of the URI
var hash = indexOfHash !== -1? uri.substring(indexOfHash + 1) : '';
var queryValues = splitKeyValuePairs(query);
var hashValues = splitKeyValuePairs(hash);
return {
query: queryValues,
hash: hashValues,
toString: function() {
var buildString = function(name, thing) {
var s = '\n[' + name + ']:\n';
if (!Object.keys) Object.keys = function(o) {
if (o !== Object(o)) {
throw new TypeError('Object.keys called on non-object');
}
var ret = [], p;
for (p in o) {
if (Object.prototype.hasOwnProperty.call(o,p)) ret.push(p);
}
return ret;
};
Object.keys(thing).forEach(function(key) {
s += ' * ' + key + ':\n';
thing[key].forEach(function(value) {
s += ' [\n';
Object.keys(value).forEach(function(valueKey) {
s += ' - ' + valueKey + ': ' + value[valueKey] + '\n';
});
s += ' ]\n';
});
});
return s;
};
var string =
buildString('Query', queryValues) +
buildString('Hash', hashValues);
return string;
}
};
}
};
})(window);