From 605f1745ffc80368697bbb3671f5ef94d5bd5c5b 2011-10-02 17:28:33 From: Etienne Massip Date: 2011-10-02 17:28:33 Subject: [PATCH] Merged #7563 from trunk (#7215). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/branches/1.2-stable@7564 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 12cee39..606c739 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -550,6 +550,7 @@ module ApplicationHelper if page =~ /^(.+?)\#(.+)$/ page, anchor = $1, $2 end + anchor = sanitize_anchor_name(anchor) if anchor.present? # check if page exists wiki_page = link_project.wiki.find_page(page) url = if anchor.present? && wiki_page.present? && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version)) && obj.page == wiki_page @@ -707,7 +708,7 @@ module ApplicationHelper text.gsub!(HEADING_RE) do level, attrs, content = $1.to_i, $2, $3 item = strip_tags(content).strip - anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') + anchor = sanitize_anchor_name(item) @parsed_headings << [level, anchor, item] "\n#{content}" end @@ -893,6 +894,10 @@ module ApplicationHelper end end + def sanitize_anchor_name(anchor) + anchor.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') + end + # Returns the javascript tags that are included in the html layout head def javascript_heads tags = javascript_include_tag(:defaults) diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index f73d679..1ac475a 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -553,6 +553,16 @@ EXPECTED assert_equal expected, textilizable(raw) end + def test_headings_with_special_chars + # This test makes sure that the generated anchor names match the expected + # ones even if the heading text contains unconventional characters + raw = 'h1. Some heading related to version 0.5' + anchor = sanitize_anchor_name("Some-heading-related-to-version-0.5") + expected = %|\n

Some heading related to version 0.5

| + + assert_equal expected, textilizable(raw) + end + def test_wiki_links_within_wiki_page_context page = WikiPage.find_by_title('Another_page' )