Commit 64f36805b02a874baa467a980029782c686ade1c

Martin Mitáš 2024-02-25T16:24:50

Fix handling tab when removing trailing whitespace. Espacially in connection with ATX headers.

diff --git a/CHANGELOG.md b/CHANGELOG.md
index e02071b..dbb6317 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -19,6 +19,10 @@ Fixes:
    same logic as other emphasis spans in respect to punctuation character and
    word boundaries.
 
+ - [#248](https://github.com/mity/md4c/issues/248):
+   Fix handling tab when removing trailing whitespace, especially in connection
+   with ATX headers.
+
 
 ## Version 0.5.2
 
diff --git a/src/md4c.c b/src/md4c.c
index 054c559..3679526 100644
--- a/src/md4c.c
+++ b/src/md4c.c
@@ -5250,10 +5250,10 @@ md_is_atxheader_line(MD_CTX* ctx, OFF beg, OFF* p_beg, OFF* p_end, unsigned* p_l
     *p_level = n;
 
     if(!(ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS)  &&  off < ctx->size  &&
-       CH(off) != _T(' ')  &&  CH(off) != _T('\t')  &&  !ISNEWLINE(off))
+       !ISBLANK(off)  &&  !ISNEWLINE(off))
         return FALSE;
 
-    while(off < ctx->size  &&  CH(off) == _T(' '))
+    while(off < ctx->size  &&  ISBLANK(off))
         off++;
     *p_beg = off;
     *p_end = off;
@@ -6248,17 +6248,17 @@ md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end,
     /* But for ATX header, we should exclude the optional trailing mark. */
     if(line->type == MD_LINE_ATXHEADER) {
         OFF tmp = line->end;
-        while(tmp > line->beg && CH(tmp-1) == _T(' '))
+        while(tmp > line->beg && ISBLANK(tmp-1))
             tmp--;
         while(tmp > line->beg && CH(tmp-1) == _T('#'))
             tmp--;
-        if(tmp == line->beg || CH(tmp-1) == _T(' ') || (ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS))
+        if(tmp == line->beg || ISBLANK(tmp-1) || (ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS))
             line->end = tmp;
     }
 
     /* Trim trailing spaces. */
     if(line->type != MD_LINE_INDENTEDCODE  &&  line->type != MD_LINE_FENCEDCODE  && line->type != MD_LINE_HTML) {
-        while(line->end > line->beg && CH(line->end-1) == _T(' '))
+        while(line->end > line->beg && ISBLANK(line->end-1))
             line->end--;
     }
 
diff --git a/test/regressions.txt b/test/regressions.txt
index 7411206..836b970 100644
--- a/test/regressions.txt
+++ b/test/regressions.txt
@@ -740,3 +740,24 @@ copy "~user1/file" to "~user2/file"
 .
 --fstrikethrough
 ````````````````````````````````
+
+
+## [Issue 248](https://github.com/mity/md4c/issues/248)
+
+(These are in spec.txt, but we need the [no-normalize] flag in order to
+catch the whitespace issues.)
+
+```````````````````````````````` example [no-normalize]
+#→Foo
+.
+<h1>Foo</h1>
+````````````````````````````````
+
+```````````````````````````````` example [no-normalize]
+  Foo *bar
+baz*→
+====
+.
+<h1>Foo <em>bar
+baz</em></h1>
+````````````````````````````````