mirror of
https://github.com/iv-org/invidious.git
synced 2025-08-09 20:24:03 +00:00
WebVTT::Builder: Add logic to escape special chars (#4414)
Note: WebVTT does allow some tags in the cue payload in some circumstances while this PR just blindly escapes everything: https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API#cue_payload_text_tags
This commit is contained in:
@@ -4,13 +4,23 @@
|
||||
module WebVTT
|
||||
# A WebVTT builder generates WebVTT files
|
||||
private class Builder
|
||||
# See https://developer.mozilla.org/en-US/docs/Web/API/WebVTT_API#cue_payload
|
||||
private ESCAPE_SUBSTITUTIONS = {
|
||||
'&' => "&",
|
||||
'<' => "<",
|
||||
'>' => ">",
|
||||
'\u200E' => "‎",
|
||||
'\u200F' => "‏",
|
||||
'\u00A0' => " ",
|
||||
}
|
||||
|
||||
def initialize(@io : IO)
|
||||
end
|
||||
|
||||
# Writes an vtt cue with the specified time stamp and contents
|
||||
def cue(start_time : Time::Span, end_time : Time::Span, text : String)
|
||||
timestamp(start_time, end_time)
|
||||
@io << text
|
||||
@io << self.escape(text)
|
||||
@io << "\n\n"
|
||||
end
|
||||
|
||||
@@ -29,6 +39,10 @@ module WebVTT
|
||||
@io << '.' << timestamp.milliseconds.to_s.rjust(3, '0')
|
||||
end
|
||||
|
||||
private def escape(text : String) : String
|
||||
return text.gsub(ESCAPE_SUBSTITUTIONS)
|
||||
end
|
||||
|
||||
def document(setting_fields : Hash(String, String)? = nil, &)
|
||||
@io << "WEBVTT\n"
|
||||
|
||||
|
Reference in New Issue
Block a user