Skip to content

Commit a8d94e6

Browse files
author
Ashe Connor
authored
Merge pull request #53 from brson/header-panic
Fix a corner case in the ATX header parser
2 parents c9ec5c5 + 873e1d9 commit a8d94e6

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/parser/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,25 @@ impl<'a, 'o> Parser<'a, 'o> {
949949
}
950950
};
951951
let count = self.first_nonspace - self.offset;
952-
self.advance_offset(&line, count, false);
953-
self.add_line(container, &line);
952+
953+
// In a rare case the above `chop` operation can leave
954+
// the line shorter than the recorded `first_nonspace`
955+
// This happens with ATX headers containing no header
956+
// text, multiple spaces and trailing hashes, e.g
957+
//
958+
// ### ###
959+
//
960+
// In this case `first_nonspace` indexes into the second
961+
// set of hashes, while `chop_trailing_hashtags` truncates
962+
// `line` to just `###` (the first three hashes).
963+
// In this case there's no text to add, and no further
964+
// processing to be done.
965+
let have_line_text = self.first_nonspace <= line.len();
966+
967+
if have_line_text {
968+
self.advance_offset(&line, count, false);
969+
self.add_line(container, &line);
970+
}
954971
} else {
955972
let start_column = self.first_nonspace + 1;
956973
container = self.add_child(container, NodeValue::Paragraph, start_column);

src/tests.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,11 @@ fn pathological_emphases() {
665665

666666
timeout_ms(move || html(&s, &exp), 4000);
667667
}
668+
669+
#[test]
670+
fn no_panic_on_empty_bookended_atx_headers() {
671+
html(
672+
"# #",
673+
"<h1></h1>\n"
674+
);
675+
}

0 commit comments

Comments
 (0)