nokogiri-html-ext 0.1.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b391309327d894afb0495dd2fcbe3e3555a0c41bd21937af25a134e0ad1d27ec
4
- data.tar.gz: 16e6cd74fd4485610a239edd904318973125de3225a1a5beca69a311fb79eda5
3
+ metadata.gz: bcf8d54ec9864f58ba4ada5d6c47e191dbc0063249afb256928bf5fc52f5f0e0
4
+ data.tar.gz: 9c72dacd89707ad6d320e3ed368860b9f58ee73fadb8427a54012fa1c18282a2
5
5
  SHA512:
6
- metadata.gz: dc29f44a0ed51d136b5c7e44ff646bfc6d024ac66308b7f539ce64e3ceaad3fe78faed345842946ed538ab4d4dd673a5d831a1cf2f1cc9689c429689c5815c0f
7
- data.tar.gz: 2e4ec5bba2a5678f854194ea5d1c1c3a054fd5ae11e62d61f6992f0a52afe87ba239abc6f5114a5e6494a3e1cab1fac0f42a9fe81a79e490f21b77bd63e24161
6
+ metadata.gz: e589cf7989b82d9b73d265494c537c098a59236c4314cc758fda219e7b908dd0e9f22cb8ead7834def9ce74d27f223468f5394ee071c8be9df98ea391440623c
7
+ data.tar.gz: 3ffd78636127f753fde326de3b20b762a6b678583274425d2d6ad35827c305ab10711a40b1ed927251a4e6040ffa6585b9056161979ffdec0052da26fdbb1963
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.2.2 / 2022-08-20
4
+
5
+ - Improve handling of escaped and invalid URLs (b0d6c75)
6
+
7
+ ## v0.2.1 / 2022-08-20
8
+
9
+ - Handle escaped URLs and invalid URLs (af78837)
10
+ - Use ruby/debug gem instead of pry-byebug (4476b9d)
11
+
12
+ ## v0.2.0 / 2022-07-02
13
+
14
+ - Make `resolve_relative_url` method public (d132dd3)
15
+
3
16
  ## v0.1.0 / 2022-07-01
4
17
 
5
18
  - Initial release! 🎉
data/README.md CHANGED
@@ -43,6 +43,8 @@ gem install nokogiri-html-ext
43
43
  nokogiri-html-ext provides two helper methods for getting and setting a document's `<base>` element's `href` attribute. The first, `base_href`, retrieves the element's `href` attribute value if it exists.
44
44
 
45
45
  ```ruby
46
+ require 'nokogiri/html-ext'
47
+
46
48
  doc = Nokogiri::HTML('<html><body>Hello, world!</body></html>')
47
49
 
48
50
  doc.base_href
@@ -62,6 +64,8 @@ doc.base_href
62
64
  The `base_href=` method allows you to manipulate the document's `<base>` element.
63
65
 
64
66
  ```ruby
67
+ require 'nokogiri/html-ext'
68
+
65
69
  doc = Nokogiri::HTML('<html><body>Hello, world!</body></html>')
66
70
 
67
71
  doc.base_href = '/foo'
@@ -92,6 +96,8 @@ URL resolution uses Ruby's built-in URL parsing and normalizing capabilities. Ab
92
96
  An abbreviated example:
93
97
 
94
98
  ```ruby
99
+ require 'nokogiri/html-ext'
100
+
95
101
  markup = <<-HTML
96
102
  <html>
97
103
  <body>
@@ -124,6 +130,17 @@ doc.at_css('img').to_s
124
130
  #=> "<img src=\"https://jgarber.example/foo.png\" srcset=\"https://jgarber.example/foo/bar.png 720w\">"
125
131
  ```
126
132
 
133
+ ### `resolve_relative_url`
134
+
135
+ You may also resolve an arbitrary `String` representing a relative URL against the document's URL (or `<base>` element's `href` attribute value):
136
+
137
+ ```ruby
138
+ doc = Nokogiri::HTML('<html><base href="/foo/bar"></html>', 'https://jgarber.example')
139
+
140
+ doc.resolve_relative_url('biz/baz')
141
+ #=> "https://jgarber.example/foo/biz/baz"
142
+ ```
143
+
127
144
  ## Contributing
128
145
 
129
146
  Interested in helping improve nokogiri-html-ext? Awesome! Your help is greatly appreciated. See [CONTRIBUTING.md](https://github.com/jgarber623/nokogiri-html-ext/blob/main/CONTRIBUTING.md) for details.
@@ -63,6 +63,23 @@ module Nokogiri
63
63
  end
64
64
  end
65
65
 
66
+ # Convert a relative URL to an absolute URL.
67
+ #
68
+ # @param url [String, #to_s]
69
+ #
70
+ # @return [String]
71
+ def resolve_relative_url(url)
72
+ url_str = url.to_s
73
+
74
+ uri_parser.unescape(
75
+ uri_parser.join(*[document.url.strip, base_href, url_str].compact.map { |u| uri_parser.escape(u) })
76
+ .normalize
77
+ .to_s
78
+ )
79
+ rescue URI::InvalidComponentError, URI::InvalidURIError
80
+ url
81
+ end
82
+
66
83
  # Convert the document's relative URLs to absolute URLs.
67
84
  #
68
85
  # @return [self]
@@ -84,14 +101,6 @@ module Nokogiri
84
101
 
85
102
  private
86
103
 
87
- def resolve_relative_url(url)
88
- uri_parser.unescape(
89
- uri_parser.join(*[document.url.strip, base_href, url].compact.map { |u| uri_parser.escape(u) })
90
- .normalize
91
- .to_s
92
- )
93
- end
94
-
95
104
  def resolve_relative_urls_for(attributes_map)
96
105
  attributes_map.each do |attribute, names|
97
106
  xpaths = names.map { |name| "//#{name}[@#{attribute}]" }
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Nokogiri
4
4
  module HTMLExt
5
- VERSION = '0.1.0'
5
+ VERSION = '0.2.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nokogiri-html-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Garber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-02 00:00:00.000000000 Z
11
+ date: 2022-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -44,7 +44,7 @@ licenses:
44
44
  - MIT
45
45
  metadata:
46
46
  bug_tracker_uri: https://github.com/jgarber623/nokogiri-html-ext/issues
47
- changelog_uri: https://github.com/jgarber623/nokogiri-html-ext/blob/v0.1.0/CHANGELOG.md
47
+ changelog_uri: https://github.com/jgarber623/nokogiri-html-ext/blob/v0.2.2/CHANGELOG.md
48
48
  rubygems_mfa_required: 'true'
49
49
  post_install_message:
50
50
  rdoc_options: []