umar
umar

Reputation: 4389

Preserve structure of an HTML page, removing all text nodes

I want to remove all text from html page that I load with nokogiri. For example, if a page has the following:

<body><script>var x = 10;</script><div>Hello</div><div><h1>Hi</h1></div></body>

I want to process it with Nokogiri and return html like the following after stripping the text like so:

<body><script>var x = 10;</script><div></div><div><h1></h1></div></body>

(That is, remove the actual h1 text, text between divs, text in p elements etc, but keep the tags. Also, don't remove text in the script tags.)

Upvotes: 1

Views: 377

Answers (1)

Phrogz
Phrogz

Reputation: 303261

require 'nokogiri'
html = "<body><script>var x = 10;</script><div>Hello</div><div><h1>Hi</h1></div></body>"
hdoc = Nokogiri::HTML(html)
hdoc.xpath( '//*[text()]' ).each do |el|
  el.content='' unless el.name=="script"
end

puts hdoc
#=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#=> <html><body>
#=> <script>var x = 10;</script><div></div>
#=> <div><h1></h1></div>
#=> </body></html>

Warning: As you did not specify how to handle a case like <div>foo<h1>bar</h1></div> the above may or may not do what you expect. Alternatively, the following may match your needs:

hdoc.xpath( '//text()' ).each do |el|
  el.remove unless el.parent.name=="script"
end

Update

Here's a more elegant solution using a single xpath to select all text nodes not part of a <script> element. I've added more text nodes to show how it handles them.

require 'nokogiri'
hdoc = Nokogiri::HTML <<ENDHTML
  <body>
  <script>var x = 10;</script>
  <div>Hello</div>
  <div>foo<h1>Hi</h1>bar</div>
  </body>
ENDHTML
hdoc.xpath( '//text()[not(parent::script)]' ).each{ |text| text.remove }
puts hdoc
#=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#=> <html><body>
#=> <script>var x = 10;</script><div></div>
#=> <div><h1></h1></div>
#=> </body></html>

For Ruby 1.9, the meat is more simply:

hdoc.xpath( '//text()[not(parent::script)]' ).each(&:remove)

Upvotes: 3

Related Questions