From 8d8e1eca1e6fcce2b4cf22c7f06cbc1facb834c4 Mon Sep 17 00:00:00 2001 From: Alexey Kopytko Date: Wed, 21 Dec 2016 11:43:01 +0900 Subject: [PATCH] rndr_header_anchor: do not remove character if nothing was added --- ext/redcarpet/html.c | 7 ++++++- test/html_render_test.rb | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ext/redcarpet/html.c b/ext/redcarpet/html.c index 1e3a9f61..2f717ba6 100644 --- a/ext/redcarpet/html.c +++ b/ext/redcarpet/html.c @@ -281,16 +281,20 @@ rndr_header_anchor(struct buf *out, const struct buf *anchor) int stripped = 0, inserted = 0; for (; i < size; ++i) { + // skip html tags if (a[i] == '<') { while (i < size && a[i] != '>') i++; + // skip html entities } else if (a[i] == '&') { while (i < size && a[i] != ';') i++; } + // replace non-ascii or invalid characters with dashes else if (!isascii(a[i]) || strchr(STRIPPED, a[i])) { if (inserted && !stripped) bufputc(out, '-'); + // and do it only once stripped = 1; } else { @@ -300,7 +304,8 @@ rndr_header_anchor(struct buf *out, const struct buf *anchor) } } - if (stripped) + // replace the last dash if there was anything added + if (stripped && inserted) out->size--; } diff --git a/test/html_render_test.rb b/test/html_render_test.rb index 42a7eb01..9bc3da95 100644 --- a/test/html_render_test.rb +++ b/test/html_render_test.rb @@ -252,6 +252,13 @@ def test_non_ascii_removal_in_header_anchors assert_equal html, render(markdown, with: [:with_toc_data]) end + def test_utf8_only_header_anchors + markdown = "# 見出し" + html = "

見出し

" + + assert_equal html, render(markdown, with: [:with_toc_data]) + end + def test_escape_entities_removal_from_anchor output = render("# Foo's & Bar's", with: [:with_toc_data]) result = %(

Foo's & Bar's

)