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 method `Axlsx#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 Sep 1, 2024
1 parent 3941855 commit 4bc15ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CHANGELOG
- **Unreleased**
- [PR #359](https://github.com/caxlsx/caxlsx/pull/359) Add `PivotTable#grand_totals` option to remove grand totals row/col
- [PR #362](https://github.com/caxlsx/caxlsx/pull/362) Use widest width even if provided as fixed value
- [PR #398](https://github.com/caxlsx/caxlsx/pull/398) Add `Axlsx#uri_parser` method for RFC2396 compatibility

- **February.26.24**: 4.1.0
- [PR #316](https://github.com/caxlsx/caxlsx/pull/316) Prevent camelization of hyperlink locations
Expand Down
16 changes: 16 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,19 @@ 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.
# This method can be removed when dropping compatibility for Ruby < 3.4
# See https://github.com/ruby/uri/pull/114 for details.
# @return [Object]
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 4bc15ef

Please sign in to comment.