开发者

Ruby: clean up HTML, use Hpricot or just regex?

I'm looking to do some rudimentary cleansing of HTML. Basically want to create a whitelist of tags that are allowed and reject anything else.

Is Hpricot worth it in this case? Does it have a feature that I've overlooked that will save me from rewriting the wheel? Or is it best to just write a whitelist of tags using regex and massage an HTML document through that?

Regex can get really tricky with HTML, and I know a lot of开发者_如何学编程 experts are strictly against it - I'm just looking for the path of least resistance.


The path of least resistance may seem to be a regex at first, but then as you feed more text through it, you realize that it breaks again and again and makes more work for you. That is why experienced programmers know to use XML/DOM parsers for such a common problem.

I recommend that you use Nokogiri and not Hpricot though because it is faster and better maintained.

https://github.com/rgrove/sanitize/

Sanitize uses Nokogiri to do exactly what you are doing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜