aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Titus Wormer <tituswormer@gmail.com>2022-07-04 13:45:32 +0200
committerLibravatar Titus Wormer <tituswormer@gmail.com>2022-07-04 13:45:32 +0200
commit3b083d023a8b6c7d4fe6b1140df90c907230b152 (patch)
tree6eda4ff9ba49eba503aea1b5f4b2bbf3484d2aab
parent990b42e7bddb9ac5b5c701d142cf07664bd62ad7 (diff)
downloadmarkdown-rs-3b083d023a8b6c7d4fe6b1140df90c907230b152.tar.gz
markdown-rs-3b083d023a8b6c7d4fe6b1140df90c907230b152.tar.bz2
markdown-rs-3b083d023a8b6c7d4fe6b1140df90c907230b152.zip
Fix opening/closing tag order of nested attention
-rw-r--r--src/construct/attention.rs2
-rw-r--r--src/util/edit_map.rs41
-rw-r--r--tests/attention.rs67
3 files changed, 61 insertions, 49 deletions
diff --git a/src/construct/attention.rs b/src/construct/attention.rs
index 95b4079..d4541b4 100644
--- a/src/construct/attention.rs
+++ b/src/construct/attention.rs
@@ -256,7 +256,7 @@ pub fn resolve(tokenizer: &mut Tokenizer) -> Vec<Event> {
}
// Opening.
- edit_map.add(
+ edit_map.add_before(
open_event_index,
0,
vec![
diff --git a/src/util/edit_map.rs b/src/util/edit_map.rs
index 0d58f01..665367a 100644
--- a/src/util/edit_map.rs
+++ b/src/util/edit_map.rs
@@ -66,18 +66,11 @@ impl EditMap {
}
}
/// Create an edit: a remove and/or add at a certain place.
- pub fn add(&mut self, index: usize, mut remove: usize, mut add: Vec<Event>) {
- assert!(!self.consumed, "cannot add after consuming");
-
- if let Some((curr_remove, mut curr_add)) = self.map.remove(&index) {
- // To do: these might have to be split in several chunks instead
- // of one, if links in `curr_add` are supported.
- remove += curr_remove;
- curr_add.append(&mut add);
- add = curr_add;
- }
-
- self.map.insert(index, (remove, add));
+ pub fn add(&mut self, index: usize, remove: usize, add: Vec<Event>) {
+ add_impl(self, index, remove, add, false);
+ }
+ pub fn add_before(&mut self, index: usize, remove: usize, add: Vec<Event>) {
+ add_impl(self, index, remove, add, true);
}
/// Done, change the events.
pub fn consume(&mut self, events: &mut [Event]) -> Vec<Event> {
@@ -142,3 +135,27 @@ impl EditMap {
next_events
}
}
+
+fn add_impl(
+ edit_map: &mut EditMap,
+ index: usize,
+ mut remove: usize,
+ mut add: Vec<Event>,
+ before: bool,
+) {
+ assert!(!edit_map.consumed, "cannot add after consuming");
+
+ if let Some((curr_remove, mut curr_add)) = edit_map.map.remove(&index) {
+ // To do: these might have to be split in several chunks instead
+ // of one, if links in `curr_add` are supported.
+ remove += curr_remove;
+ if before {
+ add.append(&mut curr_add);
+ } else {
+ curr_add.append(&mut add);
+ add = curr_add;
+ }
+ }
+
+ edit_map.map.insert(index, (remove, add));
+}
diff --git a/tests/attention.rs b/tests/attention.rs
index 221b265..11ba7d8 100644
--- a/tests/attention.rs
+++ b/tests/attention.rs
@@ -386,12 +386,11 @@ fn attention() {
"should not support adjacent emphasis in certain cases"
);
- // To do: `edit_map`: insert before.
- // assert_eq!(
- // micromark("***foo** bar*"),
- // "<p><em><strong>foo</strong> bar</em></p>",
- // "complex (1)"
- // );
+ assert_eq!(
+ micromark("***foo** bar*"),
+ "<p><em><strong>foo</strong> bar</em></p>",
+ "complex (1)"
+ );
assert_eq!(
micromark("*foo **bar***"),
"<p><em>foo <strong>bar</strong></em></p>",
@@ -403,12 +402,11 @@ fn attention() {
"complex (3)"
);
- // To do: `edit_map`: insert before.
- // assert_eq!(
- // micromark("foo***bar***baz"),
- // "<p>foo<em><strong>bar</strong></em>baz</p>",
- // "complex (a)"
- // );
+ assert_eq!(
+ micromark("foo***bar***baz"),
+ "<p>foo<em><strong>bar</strong></em>baz</p>",
+ "complex (a)"
+ );
assert_eq!(
micromark("foo******bar*********baz"),
"<p>foo<strong><strong><strong>bar</strong></strong></strong>***baz</p>",
@@ -488,12 +486,11 @@ fn attention() {
"should support nesting emphasis and strong (6)"
);
- // To do: `edit_map`: insert before.
- // assert_eq!(
- // micromark("***foo* bar**"),
- // "<p><strong><em>foo</em> bar</strong></p>",
- // "should support nesting emphasis and strong (7)"
- // );
+ assert_eq!(
+ micromark("***foo* bar**"),
+ "<p><strong><em>foo</em> bar</strong></p>",
+ "should support nesting emphasis and strong (7)"
+ );
assert_eq!(
micromark("**foo *bar***"),
@@ -562,7 +559,7 @@ fn attention() {
"should support strong emphasis around the other marker"
);
- // To do: `edit_map`: insert before / resizing attention bug?
+ // To do: resizing attention bug?
// assert_eq!(
// micromark("**foo*"),
// "<p>*<em>foo</em></p>",
@@ -575,14 +572,14 @@ fn attention() {
"should support a superfluous marker at the end of emphasis"
);
- // To do: `edit_map`: insert before / resizing attention bug?
+ // To do: resizing attention bug?
// assert_eq!(
// micromark("***foo**"),
// "<p>*<strong>foo</strong></p>",
// "should support a superfluous marker at the start of strong"
// );
- // To do: `edit_map`: insert before / resizing attention bug?
+ // To do: resizing attention bug?
// assert_eq!(
// micromark("****foo*"),
// "<p>***<em>foo</em></p>",
@@ -638,7 +635,7 @@ fn attention() {
"should support strong emphasis around the other marker"
);
- // To do: `edit_map`: insert before / resizing attention bug?
+ // To do: resizing attention bug?
// assert_eq!(
// micromark("__foo_"),
// "<p>_<em>foo</em></p>",
@@ -651,14 +648,14 @@ fn attention() {
"should support a superfluous marker at the end of emphasis"
);
- // To do: `edit_map`: insert before / resizing attention bug?
+ // To do: resizing attention bug?
// assert_eq!(
// micromark("___foo__"),
// "<p>_<strong>foo</strong></p>",
// "should support a superfluous marker at the start of strong"
// );
- // To do: `edit_map`: insert before / resizing attention bug?
+ // To do: resizing attention bug?
// assert_eq!(
// micromark("____foo_"),
// "<p>___<em>foo</em></p>",
@@ -721,19 +718,17 @@ fn attention() {
);
// Rule 14.
- // To do: `edit_map`: insert before.
- // assert_eq!(
- // micromark("***foo***"),
- // "<p><em><strong>foo</strong></em></p>",
- // "should support strong directly in emphasis w/ `*`"
- // );
+ assert_eq!(
+ micromark("***foo***"),
+ "<p><em><strong>foo</strong></em></p>",
+ "should support strong directly in emphasis w/ `*`"
+ );
- // To do: `edit_map`: insert before.
- // assert_eq!(
- // micromark("___foo___"),
- // "<p><em><strong>foo</strong></em></p>",
- // "should support strong directly in emphasis w/ `_`"
- // );
+ assert_eq!(
+ micromark("___foo___"),
+ "<p><em><strong>foo</strong></em></p>",
+ "should support strong directly in emphasis w/ `_`"
+ );
// Rule 15.
// To do: interleaving attention.