开发者

Nokogiri prevent converting entities

开发者 https://www.devze.com 2023-03-13 01:39 出处:网络
def wrap(content) require \"Nokogiri\" doc = Nokogiri::HTML.fragment(\"<div>\"+content+\"</div>\")
def wrap(content)
  require "Nokogiri"
  doc = Nokogiri::HTML.fragment("<div>"+content+"</div>")
  chunks = doc.at("div").traverse do |p|
    if p.is_a?(Nokogiri::XML::Text)
      input = p.content
      p.content = input.scan(/.{1,5}/).join("&shy;")
    end
  end
  doc.at("div").inner_html
end

wrap("aaaaaaaaaa")

gives me

"aaaaa&amp;shy;aaaaa"

instead of

"aaaaa&shy;aaaaa"

H开发者_如何学Cow get the second result ?


Return

doc.at("div").text

instead of

doc.at("div").inner_html

This, however, strips all HTML from the result. If you need to retain other markup, you can probably get away with using CGI.unescapeHTML:

CGI.unescapeHTML(doc.at("div").inner_html)
0

精彩评论

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