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.
- checksums.yaml +4 -4
- data/Gemfile +12 -17
- data/LICENSE-DEPENDENCIES.md +6 -6
- data/README.md +11 -5
- data/dependencies.yml +9 -8
- data/ext/nokogiri/extconf.rb +191 -154
- data/ext/nokogiri/gumbo.c +69 -53
- data/ext/nokogiri/html4_document.c +10 -4
- data/ext/nokogiri/html4_element_description.c +18 -18
- data/ext/nokogiri/html4_sax_parser.c +40 -0
- data/ext/nokogiri/html4_sax_parser_context.c +48 -58
- data/ext/nokogiri/html4_sax_push_parser.c +26 -25
- data/ext/nokogiri/libxml2_polyfill.c +114 -0
- data/ext/nokogiri/nokogiri.c +9 -2
- data/ext/nokogiri/nokogiri.h +25 -33
- data/ext/nokogiri/test_global_handlers.c +1 -1
- data/ext/nokogiri/xml_attr.c +1 -1
- data/ext/nokogiri/xml_cdata.c +3 -12
- data/ext/nokogiri/xml_comment.c +3 -8
- data/ext/nokogiri/xml_document.c +173 -158
- data/ext/nokogiri/xml_document_fragment.c +10 -25
- data/ext/nokogiri/xml_dtd.c +1 -1
- data/ext/nokogiri/xml_element_content.c +9 -9
- data/ext/nokogiri/xml_encoding_handler.c +4 -4
- data/ext/nokogiri/xml_namespace.c +6 -10
- data/ext/nokogiri/xml_node.c +142 -108
- data/ext/nokogiri/xml_node_set.c +46 -44
- data/ext/nokogiri/xml_reader.c +74 -100
- data/ext/nokogiri/xml_relax_ng.c +35 -56
- data/ext/nokogiri/xml_sax_parser.c +156 -88
- data/ext/nokogiri/xml_sax_parser_context.c +220 -128
- data/ext/nokogiri/xml_sax_push_parser.c +69 -50
- data/ext/nokogiri/xml_schema.c +51 -87
- data/ext/nokogiri/xml_syntax_error.c +19 -11
- data/ext/nokogiri/xml_text.c +3 -6
- data/ext/nokogiri/xml_xpath_context.c +104 -104
- data/ext/nokogiri/xslt_stylesheet.c +16 -11
- data/gumbo-parser/Makefile +18 -0
- data/gumbo-parser/src/ascii.c +2 -2
- data/gumbo-parser/src/error.c +76 -48
- data/gumbo-parser/src/error.h +5 -1
- data/gumbo-parser/src/nokogiri_gumbo.h +11 -2
- data/gumbo-parser/src/parser.c +66 -25
- data/gumbo-parser/src/tokenizer.c +7 -6
- data/lib/nokogiri/class_resolver.rb +1 -1
- data/lib/nokogiri/css/node.rb +6 -2
- data/lib/nokogiri/css/parser.rb +6 -4
- data/lib/nokogiri/css/parser.y +2 -2
- data/lib/nokogiri/css/parser_extras.rb +6 -66
- data/lib/nokogiri/css/selector_cache.rb +38 -0
- data/lib/nokogiri/css/tokenizer.rb +4 -4
- data/lib/nokogiri/css/tokenizer.rex +9 -8
- data/lib/nokogiri/css/xpath_visitor.rb +44 -27
- data/lib/nokogiri/css.rb +86 -20
- data/lib/nokogiri/decorators/slop.rb +3 -5
- data/lib/nokogiri/encoding_handler.rb +2 -2
- data/lib/nokogiri/html4/document.rb +45 -24
- data/lib/nokogiri/html4/document_fragment.rb +124 -12
- data/lib/nokogiri/html4/encoding_reader.rb +2 -2
- data/lib/nokogiri/html4/sax/parser.rb +23 -38
- data/lib/nokogiri/html4/sax/parser_context.rb +4 -9
- data/lib/nokogiri/html4.rb +9 -14
- data/lib/nokogiri/html5/builder.rb +40 -0
- data/lib/nokogiri/html5/document.rb +61 -30
- data/lib/nokogiri/html5/document_fragment.rb +130 -20
- data/lib/nokogiri/html5/node.rb +4 -4
- data/lib/nokogiri/html5.rb +114 -138
- data/lib/nokogiri/version/constant.rb +1 -1
- data/lib/nokogiri/version/info.rb +6 -5
- data/lib/nokogiri/xml/attr.rb +2 -2
- data/lib/nokogiri/xml/builder.rb +8 -1
- data/lib/nokogiri/xml/document.rb +74 -31
- data/lib/nokogiri/xml/document_fragment.rb +86 -15
- data/lib/nokogiri/xml/namespace.rb +1 -2
- data/lib/nokogiri/xml/node.rb +113 -35
- data/lib/nokogiri/xml/node_set.rb +12 -10
- data/lib/nokogiri/xml/parse_options.rb +1 -1
- data/lib/nokogiri/xml/pp/node.rb +6 -1
- data/lib/nokogiri/xml/reader.rb +51 -17
- data/lib/nokogiri/xml/relax_ng.rb +57 -20
- data/lib/nokogiri/xml/sax/document.rb +174 -83
- data/lib/nokogiri/xml/sax/parser.rb +115 -41
- data/lib/nokogiri/xml/sax/parser_context.rb +116 -8
- data/lib/nokogiri/xml/sax/push_parser.rb +3 -0
- data/lib/nokogiri/xml/sax.rb +48 -0
- data/lib/nokogiri/xml/schema.rb +112 -45
- data/lib/nokogiri/xml/searchable.rb +39 -43
- data/lib/nokogiri/xml/syntax_error.rb +23 -1
- data/lib/nokogiri/xml/xpath_context.rb +14 -3
- data/lib/nokogiri/xml.rb +14 -25
- data/lib/nokogiri/xslt/stylesheet.rb +29 -7
- data/lib/nokogiri/xslt.rb +4 -10
- data/lib/nokogiri.rb +1 -1
- data/lib/xsd/xmlparser/nokogiri.rb +3 -4
- data/patches/libxml2/0019-xpath-Use-separate-static-hash-table-for-standard-fu.patch +244 -0
- data/ports/archives/libxml2-2.13.7.tar.xz +0 -0
- data/ports/archives/libxslt-1.1.43.tar.xz +0 -0
- metadata +13 -14
- data/ext/nokogiri/libxml2_backwards_compat.c +0 -121
- data/patches/libxml2/0003-libxml2.la-is-in-top_builddir.patch +0 -25
- data/ports/archives/libxml2-2.11.4.tar.xz +0 -0
- data/ports/archives/libxslt-1.1.38.tar.xz +0 -0
data/lib/nokogiri/html5.rb
CHANGED
@@ -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
|
-
#
|
6
|
+
# Copyright 2013-2021 Sam Ruby, Stephen Checkoway
|
6
7
|
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
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
|
-
#
|
12
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
13
|
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
#
|
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
|
-
#
|
26
|
-
|
27
|
-
|
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(
|
36
|
+
# doc = Nokogiri.HTML5(input)
|
41
37
|
#
|
42
38
|
# Parse an HTML5 fragment:
|
43
39
|
#
|
44
|
-
# fragment = Nokogiri::HTML5.fragment(
|
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
|
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
|
-
#
|
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
|
-
#
|
57
|
-
#
|
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
|
-
#
|
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
|
-
#
|
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
|
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
|
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
|
97
|
-
#
|
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
|
-
#
|
125
|
+
# +ArgumentError+ is thrown.
|
120
126
|
#
|
121
|
-
# This limit (which defaults to
|
122
|
-
#
|
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
|
138
|
+
# option. If a given element would exceed this limit, then an +ArgumentError+ is thrown.
|
133
139
|
#
|
134
|
-
# This limit (which defaults to
|
135
|
-
#
|
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
|
146
|
-
# methods. In particular,
|
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,
|
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.
|
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+
|
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
|
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 {
|
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>아는 길도 물어가라</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
|
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
|
215
|
-
#
|
216
|
-
#
|
217
|
-
#
|
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
|
220
|
-
# <code>gumbo_parse_with_options</code> method, using the default options.
|
221
|
-
#
|
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
|
-
#
|
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
|
-
#
|
232
|
-
def parse(
|
233
|
-
Document.parse(
|
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
|
-
#
|
243
|
-
|
244
|
-
|
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.
|
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.
|
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
|
#
|
@@ -94,11 +94,14 @@ module Nokogiri
|
|
94
94
|
nokogiri["version"] = Nokogiri::VERSION
|
95
95
|
|
96
96
|
unless jruby?
|
97
|
-
# enable gems
|
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,
|
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
|
data/lib/nokogiri/xml/attr.rb
CHANGED
@@ -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
|
data/lib/nokogiri/xml/builder.rb
CHANGED
@@ -475,7 +475,14 @@ module Nokogiri
|
|
475
475
|
if block
|
476
476
|
old_parent = @doc_builder.parent
|
477
477
|
@doc_builder.parent = @node
|
478
|
-
|
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.
|
9
|
-
# is created by parsing
|
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
|
-
#
|
13
|
-
#
|
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
|
-
#
|
27
|
+
# call-seq:
|
28
|
+
# parse(input) { |options| ... } => Nokogiri::XML::Document
|
29
|
+
# parse(input, url:, encoding:, options:) => Nokogiri::XML::Document
|
24
30
|
#
|
25
|
-
#
|
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
|
-
#
|
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
|
-
#
|
31
|
-
#
|
37
|
+
# [Required Parameters]
|
38
|
+
# - +input+ (String | IO) The content to be parsed.
|
32
39
|
#
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# Nokogiri::XML::ParseOptions for more information.
|
40
|
+
# [Optional Keyword Arguments]
|
41
|
+
# - +url:+ (String) The base URI for this document.
|
36
42
|
#
|
37
|
-
# +
|
38
|
-
#
|
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
|
-
#
|
41
|
-
#
|
42
|
-
#
|
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
|
-
#
|
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
|
-
|
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
|
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
|
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 { |
|
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
|