Skip to content

Commit a16b4e3

Browse files
committed
Support styles in vtt. Grabbed code from w3c#31
1 parent c1cdda7 commit a16b4e3

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

parser.js

+34-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
linePos = 0,
3636
lines = input.split(NEWLINE),
3737
alreadyCollected = false,
38+
styles = [],
3839
cues = [],
3940
errors = []
4041
function err(message, col) {
@@ -121,6 +122,29 @@
121122
continue
122123
}
123124

125+
/* STYLES */
126+
if(/^STYLE($|[ \t])/.test(cue.id)) {
127+
var style = []
128+
var invalid = false
129+
linePos++
130+
while(lines[linePos] != "" && lines[linePos] != undefined) {
131+
if(lines[linePos].indexOf("-->") != -1) {
132+
err("Cannot have timestamp in a style block.")
133+
invalid = true
134+
}
135+
style.push(lines[linePos])
136+
linePos++
137+
}
138+
if(cues.length) {
139+
err("Style blocks cannot appear after the first cue.")
140+
continue
141+
}
142+
if (!invalid) {
143+
styles.push(style.join('\n'))
144+
}
145+
continue
146+
}
147+
124148
linePos++
125149

126150
if(lines[linePos] == "" || lines[linePos] == undefined) {
@@ -190,7 +214,7 @@
190214
return 0
191215
})
192216
/* END */
193-
return {cues:cues, errors:errors, time:Date.now()-startTime}
217+
return {cues:cues, errors:errors, time:Date.now()-startTime, styles: styles}
194218
}
195219
}
196220

@@ -836,8 +860,16 @@
836860
+ serializeCueSettings(cue)
837861
+ "\n" + serializeTree(cue.tree.children) + "\n\n"
838862
}
839-
this.serialize = function(cues) {
863+
function serializeStyle(style) {
864+
return "STYLE\n" + style + "\n\n"
865+
}
866+
this.serialize = function(cues, styles) {
840867
var result = "WEBVTT\n\n"
868+
if (styles) {
869+
for(var i=0;i<styles.length;i++) {
870+
result += serializeStyle(styles[i])
871+
}
872+
}
841873
for(var i=0;i<cues.length;i++) {
842874
result += serializeCue(cues[i])
843875
}

0 commit comments

Comments
 (0)