Merge pull request #3144 from floriank/fix/20138-toc-links-broken

[20138] use absolute urls for header linking in ToCs
pull/3045/merge
Jan Sandbrink 9 years ago
commit a13d498448
  1. 20
      lib/open_project/text_formatting.rb
  2. 12
      spec/legacy/unit/helpers/application_helper_spec.rb
  3. 110
      spec/lib/open_project/text_formatting_spec.rb

@ -379,7 +379,8 @@ module OpenProject
item = strip_tags(content).strip item = strip_tags(content).strip
anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-') anchor = item.gsub(%r{[^\w\s\-]}, '').gsub(%r{\s+(\-+\s*)?}, '-')
@parsed_headings << [level, anchor, item] @parsed_headings << [level, anchor, item]
"<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"##{anchor}\" class=\"wiki-anchor\">&para;</a></h#{level}>" url = full_url(anchor)
"<a name=\"#{anchor}\"></a>\n<h#{level} #{attrs}>#{content}<a href=\"#{url}\" class=\"wiki-anchor\">&para;</a></h#{level}>"
end end
end end
@ -408,7 +409,8 @@ module OpenProject
elsif started elsif started
out << '</li><li>' out << '</li><li>'
end end
out << "<a href=\"##{anchor}\">#{item}</a>" url = full_url anchor
out << "<a href=\"#{url}\">#{item}</a>"
current = level current = level
started = true started = true
end end
@ -418,5 +420,19 @@ module OpenProject
end end
end end
end end
#
# displays the current url plus an optional anchor
#
def full_url(anchor_name = '')
return "##{anchor_name}" if current_request.nil?
current = url_for
return current if anchor_name.blank?
"#{current}##{anchor_name}"
end
def current_request
request rescue nil
end
end end
end end

@ -394,14 +394,23 @@ EXPECTED
assert_equal expected.gsub(%r{[\r\n\t]}, ''), format_text(raw).gsub(%r{[\r\n\t]}, '') assert_equal expected.gsub(%r{[\r\n\t]}, ''), format_text(raw).gsub(%r{[\r\n\t]}, '')
end end
it 'should headings' do it 'should headings without url' do
undef request
raw = 'h1. Some heading' raw = 'h1. Some heading'
expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>| expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="#Some-heading" class="wiki-anchor">&para;</a></h1>|
assert_equal expected, format_text(raw) assert_equal expected, format_text(raw)
end end
it 'should headings with url' do
raw = 'h1. Some heading'
expected = %|<a name="Some-heading"></a>\n<h1 >Some heading<a href="/issues#Some-heading" class="wiki-anchor">&para;</a></h1>|
assert_equal expected, format_text(raw)
end
it 'should table of content' do it 'should table of content' do
undef request
@project.wiki.start_page = 'Wiki' @project.wiki.start_page = 'Wiki'
@project.wiki.save! @project.wiki.save!
FactoryGirl.create :wiki_page_with_content, wiki: @project.wiki, title: 'Wiki' FactoryGirl.create :wiki_page_with_content, wiki: @project.wiki, title: 'Wiki'
@ -464,6 +473,7 @@ RAW
end end
it 'should table of content should contain included page headings' do it 'should table of content should contain included page headings' do
undef request
@project.wiki.start_page = 'Wiki' @project.wiki.start_page = 'Wiki'
@project.save! @project.save!
page = FactoryGirl.create :wiki_page_with_content, wiki: @project.wiki, title: 'Wiki' page = FactoryGirl.create :wiki_page_with_content, wiki: @project.wiki, title: 'Wiki'

@ -573,39 +573,83 @@ WIKI_TEXT
subject(:html) { format_text(wiki_text) } subject(:html) { format_text(wiki_text) }
it 'emits a table of contents for headings h1-h4' do context 'w/ request present' do
expect(html).to be_html_eql(%{ let(:request) { ActionController::TestRequest.new }
<fieldset class='form--fieldset -collapsible'> let(:url_for) { '/test' }
<legend class='form--fieldset-legend' title='Show/Hide table of contents' onclick='toggleFieldset(this);'>
<a href='javascript:'>Table of Contents</a> it 'emits a table of contents for headings h1-h4 with links present' do
</legend> expect(html).to be_html_eql(%{
<div> <fieldset class='form--fieldset -collapsible'>
<ul class="toc"> <legend class='form--fieldset-legend' title='Show/Hide table of contents' onclick='toggleFieldset(this);'>
<li> <a href='javascript:'>Table of Contents</a>
<a href="#Orange">Orange</a> </legend>
<ul> <div>
<li> <ul class="toc">
<a href="#Varietes">Varietes</a> <li>
<ul> <a href="/test#Orange">Orange</a>
<li> <ul>
<a href="#Common-Oranges">Common Oranges</a> <li>
<ul> <a href="/test#Varietes">Varietes</a>
<li><a href="#Valencia">Valencia</a></li> <ul>
<li><a href="#Harts-Tardiff-Valencia">Hart's Tardiff Valencia</a></li> <li>
</ul> <a href="/test#Common-Oranges">Common Oranges</a>
</li> <ul>
<li><a href="#Navel-Oranges">Navel Oranges</a></li> <li><a href="/test#Valencia">Valencia</a></li>
<li><a href="#Blood-Oranges">Blood Oranges</a></li> <li><a href="/test#Harts-Tardiff-Valencia">Hart's Tardiff Valencia</a></li>
<li><a href="#Acidless-Oranges">Acidless Oranges</a></li> </ul>
</ul> </li>
</li> <li><a href="/test#Navel-Oranges">Navel Oranges</a></li>
<li><a href="#Attributes">Attributes</a></li> <li><a href="/test#Blood-Oranges">Blood Oranges</a></li>
</ul> <li><a href="/test#Acidless-Oranges">Acidless Oranges</a></li>
</li> </ul>
</ul> </li>
</div> <li><a href="/test#Attributes">Attributes</a></li>
</fieldset> </ul>
}).at_path('fieldset') </li>
</ul>
</div>
</fieldset>
}).at_path('fieldset')
end
end
context 'w/o request present' do
let(:request) { nil }
it 'emits a table of contents for headings h1-h4 with anchors' do
expect(html).to be_html_eql(%{
<fieldset class='form--fieldset -collapsible'>
<legend class='form--fieldset-legend' title='Show/Hide table of contents' onclick='toggleFieldset(this);'>
<a href='javascript:'>Table of Contents</a>
</legend>
<div>
<ul class="toc">
<li>
<a href="#Orange">Orange</a>
<ul>
<li>
<a href="#Varietes">Varietes</a>
<ul>
<li>
<a href="#Common-Oranges">Common Oranges</a>
<ul>
<li><a href="#Valencia">Valencia</a></li>
<li><a href="#Harts-Tardiff-Valencia">Hart's Tardiff Valencia</a></li>
</ul>
</li>
<li><a href="#Navel-Oranges">Navel Oranges</a></li>
<li><a href="#Blood-Oranges">Blood Oranges</a></li>
<li><a href="#Acidless-Oranges">Acidless Oranges</a></li>
</ul>
</li>
<li><a href="#Attributes">Attributes</a></li>
</ul>
</li>
</ul>
</div>
</fieldset>
}).at_path('fieldset')
end
end end
end end

Loading…
Cancel
Save