nokogiri 1.16.3 → 1.18.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of nokogiri might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile +14 -22
- data/LICENSE-DEPENDENCIES.md +6 -6
- data/README.md +8 -5
- data/dependencies.yml +9 -9
- data/ext/nokogiri/extconf.rb +188 -142
- 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 +25 -24
- data/ext/nokogiri/libxml2_polyfill.c +114 -0
- data/ext/nokogiri/nokogiri.c +9 -2
- data/ext/nokogiri/nokogiri.h +18 -33
- data/ext/nokogiri/xml_attr.c +1 -1
- data/ext/nokogiri/xml_cdata.c +2 -10
- data/ext/nokogiri/xml_comment.c +3 -8
- data/ext/nokogiri/xml_document.c +163 -156
- 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 -6
- data/ext/nokogiri/xml_node.c +141 -104
- data/ext/nokogiri/xml_node_set.c +46 -44
- data/ext/nokogiri/xml_reader.c +54 -58
- 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 +219 -131
- data/ext/nokogiri/xml_sax_push_parser.c +68 -49
- data/ext/nokogiri/xml_schema.c +50 -85
- data/ext/nokogiri/xml_syntax_error.c +19 -11
- data/ext/nokogiri/xml_text.c +2 -4
- data/ext/nokogiri/xml_xpath_context.c +103 -100
- data/ext/nokogiri/xslt_stylesheet.c +8 -8
- 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 +63 -25
- data/gumbo-parser/src/tokenizer.c +6 -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 +42 -6
- 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 +44 -23
- data/lib/nokogiri/html4/document_fragment.rb +124 -12
- data/lib/nokogiri/html4/encoding_reader.rb +1 -1
- 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 -72
- data/lib/nokogiri/version/constant.rb +1 -1
- data/lib/nokogiri/xml/builder.rb +8 -1
- data/lib/nokogiri/xml/document.rb +70 -26
- data/lib/nokogiri/xml/document_fragment.rb +84 -13
- data/lib/nokogiri/xml/node.rb +82 -11
- data/lib/nokogiri/xml/node_set.rb +9 -7
- data/lib/nokogiri/xml/parse_options.rb +1 -1
- data/lib/nokogiri/xml/pp/node.rb +6 -1
- data/lib/nokogiri/xml/reader.rb +46 -13
- 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 +38 -42
- data/lib/nokogiri/xml/syntax_error.rb +22 -0
- data/lib/nokogiri/xml/xpath_context.rb +14 -3
- data/lib/nokogiri/xml.rb +13 -24
- data/lib/nokogiri/xslt.rb +3 -9
- 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.5.tar.xz +0 -0
- data/ports/archives/libxslt-1.1.42.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.12.6.tar.xz +0 -0
- data/ports/archives/libxslt-1.1.39.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,56 +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(
|
274
|
+
# Convenience method for Nokogiri::HTML5::Document.parse
|
275
|
+
def parse(...)
|
276
|
+
Document.parse(...)
|
234
277
|
end
|
235
278
|
|
236
|
-
#
|
237
|
-
|
238
|
-
|
239
|
-
DocumentFragment.parse(string, encoding, options)
|
279
|
+
# Convenience method for Nokogiri::HTML5::DocumentFragment.parse
|
280
|
+
def fragment(...)
|
281
|
+
DocumentFragment.parse(...)
|
240
282
|
end
|
241
283
|
|
242
284
|
# :nodoc:
|
@@ -267,11 +309,11 @@ module Nokogiri
|
|
267
309
|
private
|
268
310
|
|
269
311
|
# Charset sniffing is a complex and controversial topic that understandably isn't done _by
|
270
|
-
# 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
|
271
313
|
# consumers of HTML as the default for HTML is iso-8859-1, most "good" producers use utf-8, and
|
272
314
|
# the Gumbo parser *only* supports utf-8.
|
273
315
|
#
|
274
|
-
# Accordingly, Nokogiri::HTML4::Document.parse provides limited encoding detection.
|
316
|
+
# Accordingly, Nokogiri::HTML4::Document.parse provides limited encoding detection. Following
|
275
317
|
# this lead, Nokogiri::HTML5 attempts to do likewise, while attempting to more closely follow
|
276
318
|
# the HTML5 standard.
|
277
319
|
#
|
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
|
@@ -180,6 +193,38 @@ module Nokogiri
|
|
180
193
|
@namespace_inheritance = false
|
181
194
|
end
|
182
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
|
+
|
183
228
|
# :call-seq:
|
184
229
|
# create_element(name, *contents_or_attrs, &block) → Nokogiri::XML::Element
|
185
230
|
#
|
@@ -326,7 +371,7 @@ module Nokogiri
|
|
326
371
|
end
|
327
372
|
|
328
373
|
##
|
329
|
-
# Validate this Document against
|
374
|
+
# Validate this Document against its DTD. Returns a list of errors on
|
330
375
|
# the document or +nil+ when there is no DTD.
|
331
376
|
def validate
|
332
377
|
return unless internal_subset
|
@@ -367,12 +412,11 @@ module Nokogiri
|
|
367
412
|
@decorators.each do |klass, list|
|
368
413
|
next unless node.is_a?(klass)
|
369
414
|
|
370
|
-
list.each { |
|
415
|
+
list.each { |mod| node.extend(mod) }
|
371
416
|
end
|
372
417
|
end
|
373
418
|
|
374
419
|
alias_method :to_xml, :serialize
|
375
|
-
alias_method :clone, :dup
|
376
420
|
|
377
421
|
# Get the hash of namespaces on the root Nokogiri::XML::Node
|
378
422
|
def namespaces
|
@@ -3,32 +3,103 @@
|
|
3
3
|
|
4
4
|
module Nokogiri
|
5
5
|
module XML
|
6
|
+
# DocumentFragment represents a fragment of an \XML document. It provides the same functionality
|
7
|
+
# exposed by XML::Node and can be used to contain one or more \XML subtrees.
|
6
8
|
class DocumentFragment < Nokogiri::XML::Node
|
7
|
-
|
8
|
-
#
|
9
|
-
|
10
|
-
|
9
|
+
# The options used to parse the document fragment. Returns the value of any options that were
|
10
|
+
# passed into the constructor as a parameter or set in a config block, else the default
|
11
|
+
# options for the specific subclass.
|
12
|
+
attr_reader :parse_options
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# :call-seq:
|
16
|
+
# parse(input) { |options| ... } → XML::DocumentFragment
|
17
|
+
# parse(input, options:) → XML::DocumentFragment
|
18
|
+
#
|
19
|
+
# Parse \XML fragment input from a String, and return a new XML::DocumentFragment. This
|
20
|
+
# method creates a new, empty XML::Document to contain the fragment.
|
21
|
+
#
|
22
|
+
# [Required Parameters]
|
23
|
+
# - +input+ (String) The content to be parsed.
|
24
|
+
#
|
25
|
+
# [Optional Keyword Arguments]
|
26
|
+
# - +options+ (Nokogiri::XML::ParseOptions) Configuration object that determines some
|
27
|
+
# behaviors during parsing. See ParseOptions for more information. The default value is
|
28
|
+
# +ParseOptions::DEFAULT_XML+.
|
29
|
+
#
|
30
|
+
# [Yields]
|
31
|
+
# If a block is given, a Nokogiri::XML::ParseOptions object is yielded to the block which
|
32
|
+
# can be configured before parsing. See Nokogiri::XML::ParseOptions for more information.
|
33
|
+
#
|
34
|
+
# [Returns] Nokogiri::XML::DocumentFragment
|
35
|
+
def parse(tags, options_ = ParseOptions::DEFAULT_XML, options: options_, &block)
|
36
|
+
new(XML::Document.new, tags, options: options, &block)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Wrapper method to separate the concerns of:
|
40
|
+
# - the native object allocator's parameter (it only requires `document`)
|
41
|
+
# - the initializer's parameters
|
42
|
+
def new(document, ...) # :nodoc:
|
43
|
+
instance = native_new(document)
|
44
|
+
instance.send(:initialize, document, ...)
|
45
|
+
instance
|
46
|
+
end
|
11
47
|
end
|
12
48
|
|
13
|
-
|
14
|
-
#
|
49
|
+
# :call-seq:
|
50
|
+
# new(document, input=nil) { |options| ... } → DocumentFragment
|
51
|
+
# new(document, input=nil, context:, options:) → DocumentFragment
|
52
|
+
#
|
53
|
+
# Parse \XML fragment input from a String, and return a new DocumentFragment that is
|
54
|
+
# associated with the given +document+.
|
55
|
+
#
|
56
|
+
# 💡 It's recommended to use either XML::DocumentFragment.parse or Node#parse rather than call
|
57
|
+
# this method directly.
|
58
|
+
#
|
59
|
+
# [Required Parameters]
|
60
|
+
# - +document+ (XML::Document) The parent document to associate the returned fragment with.
|
61
|
+
#
|
62
|
+
# [Optional Parameters]
|
63
|
+
# - +input+ (String) The content to be parsed.
|
64
|
+
#
|
65
|
+
# [Optional Keyword Arguments]
|
66
|
+
# - +context:+ (Nokogiri::XML::Node) The <b>context node</b> for the subtree created. See
|
67
|
+
# below for more information.
|
68
|
+
#
|
69
|
+
# - +options:+ (Nokogiri::XML::ParseOptions) Configuration object that determines some
|
70
|
+
# behaviors during parsing. See ParseOptions for more information. The default value is
|
71
|
+
# +ParseOptions::DEFAULT_XML+.
|
72
|
+
#
|
73
|
+
# [Yields]
|
74
|
+
# If a block is given, a Nokogiri::XML::ParseOptions object is yielded to the block which
|
75
|
+
# can be configured before parsing. See ParseOptions for more information.
|
76
|
+
#
|
77
|
+
# [Returns] XML::DocumentFragment
|
78
|
+
#
|
79
|
+
# === Context \Node
|
80
|
+
#
|
81
|
+
# If a context node is specified using +context:+, then the fragment will be created by
|
82
|
+
# calling Node#parse on that node, so the parser will behave as if that Node is the parent of
|
83
|
+
# the fragment subtree, and will resolve namespaces relative to that node.
|
15
84
|
#
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
85
|
+
def initialize(
|
86
|
+
document, tags = nil,
|
87
|
+
context_ = nil, options_ = ParseOptions::DEFAULT_XML,
|
88
|
+
context: context_, options: options_
|
89
|
+
) # rubocop:disable Lint/MissingSuper
|
20
90
|
return self unless tags
|
21
91
|
|
22
92
|
options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
|
93
|
+
@parse_options = options
|
23
94
|
yield options if block_given?
|
24
95
|
|
25
|
-
children = if
|
96
|
+
children = if context
|
26
97
|
# Fix for issue#490
|
27
98
|
if Nokogiri.jruby?
|
28
99
|
# fix for issue #770
|
29
|
-
|
100
|
+
context.parse("<root #{namespace_declarations(context)}>#{tags}</root>", options).children
|
30
101
|
else
|
31
|
-
|
102
|
+
context.parse(tags, options)
|
32
103
|
end
|
33
104
|
else
|
34
105
|
wrapper_doc = XML::Document.parse("<root>#{tags}</root>", nil, nil, options)
|