2012-02-29 27 views
3

引入nokogiri 1.5.0如何防止引入nokogiri來自編碼實體在HTML片段

我無法輸出解析後的片段與具有查詢參數的鏈路,特別是與在href&符號。 &符號被其html實體取代。

f = Nokogiri::HTML.fragment(%q{<a href="http://example.com?this=1&that=2">Testing</a>}) 
f.to_s # => "<a href=\"http://example.com?this=1&amp;that=2\">Testing</a>" 
f.to_html # => "<a href=\"http://example.com?this=1&amp;that=2\">Testing</a>" 

沒有幫助使用to_html(encoding: 'UTF-8')或US-ASCII。

這似乎很常見,解析一個有效的鏈接格式,並希望將其作爲有效的HTML呈現。

How to make Nokogiri transparently return un/encoded Html entities untouched?沒有幫助。

回答

4

Nokogiri的HTML解析器會自動糾正源文檔中的錯誤。 URL中的裸號是actually an error,所以Nokogiri正在糾正它。如果你看看f.errors,你可以看到它不認爲&that是一個有效的實體並且缺少一個分號,所以它修正了&符號到&amp;,使它成爲有效的HTML。

+0

感謝您提供參考。 – aceofspades 2012-03-01 21:51:32

相關問題