From 4ccb8983606fbb1a5fcfd242fd15aa6275a42aaf Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Tue, 14 Feb 2012 08:09:23 +0000 Subject: [PATCH] Fixed: {{toc}} uses identical anchors for subsections with the same name (#8194). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8871 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/application_helper.rb | 6 +++++ test/unit/helpers/application_helper_test.rb | 27 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ac59ae87f..83a28bc01 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -503,6 +503,7 @@ module ApplicationHelper text = Redmine::WikiFormatting.to_html(Setting.text_formatting, text, :object => obj, :attribute => attr) @parsed_headings = [] + @heading_anchors = {} @current_section = 0 if options[:edit_section_links] parse_sections(text, project, obj, attr, only_path, options) @@ -816,6 +817,11 @@ module ApplicationHelper 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)) + @heading_anchors[anchor] ||= 0 + idx = (@heading_anchors[anchor] += 1) + if idx > 1 + anchor = "#{anchor}-#{idx}" + end @parsed_headings << [level, anchor, item] "\n#{content}" end diff --git a/test/unit/helpers/application_helper_test.rb b/test/unit/helpers/application_helper_test.rb index a8652b3f9..7024f6fa0 100644 --- a/test/unit/helpers/application_helper_test.rb +++ b/test/unit/helpers/application_helper_test.rb @@ -837,6 +837,33 @@ RAW assert textilizable(raw).gsub("\n", "").include?(expected) end + def test_table_of_content_should_generate_unique_anchors + raw = <<-RAW +{{toc}} + +h1. Title + +h2. Subtitle + +h2. Subtitle +RAW + + expected = '' + + @project = Project.find(1) + result = textilizable(raw).gsub("\n", "") + assert_include expected, result + assert_include '', result + assert_include '', result + end + def test_table_of_content_should_contain_included_page_headings raw = <<-RAW {{toc}} -- 2.39.5