开发者

How get inner_html of ruby Nokogiri NodeSet unescaped?

开发者 https://www.devze.com 2022-12-11 17:44 出处:网络
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?

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 &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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消