From 0eb723e125b69d5538dcd52194bbaafe2b7e8c70 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Tue, 9 Oct 2007 02:51:38 -0500 Subject: [PATCH 1/3] Accessibility: Use Uploaded File Descriptions The file upload dialog asks for a description of the image or file to be uploaded. Use this as the default alt-text for the image and as a title attribute for a file link. --- app/models/web.rb | 5 +++++ app/views/file/file.rhtml | 4 ++-- lib/url_generator.rb | 23 +++++++++++++---------- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/models/web.rb b/app/models/web.rb index 59129120..1e1d5585 100644 --- a/app/models/web.rb +++ b/app/models/web.rb @@ -47,6 +47,11 @@ class Web < ActiveRecord::Base WikiFile.find_by_file_name(file_name) != nil end + def description(file_name) + file = WikiFile.find_by_file_name(file_name) + file.description if file + end + def markup read_attribute('markup').to_sym end diff --git a/app/views/file/file.rhtml b/app/views/file/file.rhtml index cbdf0325..37c490b7 100644 --- a/app/views/file/file.rhtml +++ b/app/views/file/file.rhtml @@ -9,7 +9,7 @@ { 'multipart' => true , 'accept-charset' => 'utf-8' }) do %>
<%= hidden_field 'file', 'file_name' %> - +

@@ -18,7 +18,7 @@ <%= link_to :back %> and edit the wiki page that refers to the file.
- +
<%= text_field "file", "description", "size" => 40 %>
diff --git a/lib/url_generator.rb b/lib/url_generator.rb index b5415e63..995742b7 100644 --- a/lib/url_generator.rb +++ b/lib/url_generator.rb @@ -17,15 +17,16 @@ class AbstractUrlGenerator known_page = web.has_page?(name) else known_page = web.has_file?(name) + description = web.description(name) end case link_type when :show page_link(mode, name, text, web.address, known_page) when :file - file_link(mode, name, text, web.address, known_page) + file_link(mode, name, text, web.address, known_page, description) when :pic - pic_link(mode, name, text, web.address, known_page) + pic_link(mode, name, text, web.address, known_page, description) else raise "Unknown link type: #{link_type}" end @@ -37,11 +38,12 @@ class UrlGenerator < AbstractUrlGenerator private - def file_link(mode, name, text, web_address, known_file) + def file_link(mode, name, text, web_address, known_file, description) + title = CGI::escapeHTML(CGI::unescapeHTML( description || text)) case mode when :export if known_file - %{#{text}} + %{#{text}} else %{#{text}} end @@ -49,7 +51,7 @@ class UrlGenerator < AbstractUrlGenerator if known_file href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file', :id => name - %{#{text}} + %{#{text}} else %{#{text}} end @@ -57,7 +59,7 @@ class UrlGenerator < AbstractUrlGenerator href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file', :id => name if known_file - %{#{text}} + %{#{text}} else %{#{text}?} end @@ -93,17 +95,18 @@ class UrlGenerator < AbstractUrlGenerator end end - def pic_link(mode, name, text, web_address, known_pic) + def pic_link(mode, name, text, web_address, known_pic, description) + alt_text = CGI::escapeHTML(CGI::unescapeHTML( description || text)) case mode when :export if known_pic - %{#{text}} + %{#{alt_text}} else %{#{text}} end when :publish if known_pic - %{#{text}} + %{#{alt_text}} else %{#{text}} end @@ -111,7 +114,7 @@ class UrlGenerator < AbstractUrlGenerator href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file', :id => name if known_pic - %{#{text}} + %{#{alt_text}} else %{#{text}?} end From 402de89abff1f37ac30341aaf2cd7c1501602513 Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Tue, 9 Oct 2007 03:16:07 -0500 Subject: [PATCH 2/3] Tests for Rev 171 One test is still broken. Will fix. --- test/test_helper.rb | 18 ++++++++++-------- test/unit/page_renderer_test.rb | 6 +++--- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 9be06248..f69767a3 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -102,18 +102,19 @@ class StubUrlGenerator < AbstractUrlGenerator super(:doesnt_need_controller) end - def file_link(mode, name, text, web_name, known_file) + def file_link(mode, name, text, web_name, known_file, description) + title = CGI::escapeHTML(CGI::unescapeHTML( description || text)) link = CGI.escape(name) case mode when :export - if known_file then %{#{text}} + if known_file then %{#{text}} else %{#{text}} end when :publish - if known_file then %{#{text}} + if known_file then %{#{text}} else %{#{text}} end else if known_file - %{#{text}} + %{#{text}} else %{#{text}?} end @@ -142,17 +143,18 @@ class StubUrlGenerator < AbstractUrlGenerator end end - def pic_link(mode, name, text, web_name, known_pic) + def pic_link(mode, name, text, web_name, known_pic, description) + alt_text = CGI::escapeHTML(CGI::unescapeHTML( description || text)) link = CGI.escape(name) case mode.to_sym when :export - if known_pic then %{#{text}} + if known_pic then %{#{alt_text}} else %{#{text}} end when :publish - if known_pic then %{#{text}} + if known_pic then %{#{alt_text}} else %{#{text}} end else - if known_pic then %{#{text}} + if known_pic then %{#{alt_text}} else %{#{text}?} end end end diff --git a/test/unit/page_renderer_test.rb b/test/unit/page_renderer_test.rb index 29cad167..17ea2fa8 100644 --- a/test/unit/page_renderer_test.rb +++ b/test/unit/page_renderer_test.rb @@ -305,10 +305,10 @@ class PageRendererTest < Test::Unit::TestCase FileUtils.rm_rf("#{RAILS_ROOT}/public/wiki1/files/*") @web.wiki_files.create(:file_name => 'square.jpg', :description => 'Square', :content => 'never mind') assert_markup_parsed_as( - '

Square

', - '[[square.jpg|Square:pic]]') + '

Blue Square

', + '[[square.jpg|Blue Square:pic]]') assert_markup_parsed_as( - '

square.jpg

', + '

Square

', '[[square.jpg:pic]]') end From fbdf4c5dfedbf0c95ab6736add4d1543528bc4de Mon Sep 17 00:00:00 2001 From: Jacques Distler Date: Tue, 9 Oct 2007 11:02:44 -0500 Subject: [PATCH 3/3] Fix Broken Test Was not picking up user-supplied alt text in [[filename|Alt text:pic]]. Fixed. --- lib/url_generator.rb | 29 ++++++++++++++++------------- test/test_helper.rb | 13 ++++++------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/lib/url_generator.rb b/lib/url_generator.rb index 995742b7..dbc68dd3 100644 --- a/lib/url_generator.rb +++ b/lib/url_generator.rb @@ -9,7 +9,6 @@ class AbstractUrlGenerator # on the render mode in options and whether the page (file) exists # in the web. def make_link(name, web, text = nil, options = {}) - text = CGI.escapeHTML(text || WikiWords.separate(name)) mode = (options[:mode] || :show).to_sym link_type = (options[:link_type] || :show).to_sym @@ -19,14 +18,20 @@ class AbstractUrlGenerator known_page = web.has_file?(name) description = web.description(name) end + if (text == name) + text = description || text + else + text = text || description + end + text = CGI.escapeHTML(CGI.unescapeHTML(text || WikiWords.separate(name))) case link_type when :show page_link(mode, name, text, web.address, known_page) when :file - file_link(mode, name, text, web.address, known_page, description) + file_link(mode, name, text, web.address, known_page) when :pic - pic_link(mode, name, text, web.address, known_page, description) + pic_link(mode, name, text, web.address, known_page) else raise "Unknown link type: #{link_type}" end @@ -38,12 +43,11 @@ class UrlGenerator < AbstractUrlGenerator private - def file_link(mode, name, text, web_address, known_file, description) - title = CGI::escapeHTML(CGI::unescapeHTML( description || text)) + def file_link(mode, name, text, web_address, known_file) case mode when :export if known_file - %{#{text}} + %{#{text}} else %{#{text}} end @@ -51,7 +55,7 @@ class UrlGenerator < AbstractUrlGenerator if known_file href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file', :id => name - %{#{text}} + %{#{text}} else %{#{text}} end @@ -59,7 +63,7 @@ class UrlGenerator < AbstractUrlGenerator href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file', :id => name if known_file - %{#{text}} + %{#{text}} else %{#{text}?} end @@ -95,18 +99,17 @@ class UrlGenerator < AbstractUrlGenerator end end - def pic_link(mode, name, text, web_address, known_pic, description) - alt_text = CGI::escapeHTML(CGI::unescapeHTML( description || text)) + def pic_link(mode, name, text, web_address, known_pic) case mode when :export if known_pic - %{#{alt_text}} + %{#{text}} else %{#{text}} end when :publish if known_pic - %{#{alt_text}} + %{#{text}} else %{#{text}} end @@ -114,7 +117,7 @@ class UrlGenerator < AbstractUrlGenerator href = @controller.url_for :controller => 'file', :web => web_address, :action => 'file', :id => name if known_pic - %{#{alt_text}} + %{#{text}} else %{#{text}?} end diff --git a/test/test_helper.rb b/test/test_helper.rb index f69767a3..d8b08a70 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -102,8 +102,7 @@ class StubUrlGenerator < AbstractUrlGenerator super(:doesnt_need_controller) end - def file_link(mode, name, text, web_name, known_file, description) - title = CGI::escapeHTML(CGI::unescapeHTML( description || text)) + def file_link(mode, name, text, web_name, known_file) link = CGI.escape(name) case mode when :export @@ -143,18 +142,18 @@ class StubUrlGenerator < AbstractUrlGenerator end end - def pic_link(mode, name, text, web_name, known_pic, description) - alt_text = CGI::escapeHTML(CGI::unescapeHTML( description || text)) + def pic_link(mode, name, text, web_name, known_pic) link = CGI.escape(name) + text = CGI.escapeHTML(CGI.unescapeHTML(text || :description)) case mode.to_sym when :export - if known_pic then %{#{alt_text}} + if known_pic then %{#{text}} else %{#{text}} end when :publish - if known_pic then %{#{alt_text}} + if known_pic then %{#{text}} else %{#{text}} end else - if known_pic then %{#{alt_text}} + if known_pic then %{#{text}} else %{#{text}?} end end end