From 5c48a52063b49c83786570364c4e8b627a8d6ff0 Mon Sep 17 00:00:00 2001 From: stevenaw Date: Wed, 26 Jan 2011 12:59:52 -0500 Subject: [PATCH] Simplified code --- parsers/parserVTT/data/unit.vtt | 2 ++ parsers/parserVTT/popcorn.parserVTT.js | 23 +++++++++++---------- parsers/parserVTT/popcorn.parserVTT.unit.js | 2 +- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/parsers/parserVTT/data/unit.vtt b/parsers/parserVTT/data/unit.vtt index ee8c610..a4c3065 100644 --- a/parsers/parserVTT/data/unit.vtt +++ b/parsers/parserVTT/data/unit.vtt @@ -7,6 +7,8 @@ our final approach into Coruscant. 00:9.712 --> 00:13.399 Very good, Lieutenant. + + 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 9350234..8583d6e 100644 --- a/parsers/parserVTT/popcorn.parserVTT.js +++ b/parsers/parserVTT/popcorn.parserVTT.js @@ -11,7 +11,7 @@ }, subs = [], lines, - i = 0, l = 0; + i = 0, len = 0, idx = 0; // [HH]:MM:SS.mmm string to SS.mmm var toSeconds = function(t_in) { @@ -35,13 +35,14 @@ }; // Here is where the magic happens - lines = data.text.replace("\r",'').split("\n"); - l = lines.length; + // Split on line breaks + lines = data.text.split(/(?:\r\n|\r|\n)/gm); + len = lines.length; - while (i < l) { + while (i < len) { var sub = {}; - sub.id = lines[i++].replace('\r', ''); + sub.id = lines[i++]; var time = lines[i++].split(" --> "); var text = []; @@ -50,21 +51,21 @@ sub.start = toSeconds(time[0]); // Filter out any trailing styling info - time[1] = time[1].replace("\r", " "); - sub.end = toSeconds(time[1].substr(0, time[1].indexOf(" "))); + idx = time[1].indexOf(" "); + sub.end = toSeconds(idx === -1 ? time[1] : time[1].substr(0, idx)); - while (lines[i] && lines[i] !== "\r") { - text.push(lines[i++].replace("\r", "")); + while (i < len && lines[i]) { + text.push(lines[i++]); } sub.text = text.join(" "); subs.push( createTrack("subtitle", sub) ); } catch (e) { // Bad cue - while (i < l && !lines[i++] !== "\r"); // Advance to end of cue + while (i < len && lines[i++]); // Advance to end of cue } // Consume empty whitespace - while (i < l && !lines[i++] === "\r"); + while (i < len && !lines[i]) { i++; } } retObj.data = subs; diff --git a/parsers/parserVTT/popcorn.parserVTT.unit.js b/parsers/parserVTT/popcorn.parserVTT.unit.js index c1e3670..2fbcff0 100644 --- a/parsers/parserVTT/popcorn.parserVTT.unit.js +++ b/parsers/parserVTT/popcorn.parserVTT.unit.js @@ -34,7 +34,7 @@ test("Popcorn 0.3 VTT Parser Plugin", function () { expect(expects); - stop( 5000 ); + stop( 10000 ); // Allow load time setTimeout(function () { -- 2.43.0