How to avoid pretty-printed HTML in Nokogiri when using to_html?

I am using Nokogiri with Ruby on Rails v2.3.8.

Is there a way to avoid pretty printed printing in Nokogiri when using to_html?

I read that to_xmllets do this with help to_xml(:indent => 0), but that doesn't work with to_html.

I am currently using gsubto delete new characters. Does Nokogiri provide any option for this?

+5
source share
2 answers

Did I solve this with .to_html(save_with: 0)?

2.1.0 :001 > require 'nokogiri'
 => true
2.1.0 :002 >  doc = Nokogiri::HTML.fragment('<ul><li><span>hello</span> boom!</li></ul>')
 => #<Nokogiri::HTML::DocumentFragment:0x4e4cbd2 name="#document-fragment" children=[#<Nokogiri::XML::Element:0x4e4c97a name="ul" children=[#<Nokogiri::XML::Element:0x4e4c47a name="li" children=[#<Nokogiri::XML::Element:0x4e4c240 name="span" children=[#<Nokogiri::XML::Text:0x4e4c0a6 "hello">]>, #<Nokogiri::XML::Text:0x4e4c86c " boom!">]>]>]>
2.1.0 :003 > doc.to_html
 => "<ul><li>\n<span>hello</span> boom!</li></ul>"
2.1.0 :004 > doc.to_html(save_with: 0)
 => "<ul><li><span>hello</span> boom!</li></ul>"

verified by: nokogiri (1.6.5) + libxml2 2.7.6.dfsg-1ubuntu1 + ruby ​​2.1.0p0 (version 2013-12-25 version 44422) [i686-linux]

+4
source

Nokogiri::HTML() Nokogiri::HTML.fragment(). to_html, , DOCTYPE "" - .

+2

All Articles