nokogiri 1.16.8-x64-mingw-ucrt → 1.17.0-x64-mingw-ucrt
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +11 -21
- data/README.md +4 -0
- data/dependencies.yml +6 -6
- data/ext/nokogiri/extconf.rb +191 -137
- 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/include/libexslt/exsltconfig.h +3 -3
- data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +12 -19
- data/ext/nokogiri/include/libxml2/libxml/c14n.h +1 -12
- data/ext/nokogiri/include/libxml2/libxml/debugXML.h +1 -1
- data/ext/nokogiri/include/libxml2/libxml/encoding.h +9 -0
- data/ext/nokogiri/include/libxml2/libxml/entities.h +12 -1
- data/ext/nokogiri/include/libxml2/libxml/hash.h +19 -0
- data/ext/nokogiri/include/libxml2/libxml/list.h +2 -2
- data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +17 -0
- data/ext/nokogiri/include/libxml2/libxml/parser.h +60 -54
- data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +9 -1
- data/ext/nokogiri/include/libxml2/libxml/pattern.h +6 -0
- data/ext/nokogiri/include/libxml2/libxml/tree.h +32 -12
- data/ext/nokogiri/include/libxml2/libxml/uri.h +11 -0
- data/ext/nokogiri/include/libxml2/libxml/valid.h +29 -2
- data/ext/nokogiri/include/libxml2/libxml/xinclude.h +7 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +21 -4
- data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +14 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +111 -15
- data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +8 -45
- data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +2 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +5 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +165 -1
- data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +7 -171
- data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +1 -0
- data/ext/nokogiri/include/libxml2/libxml/xpath.h +4 -0
- data/ext/nokogiri/include/libxslt/xsltInternals.h +3 -0
- data/ext/nokogiri/include/libxslt/xsltconfig.h +4 -37
- 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 +130 -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 +213 -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 +2 -2
- data/ext/nokogiri/xslt_stylesheet.c +8 -8
- data/lib/nokogiri/3.1/nokogiri.so +0 -0
- data/lib/nokogiri/3.2/nokogiri.so +0 -0
- data/lib/nokogiri/3.3/nokogiri.so +0 -0
- 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 +6 -8
- data/lib/nokogiri/xml/syntax_error.rb +22 -0
- data/lib/nokogiri/xml.rb +13 -24
- data/lib/nokogiri/xslt.rb +3 -9
- data/lib/xsd/xmlparser/nokogiri.rb +3 -4
- metadata +8 -4
- data/ext/nokogiri/libxml2_backwards_compat.c +0 -121
@@ -25,27 +25,145 @@ module Nokogiri
|
|
25
25
|
#
|
26
26
|
# 💡 HTML5 functionality is not available when running JRuby.
|
27
27
|
class DocumentFragment < Nokogiri::HTML4::DocumentFragment
|
28
|
+
class << self
|
29
|
+
# :call-seq:
|
30
|
+
# parse(input, **options) → HTML5::DocumentFragment
|
31
|
+
#
|
32
|
+
# Parse \HTML5 fragment input from a String, and return a new HTML5::DocumentFragment. This
|
33
|
+
# method creates a new, empty HTML5::Document to contain the fragment.
|
34
|
+
#
|
35
|
+
# [Parameters]
|
36
|
+
# - +input+ (String | IO) The HTML5 document fragment to parse.
|
37
|
+
#
|
38
|
+
# [Optional Keyword Arguments]
|
39
|
+
# - +encoding:+ (String | Encoding) The encoding, or name of the encoding, that should be
|
40
|
+
# used when processing the document. When not provided, the encoding will be determined
|
41
|
+
# based on the document content. Also see Nokogiri::HTML5 for a longer explanation of how
|
42
|
+
# encoding is handled by the parser.
|
43
|
+
#
|
44
|
+
# - +context:+ (String | Nokogiri::XML::Node) The node, or the name of an HTML5 element, "in
|
45
|
+
# context" of which to parse the document fragment. See below for more
|
46
|
+
# information. (default +"body"+)
|
47
|
+
#
|
48
|
+
# - +max_errors:+ (Integer) The maximum number of parse errors to record. (default
|
49
|
+
# +Nokogiri::Gumbo::DEFAULT_MAX_ERRORS+ which is currently 0)
|
50
|
+
#
|
51
|
+
# - +max_tree_depth:+ (Integer) The maximum depth of the parse tree. (default
|
52
|
+
# +Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH+)
|
53
|
+
#
|
54
|
+
# - +max_attributes:+ (Integer) The maximum number of attributes allowed on an
|
55
|
+
# element. (default +Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES+)
|
56
|
+
#
|
57
|
+
# - +parse_noscript_content_as_text:+ (Boolean) Whether to parse the content of +noscript+
|
58
|
+
# elements as text. (default +false+)
|
59
|
+
#
|
60
|
+
# See rdoc-ref:HTML5@Parsing+options for a complete description of these parsing options.
|
61
|
+
#
|
62
|
+
# [Returns] Nokogiri::HTML5::DocumentFragment
|
63
|
+
#
|
64
|
+
# === Context \Node
|
65
|
+
#
|
66
|
+
# If a context node is specified using +context:+, then the parser will behave as if that
|
67
|
+
# Node, or a hypothetical tag named as specified, is the parent of the fragment subtree.
|
68
|
+
#
|
69
|
+
def parse(
|
70
|
+
input,
|
71
|
+
encoding_ = nil, positional_options_hash = nil,
|
72
|
+
encoding: encoding_, **options
|
73
|
+
)
|
74
|
+
unless positional_options_hash.nil? || positional_options_hash.empty?
|
75
|
+
options.merge!(positional_options_hash)
|
76
|
+
end
|
77
|
+
|
78
|
+
context = options.delete(:context)
|
79
|
+
|
80
|
+
document = HTML5::Document.new
|
81
|
+
document.encoding = "UTF-8"
|
82
|
+
input = HTML5.read_and_encode(input, encoding)
|
83
|
+
|
84
|
+
new(document, input, context, options)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
28
88
|
attr_accessor :document
|
29
89
|
attr_accessor :errors
|
30
90
|
|
31
91
|
# Get the parser's quirks mode value. See HTML5::QuirksMode.
|
32
92
|
#
|
33
|
-
# This method returns `nil` if the parser was not invoked (e.g.,
|
93
|
+
# This method returns `nil` if the parser was not invoked (e.g.,
|
94
|
+
# `Nokogiri::HTML5::DocumentFragment.new(doc)`).
|
34
95
|
#
|
35
96
|
# Since v1.14.0
|
36
97
|
attr_reader :quirks_mode
|
37
98
|
|
38
|
-
#
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
99
|
+
#
|
100
|
+
# :call-seq:
|
101
|
+
# new(document, input, **options) → HTML5::DocumentFragment
|
102
|
+
#
|
103
|
+
# Parse \HTML5 fragment input from a String, and return a new HTML5::DocumentFragment.
|
104
|
+
#
|
105
|
+
# 💡 It's recommended to use either HTML5::DocumentFragment.parse or HTML5::Node#fragment
|
106
|
+
# rather than call this method directly.
|
107
|
+
#
|
108
|
+
# [Required Parameters]
|
109
|
+
# - +document+ (HTML5::Document) The parent document to associate the returned fragment with.
|
110
|
+
#
|
111
|
+
# [Optional Parameters]
|
112
|
+
# - +input+ (String) The content to be parsed.
|
113
|
+
#
|
114
|
+
# [Optional Keyword Arguments]
|
115
|
+
# - +encoding:+ (String | Encoding) The encoding, or name of the encoding, that should be
|
116
|
+
# used when processing the document. When not provided, the encoding will be determined
|
117
|
+
# based on the document content. Also see Nokogiri::HTML5 for a longer explanation of how
|
118
|
+
# encoding is handled by the parser.
|
119
|
+
#
|
120
|
+
# - +context:+ (String | Nokogiri::XML::Node) The node, or the name of an HTML5 element, in
|
121
|
+
# which to parse the document fragment. (default +"body"+)
|
122
|
+
#
|
123
|
+
# - +max_errors:+ (Integer) The maximum number of parse errors to record. (default
|
124
|
+
# +Nokogiri::Gumbo::DEFAULT_MAX_ERRORS+ which is currently 0)
|
125
|
+
#
|
126
|
+
# - +max_tree_depth:+ (Integer) The maximum depth of the parse tree. (default
|
127
|
+
# +Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH+)
|
128
|
+
#
|
129
|
+
# - +max_attributes:+ (Integer) The maximum number of attributes allowed on an
|
130
|
+
# element. (default +Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES+)
|
131
|
+
#
|
132
|
+
# - +parse_noscript_content_as_text:+ (Boolean) Whether to parse the content of +noscript+
|
133
|
+
# elements as text. (default +false+)
|
134
|
+
#
|
135
|
+
# See rdoc-ref:HTML5@Parsing+options for a complete description of these parsing options.
|
136
|
+
#
|
137
|
+
# [Returns] HTML5::DocumentFragment
|
138
|
+
#
|
139
|
+
# === Context \Node
|
140
|
+
#
|
141
|
+
# If a context node is specified using +context:+, then the parser will behave as if that
|
142
|
+
# Node, or a hypothetical tag named as specified, is the parent of the fragment subtree.
|
143
|
+
#
|
144
|
+
def initialize(
|
145
|
+
doc, input = nil,
|
146
|
+
context_ = nil, positional_options_hash = nil,
|
147
|
+
context: context_,
|
148
|
+
**options
|
149
|
+
) # rubocop:disable Lint/MissingSuper
|
150
|
+
unless positional_options_hash.nil? || positional_options_hash.empty?
|
151
|
+
options.merge!(positional_options_hash)
|
152
|
+
end
|
153
|
+
|
154
|
+
@document = doc
|
155
|
+
@errors = []
|
156
|
+
return self unless input
|
157
|
+
|
158
|
+
input = Nokogiri::HTML5.read_and_encode(input, nil)
|
159
|
+
|
160
|
+
context = options.delete(:context) if options.key?(:context)
|
161
|
+
|
162
|
+
options[:max_attributes] ||= Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES
|
163
|
+
options[:max_errors] ||= options.delete(:max_parse_errors) || Nokogiri::Gumbo::DEFAULT_MAX_ERRORS
|
164
|
+
options[:max_tree_depth] ||= Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH
|
165
|
+
|
166
|
+
Nokogiri::Gumbo.fragment(self, input, context, **options)
|
49
167
|
end
|
50
168
|
|
51
169
|
def serialize(options = {}, &block) # :nodoc:
|
@@ -54,14 +172,6 @@ module Nokogiri
|
|
54
172
|
XML::Node.instance_method(:serialize).bind_call(self, options, &block)
|
55
173
|
end
|
56
174
|
|
57
|
-
# Parse a document fragment from +tags+, returning a Nodeset.
|
58
|
-
def self.parse(tags, encoding = nil, options = {})
|
59
|
-
doc = HTML5::Document.new
|
60
|
-
tags = HTML5.read_and_encode(tags, encoding)
|
61
|
-
doc.encoding = "UTF-8"
|
62
|
-
new(doc, tags, nil, options)
|
63
|
-
end
|
64
|
-
|
65
175
|
def extract_params(params) # :nodoc:
|
66
176
|
handler = params.find do |param|
|
67
177
|
![Hash, String, Symbol].include?(param.class)
|
data/lib/nokogiri/html5/node.rb
CHANGED
@@ -29,7 +29,7 @@ module Nokogiri
|
|
29
29
|
# 💡 HTML5 functionality is not available when running JRuby.
|
30
30
|
module Node
|
31
31
|
def inner_html(options = {})
|
32
|
-
return super
|
32
|
+
return super unless document.is_a?(HTML5::Document)
|
33
33
|
|
34
34
|
result = options[:preserve_newline] && prepend_newline? ? +"\n" : +""
|
35
35
|
result << children.map { |child| child.to_html(options) }.join
|
@@ -37,7 +37,7 @@ module Nokogiri
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def write_to(io, *options)
|
40
|
-
return super
|
40
|
+
return super unless document.is_a?(HTML5::Document)
|
41
41
|
|
42
42
|
options = options.first.is_a?(Hash) ? options.shift : {}
|
43
43
|
encoding = options[:encoding] || options[0]
|
@@ -68,7 +68,7 @@ module Nokogiri
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def fragment(tags)
|
71
|
-
return super
|
71
|
+
return super unless document.is_a?(HTML5::Document)
|
72
72
|
|
73
73
|
DocumentFragment.new(document, tags, self)
|
74
74
|
end
|
@@ -81,7 +81,7 @@ module Nokogiri
|
|
81
81
|
# annoying with attribute names like xml:lang since libxml2 will
|
82
82
|
# actually create the xml namespace if it doesn't exist already.
|
83
83
|
def add_child_node_and_reparent_attrs(node)
|
84
|
-
return super
|
84
|
+
return super unless document.is_a?(HTML5::Document)
|
85
85
|
|
86
86
|
# I'm not sure what this method is supposed to do. Reparenting
|
87
87
|
# namespaces is handled by libxml2, including child namespaces which
|
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
|