在测试将嵌入式 soundcloud-track url 更改为 iframe 的方法时。转义结果时出现错误。
有人可以解释为什么在 CASE 2 中转义 +token+ 吗?我可以按原样使用结果并附加到它。
下面详细介绍这两种情况。
#ruby --version
# => ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.2.1]
情况1
require 'cgi'
class String
def embedda
compiled = self
compiled = soundcloud_replace(compiled)
return compiled
end
private
def soundcloud_replace(compiled)
r = /(https?:\/\/(?:www.)?soundcloud.com\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*(?!\/sets(?:\/|$))(?:\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*){1,2}\/?)/i
compiled.gsub!(r) { |match| soundcloud_player(match) }
return compiled
end
def soundcloud_player(token)
# return token # => +matched soundcloud url+ in pry
# puts token # => +matched soundcloud url+ in pry
url_encoded_string = CGI::escape(token)
# puts url_encoded_string # => +escaped matched soundcloud url+
# "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{token}\"></iframe>"
# => Correct interpolated string with non-encoded url
"<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{url_encoded_string}\"></iframe>"
# => Correct interpolated string with encoded url
end
end
案例二
require 'cgi'
class String
def embedda
compiled = self
compiled = soundcloud_replace(compiled)
return compiled
end
private
def soundcloud_replace(compiled)
compiled.gsub!(/(https?:\/\/(?:www.)?soundcloud.com\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*(?!\/sets(?:\/|$))(?:\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*){1,2}\/?)/i, soundcloud_player("\\1"))
return compiled
end
def soundcloud_player(token)
# return token # => +matched soundcloud url+ in pry
# puts token # => /1 in pry
url_encoded_string = CGI::escape(token)
# puts url_encoded_string = CGI::escape(token) # => %5C1 in pry
# "<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{token}\"></iframe>"
# => Correct interpolated string with non-encoded url
"<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"https://w.soundcloud.com/player/?url=#{url_encoded_string}\"></iframe>"
# => Interpolated string with ...?url=%5C1
end
end