},
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) {
};
// 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 = [];
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;