How to get inner_html of ruby ​​Nokogiri NodeSet without binding?

I would like to get unescaped internal html from Nokogiri NodeSet. Does anyone know how to do this?

+7
ruby nokogiri
source share
4 answers

Nothing okey with?

nodeset.inner_html 
+4
source share

loofah gem helped me a lot.

+2
source share

Wrap your nodes in CDATA:

 def wrap_in_cdata(node) # Using Nokogiri::XML::Node#content instead of #inner_html (which # escapes HTML entities) so nested nodes will not work node.inner_html = node.document.create_cdata(node.content) node end 

Nokogiri::XML::Node#inner_html deletes HTML objects except for CDATA sections.

 fragment = Nokogiri::HTML.fragment "<div>Here is an unescaped string: <span>Turn left > right > straight & reach your destination.</span></div>" puts fragment.inner_html # <div>Here is an unescaped string: <span>Turn left &gt; right &gt; straight &amp; reach your destination.</span></div> fragment.xpath(".//span").each {|node| node.inner_html = node.document.create_cdata(node.content) } fragment.inner_html # <div>Here is an unescaped string: <span>Turn left > right > straight & reach your destination.</span>\n</div> 
+1
source share

An older version of libxml2 may cause Nokogiri to return some escaped characters. I recently had this problem.

0
source share

All Articles