Permalink
Browse files

Traversing nodes when stripping tags

  • Loading branch information...
1 parent 9a6ee97 commit 0c3105ba819c88404910a3f5254488cd3c86a316 @brynary committed Dec 5, 2008
Showing with 11 additions and 1 deletion.
  1. +11 −1 lib/dryopteris/sanitize.rb
View
@@ -13,7 +13,11 @@ def strip_tags(string_or_io, encoding=nil)
return "" if string_or_io.strip.size == 0
doc = Nokogiri::HTML.parse(string_or_io, nil, encoding)
- doc.text
+ doc.xpath("html/body/*").each do |node|
+ traverse_conditionally_top_down(node, :remove_tags_from_node)
+ end
+ snippet = doc.xpath("html/body").first
+ snippet.nil? ? "" : snippet.inner_html
end
def sanitize(string_or_io, encoding=nil)
@@ -34,6 +38,12 @@ def traverse_conditionally_top_down(node, method_name)
node.children.each {|j| traverse_conditionally_top_down(j, method_name)}
end
+ def remove_tags_from_node(node)
+ replacement_killer = Nokogiri::XML::Text.new(node.text, node.document)
+ node.add_next_sibling(replacement_killer)
+ node.remove
+ return true
+ end
def sanitize_node(node)
case node.type

0 comments on commit 0c3105b

Please sign in to comment.