8000 Added `CGI::Util.escapeURIComponent` and `unescapeURIComponent` by francois · Pull Request #8913 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Added CGI::Util.escapeURIComponent and unescapeURIComponent #8913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
29 changes: 29 additions & 0 deletions rbi/stdlib/cgi.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,35 @@ module CGI::Util
.returns(::T.untyped)
end
def unescape_html(_); end

# URL-encode a string following RFC 3986 Space characters (+“ ”+) are encoded with (+“%20”+)
#
# ```ruby
# url_encoded_string = CGI.escape("'Stop!' said Fred")
# # => "%27Stop%21%27%20said%20Fred"
# ```
sig do
params(
string: ::T.untyped
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these really untyped?

Copy link
Contributor Author
@francois francois May 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No they aren't, but if you look at the whole file, you'll see that everything else in this file is untyped. I wanted to make the smallest possible change that would be acceptable. I have another PR waiting in the wings with stronger type assertions, where inputs are String and return values are String as well.

I can type these two methods now, and the next PR will type the remaining methods.

Happy to do whatever will get this upstreamed!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with T.untyped for now, thank you for making incremental changes!

)
.returns(::String)
end
def escapeURIComponent(string); end

# URL-decode a string following RFC 3986 with encoding(optional).
#
# ```ruby
# string = CGI.unescape("%27Stop%21%27+said%20Fred")
# # => "'Stop!'+said Fred"
# ```
sig do
params(
string: ::T.untyped,
encoding: ::T.untyped
)
.returns(::String)
end
def unescapeURIComponent(string, encoding = T.unsafe(nil)); end
end

module CGI::Escape
Expand Down
6 changes: 6 additions & 0 deletions test/testdata/rbi/cgi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# typed: true
require 'cgi'

T.assert_type!(CGI.escapeURIComponent("'Stop!' said Fred"), String)
T.assert_type!(CGI.unescapeURIComponent("%27Stop%21%27+said%20Fred"), String)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test failure is because you're missing an error annotation for this line.

https://buildkite.com/sorbet/sorbet/builds/36003#0196fd92-37e9-466c-8874-ce6f2a166a59/143-446

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the tip on how to run Bazel for CGI only. I have a green build for testdata/../cgi on my end.

T.assert_type!(CGI.unescapeURIComponent("%27Stop%21%27+said%20Fred", "ISO-8859-1"), String)
0