Skip to content

Commit

Permalink
rndr_header_anchor: do not remove character if nothing was added
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Dec 21, 2016
1 parent 820dadb commit 8d8e1ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ext/redcarpet/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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--;
}

Expand Down
7 changes: 7 additions & 0 deletions test/html_render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<h1 id=\"\">見出し</h1>"

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 = %(<h1 id="foos-bars">Foo&#39;s &amp; Bar&#39;s</h1>)
Expand Down

0 comments on commit 8d8e1ec

Please sign in to comment.