##// END OF EJS Templates
Fixed: Deselecting textile text formatting causes interning empty string errors (#4867)....
Jean-Philippe Lang -
r3332:0fcc436f226f
parent child
Show More
@@ -1,131 +1,131
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module WikiFormatting
20 20 @@formatters = {}
21 21
22 22 class << self
23 23 def map
24 24 yield self
25 25 end
26 26
27 27 def register(name, formatter, helper)
28 raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_sym]
29 @@formatters[name.to_sym] = {:formatter => formatter, :helper => helper}
28 raise ArgumentError, "format name '#{name}' is already taken" if @@formatters[name.to_s]
29 @@formatters[name.to_s] = {:formatter => formatter, :helper => helper}
30 30 end
31 31
32 32 def formatter_for(name)
33 entry = @@formatters[name.to_sym]
33 entry = @@formatters[name.to_s]
34 34 (entry && entry[:formatter]) || Redmine::WikiFormatting::NullFormatter::Formatter
35 35 end
36 36
37 37 def helper_for(name)
38 entry = @@formatters[name.to_sym]
38 entry = @@formatters[name.to_s]
39 39 (entry && entry[:helper]) || Redmine::WikiFormatting::NullFormatter::Helper
40 40 end
41 41
42 42 def format_names
43 43 @@formatters.keys.map
44 44 end
45 45
46 46 def to_html(format, text, options = {}, &block)
47 47 text = if Setting.cache_formatted_text? && text.size > 2.kilobyte && cache_store && cache_key = cache_key_for(format, options[:object], options[:attribute])
48 48 # Text retrieved from the cache store may be frozen
49 49 # We need to dup it so we can do in-place substitutions with gsub!
50 50 cache_store.fetch cache_key do
51 51 formatter_for(format).new(text).to_html
52 52 end.dup
53 53 else
54 54 formatter_for(format).new(text).to_html
55 55 end
56 56 if block_given?
57 57 execute_macros(text, block)
58 58 end
59 59 text
60 60 end
61 61
62 62 # Returns a cache key for the given text +format+, +object+ and +attribute+ or nil if no caching should be done
63 63 def cache_key_for(format, object, attribute)
64 64 if object && attribute && !object.new_record? && object.respond_to?(:updated_on) && !format.blank?
65 65 "formatted_text/#{format}/#{object.class.model_name.cache_key}/#{object.id}-#{attribute}-#{object.updated_on.to_s(:number)}"
66 66 end
67 67 end
68 68
69 69 # Returns the cache store used to cache HTML output
70 70 def cache_store
71 71 ActionController::Base.cache_store
72 72 end
73 73
74 74 MACROS_RE = /
75 75 (!)? # escaping
76 76 (
77 77 \{\{ # opening tag
78 78 ([\w]+) # macro name
79 79 (\(([^\}]*)\))? # optional arguments
80 80 \}\} # closing tag
81 81 )
82 82 /x unless const_defined?(:MACROS_RE)
83 83
84 84 # Macros substitution
85 85 def execute_macros(text, macros_runner)
86 86 text.gsub!(MACROS_RE) do
87 87 esc, all, macro = $1, $2, $3.downcase
88 88 args = ($5 || '').split(',').each(&:strip)
89 89 if esc.nil?
90 90 begin
91 91 macros_runner.call(macro, args)
92 92 rescue => e
93 93 "<div class=\"flash error\">Error executing the <strong>#{macro}</strong> macro (#{e})</div>"
94 94 end || all
95 95 else
96 96 all
97 97 end
98 98 end
99 99 end
100 100 end
101 101
102 102 # Default formatter module
103 103 module NullFormatter
104 104 class Formatter
105 105 include ActionView::Helpers::TagHelper
106 106 include ActionView::Helpers::TextHelper
107 107 include ActionView::Helpers::UrlHelper
108 108
109 109 def initialize(text)
110 110 @text = text
111 111 end
112 112
113 113 def to_html(*args)
114 114 simple_format(auto_link(CGI::escapeHTML(@text)))
115 115 end
116 116 end
117 117
118 118 module Helper
119 119 def wikitoolbar_for(field_id)
120 120 end
121 121
122 122 def heads_for_wiki_formatter
123 123 end
124 124
125 125 def initial_page_content(page)
126 126 page.pretty_title.to_s
127 127 end
128 128 end
129 129 end
130 130 end
131 131 end
@@ -1,35 +1,45
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2009 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.dirname(__FILE__) + '/../../../test_helper'
19 19
20 20 class Redmine::WikiFormattingTest < ActiveSupport::TestCase
21 21
22 def test_textile_formatter
23 assert_equal Redmine::WikiFormatting::Textile::Formatter, Redmine::WikiFormatting.formatter_for('textile')
24 assert_equal Redmine::WikiFormatting::Textile::Helper, Redmine::WikiFormatting.helper_for('textile')
25 end
26
27 def test_null_formatter
28 assert_equal Redmine::WikiFormatting::NullFormatter::Formatter, Redmine::WikiFormatting.formatter_for('')
29 assert_equal Redmine::WikiFormatting::NullFormatter::Helper, Redmine::WikiFormatting.helper_for('')
30 end
31
22 32 def test_should_link_urls_and_email_addresses
23 33 raw = <<-DIFF
24 34 This is a sample *text* with a link: http://www.redmine.org
25 35 and an email address foo@example.net
26 36 DIFF
27 37
28 38 expected = <<-EXPECTED
29 39 <p>This is a sample *text* with a link: <a href="http://www.redmine.org">http://www.redmine.org</a><br />
30 40 and an email address <a href="mailto:foo@example.net">foo@example.net</a></p>
31 41 EXPECTED
32 42
33 43 assert_equal expected.gsub(%r{[\r\n\t]}, ''), Redmine::WikiFormatting::NullFormatter::Formatter.new(raw).to_html.gsub(%r{[\r\n\t]}, '')
34 44 end
35 45 end
General Comments 0
You need to be logged in to leave comments. Login now