diff --git a/tests/run-tests.js b/tests/run-tests.js index 964680b..7e26700 100644 --- a/tests/run-tests.js +++ b/tests/run-tests.js @@ -8,12 +8,17 @@ function parse(file) { var result = 0; try { var parser = new WebVTTParser(); + parser.oncue = function (cue) { + if (result >= 0) + ++result; + } parser.onerror = function () { - result = 1; + result = -1; } parser.parse(text); + parser.flush(); } catch (e) { - result = 2; + result = -2; } return result; } @@ -27,6 +32,7 @@ function test(file) { } var TESTS = [ + "no-newline-at-end.vtt", "cue-identifier.vtt", "fail-bad-utf8.vtt", "many-comments.vtt", @@ -35,7 +41,6 @@ var TESTS = [ "line-breaks.vtt", "not-only-nested-cues.vtt", "only-nested-cues.vtt", - "no-newline-at-end.vtt", "voice-spans.vtt" ]; diff --git a/vtt.js b/vtt.js index 299a998..6752fa9 100644 --- a/vtt.js +++ b/vtt.js @@ -55,7 +55,8 @@ WebVTTParser.prototype = { parse: function (data) { var self = this; - self.buffer += data; + if (data) + self.buffer += data; function collectNextLine() { var buffer = self.buffer; @@ -124,7 +125,7 @@ WebVTTParser.prototype = { settings: "", startTime: 0, endTime: 0, - contnet: "" + content: "" }; self.state = "CUE"; // 30-39 - Check if self line contains an optional identifier or timing data. @@ -153,7 +154,7 @@ WebVTTParser.prototype = { self.state = "ID"; continue; } - if (!self.cue.content) + if (self.cue.content) self.cue.content += "\n"; self.cue.content += line; continue; @@ -169,5 +170,17 @@ WebVTTParser.prototype = { } catch (e) { self.onerror && self.onerror(); } + }, + flush: function () { + var self = this; + if (self.state === "ID") + return; + // Synthesize the end of the current block. + self.buffer += "\n\n"; + self.parse(); + if (self.buffer) { + // Incompletely parsed file. + self.onerror && self.onerror(); + } } };