Commit fd7b5fe08542e3398601c450d9f6d0a56fbc2d32

Martin Mitas 2021-02-05T21:40:47

md_analyze_line: Fix implicit ending of HTML blocks... ... when the HTML block is not explicitly ended (before the enclosing container block ends). Fixes #149.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e34e993..ee51733 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,15 @@
 # MD4C Change Log
 
 
+## Next Version (Work in Progress)
+
+Fixes:
+
+ * [#149](https://github.com/mity/md4c/issues/149):
+   A HTML block started in a container block (and not explicitly finished in
+   the block) could eat 1 line of actual contents.
+
+
 ## Version 0.4.7
 
 Changes:
diff --git a/src/md4c.c b/src/md4c.c
index 19ba212..bff2fd3 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -5779,25 +5779,30 @@ md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end,
 
         /* Check whether we are HTML block continuation. */
         if(pivot_line->type == MD_LINE_HTML  &&  ctx->html_block_type > 0) {
-            int html_block_type;
+            if(n_parents < ctx->n_containers) {
+                /* HTML block is implicitly ended if the enclosing container
+                 * block ends. */
+                ctx->html_block_type = 0;
+            } else {
+                int html_block_type;
 
-            html_block_type = md_is_html_block_end_condition(ctx, off, &off);
-            if(html_block_type > 0) {
-                MD_ASSERT(html_block_type == ctx->html_block_type);
+                html_block_type = md_is_html_block_end_condition(ctx, off, &off);
+                if(html_block_type > 0) {
+                    MD_ASSERT(html_block_type == ctx->html_block_type);
 
-                /* Make sure this is the last line of the block. */
-                ctx->html_block_type = 0;
+                    /* Make sure this is the last line of the block. */
+                    ctx->html_block_type = 0;
 
-                /* Some end conditions serve as blank lines at the same time. */
-                if(html_block_type == 6 || html_block_type == 7) {
-                    line->type = MD_LINE_BLANK;
-                    line->indent = 0;
-                    break;
+                    /* Some end conditions serve as blank lines at the same time. */
+                    if(html_block_type == 6 || html_block_type == 7) {
+                        line->type = MD_LINE_BLANK;
+                        line->indent = 0;
+                        break;
+                    }
                 }
-            }
 
-            if(n_parents == ctx->n_containers) {
                 line->type = MD_LINE_HTML;
+                n_parents = ctx->n_containers;
                 break;
             }
         }
diff --git a/test/coverage.txt b/test/coverage.txt
index f09721f..66d5cc8 100644
--- a/test/coverage.txt
+++ b/test/coverage.txt
@@ -306,6 +306,24 @@ foo
 ````````````````````````````````
 
 
+### [Issue 149](https://github.com/mity/md4c/issues/149)
+
+```````````````````````````````` example
+- <script>
+- foo
+bar
+</script>
+.
+<ul>
+<li><script>
+</li>
+<li>foo
+bar
+</script></li>
+</ul>
+````````````````````````````````
+
+
 ## Code coverage
 
 ### `md_is_unicode_whitespace__()`