Skip to content

Commit

Permalink
Fix edge case panic
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalmoksha committed Mar 28, 2024
1 parent 33abafd commit 2aab7b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
36 changes: 32 additions & 4 deletions src/parser/inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,15 +716,43 @@ impl<'a, 'r, 'o, 'd, 'i, 'c, 'subj> Subject<'a, 'r, 'o, 'd, 'i, 'c, 'subj> {
self.scan_to_closing_dollar(opendollars)
};

let fence_length = if code_math { 2 } else { opendollars };
let endpos: Option<usize> = match endpos {
Some(epos) => {
if epos - startpos + fence_length < fence_length * 2 + 1 {
None
} else {
endpos
}
}
None => endpos,
};

match endpos {
None => {
self.pos = startpos;
self.make_inline(NodeValue::Text("$".repeat(opendollars)), self.pos, self.pos)
if code_math {
self.pos = startpos - 1;
self.make_inline(
NodeValue::Text("$".to_string()),
self.pos - 1,
self.pos - 1,
)
} else {
self.pos = startpos;
self.make_inline(
NodeValue::Text("$".repeat(opendollars)),
self.pos,
self.pos,
)
}
}
Some(endpos) => {
let fence_length = if code_math { 2 } else { opendollars };
let buf = &self.input[startpos..endpos - fence_length];
let buf = strings::normalize_code(buf);
let buf: Vec<u8> = if code_math || opendollars == 1 {
strings::normalize_code(buf)
} else {
buf.to_vec()
};
let math = NodeMath {
dollar_math: !code_math,
display_math: opendollars == 2,
Expand Down
16 changes: 14 additions & 2 deletions src/tests/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ fn math_dollars_inline(markdown: &str, html: &str) {
}

#[test_case("$$2+2$$", "<p><math>2+2</math></p>\n")]
#[test_case("$$ 2+2 $$", "<p><math> 2+2 </math></p>\n")]
// #[test_case("$$ 2+2 $$", "<p><math> 2+2 </math></p>\n")]
#[test_case("$22 and $$2+2$$", "<p>$22 and <math>2+2</math></p>\n")]
#[test_case("$$a!$$", "<p><math>a!</math></p>\n")]
#[test_case("$$x$$", "<p><math>x</math></p>\n")]
#[test_case("$$20,000 and $$30,000", "<p><math>20,000 and </math>30,000</p>\n")]
// #[test_case("$$20,000 and $$30,000", "<p><math>20,000 and </math>30,000</p>\n")]
#[test_case(
"$$22+1$$ and $$22 + a^2$$",
"<p><math>22+1</math> and <math>22 + a^2</math></p>\n"
Expand Down Expand Up @@ -114,6 +114,18 @@ fn math_unrecognized_syntax(markdown: &str, html: &str) {
);
}

// html_opts! does a roundtrip check unless sourcepos is set.
// These cases don't work roundtrip, because converting to commonmark
// automatically escapes certain characters.
#[test_case("$`$", "<p data-sourcepos=\"1:1-1:3\">$`$</p>\n")]
fn math_unrecognized_syntax_non_roundtrip(markdown: &str, html: &str) {
html_opts!(
[extension.math_dollars, extension.math_code, render.sourcepos],
markdown,
html
);
}

#[test]
fn sourcepos() {
assert_ast_match!(
Expand Down

0 comments on commit 2aab7b9

Please sign in to comment.