Is there a ruby gem that does diff between HTML documents?

老子叫甜甜 提交于 2020-01-13 05:33:07

问题


Doing a diff of two different html documents turns out to be an entirely different problem than simply doing a diff of plain text. For example, if I do a naive LCS diff between:

Google</p>

and

Google</a></p>

the diff result is NOT:

</a>

but

/a></

I've tried most gems out there that claim to be html diff but all of them seem to be just implementing text based LCS diff. Is there any gem that does a diff while taking html tags into account?


回答1:


After much searching for a gem to do this for me, I discovered that I can simply do a string compare between two parsed Nokogiri documents:

def should_match_html(html_text1, html_text2)
  dom1 = Nokogiri::HTML(html_text1)
  dom2 = Nokogiri::HTML(html_text2)
  dom1.to_s.should == dom2.to_s
end

Then you can simply add this in your spec:

should_match_html expected_html, actual_html

The best part is that the built-in rspec matcher will automatically provide you a line-by-line diff result of the mismatched lines.




回答2:


Try Samy diffy or rubygems html-diff



来源:https://stackoverflow.com/questions/9097074/is-there-a-ruby-gem-that-does-diff-between-html-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!