From ad9db94bfd86a7e939221359ce7f5e85c39f7e05 Mon Sep 17 00:00:00 2001 From: Alastor Wu Date: Tue, 12 Jul 2016 11:02:22 +0800 Subject: [PATCH] Bug 1278748 - part6 : modify timestamp parsing rule. r=rillian MozReview-Commit-ID: 2KfEeTww1eX --HG-- extra : rebase_source : e0d278b8dcb9b5a3d4911be1ad72c32c7b6e7de8 --- dom/media/webvtt/vtt.jsm | 37 ++++++++++--------- .../webvtt-file-parsing/001.html.ini | 9 ----- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/dom/media/webvtt/vtt.jsm b/dom/media/webvtt/vtt.jsm index 7c70874751ac..21ee2df113aa 100644 --- a/dom/media/webvtt/vtt.jsm +++ b/dom/media/webvtt/vtt.jsm @@ -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. diff --git a/testing/web-platform/meta/webvtt/webvtt-file-format-parsing/webvtt-file-parsing/001.html.ini b/testing/web-platform/meta/webvtt/webvtt-file-format-parsing/webvtt-file-parsing/001.html.ini index a2968be74924..7bc7e387887b 100644 --- a/testing/web-platform/meta/webvtt/webvtt-file-format-parsing/webvtt-file-parsing/001.html.ini +++ b/testing/web-platform/meta/webvtt/webvtt-file-format-parsing/webvtt-file-parsing/001.html.ini @@ -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 \ No newline at end of file