From e119b92a701bace0732090e325762202573cbf1d Mon Sep 17 00:00:00 2001 From: stevenaw Date: Thu, 27 Jan 2011 11:16:00 -0500 Subject: [PATCH] [#291] Error checking, null tests --- parsers/parserVTT/data/data.vtt | 4 +++ parsers/parserVTT/data/unit.vtt | 29 ++++++++++++++++-- parsers/parserVTT/popcorn.parserVTT.js | 30 +++++++++++-------- parsers/parserVTT/popcorn.parserVTT.unit.html | 1 + parsers/parserVTT/popcorn.parserVTT.unit.js | 4 +-- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/parsers/parserVTT/data/data.vtt b/parsers/parserVTT/data/data.vtt index 6ef05aa..5d74f54 100644 --- a/parsers/parserVTT/data/data.vtt +++ b/parsers/parserVTT/data/data.vtt @@ -6,3 +6,7 @@ our final approach into Coruscant. 2 00:00:9.712 --> 00:00:13.399 Very good, Lieutenant. + +Track-3 +00:00:15.542 --> 00:00:18.542 A:start D:vertical L:98% +It's a trap! diff --git a/parsers/parserVTT/data/unit.vtt b/parsers/parserVTT/data/unit.vtt index a4c3065..2d5a606 100644 --- a/parsers/parserVTT/data/unit.vtt +++ b/parsers/parserVTT/data/unit.vtt @@ -1,14 +1,39 @@ 1 -00:00:02.400 --> 00:00:07.200 +00:00:02.400 --> 00:00:07.200 Senator, we're making our final approach into Coruscant. 2 -00:9.712 --> 00:13.399 +00:09.712-->00:13.399 Very good, Lieutenant. +3 +00:00:13.400 --> 00:00:20 +A bad cue, no milliseconds. Should be ignored + +--> +00:00:14.456--> 00:00:20.000 +A bad cue, bad id. Should be ignored + +4 +00:00:9.456--> 00:00:20.000 +A bad cue, seconds are single digit. Should be ignored + +5 +00:1:00.456--> 00:00:20.000 +A bad cue, minutes are single digit. Should be ignored + +6 +1:00:00.456--> 00:00:20.000 +A bad cue, hours are supplied as single digit. Should be ignored + +7 +00:00:00.46--> 00:00:20.000 +A bad cue, ms are not 3 digits. Should be ignored + Track-3 00:00:15.042 --> 00:00:18.042 A:start D:vertical L:98% It's a trap! + diff --git a/parsers/parserVTT/popcorn.parserVTT.js b/parsers/parserVTT/popcorn.parserVTT.js index 8583d6e..cf0248a 100644 --- a/parsers/parserVTT/popcorn.parserVTT.js +++ b/parsers/parserVTT/popcorn.parserVTT.js @@ -2,23 +2,27 @@ (function (Popcorn) { Popcorn.parser( "parseVTT", "VTT", function( data ) { - + // declare needed variables var retObj = { title: "", remote: "", data: [] }, - subs = [], - lines, + lines, time, text, sub, + subs = [], i = 0, len = 0, idx = 0; - // [HH]:MM:SS.mmm string to SS.mmm + // [HH:]MM:SS.mmm string to SS.mmm float + // Throws exception if invalid var toSeconds = function(t_in) { var t = t_in.split(':'); - var time, l = t.length-1; + var time, l = t_in.length; - try { + if (l !== 12 && l !== 9) throw "Bad cue"; + l = t.length - 1; + + try { time = parseInt(t[l-1], 10)*60 + parseFloat(t[l], 10); if (l === 2) // Hours given @@ -40,14 +44,16 @@ len = lines.length; while (i < len) { - var sub = {}; - - sub.id = lines[i++]; - - var time = lines[i++].split(" --> "); - var text = []; + sub = {}; + text = []; try { + sub.id = lines[i++]; + + if (!sub.id || sub.id.indexOf("-->") !== -1) throw "Bad cue"; + + time = lines[i++].split(/[\t ]*-->[\t ]*/); + sub.start = toSeconds(time[0]); // Filter out any trailing styling info diff --git a/parsers/parserVTT/popcorn.parserVTT.unit.html b/parsers/parserVTT/popcorn.parserVTT.unit.html index b4204fe..a9b6683 100644 --- a/parsers/parserVTT/popcorn.parserVTT.unit.html +++ b/parsers/parserVTT/popcorn.parserVTT.unit.html @@ -26,6 +26,7 @@ popcorn-api-draft.js