From 4ee94a6574634496346dca1e5a8c1eb119aeb82a Mon Sep 17 00:00:00 2001 From: bootstraponline Date: Thu, 29 Nov 2012 23:47:25 -0700 Subject: [PATCH] Fix UTF-8 header and add test --- lib/gollum/frontend/views/page.rb | 7 ++++--- test/test_app.rb | 16 +++++++++++++++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/gollum/frontend/views/page.rb b/lib/gollum/frontend/views/page.rb index a359e66a..4329d64a 100644 --- a/lib/gollum/frontend/views/page.rb +++ b/lib/gollum/frontend/views/page.rb @@ -109,7 +109,7 @@ module Precious # Wraps page formatted data to Nokogiri::HTML document. # def build_document(content) - Nokogiri::HTML(%{
} + content + %{
}) + Nokogiri::HTML::fragment(%{
} + content.to_s + %{
}, 'UTF-8') end # Finds header node inside Nokogiri::HTML document. @@ -134,7 +134,7 @@ module Precious def page_header_from_content(content) doc = build_document(content) title = find_header_node(doc) - Sanitize.clean(title.to_html(:encoding => 'UTF-8')).strip unless title.empty? + Sanitize.clean(title.to_xhtml(:encoding => 'UTF-8')).strip unless title.empty? end # Returns page content without title if it was extracted. @@ -143,7 +143,8 @@ module Precious doc = build_document(content) title = find_header_node(doc) title.remove unless title.empty? - doc.css("div#gollum-root").inner_html + # .inner_html will cause href escaping on UTF-8 + doc.css("div#gollum-root").children.to_xhtml(:encoding => 'UTF-8') end end end diff --git a/test/test_app.rb b/test/test_app.rb index dadbc85c..02a48066 100644 --- a/test/test_app.rb +++ b/test/test_app.rb @@ -38,10 +38,24 @@ context "Frontend" do assert_equal 'm-plus-f', 'μ†ℱ'.to_url end - test 'utf-8 kcode' do + test "utf-8 kcode" do assert_equal 'μ†ℱ'.scan(/./), ["μ", "†", "ℱ"] end + test "UTF-8 headers href preserved" do + page = 'utfh1' + text = '한글' + + # don't use h1 or it will be promoted to replace file name + # which doesn't generate a normal header link + @wiki.write_page(page, :markdown, '## ' + text, + { :name => 'user1', :email => 'user1' }); + + get page + + assert_match /

#{text}<\/a><\/h2>/, last_response.body + end + test "retain edit information" do page1 = 'page1' user1 = 'user1'