Skip to content

programmatic video rendering framework that is actually fast

fix(webvtt): accept a leading byte-order mark (#293)

Fuad779928ac7f6d
diff --git a/webvtt-parser/src/lib.rs b/webvtt-parser/src/lib.rs
index 790b620c..f73ce53c 100644
--- a/webvtt-parser/src/lib.rs
+++ b/webvtt-parser/src/lib.rs
@@ -626,6 +626,31 @@ mod tests {
}
}
+ #[test]
+ fn optional_leading_byte_order_mark() {
+ let plain = "WEBVTT\n\n00:00:00.000 --> 00:00:01.000\nHello\n";
+ let prefixed = format!("\u{feff}{plain}");
+ let expected = Vtt::parse(plain).unwrap();
+ assert_eq!(expected.cues.len(), 1);
+ assert_eq!(Vtt::parse(&prefixed).unwrap(), expected);
+
+ for invalid in ["\u{feff}\u{feff}WEBVTT\n", "\u{feff}NOTVTT\n"] {
+ assert!(Vtt::parse(invalid).is_err());
+ }
+ }
+
+ #[test]
+ fn byte_order_mark_preserves_error_offsets() {
+ let plain = include_str!("../tests/invalid.vtt");
+ let bom = "\u{feff}";
+ let prefixed = format!("{bom}{plain}");
+ let plain_error = Vtt::parse(plain).unwrap_err();
+ let prefixed_error = Vtt::parse(&prefixed).unwrap_err();
+ assert_eq!(prefixed_error.offset, plain_error.offset + bom.len());
+ assert_eq!(prefixed_error.line, plain_error.line);
+ assert_eq!(prefixed_error.fragment, plain_error.fragment);
+ }
+
#[test]
fn timing_line_terminates_header_without_blank_separator() {
for ending in ["\n", "\r\n"] {
diff --git a/webvtt-parser/src/vtt_parser.rs b/webvtt-parser/src/vtt_parser.rs
index 3375d12c..a540d43c 100644
--- a/webvtt-parser/src/vtt_parser.rs
+++ b/webvtt-parser/src/vtt_parser.rs
@@ -157,6 +157,7 @@ fn parse_payload(input: Span<'_>) -> IResult<Span<'_>, Span<'_>> {
}
pub fn parse(text: Span) -> IResult<Span, Vtt> {
+ let (text, _) = opt(tag("\u{feff}"))(text)?;
let (text, _) = context("WebVTT file must start with WEBVTT", tag(START_MARKER))(text)?;
let (text, header_comment) = not_line_ending(text)?;
if !header_comment.is_empty()