nokogiri 1.15.3 → 1.18.7

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.
Files changed (102) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +12 -17
  3. data/LICENSE-DEPENDENCIES.md +6 -6
  4. data/README.md +11 -5
  5. data/dependencies.yml +9 -8
  6. data/ext/nokogiri/extconf.rb +191 -154
  7. data/ext/nokogiri/gumbo.c +69 -53
  8. data/ext/nokogiri/html4_document.c +10 -4
  9. data/ext/nokogiri/html4_element_description.c +18 -18
  10. data/ext/nokogiri/html4_sax_parser.c +40 -0
  11. data/ext/nokogiri/html4_sax_parser_context.c +48 -58
  12. data/ext/nokogiri/html4_sax_push_parser.c +26 -25
  13. data/ext/nokogiri/libxml2_polyfill.c +114 -0
  14. data/ext/nokogiri/nokogiri.c +9 -2
  15. data/ext/nokogiri/nokogiri.h +25 -33
  16. data/ext/nokogiri/test_global_handlers.c +1 -1
  17. data/ext/nokogiri/xml_attr.c +1 -1
  18. data/ext/nokogiri/xml_cdata.c +3 -12
  19. data/ext/nokogiri/xml_comment.c +3 -8
  20. data/ext/nokogiri/xml_document.c +173 -158
  21. data/ext/nokogiri/xml_document_fragment.c +10 -25
  22. data/ext/nokogiri/xml_dtd.c +1 -1
  23. data/ext/nokogiri/xml_element_content.c +9 -9
  24. data/ext/nokogiri/xml_encoding_handler.c +4 -4
  25. data/ext/nokogiri/xml_namespace.c +6 -10
  26. data/ext/nokogiri/xml_node.c +142 -108
  27. data/ext/nokogiri/xml_node_set.c +46 -44
  28. data/ext/nokogiri/xml_reader.c +74 -100
  29. data/ext/nokogiri/xml_relax_ng.c +35 -56
  30. data/ext/nokogiri/xml_sax_parser.c +156 -88
  31. data/ext/nokogiri/xml_sax_parser_context.c +220 -128
  32. data/ext/nokogiri/xml_sax_push_parser.c +69 -50
  33. data/ext/nokogiri/xml_schema.c +51 -87
  34. data/ext/nokogiri/xml_syntax_error.c +19 -11
  35. data/ext/nokogiri/xml_text.c +3 -6
  36. data/ext/nokogiri/xml_xpath_context.c +104 -104
  37. data/ext/nokogiri/xslt_stylesheet.c +16 -11
  38. data/gumbo-parser/Makefile +18 -0
  39. data/gumbo-parser/src/ascii.c +2 -2
  40. data/gumbo-parser/src/error.c +76 -48
  41. data/gumbo-parser/src/error.h +5 -1
  42. data/gumbo-parser/src/nokogiri_gumbo.h +11 -2
  43. data/gumbo-parser/src/parser.c +66 -25
  44. data/gumbo-parser/src/tokenizer.c +7 -6
  45. data/lib/nokogiri/class_resolver.rb +1 -1
  46. data/lib/nokogiri/css/node.rb +6 -2
  47. data/lib/nokogiri/css/parser.rb +6 -4
  48. data/lib/nokogiri/css/parser.y +2 -2
  49. data/lib/nokogiri/css/parser_extras.rb +6 -66
  50. data/lib/nokogiri/css/selector_cache.rb +38 -0
  51. data/lib/nokogiri/css/tokenizer.rb +4 -4
  52. data/lib/nokogiri/css/tokenizer.rex +9 -8
  53. data/lib/nokogiri/css/xpath_visitor.rb +44 -27
  54. data/lib/nokogiri/css.rb +86 -20
  55. data/lib/nokogiri/decorators/slop.rb +3 -5
  56. data/lib/nokogiri/encoding_handler.rb +2 -2
  57. data/lib/nokogiri/html4/document.rb +45 -24
  58. data/lib/nokogiri/html4/document_fragment.rb +124 -12
  59. data/lib/nokogiri/html4/encoding_reader.rb +2 -2
  60. data/lib/nokogiri/html4/sax/parser.rb +23 -38
  61. data/lib/nokogiri/html4/sax/parser_context.rb +4 -9
  62. data/lib/nokogiri/html4.rb +9 -14
  63. data/lib/nokogiri/html5/builder.rb +40 -0
  64. data/lib/nokogiri/html5/document.rb +61 -30
  65. data/lib/nokogiri/html5/document_fragment.rb +130 -20
  66. data/lib/nokogiri/html5/node.rb +4 -4
  67. data/lib/nokogiri/html5.rb +114 -138
  68. data/lib/nokogiri/version/constant.rb +1 -1
  69. data/lib/nokogiri/version/info.rb +6 -5
  70. data/lib/nokogiri/xml/attr.rb +2 -2
  71. data/lib/nokogiri/xml/builder.rb +8 -1
  72. data/lib/nokogiri/xml/document.rb +74 -31
  73. data/lib/nokogiri/xml/document_fragment.rb +86 -15
  74. data/lib/nokogiri/xml/namespace.rb +1 -2
  75. data/lib/nokogiri/xml/node.rb +113 -35
  76. data/lib/nokogiri/xml/node_set.rb +12 -10
  77. data/lib/nokogiri/xml/parse_options.rb +1 -1
  78. data/lib/nokogiri/xml/pp/node.rb +6 -1
  79. data/lib/nokogiri/xml/reader.rb +51 -17
  80. data/lib/nokogiri/xml/relax_ng.rb +57 -20
  81. data/lib/nokogiri/xml/sax/document.rb +174 -83
  82. data/lib/nokogiri/xml/sax/parser.rb +115 -41
  83. data/lib/nokogiri/xml/sax/parser_context.rb +116 -8
  84. data/lib/nokogiri/xml/sax/push_parser.rb +3 -0
  85. data/lib/nokogiri/xml/sax.rb +48 -0
  86. data/lib/nokogiri/xml/schema.rb +112 -45
  87. data/lib/nokogiri/xml/searchable.rb +39 -43
  88. data/lib/nokogiri/xml/syntax_error.rb +23 -1
  89. data/lib/nokogiri/xml/xpath_context.rb +14 -3
  90. data/lib/nokogiri/xml.rb +14 -25
  91. data/lib/nokogiri/xslt/stylesheet.rb +29 -7
  92. data/lib/nokogiri/xslt.rb +4 -10
  93. data/lib/nokogiri.rb +1 -1
  94. data/lib/xsd/xmlparser/nokogiri.rb +3 -4
  95. data/patches/libxml2/0019-xpath-Use-separate-static-hash-table-for-standard-fu.patch +244 -0
  96. data/ports/archives/libxml2-2.13.7.tar.xz +0 -0
  97. data/ports/archives/libxslt-1.1.43.tar.xz +0 -0
  98. metadata +13 -14
  99. data/ext/nokogiri/libxml2_backwards_compat.c +0 -121
  100. data/patches/libxml2/0003-libxml2.la-is-in-top_builddir.patch +0 -25
  101. data/ports/archives/libxml2-2.11.4.tar.xz +0 -0
  102. data/ports/archives/libxslt-1.1.38.tar.xz +0 -0
@@ -1,66 +1,71 @@
1
1
  # coding: utf-8
2
2
  # frozen_string_literal: true
3
3
 
4
+ # This file includes code from the Nokogumbo project, whose license follows.
4
5
  #
5
- # Copyright 2013-2021 Sam Ruby, Stephen Checkoway
6
+ # Copyright 2013-2021 Sam Ruby, Stephen Checkoway
6
7
  #
7
- # Licensed under the Apache License, Version 2.0 (the "License");
8
- # you may not use this file except in compliance with the License.
9
- # You may obtain a copy of the License at
8
+ # Licensed under the Apache License, Version 2.0 (the "License");
9
+ # you may not use this file except in compliance with the License.
10
+ # You may obtain a copy of the License at
10
11
  #
11
- # http://www.apache.org/licenses/LICENSE-2.0
12
+ # http://www.apache.org/licenses/LICENSE-2.0
12
13
  #
13
- # Unless required by applicable law or agreed to in writing, software
14
- # distributed under the License is distributed on an "AS IS" BASIS,
15
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
- # See the License for the specific language governing permissions and
17
- # limitations under the License.
14
+ # Unless required by applicable law or agreed to in writing, software
15
+ # distributed under the License is distributed on an "AS IS" BASIS,
16
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ # See the License for the specific language governing permissions and
18
+ # limitations under the License.
18
19
  #
19
20
 
20
21
  require_relative "html5/document"
21
22
  require_relative "html5/document_fragment"
22
23
  require_relative "html5/node"
24
+ require_relative "html5/builder"
23
25
 
24
26
  module Nokogiri
25
- # Since v1.12.0
26
- #
27
- # ⚠ HTML5 functionality is not available when running JRuby.
28
- #
29
- # Parse an HTML5 document. Convenience method for {Nokogiri::HTML5::Document.parse}
30
- def self.HTML5(input, url = nil, encoding = nil, **options, &block)
31
- Nokogiri::HTML5::Document.parse(input, url, encoding, **options, &block)
27
+ # Convenience method for Nokogiri::HTML5::Document.parse
28
+ def self.HTML5(...)
29
+ Nokogiri::HTML5::Document.parse(...)
32
30
  end
33
31
 
34
32
  # == Usage
35
33
  #
36
- # ⚠ HTML5 functionality is not available when running JRuby.
37
- #
38
34
  # Parse an HTML5 document:
39
35
  #
40
- # doc = Nokogiri.HTML5(string)
36
+ # doc = Nokogiri.HTML5(input)
41
37
  #
42
38
  # Parse an HTML5 fragment:
43
39
  #
44
- # fragment = Nokogiri::HTML5.fragment(string)
40
+ # fragment = Nokogiri::HTML5.fragment(input)
41
+ #
42
+ # ⚠ HTML5 functionality is not available when running JRuby.
45
43
  #
46
44
  # == Parsing options
47
45
  #
48
- # The document and fragment parsing methods support options that are different from Nokogiri's.
46
+ # The document and fragment parsing methods support options that are different from
47
+ # Nokogiri::HTML4::Document or Nokogiri::XML::Document.
48
+ #
49
+ # - <tt>Nokogiri.HTML5(input, url:, encoding:, **parse_options)</tt>
50
+ # - <tt>Nokogiri::HTML5.parse(input, url:, encoding:, **parse_options)</tt>
51
+ # - <tt>Nokogiri::HTML5::Document.parse(input, url:, encoding:, **parse_options)</tt>
52
+ # - <tt>Nokogiri::HTML5.fragment(input, encoding:, **parse_options)</tt>
53
+ # - <tt>Nokogiri::HTML5::DocumentFragment.parse(input, encoding:, **parse_options)</tt>
49
54
  #
50
- # - <tt>Nokogiri.HTML5(html, url = nil, encoding = nil, options = {})</tt>
51
- # - <tt>Nokogiri::HTML5.parse(html, url = nil, encoding = nil, options = {})</tt>
52
- # - <tt>Nokogiri::HTML5::Document.parse(html, url = nil, encoding = nil, options = {})</tt>
53
- # - <tt>Nokogiri::HTML5.fragment(html, encoding = nil, options = {})</tt>
54
- # - <tt>Nokogiri::HTML5::DocumentFragment.parse(html, encoding = nil, options = {})</tt>
55
+ # The four currently supported parse options are
55
56
  #
56
- # The three currently supported options are +:max_errors+, +:max_tree_depth+ and
57
- # +:max_attributes+, described below.
57
+ # - +max_errors:+ (Integer, default 0) Maximum number of parse errors to report in HTML5::Document#errors.
58
+ # - +max_tree_depth:+ (Integer, default +Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH+) Maximum tree depth to parse.
59
+ # - +max_attributes:+ (Integer, default +Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES+) Maximum number of attributes to parse per element.
60
+ # - +parse_noscript_content_as_text:+ (Boolean, default false) When enabled, parse +noscript+ tag content as text, mimicking the behavior of web browsers.
58
61
  #
59
- # === Error reporting
62
+ # These options are explained in the following sections.
63
+ #
64
+ # === Error reporting: +max_errors:+
60
65
  #
61
66
  # Nokogiri contains an experimental HTML5 parse error reporting facility. By default, no parse
62
67
  # errors are reported but this can be configured by passing the +:max_errors+ option to
63
- # {HTML5.parse} or {HTML5.fragment}.
68
+ # HTML5.parse or HTML5.fragment.
64
69
  #
65
70
  # For example, this script:
66
71
  #
@@ -86,20 +91,21 @@ module Nokogiri
86
91
  #
87
92
  # Using <tt>max_errors: -1</tt> results in an unlimited number of errors being returned.
88
93
  #
89
- # The errors returned by {HTML5::Document#errors} are instances of {Nokogiri::XML::SyntaxError}.
94
+ # The errors returned by HTML5::Document#errors are instances of Nokogiri::XML::SyntaxError.
90
95
  #
91
- # The {https://html.spec.whatwg.org/multipage/parsing.html#parse-errors HTML standard} defines a
96
+ # The {HTML standard}[https://html.spec.whatwg.org/multipage/parsing.html#parse-errors] defines a
92
97
  # number of standard parse error codes. These error codes only cover the "tokenization" stage of
93
98
  # parsing HTML. The parse errors in the "tree construction" stage do not have standardized error
94
99
  # codes (yet).
95
100
  #
96
- # As a convenience to Nokogiri users, the defined error codes are available via
97
- # {Nokogiri::XML::SyntaxError#str1} method.
101
+ # As a convenience to Nokogiri users, the defined error codes are available
102
+ # via Nokogiri::XML::SyntaxError#str1 method.
98
103
  #
99
104
  # doc = Nokogiri::HTML5.parse('<span/>Hi there!</span foo=bar />', max_errors: 10)
100
105
  # doc.errors.each do |err|
101
106
  # puts("#{err.line}:#{err.column}: #{err.str1}")
102
107
  # end
108
+ # doc = Nokogiri::HTML5.parse('<span/>Hi there!</span foo=bar />',
103
109
  # # => 1:1: generic-parser
104
110
  # # 1:1: non-void-html-element-start-tag-with-trailing-solidus
105
111
  # # 1:17: end-tag-with-trailing-solidus
@@ -112,40 +118,75 @@ module Nokogiri
112
118
  # are not part of Nokogiri's public API. That is, these are subject to change without Nokogiri's
113
119
  # major version number changing. These may be stabilized in the future.
114
120
  #
115
- # === Maximum tree depth
121
+ # === Maximum tree depth: +max_tree_depth:+
116
122
  #
117
123
  # The maximum depth of the DOM tree parsed by the various parsing methods is configurable by the
118
124
  # +:max_tree_depth+ option. If the depth of the tree would exceed this limit, then an
119
- # {::ArgumentError} is thrown.
125
+ # +ArgumentError+ is thrown.
120
126
  #
121
- # This limit (which defaults to <tt>Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH = 400</tt>) can be
122
- # removed by giving the option <tt>max_tree_depth: -1</tt>.
127
+ # This limit (which defaults to +Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH+) can be removed by
128
+ # giving the option <tt>max_tree_depth: -1</tt>.
123
129
  #
124
130
  # html = '<!DOCTYPE html>' + '<div>' * 1000
125
131
  # doc = Nokogiri.HTML5(html)
126
132
  # # raises ArgumentError: Document tree depth limit exceeded
127
133
  # doc = Nokogiri.HTML5(html, max_tree_depth: -1)
128
134
  #
129
- # === Attribute limit per element
135
+ # === Attribute limit per element: +max_attributes:+
130
136
  #
131
137
  # The maximum number of attributes per DOM element is configurable by the +:max_attributes+
132
- # option. If a given element would exceed this limit, then an {::ArgumentError} is thrown.
138
+ # option. If a given element would exceed this limit, then an +ArgumentError+ is thrown.
133
139
  #
134
- # This limit (which defaults to <tt>Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES = 400</tt>) can be
135
- # removed by giving the option <tt>max_attributes: -1</tt>.
140
+ # This limit (which defaults to +Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES+) can be removed by
141
+ # giving the option <tt>max_attributes: -1</tt>.
136
142
  #
137
- # html = '<!DOCTYPE html><div ' + (1..1000).map { |x| "attr-#{x}" }.join(' ') + '>'
143
+ # html = '<!DOCTYPE html><div ' + (1..1000).map { |x| "attr-#{x}" }.join(' # ') + '>'
138
144
  # # "<!DOCTYPE html><div attr-1 attr-2 attr-3 ... attr-1000>"
139
145
  # doc = Nokogiri.HTML5(html)
140
146
  # # raises ArgumentError: Attributes per element limit exceeded
147
+ #
141
148
  # doc = Nokogiri.HTML5(html, max_attributes: -1)
149
+ # # parses successfully
150
+ #
151
+ # === Parse +noscript+ elements' content as text: +parse_noscript_content_as_text:+
152
+ #
153
+ # By default, the content of +noscript+ elements is parsed as HTML elements. Browsers that
154
+ # support scripting parse the content of +noscript+ elements as raw text.
155
+ #
156
+ # The +:parse_noscript_content_as_text+ option causes Nokogiri to parse the content of +noscript+
157
+ # elements as a single text node.
158
+ #
159
+ # html = "<!DOCTYPE html><noscript><meta charset='UTF-8'><link rel=stylesheet href=!></noscript>"
160
+ # doc = Nokogiri::HTML5.parse(html, parse_noscript_content_as_text: true)
161
+ # pp doc.at_xpath("/html/head/noscript")
162
+ # # => #(Element:0x878c {
163
+ # # name = "noscript",
164
+ # # children = [ #(Text "<meta charset='UTF-8'><link rel=stylesheet href=!>")]
165
+ # # })
166
+ #
167
+ # In contrast, <tt>parse_noscript_content_as_text: false</tt> (the default) causes the +noscript+
168
+ # element in the previous example to have two children, a +meta+ element and a +link+ element.
169
+ #
170
+ # doc = Nokogiri::HTML5.parse(html)
171
+ # puts doc.at_xpath("/html/head/noscript")
172
+ # # => #(Element:0x96b4 {
173
+ # # name = "noscript",
174
+ # # children = [
175
+ # # #(Element:0x97e0 { name = "meta", attribute_nodes = [ #(Attr:0x990c { name = "charset", value = "UTF-8" })] }),
176
+ # # #(Element:0x9b00 {
177
+ # # name = "link",
178
+ # # attribute_nodes = [
179
+ # # #(Attr:0x9c2c { name = "rel", value = "stylesheet" }),
180
+ # # #(Attr:0x9dd0 { name = "href", value = "!" })]
181
+ # # })]
182
+ # # })
142
183
  #
143
184
  # == HTML Serialization
144
185
  #
145
- # After parsing HTML, it may be serialized using any of the {Nokogiri::XML::Node} serialization
146
- # methods. In particular, {XML::Node#serialize}, {XML::Node#to_html}, and {XML::Node#to_s} will
186
+ # After parsing HTML, it may be serialized using any of the Nokogiri::XML::Node serialization
187
+ # methods. In particular, XML::Node#serialize, XML::Node#to_html, and XML::Node#to_s will
147
188
  # serialize a given node and its children. (This is the equivalent of JavaScript's
148
- # +Element.outerHTML+.) Similarly, {XML::Node#inner_html} will serialize the children of a given
189
+ # +Element.outerHTML+.) Similarly, XML::Node#inner_html will serialize the children of a given
149
190
  # node. (This is the equivalent of JavaScript's +Element.innerHTML+.)
150
191
  #
151
192
  # doc = Nokogiri::HTML5("<!DOCTYPE html><span>Hello world!</span>")
@@ -153,12 +194,12 @@ module Nokogiri
153
194
  # # => <!DOCTYPE html><html><head></head><body><span>Hello world!</span></body></html>
154
195
  #
155
196
  # Due to quirks in how HTML is parsed and serialized, it's possible for a DOM tree to be
156
- # serialized and then re-parsed, resulting in a different DOM. Mostly, this happens with DOMs
197
+ # serialized and then re-parsed, resulting in a different DOM. Mostly, this happens with DOMs
157
198
  # produced from invalid HTML. Unfortunately, even valid HTML may not survive serialization and
158
199
  # re-parsing.
159
200
  #
160
- # In particular, a newline at the start of +pre+, +listing+, and +textarea+ elements is ignored by
161
- # the parser.
201
+ # In particular, a newline at the start of +pre+, +listing+, and +textarea+
202
+ # elements is ignored by the parser.
162
203
  #
163
204
  # doc = Nokogiri::HTML5(<<-EOF)
164
205
  # <!DOCTYPE html>
@@ -187,73 +228,57 @@ module Nokogiri
187
228
  #
188
229
  # == Encodings
189
230
  #
190
- # Nokogiri always parses HTML5 using {https://en.wikipedia.org/wiki/UTF-8 UTF-8}; however, the
231
+ # Nokogiri always parses HTML5 using {UTF-8}[https://en.wikipedia.org/wiki/UTF-8]; however, the
191
232
  # encoding of the input can be explicitly selected via the optional +encoding+ parameter. This is
192
233
  # most useful when the input comes not from a string but from an IO object.
193
234
  #
194
235
  # When serializing a document or node, the encoding of the output string can be specified via the
195
236
  # +:encoding+ options. Characters that cannot be encoded in the selected encoding will be encoded
196
- # as {https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references HTML numeric
197
- # entities}.
237
+ # as {HTML numeric
238
+ # entities}[https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references].
198
239
  #
199
240
  # frag = Nokogiri::HTML5.fragment('<span>아는 길도 물어가라</span>')
200
241
  # html = frag.serialize(encoding: 'US-ASCII')
201
242
  # puts html
202
243
  # # => <span>&#xc544;&#xb294; &#xae38;&#xb3c4; &#xbb3c;&#xc5b4;&#xac00;&#xb77c;</span>
244
+ #
203
245
  # frag = Nokogiri::HTML5.fragment(html)
204
246
  # puts frag.serialize
205
247
  # # => <span>아는 길도 물어가라</span>
206
248
  #
207
- # (There's a {https://bugs.ruby-lang.org/issues/15033 bug} in all current versions of Ruby that
249
+ # (There's a {bug}[https://bugs.ruby-lang.org/issues/15033] in all current versions of Ruby that
208
250
  # can cause the entity encoding to fail. Of the mandated supported encodings for HTML, the only
209
251
  # encoding I'm aware of that has this bug is <tt>'ISO-2022-JP'</tt>. We recommend avoiding this
210
252
  # encoding.)
211
253
  #
212
254
  # == Notes
213
255
  #
214
- # * The {Nokogiri::HTML5.fragment} function takes a string and parses it
215
- # as a HTML5 document. The +<html>+, +<head>+, and +<body>+ elements are
216
- # removed from this document, and any children of these elements that remain
217
- # are returned as a {Nokogiri::HTML5::DocumentFragment}.
256
+ # * The Nokogiri::HTML5.fragment function takes a String or IO and parses it as a HTML5 document
257
+ # in a +body+ context. As a result, the +html+, +head+, and +body+ elements are removed from
258
+ # this document, and any children of these elements that remain are returned as a
259
+ # Nokogiri::HTML5::DocumentFragment; but you can pass in a different context (e.g., "html" to
260
+ # get +head+ and +body+ tags in the result).
218
261
  #
219
- # * The {Nokogiri::HTML5.parse} function takes a string and passes it to the
220
- # <code>gumbo_parse_with_options</code> method, using the default options.
221
- # The resulting Gumbo parse tree is then walked.
262
+ # * The Nokogiri::HTML5.parse function takes a String or IO and passes it to the
263
+ # <code>gumbo_parse_with_options</code> method, using the default options. The resulting Gumbo
264
+ # parse tree is then walked.
222
265
  #
223
266
  # * Instead of uppercase element names, lowercase element names are produced.
224
267
  #
225
- # * Instead of returning +unknown+ as the element name for unknown tags, the
226
- # original tag name is returned verbatim.
268
+ # * Instead of returning +unknown+ as the element name for unknown tags, the original tag name is
269
+ # returned verbatim.
227
270
  #
228
271
  # Since v1.12.0
229
272
  module HTML5
230
273
  class << self
231
- # Parse an HTML 5 document. Convenience method for {Nokogiri::HTML5::Document.parse}
232
- def parse(string, url = nil, encoding = nil, **options, &block)
233
- Document.parse(string, url, encoding, **options, &block)
234
- end
235
-
236
- # Parse a fragment from +string+. Convenience method for
237
- # {Nokogiri::HTML5::DocumentFragment.parse}.
238
- def fragment(string, encoding = nil, **options)
239
- DocumentFragment.parse(string, encoding, options)
274
+ # Convenience method for Nokogiri::HTML5::Document.parse
275
+ def parse(...)
276
+ Document.parse(...)
240
277
  end
241
278
 
242
- # Fetch and parse a HTML document from the web, following redirects,
243
- # handling https, and determining the character encoding using HTML5
244
- # rules. +uri+ may be a +String+ or a +URI+. +options+ contains
245
- # http headers and special options. Everything which is not a
246
- # special option is considered a header. Special options include:
247
- # * :follow_limit => number of redirects which are followed
248
- # * :basic_auth => [username, password]
249
- def get(uri, options = {})
250
- # TODO: deprecate
251
- warn(
252
- "Nokogiri::HTML5.get is deprecated and will be removed in a future version of Nokogiri.",
253
- uplevel: 1,
254
- category: :deprecated,
255
- )
256
- get_impl(uri, options)
279
+ # Convenience method for Nokogiri::HTML5::DocumentFragment.parse
280
+ def fragment(...)
281
+ DocumentFragment.parse(...)
257
282
  end
258
283
 
259
284
  # :nodoc:
@@ -283,61 +308,12 @@ module Nokogiri
283
308
 
284
309
  private
285
310
 
286
- def get_impl(uri, options = {})
287
- headers = options.clone
288
- headers = { follow_limit: headers } if Numeric === headers # deprecated
289
- limit = headers[:follow_limit] ? headers.delete(:follow_limit).to_i : 10
290
-
291
- require "net/http"
292
- uri = URI(uri) unless URI === uri
293
-
294
- http = Net::HTTP.new(uri.host, uri.port)
295
-
296
- # TLS / SSL support
297
- http.use_ssl = true if uri.scheme == "https"
298
-
299
- # Pass through Net::HTTP override values, which currently include:
300
- # :ca_file, :ca_path, :cert, :cert_store, :ciphers,
301
- # :close_on_empty_response, :continue_timeout, :key, :open_timeout,
302
- # :read_timeout, :ssl_timeout, :ssl_version, :use_ssl,
303
- # :verify_callback, :verify_depth, :verify_mode
304
- options.each do |key, _value|
305
- http.send("#{key}=", headers.delete(key)) if http.respond_to?("#{key}=")
306
- end
307
-
308
- request = Net::HTTP::Get.new(uri.request_uri)
309
-
310
- # basic authentication
311
- auth = headers.delete(:basic_auth)
312
- auth ||= [uri.user, uri.password] if uri.user && uri.password
313
- request.basic_auth(auth.first, auth.last) if auth
314
-
315
- # remaining options are treated as headers
316
- headers.each { |key, value| request[key.to_s] = value.to_s }
317
-
318
- response = http.request(request)
319
-
320
- case response
321
- when Net::HTTPSuccess
322
- doc = parse(reencode(response.body, response["content-type"]), options)
323
- doc.instance_variable_set(:@response, response)
324
- doc.class.send(:attr_reader, :response)
325
- doc
326
- when Net::HTTPRedirection
327
- response.value if limit <= 1
328
- location = URI.join(uri, response["location"])
329
- get_impl(location, options.merge(follow_limit: limit - 1))
330
- else
331
- response.value
332
- end
333
- end
334
-
335
311
  # Charset sniffing is a complex and controversial topic that understandably isn't done _by
336
- # default_ by the Ruby Net::HTTP library. This being said, it is a very real problem for
312
+ # default_ by the Ruby Net::HTTP library. This being said, it is a very real problem for
337
313
  # consumers of HTML as the default for HTML is iso-8859-1, most "good" producers use utf-8, and
338
314
  # the Gumbo parser *only* supports utf-8.
339
315
  #
340
- # Accordingly, Nokogiri::HTML4::Document.parse provides limited encoding detection. Following
316
+ # Accordingly, Nokogiri::HTML4::Document.parse provides limited encoding detection. Following
341
317
  # this lead, Nokogiri::HTML5 attempts to do likewise, while attempting to more closely follow
342
318
  # the HTML5 standard.
343
319
  #
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Nokogiri
4
4
  # The version of Nokogiri you are using
5
- VERSION = "1.15.3"
5
+ VERSION = "1.18.7"
6
6
  end
@@ -94,11 +94,14 @@ module Nokogiri
94
94
  nokogiri["version"] = Nokogiri::VERSION
95
95
 
96
96
  unless jruby?
97
- # enable gems like nokogumbo to build with the following in their extconf.rb:
97
+ # enable gems to build against Nokogiri with the following in their extconf.rb:
98
98
  #
99
99
  # append_cflags(Nokogiri::VERSION_INFO["nokogiri"]["cppflags"])
100
100
  # append_ldflags(Nokogiri::VERSION_INFO["nokogiri"]["ldflags"])
101
101
  #
102
+ # though, this won't work on all platform and versions of Ruby, and won't be supported
103
+ # forever, see https://github.com/sparklemotion/nokogiri/discussions/2746 for context.
104
+ #
102
105
  cppflags = ["-I#{header_directory.shellescape}"]
103
106
  ldflags = []
104
107
 
@@ -108,7 +111,8 @@ module Nokogiri
108
111
  end
109
112
 
110
113
  if windows?
111
- # on windows, nokogumbo needs to link against nokogiri.so to resolve symbols. see #2167
114
+ # on windows, third party libraries that wish to link against nokogiri
115
+ # should link against nokogiri.so to resolve symbols. see #2167
112
116
  lib_directory = File.expand_path(File.join(File.dirname(__FILE__), "../#{ruby_minor}"))
113
117
  unless File.exist?(lib_directory)
114
118
  lib_directory = File.expand_path(File.join(File.dirname(__FILE__), ".."))
@@ -136,9 +140,6 @@ module Nokogiri
136
140
  libxml["source"] = "packaged"
137
141
  libxml["precompiled"] = libxml2_precompiled?
138
142
  libxml["patches"] = Nokogiri::LIBXML2_PATCHES
139
-
140
- # this is for nokogumbo and shouldn't be forever
141
- libxml["libxml2_path"] = header_directory
142
143
  else
143
144
  libxml["source"] = "system"
144
145
  end
@@ -18,8 +18,6 @@ module Nokogiri
18
18
  # - +value+ → (String) The value of the attribute.
19
19
  # - +namespace+ → (Namespace, nil) The Namespace of the attribute, or +nil+ if there is no namespace.
20
20
  #
21
- # ⚡ This is an experimental feature, available since v1.14.0
22
- #
23
21
  # *Example*
24
22
  #
25
23
  # doc = Nokogiri::XML.parse(<<~XML)
@@ -52,6 +50,8 @@ module Nokogiri
52
50
  # # href = "http://nokogiri.org/ns/noko"
53
51
  # # })}
54
52
  #
53
+ # Since v1.14.0
54
+ #
55
55
  def deconstruct_keys(keys)
56
56
  { name: name, value: value, namespace: namespace }
57
57
  end
@@ -475,7 +475,14 @@ module Nokogiri
475
475
  if block
476
476
  old_parent = @doc_builder.parent
477
477
  @doc_builder.parent = @node
478
- value = @doc_builder.instance_eval(&block)
478
+
479
+ arity = @doc_builder.arity || block.arity
480
+ value = if arity <= 0
481
+ @doc_builder.instance_eval(&block)
482
+ else
483
+ yield(@doc_builder)
484
+ end
485
+
479
486
  @doc_builder.parent = old_parent
480
487
  return value
481
488
  end
@@ -5,12 +5,12 @@ require "pathname"
5
5
 
6
6
  module Nokogiri
7
7
  module XML
8
- # Nokogiri::XML::Document is the main entry point for dealing with XML documents. The Document
9
- # is created by parsing an XML document. See Nokogiri::XML::Document.parse for more information
10
- # on parsing.
8
+ # Nokogiri::XML::Document is the main entry point for dealing with \XML documents. The Document
9
+ # is created by parsing \XML content from a String or an IO object. See
10
+ # Nokogiri::XML::Document.parse for more information on parsing.
11
11
  #
12
- # For searching a Document, see Nokogiri::XML::Searchable#css and
13
- # Nokogiri::XML::Searchable#xpath
12
+ # Document inherits a great deal of functionality from its superclass Nokogiri::XML::Node, so
13
+ # please read that class's documentation as well.
14
14
  class Document < Nokogiri::XML::Node
15
15
  # See http://www.w3.org/TR/REC-xml-names/#ns-decl for more details. Note that we're not
16
16
  # attempting to handle unicode characters partly because libxml2 doesn't handle unicode
@@ -19,33 +19,45 @@ module Nokogiri
19
19
  NCNAME_CHAR = NCNAME_START_CHAR + "\\-\\.0-9"
20
20
  NCNAME_RE = /^xmlns(?::([#{NCNAME_START_CHAR}][#{NCNAME_CHAR}]*))?$/
21
21
 
22
+ OBJECT_DUP_METHOD = Object.instance_method(:dup)
23
+ OBJECT_CLONE_METHOD = Object.instance_method(:clone)
24
+ private_constant :OBJECT_DUP_METHOD, :OBJECT_CLONE_METHOD
25
+
22
26
  class << self
23
- # Parse an XML file.
27
+ # call-seq:
28
+ # parse(input) { |options| ... } => Nokogiri::XML::Document
29
+ # parse(input, url:, encoding:, options:) => Nokogiri::XML::Document
24
30
  #
25
- # +string_or_io+ may be a String, or any object that responds to
26
- # _read_ and _close_ such as an IO, or StringIO.
31
+ # Parse \XML input from a String or IO object, and return a new XML::Document.
27
32
  #
28
- # +url+ (optional) is the URI where this document is located.
33
+ # 🛡 By default, Nokogiri treats documents as untrusted, and so does not attempt to load DTDs
34
+ # or access the network. See Nokogiri::XML::ParseOptions for a complete list of options; and
35
+ # that module's DEFAULT_XML constant for what's set (and not set) by default.
29
36
  #
30
- # +encoding+ (optional) is the encoding that should be used when processing
31
- # the document.
37
+ # [Required Parameters]
38
+ # - +input+ (String | IO) The content to be parsed.
32
39
  #
33
- # +options+ (optional) is a configuration object that sets options during
34
- # parsing, such as Nokogiri::XML::ParseOptions::RECOVER. See the
35
- # Nokogiri::XML::ParseOptions for more information.
40
+ # [Optional Keyword Arguments]
41
+ # - +url:+ (String) The base URI for this document.
36
42
  #
37
- # +block+ (optional) is passed a configuration object on which
38
- # parse options may be set.
43
+ # - +encoding:+ (String) The name of the encoding that should be used when processing the
44
+ # document. When not provided, the encoding will be determined based on the document
45
+ # content.
39
46
  #
40
- # By default, Nokogiri treats documents as untrusted, and so
41
- # does not attempt to load DTDs or access the network. See
42
- # Nokogiri::XML::ParseOptions for a complete list of options;
43
- # and that module's DEFAULT_XML constant for what's set (and not
44
- # set) by default.
47
+ # - +options:+ (Nokogiri::XML::ParseOptions) Configuration object that determines some
48
+ # behaviors during parsing. See ParseOptions for more information. The default value is
49
+ # +ParseOptions::DEFAULT_XML+.
45
50
  #
46
- # Nokogiri.XML() is a convenience method which will call this method.
51
+ # [Yields]
52
+ # If a block is given, a Nokogiri::XML::ParseOptions object is yielded to the block which
53
+ # can be configured before parsing. See Nokogiri::XML::ParseOptions for more information.
47
54
  #
48
- def parse(string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML)
55
+ # [Returns] Nokogiri::XML::Document
56
+ def parse(
57
+ string_or_io,
58
+ url_ = nil, encoding_ = nil, options_ = XML::ParseOptions::DEFAULT_XML,
59
+ url: url_, encoding: encoding_, options: options_
60
+ )
49
61
  options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
50
62
  yield options if block_given?
51
63
 
@@ -60,6 +72,7 @@ module Nokogiri
60
72
  end
61
73
 
62
74
  doc = if string_or_io.respond_to?(:read)
75
+ # TODO: should we instead check for respond_to?(:to_path) ?
63
76
  if string_or_io.is_a?(Pathname)
64
77
  # resolve the Pathname to the file and open it as an IO object, see #2110
65
78
  string_or_io = string_or_io.expand_path.open
@@ -174,13 +187,44 @@ module Nokogiri
174
187
  # Since v1.12.4
175
188
  attr_accessor :namespace_inheritance
176
189
 
177
- # :nodoc:
178
- def initialize(*args) # rubocop:disable Lint/MissingSuper
190
+ def initialize(*args) # :nodoc: # rubocop:disable Lint/MissingSuper
179
191
  @errors = []
180
192
  @decorators = nil
181
193
  @namespace_inheritance = false
182
194
  end
183
195
 
196
+ #
197
+ # :call-seq:
198
+ # dup → Nokogiri::XML::Document
199
+ # dup(level) → Nokogiri::XML::Document
200
+ #
201
+ # Duplicate this node.
202
+ #
203
+ # [Parameters]
204
+ # - +level+ (optional Integer). 0 is a shallow copy, 1 (the default) is a deep copy.
205
+ # [Returns] The new Nokogiri::XML::Document
206
+ #
207
+ def dup(level = 1)
208
+ copy = OBJECT_DUP_METHOD.bind_call(self)
209
+ copy.initialize_copy_with_args(self, level)
210
+ end
211
+
212
+ #
213
+ # :call-seq:
214
+ # clone → Nokogiri::XML::Document
215
+ # clone(level) → Nokogiri::XML::Document
216
+ #
217
+ # Clone this node.
218
+ #
219
+ # [Parameters]
220
+ # - +level+ (optional Integer). 0 is a shallow copy, 1 (the default) is a deep copy.
221
+ # [Returns] The new Nokogiri::XML::Document
222
+ #
223
+ def clone(level = 1)
224
+ copy = OBJECT_CLONE_METHOD.bind_call(self)
225
+ copy.initialize_copy_with_args(self, level)
226
+ end
227
+
184
228
  # :call-seq:
185
229
  # create_element(name, *contents_or_attrs, &block) → Nokogiri::XML::Element
186
230
  #
@@ -327,10 +371,10 @@ module Nokogiri
327
371
  end
328
372
 
329
373
  ##
330
- # Validate this Document against it's DTD. Returns a list of errors on
374
+ # Validate this Document against its DTD. Returns a list of errors on
331
375
  # the document or +nil+ when there is no DTD.
332
376
  def validate
333
- return nil unless internal_subset
377
+ return unless internal_subset
334
378
 
335
379
  internal_subset.validate(self)
336
380
  end
@@ -368,12 +412,11 @@ module Nokogiri
368
412
  @decorators.each do |klass, list|
369
413
  next unless node.is_a?(klass)
370
414
 
371
- list.each { |moodule| node.extend(moodule) }
415
+ list.each { |mod| node.extend(mod) }
372
416
  end
373
417
  end
374
418
 
375
419
  alias_method :to_xml, :serialize
376
- alias_method :clone, :dup
377
420
 
378
421
  # Get the hash of namespaces on the root Nokogiri::XML::Node
379
422
  def namespaces
@@ -427,8 +470,6 @@ module Nokogiri
427
470
  # instructions. If you have a use case and would like this functionality, please let us know
428
471
  # by opening an issue or a discussion on the github project.
429
472
  #
430
- # ⚡ This is an experimental feature, available since v1.14.0
431
- #
432
473
  # *Example*
433
474
  #
434
475
  # doc = Nokogiri::XML.parse(<<~XML)
@@ -455,6 +496,8 @@ module Nokogiri
455
496
  # doc.deconstruct_keys([:root])
456
497
  # # => {:root=>nil}
457
498
  #
499
+ # Since v1.14.0
500
+ #
458
501
  def deconstruct_keys(keys)
459
502
  { root: root }
460
503
  end