Repository: Python-Markdown/markdown (python)

Issue:
Combined emphasis and strong emphasis wrong output
Not sure `***word***` is actually supported, although it does seem to work in general. But with 3.2.2:

```
>>> markdown.markdown("""traced ***along*** bla **blocked** if other ***or***""")
'<p>traced <strong><em>along</em></strong> bla <strong>blocked*<em> if other </em></strong>or***</p>'
```

I.e. the strong emphasis on "blocked" is not ended, but emphasis started instead.

Pull request description:
Fix issues with complex emphasis
Resolves issue that can occur with complex emphasis combinations.
Fixes #979

Patch:
diff --git a/docs/change_log/release-3.3.md b/docs/change_log/release-3.3.md
new file mode 100644
index 0000000..bc7cd40
--- /dev/null
+++ b/docs/change_log/release-3.3.md
@@ -0,0 +1,16 @@
+title: Release Notes for v3.3
+
+# Python-Markdown 3.3 Release Notes
+
+Python-Markdown version 3.3 supports Python versions 3.5, 3.6, 3.7, 3.8, and
+PyPy3.
+
+## Backwards-incompatible changes
+
+## New features
+
+## Bug fixes
+
+The following bug fixes are included in the 3.3 release:
+
+* Fix issues with complex emphasis (#979).
diff --git a/markdown/extensions/legacy_em.py b/markdown/extensions/legacy_em.py
index c3d7b54..7fddb77 100644
--- a/markdown/extensions/legacy_em.py
+++ b/markdown/extensions/legacy_em.py
@@ -21,7 +21,7 @@ EMPHASIS_RE = r'(_)([^_]+)\1'
 STRONG_RE = r'(_{2})(.+?)\1'
 
 # __strong_em___
-STRONG_EM_RE = r'(_)\1(?!\1)(.+?)\1(?!\1)(.+?)\1{3}'
+STRONG_EM_RE = r'(_)\1(?!\1)([^_]+?)\1(?!\1)(.+?)\1{3}'
 
 
 class LegacyUnderscoreProcessor(UnderscoreProcessor):
diff --git a/markdown/inlinepatterns.py b/markdown/inlinepatterns.py
index ad95519..0655047 100644
--- a/markdown/inlinepatterns.py
+++ b/markdown/inlinepatterns.py
@@ -135,8 +135,8 @@ STRONG_EM_RE = r'(\*)\1{2}(.+?)\1{2}(.*?)\1'
 # ___strong__em_
 STRONG_EM2_RE = r'(_)\1{2}(.+?)\1{2}(.*?)\1'
 
-# __strong_em___
-STRONG_EM3_RE = r'(\*)\1(?!\1)(.+?)\1(?!\1)(.+?)\1{3}'
+# **strong*em***
+STRONG_EM3_RE = r'(\*)\1(?!\1)([^*]+?)\1(?!\1)(.+?)\1{3}'
 
 # [text](url) or [text](<url>) or [text](url "title")
 LINK_RE = NOIMG + r'\['


Test patch:
diff --git a/tests/test_syntax/extensions/test_legacy_em.py b/tests/test_syntax/extensions/test_legacy_em.py
index efad43d..c5daf1c 100644
--- a/tests/test_syntax/extensions/test_legacy_em.py
+++ b/tests/test_syntax/extensions/test_legacy_em.py
@@ -50,3 +50,17 @@ class TestLegacyEm(TestCase):
             '<p>This is text <strong>bold<em>italic bold</em></strong> with more text</p>',
             extensions=['legacy_em']
         )
+
+    def test_complex_multple_underscore_type(self):
+
+        self.assertMarkdownRenders(
+            'traced ___along___ bla __blocked__ if other ___or___',
+            '<p>traced <strong><em>along</em></strong> bla <strong>blocked</strong> if other <strong><em>or</em></strong></p>'  # noqa: E501
+        )
+
+    def test_complex_multple_underscore_type_variant2(self):
+
+        self.assertMarkdownRenders(
+            'on the __1-4 row__ of the AP Combat Table ___and___ receive',
+            '<p>on the <strong>1-4 row</strong> of the AP Combat Table <strong><em>and</em></strong> receive</p>'
+        )
diff --git a/tests/test_syntax/inline/test_emphasis.py b/tests/test_syntax/inline/test_emphasis.py
index b4f1d0d..1e7fafa 100644
--- a/tests/test_syntax/inline/test_emphasis.py
+++ b/tests/test_syntax/inline/test_emphasis.py
@@ -114,3 +114,17 @@ class TestNotEmphasis(TestCase):
             'This text is **bold *italic* *italic* bold**',
             '<p>This text is <strong>bold <em>italic</em> <em>italic</em> bold</strong></p>'
         )
+
+    def test_complex_multple_emphasis_type(self):
+
+        self.assertMarkdownRenders(
+            'traced ***along*** bla **blocked** if other ***or***',
+            '<p>traced <strong><em>along</em></strong> bla <strong>blocked</strong> if other <strong><em>or</em></strong></p>'  # noqa: E501
+        )
+
+    def test_complex_multple_emphasis_type_variant2(self):
+
+        self.assertMarkdownRenders(
+            'on the **1-4 row** of the AP Combat Table ***and*** receive',
+            '<p>on the <strong>1-4 row</strong> of the AP Combat Table <strong><em>and</em></strong> receive</p>'
+        )