Skip to content

Commit

Permalink
add ability to flush parser at the end of a file
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasgal committed Jul 10, 2013
1 parent 2453402 commit 7be2e1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
11 changes: 8 additions & 3 deletions tests/run-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -27,6 +32,7 @@ function test(file) {
}

var TESTS = [
"no-newline-at-end.vtt",
"cue-identifier.vtt",
"fail-bad-utf8.vtt",
"many-comments.vtt",
Expand All @@ -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"
];

Expand Down
19 changes: 16 additions & 3 deletions vtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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();
}
}
};

0 comments on commit 7be2e1e

Please sign in to comment.