programmatic video rendering framework that is actually fast
fix(webvtt): accept whitespace in cue timing lines (#294)
Bohdan32925901ed82
diff --git a/webvtt-parser/src/lib.rs b/webvtt-parser/src/lib.rsindex 9494f990..790b620c 100644--- a/webvtt-parser/src/lib.rs+++ b/webvtt-parser/src/lib.rs@@ -607,6 +607,25 @@ mod tests {}}+ #[test]+ fn cue_timing_whitespace() {+ let expected =+ Vtt::parse("WEBVTT\n\nintro\n00:00.000 --> 00:01.000 align:end\nHello\n").unwrap();+ for timing in [+ " 00:00.000 --> 00:01.000",+ "\t00:00.000\t-->\x0c00:01.000",+ "\x0c00:00.000\x0c-->\t00:01.000",+ "00:00.000-->00:01.000",+ ] {+ let input = format!("WEBVTT\n\nintro\n{timing} align:end\nHello\n");+ assert_eq!(Vtt::parse(&input).unwrap(), expected);+ }+ for separator in ['\n', '\x0b'] {+ let input = format!("WEBVTT\n\nintro\n00:00.000{separator}--> 00:01.000\nHello\n");+ assert!(Vtt::parse(&input).is_err());+ }+ }+#[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.rsindex 6848567f..3375d12c 100644--- a/webvtt-parser/src/vtt_parser.rs+++ b/webvtt-parser/src/vtt_parser.rs@@ -1,8 +1,10 @@use crate::Span;use nom::branch::alt;+use nom::bytes::complete::{tag, take_while};use nom::character::complete::{digit1, line_ending, not_line_ending};use nom::error::{Error, ErrorKind, context};-use nom::{IResult, Slice, bytes::complete::tag, combinator::opt, multi::many0};+use nom::sequence::preceded;+use nom::{IResult, Slice, combinator::opt, multi::many0};use crate::cue_settings_parser::parse_cue_settings;use crate::{START_MARKER, Time, Vtt, VttCue};@@ -93,14 +95,21 @@ fn parse_cue_identifier(input: Span<'_>) -> IResult<Span<'_>, Option<&str>> {}}+fn timing_whitespace(input: Span) -> IResult<Span, Span> {+ take_while(|c| matches!(c, ' ' | '\t' | '\x0c'))(input)+}+fn parse_cue(input: Span) -> IResult<Span, VttCue> {let (input, _) = many0(line_ending)(input)?;- let mut parse_time = alt((parse_time_without_hours, parse_time_with_hours));+ let mut parse_time = preceded(+ timing_whitespace,+ alt((parse_time_without_hours, parse_time_with_hours)),+ );let (input, name) = parse_cue_identifier(input)?;let (input, start) = parse_time(input)?;- let (input, _) = tag(" --> ")(input)?;+ let (input, _) = preceded(timing_whitespace, tag("-->"))(input)?;let (input, end) = parse_time(input)?;if end.as_milliseconds() < start.as_milliseconds() {return Err(nom::Err::Failure(Error::new(input, ErrorKind::Verify)));