【发布时间】:2020-12-23 11:19:53
【问题描述】:
我正在使用URI.encode 来生成 HTML 数据 URL:
visit "data:text/html,#{URI::encode(html)}"
升级到 Ruby 2.7.1 后,解释器开始警告:
warning: URI.escape is obsolete
推荐的替代品是CGI.escape 和URI.encode_www_form_component。但是,他们并没有做同样的事情:
2.7.1 :007 > URI.escape '<html>this and that</html>'
(irb):7: warning: URI.escape is obsolete
=> "%3Chtml%3Ethis%20and%20that%3C/html%3E"
2.7.1 :008 > CGI.escape '<html>this and that</html>'
=> "%3Chtml%3Ethis+and+that%3C%2Fhtml%3E"
2.7.1 :009 > URI.encode_www_form_component '<html>this and that</html>'
=> "%3Chtml%3Ethis+and+that%3C%2Fhtml%3E"
这些细微编码差异的结果 - html 页面中的空格被 + 替换。我的问题是 - 对于这个用例,URI.encode 的替代品是什么?
【问题讨论】:
-
看看
ERB::Util.url_encode——它将空间编码为%20,还将/编码为%2F(这很好)
标签: ruby