// https://github.com/w3c/webvtt.js // // Any copyright is dedicated to the Public Domain. // http://creativecommons.org/publicdomain/zero/1.0/ // Not intended to be fast, but if you can make it faster, please help out! // !function(){var e={direction:"horizontal",snapToLines:!0,linePosition:"auto",lineAlign:"start",textPosition:"auto",positionAlign:"auto",size:100,alignment:"center"},t=function(t){t||(t={"&":"&","<":"<",">":">","&lrm":"‎","&rlm":"‏"," ":" "}),this.entities=t,this.parse=function(a,s){a=a.replace(/\0/g,"�");var r=Date.now(),o=0,l=a.split(/\r\n|\r|\n/),f=!1,u=[],c=[],p=[];function d(e,t){p.push({message:e,line:o+1,col:t})}var m=l[o],g=m.length,h=0,v="WEBVTT".length;for("\ufeff"===m[0]&&(h=1,v+=1),(gv&&" "!==m[v]&&"\t"!==m[v])&&d('No valid signature. (File needs to start with "WEBVTT".)'),o++;""!=l[o]&&null!=l[o];){if(d("No blank line after the signature."),-1!=l[o].indexOf("--\x3e")){f=!0;break}o++}for(;null!=l[o];){for(var b;!f&&""==l[o];)o++;if(!f&&null==l[o])break;b=Object.assign({},e,{id:"",startTime:0,endTime:0,pauseOnExit:!1,direction:"horizontal",snapToLines:!0,linePosition:"auto",lineAlign:"start",textPosition:"auto",positionAlign:"auto",size:100,alignment:"center",text:"",tree:null});var T=!0;if(-1==l[o].indexOf("--\x3e")){if(b.id=l[o],/^NOTE($|[ \t])/.test(b.id)){for(o++;""!=l[o]&&null!=l[o];)-1!=l[o].indexOf("--\x3e")&&d("Cannot have timestamp in a comment."),o++;continue}if(/^STYLE($|[ \t])/.test(b.id)){var x=[],y=!1;for(o++;""!=l[o]&&null!=l[o];)-1!=l[o].indexOf("--\x3e")&&(d("Cannot have timestamp in a style block."),y=!0),x.push(l[o]),o++;if(c.length){d("Style blocks cannot appear after the first cue.");continue}y||u.push(x.join("\n"));continue}if(""==l[++o]||null==l[o]){d("Cue identifier cannot be standalone.");continue}if(-1==l[o].indexOf("--\x3e")){T=!1,d("Cue identifier needs to be followed by timestamp.");continue}}f=!1;var w=new n(l[o],d),O=0;if(c.length>0&&(O=c[c.length-1].startTime),!T||w.parse(b,O)){for(o++;""!=l[o]&&null!=l[o];){if(-1!=l[o].indexOf("--\x3e")){d("Blank line missing before cue."),f=!0;break}""!=b.text&&(b.text+="\n"),b.text+=l[o],o++}var A=new i(b.text,d,s,t);b.tree=A.parse(b.startTime,b.endTime),c.push(b)}else for(b=null,o++;""!=l[o]&&null!=l[o];){if(-1!=l[o].indexOf("--\x3e")){f=!0;break}o++}}return c.sort(function(e,t){return e.startTimet.startTime?1:e.endTime>t.endTime?-1:e.endTime2||parseInt(t,10)>59)&&(l="hours"),":"==e[a])if(a++,2==(n=o(/\d/)).length){if("hours"==l||":"==e[a]){if(":"!=e[a])return void s("No seconds found or minutes is greater than 59.");if(a++,2!=(i=o(/\d/)).length)return void s("Must be exactly two digits.")}else{if(2!=t.length)return void s("Must be exactly two digits.");i=n,n=t,t="0"}if("."==e[a])if(a++,3==(r=o(/\d/)).length)if(parseInt(n,10)>59)s("You cannot have more than 59 minutes.");else{if(!(parseInt(i,10)>59))return 60*parseInt(t,10)*60+60*parseInt(n,10)+parseInt(i,10)+parseInt(r,10)/1e3;s("You cannot have more than 59 seconds.")}else s("Milliseconds must be given in three digits.");else s('No decimal separator (".") found.')}else s("Must be exactly two digits.");else s("No time unit separator found.");else s("Timestamp must start with a character in the range 0-9.");else s("No timestamp found.")}this.parse=function(t,o){if(r(n),t.startTime=l(),null!=t.startTime)if(t.startTime"==e[++a]){if(a++,i.test(e[a])&&s("'--\x3e' not separated from timestamp by whitespace."),r(n),t.endTime=l(),null!=t.endTime)return t.endTime<=t.startTime&&s("End timestamp is not greater than start timestamp."),i.test(e[a])&&!1,r(n),function(e,t){for(var i=e.split(n),a=[],r=0;r100)){s("Line position cannot be >100%.");continue}if(""===c||isNaN(c)||!isFinite(c)){s("Line position needs to be a number");continue}if(void 0!==u){if(!["start","center","end"].includes(u)){s("Line alignment needs to be one of start, center or end");continue}t.lineAlign=u}t.snapToLines=!p,t.linePosition=parseFloat(c),parseFloat(c).toString()!==c&&(t.nonSerializable=!0)}else if("position"==l){if(/,/.test(f)){var d;f=(d=f.split(","))[0];var m=d[1]}if("%"!=f[f.length-1]){s("Text position must be a percentage.");continue}if(parseInt(f,10)>100||parseInt(f,10)<0){s("Text position needs to be between 0 and 100%.");continue}if(""===(c=f.slice(0,f.length-1))||isNaN(c)||!isFinite(c)){s("Line position needs to be a number");continue}if(void 0!==m){if(!["line-left","center","line-right"].includes(m)){s("Position alignment needs to be one of line-left, center or line-right");continue}t.positionAlign=m}t.textPosition=parseFloat(c)}else if("size"==l){if("%"!=f[f.length-1]){s("Size must be a percentage.");continue}if(parseInt(f,10)>100){s("Size cannot be >100%.");continue}var g=f.slice(0,f.length-1);if(void 0===g||""===g||isNaN(g)){s("Size needs to be a number"),g=100;continue}if((g=parseFloat(g))<0||g>100){s("Size needs to be between 0 and 100%.");continue}t.size=g}else if("align"==l){var h=["start","center","end","left","right"];if(-1==h.indexOf(f)){s("Alignment can only be set to one of "+h.join(", ")+".");continue}t.alignment=f}else s("Invalid setting.")}}(e.substring(a),t),!0}else s("No valid timestamp separator found.");else s("No valid timestamp separator found.");else s("No valid timestamp separator found.")},this.parseTimestamp=function(){var t=l();if(null==e[a])return t;s("Timestamp must not have trailing characters.")}},i=function(e,t,i,a){this.entities=a;var s=this,r=(e=e,0),o=function(e){"metadata"!=i&&t(e,r+1)};function l(){for(var t="data",n="",i="",l=[];null!=e[r-1]||0==r;){var f=e[r];if("data"==t)if("&"==f)i=f,t="escape";else if("<"==f&&""==n)t="tag";else{if("<"==f||null==f)return["text",n];n+=f}else if("escape"==t){if("<"==f||null==f){let e;return o("Incorrect escape."),(e=i.match(/^&#([0-9]+)$/))?n+=String.fromCharCode(e[1]):s.entities[i]?n+=s.entities[i]:n+=i,["text",n]}if("&"==f)o("Incorrect escape."),n+=i,i=f;else if(/[a-z#0-9]/i.test(f))i+=f;else if(";"==f){let e;(e=i.match(/^&#(x?[0-9]+)$/))?n+=String.fromCharCode("0"+e[1]):s.entities[i+f]?n+=s.entities[i+f]:(e=Object.keys(a).find(e=>i.startsWith(e)))?n+=s.entities[e]+i.slice(e.length)+f:(o("Incorrect escape."),n+=i+";"),t="data"}else o("Incorrect escape."),n+=i+f,t="data"}else if("tag"==t)if("\t"==f||"\n"==f||"\f"==f||" "==f)t="start tag annotation";else if("."==f)t="start tag class";else if("/"==f)t="end tag";else if(/\d/.test(f))n=f,t="timestamp tag";else{if(">"==f||null==f)return">"==f&&r++,["start tag","",[],""];n=f,t="start tag"}else if("start tag"==t)if("\t"==f||"\f"==f||" "==f)t="start tag annotation";else if("\n"==f)i=f,t="start tag annotation";else if("."==f)t="start tag class";else{if(">"==f||null==f)return">"==f&&r++,["start tag",n,[],""];n+=f}else if("start tag class"==t)if("\t"==f||"\f"==f||" "==f)i&&l.push(i),i="",t="start tag annotation";else if("\n"==f)i&&l.push(i),i=f,t="start tag annotation";else if("."==f)i&&l.push(i),i="";else{if(">"==f||null==f)return">"==f&&r++,i&&l.push(i),["start tag",n,l,""];i+=f}else if("start tag annotation"==t){if(">"==f||null==f)return">"==f&&r++,["start tag",n,l,i=i.split(/[\u0020\t\f\r\n]+/).filter(function(e){if(e)return!0}).join(" ")];i+=f}else if("end tag"==t){if(">"==f||null==f)return">"==f&&r++,["end tag",n];n+=f}else if("timestamp tag"==t){if(">"==f||null==f)return">"==f&&r++,["timestamp",n];n+=f}else o("Never happens.");r++}}this.parse=function(t,a){var s={children:[]},f=s,u=[];function c(e){f.children.push({type:"object",name:e[1],classes:e[2],children:[],parent:f}),f=f.children[f.children.length-1]}function p(e){for(var t=f;t;){if(t.name==e)return!0;t=t.parent}}for(;null!=e[r];){var d=l();if("text"==d[0])f.children.push({type:"text",value:d[1],parent:f});else if("start tag"==d[0]){"chapters"==i&&o("Start tags not allowed in chapter title text.");var m=d[1];"v"!=m&&"lang"!=m&&""!=d[3]&&o("Only and can have an annotation."),"c"==m||"i"==m||"b"==m||"u"==m||"ruby"==m?c(d):"rt"==m&&"ruby"==f.name?c(d):"v"==m?(p("v")&&o(" cannot be nested inside itself."),c(d),f.value=d[3],d[3]||o(" requires an annotation.")):"lang"==m?(c(d),f.value=d[3]):o("Incorrect start tag.")}else if("end tag"==d[0])"chapters"==i&&o("End tags not allowed in chapter title text."),d[1]==f.name?f=f.parent:"ruby"==d[1]&&"rt"==f.name?f=f.parent.parent:o("Incorrect end tag.");else if("timestamp"==d[0]){"chapters"==i&&o("Timestamp not allowed in chapter title text.");var g=new n(d[1],o).parseTimestamp();null!=g&&((g<=t||g>=a)&&o("Timestamp must be between start timestamp and end timestamp."),u.length>0&&u[u.length-1]>=g&&o("Timestamp must be greater than any previous timestamp."),f.children.push({type:"timestamp",value:g,parent:f}),u.push(g))}}for(;f.parent;)"v"!=f.name&&o("Required end tag missing."),f=f.parent;return function e(t){const n={...t};return t.children&&(n.children=t.children.map(e)),n.parent&&delete n.parent,n}(s)}},a=function(){function t(e){const t=("00"+1e3*(e-Math.floor(e)).toFixed(3)).slice(-3);let n=0,i=0,a=0;return e>=3600&&(n=Math.floor(e/3600)),i=Math.floor((e-3600*n)/60),a=Math.floor(e-3600*n-60*i),(n?n+":":"")+(""+i).padStart(2,"0")+":"+(""+a).padStart(2,"0")+"."+t}function n(n){return(void 0!==n.id?n.id+"\n":"")+t(n.startTime)+" --\x3e "+t(n.endTime)+function(t){var n="";const i=Object.keys(e).filter(n=>t[n]!==e[n]);return i.includes("direction")&&(n+=" vertical:"+t.direction),i.includes("alignment")&&(n+=" align:"+t.alignment),i.includes("size")&&(n+=" size:"+t.size+"%"),(i.includes("lineAlign")||i.includes("linePosition"))&&(n+=" line:"+t.linePosition+(t.snapToLines?"":"%")+(t.lineAlign&&t.lineAlign!=e.lineAlign?","+t.lineAlign:"")),(i.includes("textPosition")||i.includes("positionAlign"))&&(n+=" position:"+t.textPosition+"%"+(t.positionAlign&&t.positionAlign!==e.positionAlign?","+t.positionAlign:"")),n}(n)+"\n"+function e(n){for(var i="",a=0;a/g,">");else if("object"==s.type){if(i+="<"+s.name,s.classes)for(var r=0;r"}else"timestamp"==s.type?i+="<"+t(s.value)+">":i+="<"+s.value+">"}return i}(n.tree.children)+"\n\n"}this.serialize=function(e,t){var i="WEBVTT\n\n";if(t)for(var a=0;a