How get inner_html of ruby Nokogiri NodeSet unescaped?
I would like to get unescaped inner html from a Nokogiri NodeSe开发者_Go百科t. Does anyone know how to do this?
Anything not okey with?
nodeset.inner_html
The loofah gem helped me a lot here.
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
escapes HTML entities except in 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 > right > straight & 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>
An old version of libxml2 might cause Nokogiri to return some escaped characters. I had this problem recently.
精彩评论