Repository: pulldown-cmark/pulldown-cmark (rust)

Issue:
pulldown-cmark panics on first pass when enabling `definition-lists`
I'm not sure if nested definition lists are valid, but using [pulldown-cmark 0.12.2](https://github.com/pulldown-cmark/pulldown-cmark/tree/v0.12.2) and the following input causes a panic:

```markdown
level one!
:
    level two!
    :
        level three!
        :

new level one!
:
```

Here's a small program that reproduces the issue:
```rust
use pulldown_cmark::{Options, Parser};

static INPUT: &str = "
level one!
:
    level two!
    :
        level three!
        :

new level one!
:
";

fn main() {
    let options = Options::ENABLE_DEFINITION_LIST;
    let parser = Parser::new_ext(INPUT, options);
    for _ in parser {}
}

```

<details>
<summary>Traceback</summary>

```
thread 'main' panicked at /Users/ytmimi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pulldown-cmark-0.12.2/src/firstpass.rs:1540:38:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: rust_begin_unwind
             at /rustc/e7c0d2750726c1f08b1de6956248ec78c4a97af6/library/std/src/panicking.rs:665:5
   1: core::panicking::panic_fmt
             at /rustc/e7c0d2750726c1f08b1de6956248ec78c4a97af6/library/core/src/panicking.rs:74:14
   2: core::panicking::panic
             at /rustc/e7c0d2750726c1f08b1de6956248ec78c4a97af6/library/core/src/panicking.rs:148:5
   3: core::option::unwrap_failed
             at /rustc/e7c0d2750726c1f08b1de6956248ec78c4a97af6/library/core/src/option.rs:2012:5
   4: core::option::Option<T>::unwrap
             at /Users/ytmimi/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:972:21
   5: pulldown_cmark::firstpass::FirstPass::pop
             at /Users/ytmimi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pulldown-cmark-0.12.2/src/firstpass.rs:1540:22
   6: pulldown_cmark::firstpass::FirstPass::parse_block
             at /Users/ytmimi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pulldown-cmark-0.12.2/src/firstpass.rs:99:13
   7: pulldown_cmark::firstpass::FirstPass::run
             at /Users/ytmimi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pulldown-cmark-0.12.2/src/firstpass.rs:76:18
   8: pulldown_cmark::firstpass::run_first_pass
             at /Users/ytmimi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pulldown-cmark-0.12.2/src/firstpass.rs:41:5
   9: pulldown_cmark::parse::Parser<F>::new_with_broken_link_callback
             at /Users/ytmimi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pulldown-cmark-0.12.2/src/parse.rs:270:34
  10: pulldown_cmark::parse::Parser::new_ext
             at /Users/ytmimi/.cargo/registry/src/index.crates.io-6f17d22bba15001f/pulldown-cmark-0.12.2/src/parse.rs:255:9
  11: example::main
             at ./src/example.rs:19:18
  12: core::ops::function::FnOnce::call_once
             at /Users/ytmimi/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
```

</details>

Pull request:
Safer definition lists implementation
Fixes #973

Patch:
diff --git a/pulldown-cmark/specs/definition_lists.txt b/pulldown-cmark/specs/definition_lists.txt
index c41e5d1..ae25016 100644
--- a/pulldown-cmark/specs/definition_lists.txt
+++ b/pulldown-cmark/specs/definition_lists.txt
@@ -520,3 +520,37 @@ third
 <dd>sixth</dd>
 </dl>
 ````````````````````````````````
+
+Nested definition lists:
+(<https://github.com/pulldown-cmark/pulldown-cmark/issues/973>):
+
+```````````````````````````````` example
+level one
+: l1
+    level two
+    : l2
+        level three
+        : l3
+
+level one
+: l1
+.
+<dl>
+<dt>level one</dt>
+<dd>
+<dl>
+<dt>l1
+level two</dt>
+<dd>
+<dl>
+<dt>l2
+level three</dt>
+<dd>l3</dd>
+</dl>
+</dd>
+</dl>
+</dd>
+<dt>level one</dt>
+<dd>l1</dd>
+</dl>
+````````````````````````````````
diff --git a/pulldown-cmark/src/tree.rs b/pulldown-cmark/src/tree.rs
index cca6208..4200d77 100644
--- a/pulldown-cmark/src/tree.rs
+++ b/pulldown-cmark/src/tree.rs
@@ -186,8 +186,6 @@ impl<T: Default> Tree<T> {
         }
         if next.is_some() {
             self.cur = next;
-        } else {
-            self.pop();
         }
     }
 }


Test patch:
diff --git a/pulldown-cmark/tests/suite/definition_lists.rs b/pulldown-cmark/tests/suite/definition_lists.rs
index 408a334..703a480 100644
--- a/pulldown-cmark/tests/suite/definition_lists.rs
+++ b/pulldown-cmark/tests/suite/definition_lists.rs
@@ -563,3 +563,38 @@ third
 
     test_markdown_html(original, expected, false, false, false);
 }
+
+#[test]
+fn definition_lists_test_26() {
+    let original = r##"level one
+: l1
+    level two
+    : l2
+        level three
+        : l3
+
+level one
+: l1
+"##;
+    let expected = r##"<dl>
+<dt>level one</dt>
+<dd>
+<dl>
+<dt>l1
+level two</dt>
+<dd>
+<dl>
+<dt>l2
+level three</dt>
+<dd>l3</dd>
+</dl>
+</dd>
+</dl>
+</dd>
+<dt>level one</dt>
+<dd>l1</dd>
+</dl>
+"##;
+
+    test_markdown_html(original, expected, false, false, false);
+}