You need to remove new lines from object / embed tags only with Nokogiri

I need to remove newlines from any object / attachment tags. I am currently trying to do this using Nokogiri by following these steps:

s = "<div>
<object height='450' width='600'>
<param name='allowfullscreen' value='true'>
<param name='allowscriptaccess' value='always'>
<param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id=3317924&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1'>
<embed src='http://vimeo.com/moogaloop.swf?clip_id=3317924&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1' type='application/x-shockwave-flash' allowfullscreen='true' allowscriptaccess='always' height='450' width='600'>
</embed>
</object>
</div>"
doc = Nokogiri::HTML(s)
doc.css('object').each { |o| o.inner_html.gsub!(/\n/, ""); puts o.inner_html }

Note that the example is for object tags only.

Printing o.inner_html at the end of the block indicates that no replacement has occurred, although the gsub text is displayed correctly. In addition, once this part is resolved, I need to make sure that the actual node object in the doc object is saved with the updated values.

Any help is most appreciated. Thank.

+3
source share
1 answer

Got it!

require 'nokogiri'
s = <<ENDHTML
<div>
<object height='450' width='600'>
  <param name='allowfullscreen' value='true'><param name='allowscriptaccess' value='always'>
  <param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id=3317924&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1'>
<embed src='http://vimeo.com/moogaloop.swf?clip_id=3317924&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1' type='application/x-shockwave-flash' allowfullscreen='true' allowscriptaccess='always' height='450' width='600'>
</embed>
</object>
</div>
ENDHTML

doc = Nokogiri::HTML(s)
doc.css('object,embed').each{ |e| e.inner_html = e.inner_html.gsub(/\n/,'') }
puts doc.serialize( save_with: 0 )

#=> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
#=> <html><body><div>
#=> <object height="450" width="600"><param name="allowfullscreen" value="true"><param name="allowscriptaccess" value="always"><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=3317924&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1"><embed src="http://vimeo.com/moogaloop.swf?clip_id=3317924&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" height="450" width="600"></embed></object>
#=> </div></body></html>
  • ; inner_html.
  • inner_html.gsub! inner_html = inner_html.gsub.
  • , serialize :save_with => 0, Nokogiri .
+6

Source: https://habr.com/ru/post/1783784/


All Articles