From 77723037945515efb23e9a450008f519afef7a29 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sun, 22 Sep 2024 20:57:02 +0900 Subject: [PATCH] fix crash when text events are emitted inside HTML blocks --- v2/src/markdown/parser.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/v2/src/markdown/parser.rs b/v2/src/markdown/parser.rs index 430e0c5..a1b62df 100644 --- a/v2/src/markdown/parser.rs +++ b/v2/src/markdown/parser.rs @@ -330,6 +330,8 @@ impl<'input, I: Iterator, Range)>> HtmlBlockReader<'input, return None; } Event::Html(html) => html, + // Text event is emitted when tags are preceded by spaces like "

" + Event::Text(text) => text, event => unreachable!("unexpected event: {event:?}"), }; self.index = 0; @@ -1250,4 +1252,17 @@ mod tests { assert_eq!(offset, expected, "{before:?}, {after:?}"); } } + + #[test] + fn text_event_inside_html_block() { + let target = MarkdownContent::new("

foo

".to_string(), None); + let parser = MarkdownParser::new(&target, None, ()); + let mut buf = Vec::new(); + let () = parser.write_to(&mut buf).unwrap(); + let buf = String::from_utf8(buf).unwrap(); + assert!( + buf.contains(r#"{"t":"html","raw":"

foo

"}"#), + "expected HTML block is not contained: {buf:?}", + ); + } }