Repository: raphlinus/pulldown-cmark (rust)

Issue:
[fuzz result] tight lists can wrap atx header within emphasis
Similar to https://github.com/raphlinus/pulldown-cmark/issues/797

```console
$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
     Running `target/debug/pulldown-cmark`
- _t
  # test
  t_
<ul>
<li><em>t
<h1>test</h1>
t</em></li>
</ul>

```

<details>

Events from pulldown-cmark:

```rust
"+ _t\n  #\n  *_\n" -> [
  Start(List(None))
    Start(Item)
      Start(Emphasis)
        Text(Borrowed("t"))
        Start(Heading { level: H1, id: None, classes: [], attrs: [] })
        End(Heading(H1))
        Text(Borrowed("*"))
      End(Emphasis)
    End(Item)
  End(List(false))
]
```

Events from pandoc:

```rust
"+ _t\n  #\n  *_\n" -> [
  Start(List(None))
    Start(Item)
      Text(Boxed("_t"))
      Start(Heading { level: H1, id: None, classes: [], attrs: [] })
      End(Heading(H1))
      Text(Boxed("*_"))
    End(Item)
  End(List(false))
]
```

Events from commonmark.js:

```rust
"+ _t\n  #\n  *_\n" -> [
  Start(List(None))
    Start(Item)
      Text(Boxed("_t"))
      Start(Heading { level: H1, id: None, classes: [], attrs: [] })
      End(Heading(H1))
      Text(Boxed("*_"))
    End(Item)
  End(List(false))
]
```

</details>

Pull request description:
Don't allow `ItemBody::Heading` to appear inside emphasis
Fixes #828

Patch:
diff --git a/specs/regression.txt b/specs/regression.txt
index c716bab..6f3b595 100644
--- a/specs/regression.txt
+++ b/specs/regression.txt
@@ -2224,3 +2224,29 @@ invalid unicode is replaced with the unknown U+FFFD.
 .
 <p>�</p>
 ````````````````````````````````
+
+ISSUE 828
+
+```````````````````````````````` example
+- _t
+  # test
+  t_
+.
+<ul>
+<li>_t
+<h1>test</h1>
+t_</li>
+</ul>
+````````````````````````````````
+
+```````````````````````````````` example
+* *_
+  #
+  N*
+.
+<ul>
+<li>*_
+<h1></h1>
+N*</li>
+</ul>
+````````````````````````````````
diff --git a/src/parse.rs b/src/parse.rs
index d9dd8ef..7631066 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -131,6 +131,7 @@ impl ItemBody {
                 | ItemBody::TableHead
                 | ItemBody::TableRow
                 | ItemBody::TableCell
+                | ItemBody::Heading(..)
         )
     }
 }
@@ -756,7 +757,6 @@ impl<'input, F: BrokenLinkCallback<'input>> Parser<'input, F> {
                 }
                 _ => {
                     prev = cur;
-                    cur = self.tree[cur_ix].next;
                     // If this is a tight list, blocks and inlines might be
                     // siblings. Inlines can't cross the boundary like that.
                     if let Some(cur_ix) = cur {
@@ -764,6 +764,7 @@ impl<'input, F: BrokenLinkCallback<'input>> Parser<'input, F> {
                             self.inline_stack.pop_all(&mut self.tree);
                         }
                     }
+                    cur = self.tree[cur_ix].next;
                 }
             }
         }


Test patch:
diff --git a/tests/suite/regression.rs b/tests/suite/regression.rs
index d863a01..1ddf4a3 100644
--- a/tests/suite/regression.rs
+++ b/tests/suite/regression.rs
@@ -2601,7 +2601,7 @@ fn regression_test_161() {
 
     test_markdown_html(original, expected, false, false, false);
 }
-
+  
 #[test]
 fn regression_test_162() {
     let original = r##"&#00000000; &#x0000000;
@@ -2611,7 +2611,7 @@ fn regression_test_162() {
 
     test_markdown_html(original, expected, false, false, false);
 }
-
+  
 #[test]
 fn regression_test_163() {
     let original = r##"&#5307530;
@@ -2621,3 +2621,36 @@ fn regression_test_163() {
 
     test_markdown_html(original, expected, false, false, false);
 }
+
+#[test]
+fn regression_test_164() {
+    let original = r##"- _t
+  # test
+  t_
+"##;
+    let expected = r##"<ul>
+<li>_t
+<h1>test</h1>
+t_</li>
+</ul>
+"##;
+
+    test_markdown_html(original, expected, false, false, false);
+}
+
+#[test]
+fn regression_test_165() {
+    let original = r##"* *_
+  #
+  N*
+"##;
+    let expected = r##"<ul>
+<li>*_
+<h1></h1>
+N*</li>
+</ul>
+"##;
+
+    test_markdown_html(original, expected, false, false, false);
+}
+