summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryuuji.yaginuma <yuuji.yaginuma@gmail.com>2025-04-14 07:55:39 +0900
committerHiroshi SHIBATA <hsbt@ruby-lang.org>2025-10-31 10:38:16 +0900
commit1dce0ae55a58aa11baf25e9ff92b64974e673361 (patch)
tree399410f3caa972c435ea01b826669c7cba11ccbe
parent08e822ba79d6d36f52bf07b06c5014024059b356 (diff)
[ruby/uri] Switch a parsing behavior completely when switching a parser
Currently, some methods' behavior(e.g. `URI.parse`) don't change when switching a parser. This is because some methods use `DEFAULT_PARSER`, but `parser=` doesn't change `DEFAULT_PARSER`. This PR introduces a constant to keep a parser's instance and change it when switching a parser. Also, change to use it in methods. https://github.com/ruby/uri/commit/aded210709
-rw-r--r--lib/uri/common.rb11
-rw-r--r--test/uri/test_common.rb3
2 files changed, 10 insertions, 4 deletions
diff --git a/lib/uri/common.rb b/lib/uri/common.rb
index 718dc3eb01..0b3bb4f099 100644
--- a/lib/uri/common.rb
+++ b/lib/uri/common.rb
@@ -30,6 +30,9 @@ module URI
remove_const(:Parser) if defined?(::URI::Parser)
const_set("Parser", parser.class)
+ remove_const(:PARSER) if defined?(::URI::PARSER)
+ const_set("PARSER", parser)
+
remove_const(:REGEXP) if defined?(::URI::REGEXP)
remove_const(:PATTERN) if defined?(::URI::PATTERN)
if Parser == RFC2396_Parser
@@ -227,7 +230,7 @@ module URI
# ["fragment", "top"]]
#
def self.split(uri)
- DEFAULT_PARSER.split(uri)
+ PARSER.split(uri)
end
# Returns a new \URI object constructed from the given string +uri+:
@@ -241,7 +244,7 @@ module URI
# if it may contain invalid URI characters.
#
def self.parse(uri)
- DEFAULT_PARSER.parse(uri)
+ PARSER.parse(uri)
end
# Merges the given URI strings +str+
@@ -297,7 +300,7 @@ module URI
#
def self.extract(str, schemes = nil, &block) # :nodoc:
warn "URI.extract is obsolete", uplevel: 1 if $VERBOSE
- DEFAULT_PARSER.extract(str, schemes, &block)
+ PARSER.extract(str, schemes, &block)
end
#
@@ -334,7 +337,7 @@ module URI
#
def self.regexp(schemes = nil)# :nodoc:
warn "URI.regexp is obsolete", uplevel: 1 if $VERBOSE
- DEFAULT_PARSER.make_regexp(schemes)
+ PARSER.make_regexp(schemes)
end
TBLENCWWWCOMP_ = {} # :nodoc:
diff --git a/test/uri/test_common.rb b/test/uri/test_common.rb
index 1291366936..569264005a 100644
--- a/test/uri/test_common.rb
+++ b/test/uri/test_common.rb
@@ -31,12 +31,14 @@ class URI::TestCommon < Test::Unit::TestCase
def test_parser_switch
assert_equal(URI::Parser, URI::RFC3986_Parser)
+ assert_equal(URI::PARSER, URI::RFC3986_PARSER)
refute defined?(URI::REGEXP)
refute defined?(URI::PATTERN)
URI.parser = URI::RFC2396_PARSER
assert_equal(URI::Parser, URI::RFC2396_Parser)
+ assert_equal(URI::PARSER, URI::RFC2396_PARSER)
assert defined?(URI::REGEXP)
assert defined?(URI::PATTERN)
assert defined?(URI::PATTERN::ESCAPED)
@@ -45,6 +47,7 @@ class URI::TestCommon < Test::Unit::TestCase
URI.parser = URI::RFC3986_PARSER
assert_equal(URI::Parser, URI::RFC3986_Parser)
+ assert_equal(URI::PARSER, URI::RFC3986_PARSER)
refute defined?(URI::REGEXP)
refute defined?(URI::PATTERN)
ensure