nokogiri-html-ext 1.2.1 → 1.3.0

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: 516557e7b4a1a13c0e5b4e45afa3f5af1bd76bbe16fa7166336f5e0860ee5395
4
- data.tar.gz: 99f1f8d2be3b02c5db36e24885f57688c47269715c25485b95559eea6942b5da
3
+ metadata.gz: 4639298a3f7cf742a7a82c93c615e93c09120d1029889f7994e954edade6fe8e
4
+ data.tar.gz: 1f3a16c7057aa11b6ec51ed28a6f7d22f64c41bac121ab1765a1a77c7275a65e
5
5
  SHA512:
6
- metadata.gz: 6d5a5a8271ec9024f795abf5aeae30b815ce37af003c5017986799009c967e8211a5cb35ab5ee359146bc361a025c895dc39e0d536d4d524a5378d8160460f13
7
- data.tar.gz: 1a0bbb92cd2dae7ee7cde736a73a98619d1c5f298b2c89906889d445ef6528d6e3ddca7d4bbd907b69df1dd99d6aeb67b7965c9c334c01de7ad97fe54411818a
6
+ metadata.gz: afdba65269d468d672cad6559772b0afeac2e7d1a9201c0052e460ad9acb897fb50c7e6fcd8a85551a3343ab4306dc05d59c2403750d3e91fed6dca23bc3bb2a
7
+ data.tar.gz: 3e85242200eb798f68903a247a992980fb3ce6142077b83b6008f0cc6297684dcfd47a4773464d45c5800022a90cf355354c6d65194c32ebb9667d683c5ebe28
@@ -7,13 +7,11 @@ module Nokogiri
7
7
  #
8
8
  # @see https://html.spec.whatwg.org/#srcset-attributes
9
9
  # @see https://html.spec.whatwg.org/#attributes-3
10
- IMAGE_CANDIDATE_STRINGS_ATTRIBUTES_MAP = {
10
+ SRCSET_ATTRIBUTES_MAP = {
11
11
  "imagesrcset" => ["link"],
12
12
  "srcset" => ["img", "source"],
13
13
  }.freeze
14
14
 
15
- private_constant :IMAGE_CANDIDATE_STRINGS_ATTRIBUTES_MAP
16
-
17
15
  # A map of HTML URL attributes and their associated element names.
18
16
  #
19
17
  # @see https://html.spec.whatwg.org/#attributes-3
@@ -28,17 +26,18 @@ module Nokogiri
28
26
  "src" => ["audio", "embed", "iframe", "img", "input", "script", "source", "track", "video"],
29
27
  }.freeze
30
28
 
31
- private_constant :URL_ATTRIBUTES_MAP
29
+ ATTRIBUTES_XPATHS =
30
+ URL_ATTRIBUTES_MAP.merge(SRCSET_ATTRIBUTES_MAP).flat_map do |attribute, names|
31
+ names.map { |name| "//#{name} / @#{attribute}" }
32
+ end
32
33
 
33
34
  URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::Generic::DEFAULT_PARSER
34
35
 
35
- private_constant :URI_PARSER
36
-
37
36
  # Get the +<base>+ element's HREF attribute value.
38
37
  #
39
38
  # @return [String, nil]
40
39
  def base_href
41
- (base = at_xpath("//base[@href]")) && base["href"].strip
40
+ at_xpath("//base / @href")&.value&.strip
42
41
  end
43
42
 
44
43
  # Set the +<base>+ element's HREF attribute value.
@@ -81,16 +80,15 @@ module Nokogiri
81
80
  #
82
81
  # @return [self]
83
82
  def resolve_relative_urls!
84
- resolve_relative_urls_for(URL_ATTRIBUTES_MAP) { |attribute| resolve_relative_url(attribute.strip) }
85
-
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(", ")
83
+ xpath(*ATTRIBUTES_XPATHS).each do |attr_node|
84
+ stripped_value = attr_node.value.strip
85
+
86
+ attr_node.value =
87
+ if SRCSET_ATTRIBUTES_MAP.key?(attr_node.name)
88
+ resolve_srcset_attributes(stripped_value.split(/\s*,\s*/))
89
+ else
90
+ resolve_relative_url(stripped_value)
91
+ end
94
92
  end
95
93
 
96
94
  self
@@ -107,20 +105,14 @@ module Nokogiri
107
105
  @doc_url_str ||= URI_PARSER.unescape(URI_PARSER.unescape(document.url)).strip
108
106
  end
109
107
 
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}]" })]
116
- end
108
+ def resolve_srcset_attributes(srcset_attributes)
109
+ srcset_attributes.map! do |candidate_string|
110
+ # rubocop:disable Style/PerlBackrefs
111
+ candidate_string.sub(/^(.+?)(\s+.+)?$/) { "#{resolve_relative_url($1)}#{$2}" }
112
+ # rubocop:enable Style/PerlBackrefs
113
+ end
117
114
 
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
115
+ srcset_attributes.join(", ")
124
116
  end
125
117
  end
126
118
  end
@@ -4,7 +4,7 @@ Gem::Specification.new do |spec|
4
4
  spec.required_ruby_version = ">= 2.7"
5
5
 
6
6
  spec.name = "nokogiri-html-ext"
7
- spec.version = "1.2.1"
7
+ spec.version = "1.3.0"
8
8
  spec.authors = ["Jason Garber"]
9
9
  spec.email = ["jason@sixtwothree.org"]
10
10
 
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.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Garber
@@ -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.2.1
45
- documentation_uri: https://rubydoc.info/gems/nokogiri-html-ext/1.2.1
44
+ changelog_uri: https://codeberg.org/jgarber/nokogiri-html-ext/releases/tag/v1.3.0
45
+ documentation_uri: https://rubydoc.info/gems/nokogiri-html-ext/1.3.0
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.2.1
48
+ source_code_uri: https://codeberg.org/jgarber/nokogiri-html-ext/src/tag/v1.3.0
49
49
  rdoc_options: []
50
50
  require_paths:
51
51
  - lib