]> source.dussan.org Git - redmine.git/commitdiff
Make sure that anchor names generated for headings fully match wiki links (#7215).
authorEtienne Massip <etienne.massip@gmail.com>
Sun, 2 Oct 2011 17:25:29 +0000 (17:25 +0000)
committerEtienne Massip <etienne.massip@gmail.com>
Sun, 2 Oct 2011 17:25:29 +0000 (17:25 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@7563 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/application_helper.rb
test/unit/helpers/application_helper_test.rb

index eb29829b289a14873e4f97532f5b4d594d70d860..25592c46f3e9c6e825ee03a23fbb9a7371b6b1c6 100644 (file)
@@ -570,6 +570,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
@@ -727,7 +728,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)
       # used for single-file wiki export
       anchor = "#{obj.page.title}_#{anchor}" if options[:wiki_links] == :anchor && (obj.is_a?(WikiContent) || obj.is_a?(WikiContent::Version))
       @parsed_headings << [level, anchor, item]
@@ -919,6 +920,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)
index db18f8aa8e7ad8cf7235d4917c35373e412c0033..1d848bb710457d824bf1f36ba48badc7590268a6 100644 (file)
@@ -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 = %|<a name="#{anchor}"></a>\n<h1 >Some heading related to version 0.5<a href="##{anchor}" class="wiki-anchor">&para;</a></h1>|
+
+    assert_equal expected, textilizable(raw)
+  end
+
   def test_wiki_links_within_wiki_page_context
 
     page = WikiPage.find_by_title('Another_page' )