diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-10-26 13:26:04 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-10-26 13:26:04 +0000 |
commit | 4e9bc0f02ddd7d90740e289e565d7f4ebd6e2c1d (patch) | |
tree | b113d96d79d9dc8cf962833e976b9543073a8acc /actionpack/lib/action_view/helpers | |
parent | 82f1e19e4c493920e692309d15f66677ac8063e5 (diff) | |
download | rails-4e9bc0f02ddd7d90740e289e565d7f4ebd6e2c1d.tar.gz rails-4e9bc0f02ddd7d90740e289e565d7f4ebd6e2c1d.tar.bz2 rails-4e9bc0f02ddd7d90740e289e565d7f4ebd6e2c1d.zip |
Added TextHelper#strip_tags for removing HTML tags from a string (using HTMLTokenizer) (closes #2229) [marcin@junkheap.net]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2750 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r-- | actionpack/lib/action_view/helpers/text_helper.rb | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 4ead8a816f..e19ba8a66c 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -202,6 +202,28 @@ module ActionView html end + # Strips all HTML tags from the input, including comments. This uses the html-scanner + # tokenizer and so it's HTML parsing ability is limited by that of html-scanner. + # + # Returns the tag free text. + def strip_tags(html) + if html.index("<") + text = "" + tokenizer = HTML::Tokenizer.new(html) + + while token = tokenizer.next + node = HTML::Node.parse(nil, 0, 0, token, false) + # result is only the content of any Text nodes + text << node.to_s if node.class == HTML::Text + end + # strip any comments, and if they have a newline at the end (ie. line with + # only a comment) strip that too + text.gsub(/<!--(.*?)-->[\n]?/m, "") + else + html # already plain text + end + end + # Returns a Cycle object whose to_s value cycles through items of an # array every time it is called. This can be used to alternate classes # for table rows: |