]> source.dussan.org Git - redmine.git/commitdiff
Strip non utf8 content when displaying diff (ruby1.9 compatibility).
authorJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 1 Jan 2011 13:40:36 +0000 (13:40 +0000)
committerJean-Philippe Lang <jp_lang@yahoo.fr>
Sat, 1 Jan 2011 13:40:36 +0000 (13:40 +0000)
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4604 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/helpers/attachments_helper.rb
app/views/common/_diff.rhtml
test/fixtures/attachments.yml
test/fixtures/files/060719210727_changeset.diff [deleted file]
test/fixtures/files/060719210727_changeset_iso8859-1.diff [new file with mode: 0644]
test/fixtures/files/060719210727_changeset_utf8.diff [new file with mode: 0644]
test/functional/attachments_controller_test.rb

index 29cdb9790c9a69dcf1478c81a414dc768a3705c6..50b44890f2499f4fa5d226b222c93f0bab8c5ccd 100644 (file)
@@ -29,6 +29,12 @@ module AttachmentsHelper
   end
   
   def to_utf8(str)
-    str
+    return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
+    begin
+      Iconv.conv('UTF-8//IGNORE', 'UTF-8', str + '  ')[0..-3]
+    rescue Iconv::InvalidEncoding
+      # "UTF-8//IGNORE" is not supported on some OS
+      str
+    end
   end
 end
index 546644dede858f8f2e933cc2679d41123ec57c6e..619790c1378bbfd106a76bbbedefa98b2614fc8d 100644 (file)
@@ -4,7 +4,7 @@
 <% if diff_type == 'sbs' -%>
 <table class="filecontent">
 <thead>
-<tr><th colspan="4" class="filename"><%= table_file.file_name %></th></tr>
+<tr><th colspan="4" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
 </thead>
 <tbody>
 <% prev_line_left, prev_line_right = nil, nil -%>
@@ -31,7 +31,7 @@
 <% else -%>
 <table class="filecontent syntaxhl">
 <thead>
-<tr><th colspan="3" class="filename"><%= table_file.file_name %></th></tr>
+<tr><th colspan="3" class="filename"><%=to_utf8 table_file.file_name %></th></tr>
 </thead>
 <tbody>
 <% prev_line_left, prev_line_right = nil, nil -%>
index 487ce5aa9d85214965091eda5daf8ea688852e37..bd4a86ac650fecfe128e521c69c03ea4944c0ebe 100644 (file)
@@ -54,11 +54,11 @@ attachments_005:
   container_type: Issue
   container_id: 3
   downloads: 0
-  disk_filename: 060719210727_changeset.diff
+  disk_filename: 060719210727_changeset_iso8859-1.diff
   digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
   id: 5
   filesize: 687
-  filename: changeset.diff
+  filename: changeset_iso8859-1.diff
   author_id: 2
   content_type: text/x-diff
 attachments_006: 
@@ -157,3 +157,15 @@ attachments_013:
   filename: foo.zip
   author_id: 2
   content_type: application/octet-stream
+attachments_014: 
+  created_on: 2006-07-19 21:07:27 +02:00
+  container_type: Issue
+  container_id: 3
+  downloads: 0
+  disk_filename: 060719210727_changeset_utf8.diff
+  digest: b91e08d0cf966d5c6ff411bd8c4cc3a2
+  id: 14
+  filesize: 687
+  filename: changeset_utf8.diff
+  author_id: 2
+  content_type: text/x-diff
diff --git a/test/fixtures/files/060719210727_changeset.diff b/test/fixtures/files/060719210727_changeset.diff
deleted file mode 100644 (file)
index af2c206..0000000
+++ /dev/null
@@ -1,13 +0,0 @@
-Index: trunk/app/controllers/issues_controller.rb
-===================================================================
---- trunk/app/controllers/issues_controller.rb (r\82vision 1483)
-+++ trunk/app/controllers/issues_controller.rb (r\82vision 1484)
-@@ -149,7 +149,7 @@
-         attach_files(@issue, params[:attachments])
-         flash[:notice] = l(:notice_successful_create)
-         Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
--        redirect_to :controller => 'issues', :action => 'show', :id => @issue,  :project_id => @project
-+        redirect_to :controller => 'issues', :action => 'show', :id => @issue
-         return
-       end             
-     end       
diff --git a/test/fixtures/files/060719210727_changeset_iso8859-1.diff b/test/fixtures/files/060719210727_changeset_iso8859-1.diff
new file mode 100644 (file)
index 0000000..9bade6a
--- /dev/null
@@ -0,0 +1,13 @@
+Index: trunk/app/controllers/issues_controller.rb
+===================================================================
+--- trunk/app/controllers/issues_controller.rb (révision 1483)
++++ trunk/app/controllers/issues_controller.rb (révision 1484)
+@@ -149,7 +149,7 @@
+         attach_files(@issue, params[:attachments])
+         flash[:notice] = 'Demande créée avec succès'
+         Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
+-        redirect_to :controller => 'issues', :action => 'show', :id => @issue,  :project_id => @project
++        redirect_to :controller => 'issues', :action => 'show', :id => @issue
+         return
+       end             
+     end       
diff --git a/test/fixtures/files/060719210727_changeset_utf8.diff b/test/fixtures/files/060719210727_changeset_utf8.diff
new file mode 100644 (file)
index 0000000..e594f20
--- /dev/null
@@ -0,0 +1,13 @@
+Index: trunk/app/controllers/issues_controller.rb\r
+===================================================================\r
+--- trunk/app/controllers/issues_controller.rb (révision 1483)\r
++++ trunk/app/controllers/issues_controller.rb (révision 1484)\r
+@@ -149,7 +149,7 @@\r
+         attach_files(@issue, params[:attachments])\r
+         flash[:notice] = 'Demande créée avec succès'\r
+         Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')\r
+-        redirect_to :controller => 'issues', :action => 'show', :id => @issue,  :project_id => @project\r
++        redirect_to :controller => 'issues', :action => 'show', :id => @issue\r
+         return\r
+       end             \r
+     end       \r
index be452bd43e7700b2044c989675d48303bd8e5fdf..f7293ff9ce04225dd739a8294086ac964069e222 100644 (file)
@@ -1,5 +1,7 @@
-# redMine - project management software
-# Copyright (C) 2006-2008  Jean-Philippe Lang
+# encoding: utf-8
+#
+# Redmine - project management software
+# Copyright (C) 2006-2011  Jean-Philippe Lang
 #
 # This program is free software; you can redistribute it and/or
 # modify it under the terms of the GNU General Public License
@@ -35,10 +37,31 @@ class AttachmentsControllerTest < ActionController::TestCase
   end
   
   def test_show_diff
-    get :show, :id => 5
+    get :show, :id => 14 # 060719210727_changeset_utf8.diff
+    assert_response :success
+    assert_template 'diff'
+    assert_equal 'text/html', @response.content_type
+    
+    assert_tag 'th',
+      :attributes => {:class => /filename/},
+      :content => /issues_controller.rb\t\(révision 1484\)/
+    assert_tag 'td',
+      :attributes => {:class => /line-code/},
+      :content => /Demande créée avec succès/
+  end
+  
+  def test_show_diff_should_strip_non_utf8_content
+    get :show, :id => 5 # 060719210727_changeset_iso8859-1.diff
     assert_response :success
     assert_template 'diff'
     assert_equal 'text/html', @response.content_type
+    
+    assert_tag 'th',
+      :attributes => {:class => /filename/},
+      :content => /issues_controller.rb\t\(rvision 1484\)/
+    assert_tag 'td',
+      :attributes => {:class => /line-code/},
+      :content => /Demande cre avec succs/
   end
   
   def test_show_text_file