Bug 1278748 - part6 : modify timestamp parsing rule. r=rillian

MozReview-Commit-ID: 2KfEeTww1eX

--HG--
extra : rebase_source : e0d278b8dcb9b5a3d4911be1ad72c32c7b6e7de8
This commit is contained in:
Alastor Wu 2016-07-12 11:02:22 +08:00
Родитель db86aafa55
Коммит ad9db94bfd
2 изменённых файлов: 19 добавлений и 27 удалений

Просмотреть файл

@ -64,29 +64,30 @@ this.EXPORTED_SYMBOLS = ["WebVTT"];
}
};
// Try to parse input as a time stamp.
function parseTimeStamp(input) {
// See spec, https://w3c.github.io/webvtt/#collect-a-webvtt-timestamp.
function collectTimeStamp(input) {
function computeSeconds(h, m, s, f) {
if (m > 59 || s > 59) {
return null;
}
// The attribute of the milli-seconds can only be three digits.
if (f.length !== 3) {
return null;
}
return (h | 0) * 3600 + (m | 0) * 60 + (s | 0) + (f | 0) / 1000;
}
var m = input.match(/^(\d+):(\d{2})(:\d{2})?\.(\d{3})/);
if (!m) {
var timestamp = input.match(/^(\d+:)?(\d{2}):(\d{2})\.(\d+)/);
if (!timestamp || timestamp.length !== 5) {
return null;
}
if (m[3]) {
// Timestamp takes the form of [hours]:[minutes]:[seconds].[milliseconds]
return computeSeconds(m[1], m[2], m[3].replace(":", ""), m[4]);
} else if (m[1] > 59) {
// Timestamp takes the form of [hours]:[minutes].[milliseconds]
// First position is hours as it's over 59.
return computeSeconds(m[1], m[2], 0, m[4]);
} else {
// Timestamp takes the form of [minutes]:[seconds].[milliseconds]
return computeSeconds(0, m[1], m[2], m[4]);
}
let hours = timestamp[1]? timestamp[1].replace(":", "") : 0;
let minutes = timestamp[2];
let seconds = timestamp[3];
let milliSeconds = timestamp[4];
return computeSeconds(hours, minutes, seconds, milliSeconds);
}
// A settings object holds key/value pairs and will ignore anything but the first
@ -170,7 +171,7 @@ this.EXPORTED_SYMBOLS = ["WebVTT"];
var oInput = input;
// 4.1 WebVTT timestamp
function consumeTimeStamp() {
var ts = parseTimeStamp(input);
var ts = collectTimeStamp(input);
if (ts === null) {
throw new ParsingError(ParsingError.Errors.BadTimeStamp,
"Malformed timestamp: " + oInput);
@ -355,7 +356,7 @@ this.EXPORTED_SYMBOLS = ["WebVTT"];
// Otherwise just ignore the end tag.
continue;
}
var ts = parseTimeStamp(t.substr(1, t.length - 2));
var ts = collectTimeStamp(t.substr(1, t.length - 2));
var node;
if (ts) {
// Timestamps are lead nodes as well.

Просмотреть файл

@ -16,13 +16,4 @@
expected: FAIL
[WebVTT parser tests, space-chars.vtt]
expected: FAIL
[WebVTT parser tests, timings-too-short.vtt]
expected: FAIL
[WebVTT parser tests, timings-too-long.vtt]
expected: FAIL
[WebVTT parser tests, timings-60.vtt]
expected: FAIL