Skip to content

Commit

Permalink
Add uri_parser method for RFC2396 compatibility
Browse files Browse the repository at this point in the history
This commit introduces a new class method `uri_parser` to handle
URI parsing in a way that's compatible with both newer and older
versions of Ruby's URI library. It checks for the presence of
`URI::RFC2396_PARSER` and falls back to `URI::DEFAULT_PARSER` if
not available, ensuring consistent behavior across different Ruby
versions.

Close caxlsx#397

Ref: ruby/uri#114
  • Loading branch information
tagliala committed Aug 29, 2024
1 parent 96a78c9 commit 314c523
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/axlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require 'cgi'
require 'set'
require 'time'
require 'uri'

if Gem.loaded_specs.key?("axlsx_styler")
raise StandardError, "Please remove `axlsx_styler` from your Gemfile, the associated functionality is now built-in to `caxlsx` directly."
Expand Down Expand Up @@ -229,4 +230,17 @@ def self.escape_formulas=(value)
Axlsx.validate_boolean(value)
@escape_formulas = value
end

# Returns a URI parser instance, preferring RFC2396_PARSER if available,
# otherwise falling back to DEFAULT_PARSER. This method ensures consistent
# URI parsing across different Ruby versions.
# Ref: ruby/uri#114
def self.uri_parser
@uri_parser ||=
if defined?(URI::RFC2396_PARSER)
URI::RFC2396_PARSER
else
URI::DEFAULT_PARSER
end
end
end
2 changes: 1 addition & 1 deletion lib/axlsx/drawing/pic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def hyperlink=(v, options = {})
def image_src=(v)
Axlsx.validate_string(v)
if remote?
RegexValidator.validate('Pic.image_src', /\A#{URI::DEFAULT_PARSER.make_regexp}\z/, v)
RegexValidator.validate('Pic.image_src', /\A#{Axlsx.uri_parser.make_regexp}\z/, v)
RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type_from_uri(v)
else
RestrictionValidator.validate 'Pic.image_src', ALLOWED_MIME_TYPES, MimeTypeUtils.get_mime_type(v)
Expand Down

0 comments on commit 314c523

Please sign in to comment.