diff options
author | Titus Wormer <tituswormer@gmail.com> | 2022-10-29 12:28:58 +0200 |
---|---|---|
committer | Titus Wormer <tituswormer@gmail.com> | 2022-10-29 12:28:58 +0200 |
commit | 48823f080218d5a13dbde2ed152b8144de5afef8 (patch) | |
tree | 0d26426681fee7f6dccf815944a0c02e2486fe0f | |
parent | 7bf7e86db9db89da11f80d2199bcd5ca7988efba (diff) | |
download | markdown-rs-48823f080218d5a13dbde2ed152b8144de5afef8.tar.gz markdown-rs-48823f080218d5a13dbde2ed152b8144de5afef8.tar.bz2 markdown-rs-48823f080218d5a13dbde2ed152b8144de5afef8.zip |
Fix attention starting and ending in different links
Example:
```markdown
[*]() [*]()
```
There was already code for:
```markdown
[*]() x*.
*x [*]().
```
But that wasn’t correct for attention at the same depth but in different places.
Closes GH-21.
Co-authored-by: Christian Murphy <christian.murphy.42@gmail.com>
-rw-r--r-- | src/construct/attention.rs | 23 | ||||
-rw-r--r-- | tests/fuzz.rs | 6 |
2 files changed, 18 insertions, 11 deletions
diff --git a/src/construct/attention.rs b/src/construct/attention.rs index dd87e05..ca7ad2e 100644 --- a/src/construct/attention.rs +++ b/src/construct/attention.rs @@ -92,8 +92,10 @@ use alloc::{vec, vec::Vec}; struct Sequence { /// Marker as a byte (`u8`) used in this sequence. marker: u8, - /// The depth in events where this sequence resides. - balance: usize, + /// We track whether sequences are in balanced events, and where those + /// events start, so that one attention doesn’t start in say, one link, and + /// end in another. + stack: Vec<usize>, /// The index into events where this sequence’s `Enter` currently resides. index: usize, /// The (shifted) point where this sequence starts. @@ -172,7 +174,7 @@ pub fn resolve(tokenizer: &mut Tokenizer) -> Option<Subresult> { // An opener matching our closer: if sequence_open.open && sequence_close.marker == sequence_open.marker - && sequence_close.balance == sequence_open.balance + && sequence_close.stack == sequence_open.stack { // If the opening can close or the closing can open, // and the close size *is not* a multiple of three, @@ -219,23 +221,20 @@ pub fn resolve(tokenizer: &mut Tokenizer) -> Option<Subresult> { } tokenizer.map.consume(&mut tokenizer.events); - None } /// Get sequences. fn get_sequences(tokenizer: &mut Tokenizer) -> Vec<Sequence> { let mut index = 0; - let mut balance = 0; + let mut stack = vec![]; let mut sequences = vec![]; while index < tokenizer.events.len() { let enter = &tokenizer.events[index]; - if enter.kind == Kind::Enter { - balance += 1; - - if enter.name == Name::AttentionSequence { + if enter.name == Name::AttentionSequence { + if enter.kind == Kind::Enter { let end = index + 1; let exit = &tokenizer.events[end]; @@ -255,7 +254,7 @@ fn get_sequences(tokenizer: &mut Tokenizer) -> Vec<Sequence> { sequences.push(Sequence { index, - balance, + stack: stack.clone(), start_point: enter.point.clone(), end_point: exit.point.clone(), size: exit.point.index - enter.point.index, @@ -272,8 +271,10 @@ fn get_sequences(tokenizer: &mut Tokenizer) -> Vec<Sequence> { marker, }); } + } else if enter.kind == Kind::Enter { + stack.push(index); } else { - balance -= 1; + stack.pop(); } index += 1; diff --git a/tests/fuzz.rs b/tests/fuzz.rs index 522e32f..e6816e6 100644 --- a/tests/fuzz.rs +++ b/tests/fuzz.rs @@ -104,5 +104,11 @@ fn fuzz() -> Result<(), String> { "9: autolink literals that end in table cell delimiter (GH-20)" ); + assert_eq!( + to_html_with_options("[*]() [*]()", &Options::gfm()), + Ok("<p><a href=\"\">*</a> <a href=\"\">*</a></p>".into()), + "10: attention in different links (GH-21)" + ); + Ok(()) } |