From 1208ec4e8d159cec255224175ac3d15778531c0f Mon Sep 17 00:00:00 2001 From: rhysd Date: Mon, 3 Jun 2024 18:02:58 +0900 Subject: [PATCH] handle HTML block on `Tag::HtmlBlock` event instead of first `Event::Html` event --- v2/src/markdown/parser.rs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/v2/src/markdown/parser.rs b/v2/src/markdown/parser.rs index 22e2753..75e05db 100644 --- a/v2/src/markdown/parser.rs +++ b/v2/src/markdown/parser.rs @@ -305,8 +305,8 @@ struct HtmlBlockReader<'input, I: Iterator, Range)>> { } impl<'input, I: Iterator, Range)>> HtmlBlockReader<'input, I> { - fn new(current: CowStr<'input>, events: I) -> Self { - Self { current, index: 0, events, end: false } + fn new(events: I) -> Self { + Self { current: "".into(), index: 0, events, end: false } } fn read_byte(&mut self) -> Option { @@ -314,7 +314,7 @@ impl<'input, I: Iterator, Range)>> HtmlBlockReader<'input, return None; } - // Current event was consumed. Fetch next event otherwise return `None`. + // Current event was consumed. Fetch next event or return `None` at the end of the block. while self.current.len() <= self.index { self.current = match self.events.next().unwrap().0 { Event::End(TagEnd::HtmlBlock) => { @@ -665,7 +665,18 @@ impl<'input, W: Write, V: TextVisitor, T: TextTokenizer> RenderTreeEncoder<'inpu self.out.write_all(br#","src":"#)?; self.rebase_link(&dest_url)?; } - HtmlBlock => continue, // HTML block is handled by `Event::Html` event + HtmlBlock => { + self.tag("html")?; + self.out.write_all(br#","raw":""#)?; + + let mut dst = StringContentEncoder(&mut self.out); + let mut src = HtmlBlockReader::new(&mut events); + self.sanitizer.clean(&mut dst, &mut src)?; + + self.out.write_all(br#""}"#)?; + // Unlike other tags, `HtmlBlockReader consumes all events until `TagEnd::HtmlBlock` + continue; + } FootnoteDefinition(name) => { self.tag("fn-def")?; @@ -705,7 +716,7 @@ impl<'input, W: Write, V: TextVisitor, T: TextTokenizer> RenderTreeEncoder<'inpu self.tag("tbody")?; self.children_begin()?; } - HtmlBlock => unreachable!(), // This event is handled in `HtmlBlockReader` + HtmlBlock => unreachable!(), // This event is handled in `Tag::HtmlBlock` event using `HtmlBlockReader` MetadataBlock(_) => unreachable!(), // This option is not enabled } } @@ -719,16 +730,7 @@ impl<'input, W: Write, V: TextVisitor, T: TextTokenizer> RenderTreeEncoder<'inpu self.text(&text, inner_range)?; self.tag_end()?; } - Event::Html(html) => { - self.tag("html")?; - self.out.write_all(br#","raw":""#)?; - - let mut dst = StringContentEncoder(&mut self.out); - let mut src = HtmlBlockReader::new(html, &mut events); - self.sanitizer.clean(&mut dst, &mut src)?; - - self.out.write_all(br#""}"#)?; - } + Event::Html(_) => unreachable!(), // This event is handled in `Tag::HtmlBlock` event using `HtmlBlockReader` Event::InlineHtml(html) => { self.tag("html")?; self.out.write_all(br#","raw":""#)?;