From 6c13445136412bc2269de3b366e7d40695c576e1 Mon Sep 17 00:00:00 2001 From: Kornel Date: Fri, 2 Jun 2023 00:00:48 +0100 Subject: [PATCH] Balance pos pushes when ignoring comments Fixes #225 --- src/reader/parser/outside_tag.rs | 7 ++++++- tests/event_reader.rs | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/reader/parser/outside_tag.rs b/src/reader/parser/outside_tag.rs index bbd28254..8104224d 100644 --- a/src/reader/parser/outside_tag.rs +++ b/src/reader/parser/outside_tag.rs @@ -93,7 +93,12 @@ impl PullParser { } } else { None }; self.inside_whitespace = true; // Reset inside_whitespace flag - self.push_pos(); + + // pos is popped whenever an event is emitted, so pushes must happen only if there will be an event to balance it + // and ignored comments don't pop + if t != Token::CommentStart || !self.config.c.ignore_comments { + self.push_pos(); + } match t { Token::OpeningTagStart if self.depth() > 0 || self.encountered < Encountered::Element || self.config.allow_multiple_root_elements => { if let Some(e) = self.set_encountered(Encountered::Element) { diff --git a/tests/event_reader.rs b/tests/event_reader.rs index 122c6cd5..30bda098 100644 --- a/tests/event_reader.rs +++ b/tests/event_reader.rs @@ -537,6 +537,17 @@ fn issue_replacement_character_entity_reference() { ); } +#[test] +fn push_pos_issue() { + let source = "L\"L"; + let parser = ParserConfig::new() + .cdata_to_characters(true) + .ignore_comments(true) + .coalesce_characters(false) + .create_reader(std::io::Cursor::new(source)); + parser.into_iter().for_each(|e| { e.unwrap(); }); +} + // clones a lot but that's fine fn trim_until_bar(s: String) -> String { match s.trim() {