Skip to content

Commit

Permalink
Added TextHelper#strip_tags for removing HTML tags from a string (usi…
Browse files Browse the repository at this point in the history
…ng HTMLTokenizer) (closes #2229) [marcin@junkheap.net]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2750 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Oct 26, 2005
1 parent 82f1e19 commit 4e9bc0f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions actionpack/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*SVN*

* Added TextHelper#strip_tags for removing HTML tags from a string (using HTMLTokenizer) #2229 [marcin@junkheap.net]

* Added a reader for flash.now, so it's possible to do stuff like flash.now[:alert] ||= 'New if not set' #2422 [Caio Chassot]


Expand Down
22 changes: 22 additions & 0 deletions actionpack/lib/action_view/helpers/text_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,28 @@ def sanitize(html)
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:
Expand Down
9 changes: 9 additions & 0 deletions actionpack/test/template/text_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,13 @@ def test_cycle_no_instance_variable_clashes
assert_equal(%w{Specialized Fuji Giant}, @cycles)
end

def test_strip_tags
assert_equal("This is a test.", strip_tags("<p>This <u>is<u> a <a href='test.html'><strong>test</strong></a>.</p>"))
assert_equal("This is a test.", strip_tags("This is a test."))
assert_equal(
%{This is a test.\n\n\nIt no longer contains any HTML.\n}, strip_tags(
%{<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}))
assert_equal("This has a here.", strip_tags("This has a <!-- comment --> here."))
end

end

0 comments on commit 4e9bc0f

Please sign in to comment.