Changeset 2750
- Timestamp:
- 10/26/05 13:26:04 (3 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_view/helpers/text_helper.rb (modified) (1 diff)
- trunk/actionpack/test/template/text_helper_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r2747 r2750 1 1 *SVN* 2 3 * Added TextHelper#strip_tags for removing HTML tags from a string (using HTMLTokenizer) #2229 [marcin@junkheap.net] 2 4 3 5 * Added a reader for flash.now, so it's possible to do stuff like flash.now[:alert] ||= 'New if not set' #2422 [Caio Chassot] trunk/actionpack/lib/action_view/helpers/text_helper.rb
r2649 r2750 203 203 end 204 204 205 # Strips all HTML tags from the input, including comments. This uses the html-scanner 206 # tokenizer and so it's HTML parsing ability is limited by that of html-scanner. 207 # 208 # Returns the tag free text. 209 def strip_tags(html) 210 if html.index("<") 211 text = "" 212 tokenizer = HTML::Tokenizer.new(html) 213 214 while token = tokenizer.next 215 node = HTML::Node.parse(nil, 0, 0, token, false) 216 # result is only the content of any Text nodes 217 text << node.to_s if node.class == HTML::Text 218 end 219 # strip any comments, and if they have a newline at the end (ie. line with 220 # only a comment) strip that too 221 text.gsub(/<!--(.*?)-->[\n]?/m, "") 222 else 223 html # already plain text 224 end 225 end 226 205 227 # Returns a Cycle object whose to_s value cycles through items of an 206 228 # array every time it is called. This can be used to alternate classes trunk/actionpack/test/template/text_helper_test.rb
r2350 r2750 269 269 end 270 270 271 def test_strip_tags 272 assert_equal("This is a test.", strip_tags("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>")) 273 assert_equal("This is a test.", strip_tags("This is a test.")) 274 assert_equal( 275 %{This is a test.\n\n\nIt no longer contains any HTML.\n}, strip_tags( 276 %{<title>This is <b>a <a href="" target="_blank">test</a></b>.</title>\n\n<!-- it has a comment -->\n\n<p>It no <b>longer <strong>contains <em>any <strike>HTML</strike></em>.</strong></b></p>\n})) 277 assert_equal("This has a here.", strip_tags("This has a <!-- comment --> here.")) 278 end 279 271 280 end