nokogiri-html-ext 1.2.0 → 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: 1a2f019c308dd4179f4846c1526d5c360bbc43883668ccf6c9b9164d953a7f36
4
- data.tar.gz: ed1e5bc79cc94d68f70608e6e68dfdeb40b557a7abc8156b7364ebc0035f5773
3
+ metadata.gz: 4639298a3f7cf742a7a82c93c615e93c09120d1029889f7994e954edade6fe8e
4
+ data.tar.gz: 1f3a16c7057aa11b6ec51ed28a6f7d22f64c41bac121ab1765a1a77c7275a65e
5
5
  SHA512:
6
- metadata.gz: 4eec44e7836e196f72e0f8f5ec49a29fe4d78ff14696bce15ccd9f3b4256a563169ac7103138a910b00e3858461c7235683daefd3bf7e1d0c52b3e155bf8e30b
7
- data.tar.gz: 75c20721391a9514ef486b6481a96aff535ba38c821e2bd764f8c4c955261e4653a0e5caacce0d6d4ad6ac4b6242a9b76316797e22601e6e4e46d997b119515d
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,13 +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
33
+
34
+ URI_PARSER = defined?(URI::RFC2396_PARSER) ? URI::RFC2396_PARSER : URI::Generic::DEFAULT_PARSER
32
35
 
33
36
  # Get the +<base>+ element's HREF attribute value.
34
37
  #
35
38
  # @return [String, nil]
36
39
  def base_href
37
- (base = at_xpath("//base[@href]")) && base["href"].strip
40
+ at_xpath("//base / @href")&.value&.strip
38
41
  end
39
42
 
40
43
  # Set the +<base>+ element's HREF attribute value.
@@ -64,11 +67,11 @@ module Nokogiri
64
67
  strs = [doc_url_str, base_href, url]
65
68
 
66
69
  strs.compact!
67
- strs.map! { |str| uri_parser.escape(str) }
70
+ strs.map! { |str| URI_PARSER.escape(str) }
68
71
 
69
72
  # Escape each component before joining (Ruby's +URI.parse+ only likes
70
73
  # ASCII) and subsequently unescaping.
71
- uri_parser.unescape(uri_parser.join(*strs).normalize.to_s)
74
+ URI_PARSER.unescape(URI_PARSER.join(*strs).normalize.to_s)
72
75
  rescue URI::InvalidComponentError, URI::InvalidURIError
73
76
  url
74
77
  end
@@ -77,16 +80,15 @@ module Nokogiri
77
80
  #
78
81
  # @return [self]
79
82
  def resolve_relative_urls!
80
- resolve_relative_urls_for(URL_ATTRIBUTES_MAP) { |attribute| resolve_relative_url(attribute.strip) }
81
-
82
- resolve_relative_urls_for(IMAGE_CANDIDATE_STRINGS_ATTRIBUTES_MAP) do |attribute|
83
- candidates = attribute.split(/\s*,\s*/)
84
-
85
- # rubocop:disable Style/PerlBackrefs
86
- candidates.map! { |candidate| candidate.sub(/^(.+?)(\s+.+)?$/) { "#{resolve_relative_url($1)}#{$2}" } }
87
- # rubocop:enable Style/PerlBackrefs
88
-
89
- 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
90
92
  end
91
93
 
92
94
  self
@@ -100,27 +102,17 @@ module Nokogiri
100
102
  #
101
103
  # @return [String]
102
104
  def doc_url_str
103
- @doc_url_str ||= uri_parser.unescape(uri_parser.unescape(document.url)).strip
105
+ @doc_url_str ||= URI_PARSER.unescape(URI_PARSER.unescape(document.url)).strip
104
106
  end
105
107
 
106
- # @param attribute [String]
107
- # @param names [Array<String>]
108
- #
109
- # @return [Array<String, Nokogiri::XML::NodeSet>]
110
- def node_sets_from(attribute, names)
111
- [attribute, xpath(*names.map { |name| "//#{name}[@#{attribute}]" })]
112
- end
113
-
114
- def resolve_relative_urls_for(attributes_map)
115
- attributes_map
116
- .map { |attribute, names| node_sets_from(attribute, names) }
117
- .each do |attribute, node_set|
118
- node_set.each { |node| node[attribute] = yield node[attribute] }
119
- end
120
- 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
121
114
 
122
- def uri_parser
123
- @uri_parser ||= URI::RFC2396_Parser.new
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.0"
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.0
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.0
45
- documentation_uri: https://rubydoc.info/gems/nokogiri-html-ext/1.2.0
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.0
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