nokogiri-html-ext 1.1.1 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0264a1818e0cad9bbcef39ba356c658de032d0bdaad3403d99e7725b59b7edf4
4
- data.tar.gz: 488934e92cc47e8275f4b13b461d2ecaf51f02c59efb7c9d3bb86103eee88fe7
3
+ metadata.gz: 516557e7b4a1a13c0e5b4e45afa3f5af1bd76bbe16fa7166336f5e0860ee5395
4
+ data.tar.gz: 99f1f8d2be3b02c5db36e24885f57688c47269715c25485b95559eea6942b5da
5
5
  SHA512:
6
- metadata.gz: 844f5305a5e7974c22fb654dfebe96dfc2f9135d24a4927301fc7434e69b69e2a8af21c7c40d17fcdf5f213c0884d885bea3843d1cf6869d3871b20b208cda6f
7
- data.tar.gz: 91d62b19ab9385e0b44e80bfc8315d472b87cb3e64b4514279e37558cec0101ee777795dea5561c9ad14b501aab4b757c8668ed504344342261c8260751c1d1a
6
+ metadata.gz: 6d5a5a8271ec9024f795abf5aeae30b815ce37af003c5017986799009c967e8211a5cb35ab5ee359146bc361a025c895dc39e0d536d4d524a5378d8160460f13
7
+ data.tar.gz: 1a0bbb92cd2dae7ee7cde736a73a98619d1c5f298b2c89906889d445ef6528d6e3ddca7d4bbd907b69df1dd99d6aeb67b7965c9c334c01de7ad97fe54411818a
data/README.md CHANGED
@@ -10,13 +10,13 @@
10
10
 
11
11
  - Resolves all relative URLs in a Nokogiri-parsed HTML document.
12
12
  - Adds helpers for getting and setting a document's `<base>` element's `href` attribute.
13
- - Supports Ruby 3.1 and newer
13
+ - Supports Ruby 2.7 and newer
14
14
 
15
15
  ## Getting Started
16
16
 
17
- Before installing and using nokogiri-html-ext, you'll want to have [Ruby](https://www.ruby-lang.org) 3.1 (or newer) installed. Using a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm) is recommended.
17
+ Before installing and using nokogiri-html-ext, you'll want to have [Ruby](https://www.ruby-lang.org) 2.7 (or newer) installed. Using a Ruby version managment tool like [rbenv](https://github.com/rbenv/rbenv), [chruby](https://github.com/postmodern/chruby), or [rvm](https://github.com/rvm/rvm) is recommended.
18
18
 
19
- nokogiri-html-ext is developed using Ruby 3.1 and is tested against additional Ruby versions using [Forgejo Actions](https://codeberg.org/jgarber/nokogiri-html-ext/actions).
19
+ nokogiri-html-ext is developed using Ruby 3.4 and is tested against additional Ruby versions using [Forgejo Actions](https://codeberg.org/jgarber/nokogiri-html-ext/actions).
20
20
 
21
21
  ## Installation
22
22
 
@@ -30,6 +30,10 @@ module Nokogiri
30
30
 
31
31
  private_constant :URL_ATTRIBUTES_MAP
32
32
 
33
+ URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::Generic::DEFAULT_PARSER
34
+
35
+ private_constant :URI_PARSER
36
+
33
37
  # Get the +<base>+ element's HREF attribute value.
34
38
  #
35
39
  # @return [String, nil]
@@ -44,7 +48,7 @@ module Nokogiri
44
48
  # one and append it to the document's +<head>+ (creating that element if
45
49
  # necessary).
46
50
  #
47
- # @param url [String, #to_s]
51
+ # @param url [String]
48
52
  #
49
53
  # @return [String]
50
54
  def base_href=(url)
@@ -52,69 +56,71 @@ module Nokogiri
52
56
 
53
57
  set_metadata_element(base)
54
58
 
55
- base["href"] = url.to_s
59
+ base["href"] = url
56
60
  end
57
61
 
58
- # Convert a relative URL to an absolute URL.
62
+ # Convert a relative URL to an absolute URL based on the current document.
59
63
  #
60
- # @param url [String, #to_s]
64
+ # @param url [String]
61
65
  #
62
66
  # @return [String]
63
67
  def resolve_relative_url(url)
64
- url_str = url.to_s
68
+ strs = [doc_url_str, base_href, url]
69
+
70
+ strs.compact!
71
+ strs.map! { |str| URI_PARSER.escape(str) }
65
72
 
66
73
  # Escape each component before joining (Ruby's +URI.parse+ only likes
67
74
  # ASCII) and subsequently unescaping.
68
- uri_parser.unescape(
69
- uri_parser
70
- .join(*[doc_url_str, base_href, url_str].filter_map { |u| uri_parser.escape(u) unless u.nil? })
71
- .normalize
72
- .to_s
73
- )
75
+ URI_PARSER.unescape(URI_PARSER.join(*strs).normalize.to_s)
74
76
  rescue URI::InvalidComponentError, URI::InvalidURIError
75
- url_str
77
+ url
76
78
  end
77
79
 
78
80
  # Convert the document's relative URLs to absolute URLs.
79
81
  #
80
82
  # @return [self]
81
- #
82
- # rubocop:disable Style/PerlBackrefs
83
83
  def resolve_relative_urls!
84
- resolve_relative_urls_for(URL_ATTRIBUTES_MAP) { |value| resolve_relative_url(value.strip) }
84
+ resolve_relative_urls_for(URL_ATTRIBUTES_MAP) { |attribute| resolve_relative_url(attribute.strip) }
85
85
 
86
- resolve_relative_urls_for(IMAGE_CANDIDATE_STRINGS_ATTRIBUTES_MAP) do |value|
87
- value
88
- .split(",")
89
- .map { |candidate| candidate.strip.sub(/^(.+?)(\s+.+)?$/) { "#{resolve_relative_url($1)}#{$2}" } }
90
- .join(", ")
86
+ resolve_relative_urls_for(IMAGE_CANDIDATE_STRINGS_ATTRIBUTES_MAP) do |attribute|
87
+ candidates = attribute.split(/\s*,\s*/)
88
+
89
+ # rubocop:disable Style/PerlBackrefs
90
+ candidates.map! { |candidate| candidate.sub(/^(.+?)(\s+.+)?$/) { "#{resolve_relative_url($1)}#{$2}" } }
91
+ # rubocop:enable Style/PerlBackrefs
92
+
93
+ candidates.join(", ")
91
94
  end
92
95
 
93
96
  self
94
97
  end
95
- # rubocop:enable Style/PerlBackrefs
96
98
 
97
99
  private
98
100
 
99
101
  # +Nokogiri::HTML4::Document#url+ may be double-escaped if the parser
100
102
  # detects non-ASCII characters. For example, +https://[skull emoji].example+
101
103
  # is returned as +"https%3A//%25E2%2598%25A0%25EF%25B8%258F.example+.
104
+ #
105
+ # @return [String]
102
106
  def doc_url_str
103
- @doc_url_str ||= uri_parser.unescape(uri_parser.unescape(document.url)).strip
107
+ @doc_url_str ||= URI_PARSER.unescape(URI_PARSER.unescape(document.url)).strip
104
108
  end
105
109
 
106
- def resolve_relative_urls_for(attributes_map)
107
- attributes_map.each do |attribute, names|
108
- xpaths = names.map { |name| "//#{name}[@#{attribute}]" }
109
-
110
- xpath(*xpaths).each do |node|
111
- node[attribute] = yield node[attribute]
112
- end
113
- end
110
+ # @param attribute [String]
111
+ # @param names [Array<String>]
112
+ #
113
+ # @return [Array<String, Nokogiri::XML::NodeSet>]
114
+ def node_sets_from(attribute, names)
115
+ [attribute, xpath(*names.map { |name| "//#{name}[@#{attribute}]" })]
114
116
  end
115
117
 
116
- def uri_parser
117
- @uri_parser ||= URI::RFC2396_PARSER
118
+ def resolve_relative_urls_for(attributes_map)
119
+ attributes_map
120
+ .map { |attribute, names| node_sets_from(attribute, names) }
121
+ .each do |attribute, node_set|
122
+ node_set.each { |node| node[attribute] = yield node[attribute] }
123
+ end
118
124
  end
119
125
  end
120
126
  end
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Gem::Specification.new do |spec|
4
- spec.required_ruby_version = ">= 3.1"
4
+ spec.required_ruby_version = ">= 2.7"
5
5
 
6
6
  spec.name = "nokogiri-html-ext"
7
- spec.version = "1.1.1"
7
+ spec.version = "1.2.1"
8
8
  spec.authors = ["Jason Garber"]
9
9
  spec.email = ["jason@sixtwothree.org"]
10
10
 
@@ -28,5 +28,5 @@ Gem::Specification.new do |spec|
28
28
  "source_code_uri" => "#{spec.homepage}/src/tag/v#{spec.version}",
29
29
  }
30
30
 
31
- spec.add_dependency "nokogiri", "~> 1.18"
31
+ spec.add_dependency "nokogiri", "~> 1.14"
32
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri-html-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Garber
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - "~>"
17
17
  - !ruby/object:Gem::Version
18
- version: '1.18'
18
+ version: '1.14'
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
- version: '1.18'
25
+ version: '1.14'
26
26
  description: Extend Nokogiri with several useful HTML-centric features.
27
27
  email:
28
28
  - jason@sixtwothree.org
@@ -41,11 +41,11 @@ licenses:
41
41
  - MIT
42
42
  metadata:
43
43
  bug_tracker_uri: https://codeberg.org/jgarber/nokogiri-html-ext/issues
44
- changelog_uri: https://codeberg.org/jgarber/nokogiri-html-ext/releases/tag/v1.1.1
45
- documentation_uri: https://rubydoc.info/gems/nokogiri-html-ext/1.1.1
44
+ changelog_uri: https://codeberg.org/jgarber/nokogiri-html-ext/releases/tag/v1.2.1
45
+ documentation_uri: https://rubydoc.info/gems/nokogiri-html-ext/1.2.1
46
46
  homepage_uri: https://codeberg.org/jgarber/nokogiri-html-ext
47
47
  rubygems_mfa_required: 'true'
48
- source_code_uri: https://codeberg.org/jgarber/nokogiri-html-ext/src/tag/v1.1.1
48
+ source_code_uri: https://codeberg.org/jgarber/nokogiri-html-ext/src/tag/v1.2.1
49
49
  rdoc_options: []
50
50
  require_paths:
51
51
  - lib
@@ -53,7 +53,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - ">="
55
55
  - !ruby/object:Gem::Version
56
- version: '3.1'
56
+ version: '2.7'
57
57
  required_rubygems_version: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="