nokogiri 1.10.10 → 1.12.5
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 +3 -0
- data/LICENSE-DEPENDENCIES.md +1173 -884
- data/LICENSE.md +1 -1
- data/README.md +176 -96
- data/dependencies.yml +12 -12
- data/ext/nokogiri/depend +38 -358
- data/ext/nokogiri/extconf.rb +716 -414
- data/ext/nokogiri/gumbo.c +584 -0
- data/ext/nokogiri/html4_document.c +166 -0
- data/ext/nokogiri/html4_element_description.c +294 -0
- data/ext/nokogiri/html4_entity_lookup.c +37 -0
- data/ext/nokogiri/html4_sax_parser_context.c +120 -0
- data/ext/nokogiri/html4_sax_push_parser.c +95 -0
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +228 -91
- data/ext/nokogiri/nokogiri.h +191 -89
- data/ext/nokogiri/test_global_handlers.c +40 -0
- data/ext/nokogiri/xml_attr.c +15 -15
- data/ext/nokogiri/xml_attribute_decl.c +18 -18
- data/ext/nokogiri/xml_cdata.c +13 -18
- data/ext/nokogiri/xml_comment.c +19 -26
- data/ext/nokogiri/xml_document.c +267 -195
- data/ext/nokogiri/xml_document_fragment.c +13 -15
- data/ext/nokogiri/xml_dtd.c +54 -48
- data/ext/nokogiri/xml_element_content.c +31 -26
- data/ext/nokogiri/xml_element_decl.c +22 -22
- data/ext/nokogiri/xml_encoding_handler.c +28 -17
- data/ext/nokogiri/xml_entity_decl.c +32 -30
- data/ext/nokogiri/xml_entity_reference.c +16 -18
- data/ext/nokogiri/xml_namespace.c +60 -51
- data/ext/nokogiri/xml_node.c +493 -407
- data/ext/nokogiri/xml_node_set.c +174 -162
- data/ext/nokogiri/xml_processing_instruction.c +17 -19
- data/ext/nokogiri/xml_reader.c +197 -172
- data/ext/nokogiri/xml_relax_ng.c +52 -28
- data/ext/nokogiri/xml_sax_parser.c +112 -112
- data/ext/nokogiri/xml_sax_parser_context.c +105 -86
- data/ext/nokogiri/xml_sax_push_parser.c +36 -27
- data/ext/nokogiri/xml_schema.c +96 -46
- data/ext/nokogiri/xml_syntax_error.c +42 -21
- data/ext/nokogiri/xml_text.c +13 -17
- data/ext/nokogiri/xml_xpath_context.c +158 -73
- data/ext/nokogiri/xslt_stylesheet.c +158 -164
- data/gumbo-parser/CHANGES.md +63 -0
- data/gumbo-parser/Makefile +101 -0
- data/gumbo-parser/THANKS +27 -0
- data/gumbo-parser/src/Makefile +34 -0
- data/gumbo-parser/src/README.md +41 -0
- data/gumbo-parser/src/ascii.c +75 -0
- data/gumbo-parser/src/ascii.h +115 -0
- data/gumbo-parser/src/attribute.c +42 -0
- data/gumbo-parser/src/attribute.h +17 -0
- data/gumbo-parser/src/char_ref.c +22225 -0
- data/gumbo-parser/src/char_ref.h +29 -0
- data/gumbo-parser/src/char_ref.rl +2154 -0
- data/gumbo-parser/src/error.c +626 -0
- data/gumbo-parser/src/error.h +148 -0
- data/gumbo-parser/src/foreign_attrs.c +104 -0
- data/gumbo-parser/src/foreign_attrs.gperf +27 -0
- data/gumbo-parser/src/gumbo.h +943 -0
- data/gumbo-parser/src/insertion_mode.h +33 -0
- data/gumbo-parser/src/macros.h +91 -0
- data/gumbo-parser/src/parser.c +4886 -0
- data/gumbo-parser/src/parser.h +41 -0
- data/gumbo-parser/src/replacement.h +33 -0
- data/gumbo-parser/src/string_buffer.c +103 -0
- data/gumbo-parser/src/string_buffer.h +68 -0
- data/gumbo-parser/src/string_piece.c +48 -0
- data/gumbo-parser/src/svg_attrs.c +174 -0
- data/gumbo-parser/src/svg_attrs.gperf +77 -0
- data/gumbo-parser/src/svg_tags.c +137 -0
- data/gumbo-parser/src/svg_tags.gperf +55 -0
- data/gumbo-parser/src/tag.c +222 -0
- data/gumbo-parser/src/tag_lookup.c +382 -0
- data/gumbo-parser/src/tag_lookup.gperf +169 -0
- data/gumbo-parser/src/tag_lookup.h +13 -0
- data/gumbo-parser/src/token_buffer.c +79 -0
- data/gumbo-parser/src/token_buffer.h +71 -0
- data/gumbo-parser/src/token_type.h +17 -0
- data/gumbo-parser/src/tokenizer.c +3463 -0
- data/gumbo-parser/src/tokenizer.h +112 -0
- data/gumbo-parser/src/tokenizer_states.h +339 -0
- data/gumbo-parser/src/utf8.c +245 -0
- data/gumbo-parser/src/utf8.h +164 -0
- data/gumbo-parser/src/util.c +68 -0
- data/gumbo-parser/src/util.h +30 -0
- data/gumbo-parser/src/vector.c +111 -0
- data/gumbo-parser/src/vector.h +45 -0
- data/lib/nokogiri/css/node.rb +1 -0
- data/lib/nokogiri/css/parser.rb +64 -63
- data/lib/nokogiri/css/parser.y +3 -3
- data/lib/nokogiri/css/parser_extras.rb +39 -36
- data/lib/nokogiri/css/syntax_error.rb +2 -1
- data/lib/nokogiri/css/tokenizer.rb +1 -0
- data/lib/nokogiri/css/xpath_visitor.rb +73 -43
- data/lib/nokogiri/css.rb +15 -14
- data/lib/nokogiri/decorators/slop.rb +1 -0
- data/lib/nokogiri/extension.rb +31 -0
- data/lib/nokogiri/gumbo.rb +14 -0
- data/lib/nokogiri/html.rb +32 -27
- data/lib/nokogiri/{html → html4}/builder.rb +3 -2
- data/lib/nokogiri/{html → html4}/document.rb +17 -30
- data/lib/nokogiri/{html → html4}/document_fragment.rb +18 -17
- data/lib/nokogiri/{html → html4}/element_description.rb +2 -1
- data/lib/nokogiri/{html → html4}/element_description_defaults.rb +2 -1
- data/lib/nokogiri/{html → html4}/entity_lookup.rb +2 -1
- data/lib/nokogiri/{html → html4}/sax/parser.rb +12 -14
- data/lib/nokogiri/html4/sax/parser_context.rb +19 -0
- data/lib/nokogiri/{html → html4}/sax/push_parser.rb +6 -5
- data/lib/nokogiri/html4.rb +40 -0
- data/lib/nokogiri/html5/document.rb +74 -0
- data/lib/nokogiri/html5/document_fragment.rb +80 -0
- data/lib/nokogiri/html5/node.rb +93 -0
- data/lib/nokogiri/html5.rb +473 -0
- data/lib/nokogiri/jruby/dependencies.rb +20 -0
- data/lib/nokogiri/syntax_error.rb +1 -0
- data/lib/nokogiri/version/constant.rb +5 -0
- data/lib/nokogiri/version/info.rb +215 -0
- data/lib/nokogiri/version.rb +3 -109
- data/lib/nokogiri/xml/attr.rb +1 -0
- data/lib/nokogiri/xml/attribute_decl.rb +1 -0
- data/lib/nokogiri/xml/builder.rb +41 -2
- data/lib/nokogiri/xml/cdata.rb +1 -0
- data/lib/nokogiri/xml/character_data.rb +1 -0
- data/lib/nokogiri/xml/document.rb +138 -41
- data/lib/nokogiri/xml/document_fragment.rb +5 -6
- data/lib/nokogiri/xml/dtd.rb +1 -0
- data/lib/nokogiri/xml/element_content.rb +1 -0
- data/lib/nokogiri/xml/element_decl.rb +1 -0
- data/lib/nokogiri/xml/entity_decl.rb +1 -0
- data/lib/nokogiri/xml/entity_reference.rb +1 -0
- data/lib/nokogiri/xml/namespace.rb +1 -0
- data/lib/nokogiri/xml/node/save_options.rb +2 -1
- data/lib/nokogiri/xml/node.rb +629 -293
- data/lib/nokogiri/xml/node_set.rb +1 -0
- data/lib/nokogiri/xml/notation.rb +1 -0
- data/lib/nokogiri/xml/parse_options.rb +12 -3
- data/lib/nokogiri/xml/pp/character_data.rb +1 -0
- data/lib/nokogiri/xml/pp/node.rb +1 -0
- data/lib/nokogiri/xml/pp.rb +3 -2
- data/lib/nokogiri/xml/processing_instruction.rb +1 -0
- data/lib/nokogiri/xml/reader.rb +9 -12
- data/lib/nokogiri/xml/relax_ng.rb +7 -2
- data/lib/nokogiri/xml/sax/document.rb +25 -30
- data/lib/nokogiri/xml/sax/parser.rb +1 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +1 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +1 -0
- data/lib/nokogiri/xml/sax.rb +5 -4
- data/lib/nokogiri/xml/schema.rb +13 -4
- data/lib/nokogiri/xml/searchable.rb +25 -16
- data/lib/nokogiri/xml/syntax_error.rb +1 -0
- data/lib/nokogiri/xml/text.rb +1 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +2 -1
- data/lib/nokogiri/xml/xpath.rb +4 -5
- data/lib/nokogiri/xml/xpath_context.rb +1 -0
- data/lib/nokogiri/xml.rb +36 -36
- data/lib/nokogiri/xslt/stylesheet.rb +2 -1
- data/lib/nokogiri/xslt.rb +17 -16
- data/lib/nokogiri.rb +32 -51
- data/lib/xsd/xmlparser/nokogiri.rb +1 -0
- data/patches/libxml2/{0002-Remove-script-macro-support.patch → 0001-Remove-script-macro-support.patch} +0 -0
- data/patches/libxml2/{0003-Update-entities-to-remove-handling-of-ssi.patch → 0002-Update-entities-to-remove-handling-of-ssi.patch} +0 -0
- data/patches/libxml2/{0004-libxml2.la-is-in-top_builddir.patch → 0003-libxml2.la-is-in-top_builddir.patch} +1 -1
- data/patches/libxml2/0004-use-glibc-strlen.patch +53 -0
- data/patches/libxml2/0005-avoid-isnan-isinf.patch +81 -0
- data/patches/libxml2/0006-update-automake-files-for-arm64.patch +2511 -0
- data/patches/libxml2/0007-Fix-XPath-recursion-limit.patch +31 -0
- data/patches/libxslt/0001-update-automake-files-for-arm64.patch +2511 -0
- data/patches/libxslt/0002-Fix-xml2-config-check-in-configure-script.patch +19 -0
- data/ports/archives/libxml2-2.9.12.tar.gz +0 -0
- metadata +139 -161
- data/ext/nokogiri/html_document.c +0 -170
- data/ext/nokogiri/html_document.h +0 -10
- data/ext/nokogiri/html_element_description.c +0 -279
- data/ext/nokogiri/html_element_description.h +0 -10
- data/ext/nokogiri/html_entity_lookup.c +0 -32
- data/ext/nokogiri/html_entity_lookup.h +0 -8
- data/ext/nokogiri/html_sax_parser_context.c +0 -116
- data/ext/nokogiri/html_sax_parser_context.h +0 -11
- data/ext/nokogiri/html_sax_push_parser.c +0 -87
- data/ext/nokogiri/html_sax_push_parser.h +0 -9
- data/ext/nokogiri/xml_attr.h +0 -9
- data/ext/nokogiri/xml_attribute_decl.h +0 -9
- data/ext/nokogiri/xml_cdata.h +0 -9
- data/ext/nokogiri/xml_comment.h +0 -9
- data/ext/nokogiri/xml_document.h +0 -23
- data/ext/nokogiri/xml_document_fragment.h +0 -10
- data/ext/nokogiri/xml_dtd.h +0 -10
- data/ext/nokogiri/xml_element_content.h +0 -10
- data/ext/nokogiri/xml_element_decl.h +0 -9
- data/ext/nokogiri/xml_encoding_handler.h +0 -8
- data/ext/nokogiri/xml_entity_decl.h +0 -10
- data/ext/nokogiri/xml_entity_reference.h +0 -9
- data/ext/nokogiri/xml_io.c +0 -61
- data/ext/nokogiri/xml_io.h +0 -11
- data/ext/nokogiri/xml_libxml2_hacks.c +0 -112
- data/ext/nokogiri/xml_libxml2_hacks.h +0 -12
- data/ext/nokogiri/xml_namespace.h +0 -14
- data/ext/nokogiri/xml_node.h +0 -13
- data/ext/nokogiri/xml_node_set.h +0 -12
- data/ext/nokogiri/xml_processing_instruction.h +0 -9
- data/ext/nokogiri/xml_reader.h +0 -10
- data/ext/nokogiri/xml_relax_ng.h +0 -9
- data/ext/nokogiri/xml_sax_parser.h +0 -39
- data/ext/nokogiri/xml_sax_parser_context.h +0 -10
- data/ext/nokogiri/xml_sax_push_parser.h +0 -9
- data/ext/nokogiri/xml_schema.h +0 -9
- data/ext/nokogiri/xml_syntax_error.h +0 -13
- data/ext/nokogiri/xml_text.h +0 -9
- data/ext/nokogiri/xml_xpath_context.h +0 -10
- data/ext/nokogiri/xslt_stylesheet.h +0 -14
- data/lib/nokogiri/html/sax/parser_context.rb +0 -16
- data/patches/libxml2/0001-Revert-Do-not-URI-escape-in-server-side-includes.patch +0 -78
- data/patches/libxml2/0005-Fix-infinite-loop-in-xmlStringLenDecodeEntities.patch +0 -32
- data/ports/archives/libxml2-2.9.10.tar.gz +0 -0
@@ -0,0 +1,473 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# Copyright 2013-2021 Sam Ruby, Stephen Checkoway
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
#
|
18
|
+
|
19
|
+
require_relative 'html5/document'
|
20
|
+
require_relative 'html5/document_fragment'
|
21
|
+
require_relative 'html5/node'
|
22
|
+
|
23
|
+
module Nokogiri
|
24
|
+
# @since v1.12.0
|
25
|
+
# @note HTML5 functionality is not available when running JRuby.
|
26
|
+
# Parse an HTML5 document. Convenience method for {Nokogiri::HTML5::Document.parse}
|
27
|
+
def self.HTML5(input, url = nil, encoding = nil, **options, &block)
|
28
|
+
Nokogiri::HTML5::Document.parse(input, url, encoding, **options, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
# == Usage
|
32
|
+
#
|
33
|
+
# Parse an HTML5 document:
|
34
|
+
#
|
35
|
+
# doc = Nokogiri.HTML5(string)
|
36
|
+
#
|
37
|
+
# Parse an HTML5 fragment:
|
38
|
+
#
|
39
|
+
# fragment = Nokogiri::HTML5.fragment(string)
|
40
|
+
#
|
41
|
+
# == Parsing options
|
42
|
+
#
|
43
|
+
# The document and fragment parsing methods support options that are different from Nokogiri's.
|
44
|
+
#
|
45
|
+
# - <tt>Nokogiri.HTML5(html, url = nil, encoding = nil, options = {})</tt>
|
46
|
+
# - <tt>Nokogiri::HTML5.parse(html, url = nil, encoding = nil, options = {})</tt>
|
47
|
+
# - <tt>Nokogiri::HTML5::Document.parse(html, url = nil, encoding = nil, options = {})</tt>
|
48
|
+
# - <tt>Nokogiri::HTML5.fragment(html, encoding = nil, options = {})</tt>
|
49
|
+
# - <tt>Nokogiri::HTML5::DocumentFragment.parse(html, encoding = nil, options = {})</tt>
|
50
|
+
#
|
51
|
+
# The three currently supported options are +:max_errors+, +:max_tree_depth+ and
|
52
|
+
# +:max_attributes+, described below.
|
53
|
+
#
|
54
|
+
# === Error reporting
|
55
|
+
#
|
56
|
+
# Nokogiri contains an experimental HTML5 parse error reporting facility. By default, no parse
|
57
|
+
# errors are reported but this can be configured by passing the +:max_errors+ option to
|
58
|
+
# {HTML5.parse} or {HTML5.fragment}.
|
59
|
+
#
|
60
|
+
# For example, this script:
|
61
|
+
#
|
62
|
+
# doc = Nokogiri::HTML5.parse('<span/>Hi there!</span foo=bar />', max_errors: 10)
|
63
|
+
# doc.errors.each do |err|
|
64
|
+
# puts(err)
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# Emits:
|
68
|
+
#
|
69
|
+
# 1:1: ERROR: Expected a doctype token
|
70
|
+
# <span/>Hi there!</span foo=bar />
|
71
|
+
# ^
|
72
|
+
# 1:1: ERROR: Start tag of nonvoid HTML element ends with '/>', use '>'.
|
73
|
+
# <span/>Hi there!</span foo=bar />
|
74
|
+
# ^
|
75
|
+
# 1:17: ERROR: End tag ends with '/>', use '>'.
|
76
|
+
# <span/>Hi there!</span foo=bar />
|
77
|
+
# ^
|
78
|
+
# 1:17: ERROR: End tag contains attributes.
|
79
|
+
# <span/>Hi there!</span foo=bar />
|
80
|
+
# ^
|
81
|
+
#
|
82
|
+
# Using <tt>max_errors: -1</tt> results in an unlimited number of errors being returned.
|
83
|
+
#
|
84
|
+
# The errors returned by {HTML5::Document#errors} are instances of {Nokogiri::XML::SyntaxError}.
|
85
|
+
#
|
86
|
+
# The {https://html.spec.whatwg.org/multipage/parsing.html#parse-errors HTML standard} defines a
|
87
|
+
# number of standard parse error codes. These error codes only cover the "tokenization" stage of
|
88
|
+
# parsing HTML. The parse errors in the "tree construction" stage do not have standardized error
|
89
|
+
# codes (yet).
|
90
|
+
#
|
91
|
+
# As a convenience to Nokogiri users, the defined error codes are available via
|
92
|
+
# {Nokogiri::XML::SyntaxError#str1} method.
|
93
|
+
#
|
94
|
+
# doc = Nokogiri::HTML5.parse('<span/>Hi there!</span foo=bar />', max_errors: 10)
|
95
|
+
# doc.errors.each do |err|
|
96
|
+
# puts("#{err.line}:#{err.column}: #{err.str1}")
|
97
|
+
# end
|
98
|
+
# # => 1:1: generic-parser
|
99
|
+
# # 1:1: non-void-html-element-start-tag-with-trailing-solidus
|
100
|
+
# # 1:17: end-tag-with-trailing-solidus
|
101
|
+
# # 1:17: end-tag-with-attributes
|
102
|
+
#
|
103
|
+
# Note that the first error is +generic-parser+ because it's an error from the tree construction
|
104
|
+
# stage and doesn't have a standardized error code.
|
105
|
+
#
|
106
|
+
# For the purposes of semantic versioning, the error messages, error locations, and error codes
|
107
|
+
# are not part of Nokogiri's public API. That is, these are subject to change without Nokogiri's
|
108
|
+
# major version number changing. These may be stabilized in the future.
|
109
|
+
#
|
110
|
+
# === Maximum tree depth
|
111
|
+
#
|
112
|
+
# The maximum depth of the DOM tree parsed by the various parsing methods is configurable by the
|
113
|
+
# +:max_tree_depth+ option. If the depth of the tree would exceed this limit, then an
|
114
|
+
# {::ArgumentError} is thrown.
|
115
|
+
#
|
116
|
+
# This limit (which defaults to <tt>Nokogiri::Gumbo::DEFAULT_MAX_TREE_DEPTH = 400</tt>) can be
|
117
|
+
# removed by giving the option <tt>max_tree_depth: -1</tt>.
|
118
|
+
#
|
119
|
+
# html = '<!DOCTYPE html>' + '<div>' * 1000
|
120
|
+
# doc = Nokogiri.HTML5(html)
|
121
|
+
# # raises ArgumentError: Document tree depth limit exceeded
|
122
|
+
# doc = Nokogiri.HTML5(html, max_tree_depth: -1)
|
123
|
+
#
|
124
|
+
# === Attribute limit per element
|
125
|
+
#
|
126
|
+
# The maximum number of attributes per DOM element is configurable by the +:max_attributes+
|
127
|
+
# option. If a given element would exceed this limit, then an {::ArgumentError} is thrown.
|
128
|
+
#
|
129
|
+
# This limit (which defaults to <tt>Nokogiri::Gumbo::DEFAULT_MAX_ATTRIBUTES = 400</tt>) can be
|
130
|
+
# removed by giving the option <tt>max_attributes: -1</tt>.
|
131
|
+
#
|
132
|
+
# html = '<!DOCTYPE html><div ' + (1..1000).map { |x| "attr-#{x}" }.join(' ') + '>'
|
133
|
+
# # "<!DOCTYPE html><div attr-1 attr-2 attr-3 ... attr-1000>"
|
134
|
+
# doc = Nokogiri.HTML5(html)
|
135
|
+
# # raises ArgumentError: Attributes per element limit exceeded
|
136
|
+
# doc = Nokogiri.HTML5(html, max_attributes: -1)
|
137
|
+
#
|
138
|
+
# == HTML Serialization
|
139
|
+
#
|
140
|
+
# After parsing HTML, it may be serialized using any of the {Nokogiri::XML::Node} serialization
|
141
|
+
# methods. In particular, {XML::Node#serialize}, {XML::Node#to_html}, and {XML::Node#to_s} will
|
142
|
+
# serialize a given node and its children. (This is the equivalent of JavaScript's
|
143
|
+
# +Element.outerHTML+.) Similarly, {XML::Node#inner_html} will serialize the children of a given
|
144
|
+
# node. (This is the equivalent of JavaScript's +Element.innerHTML+.)
|
145
|
+
#
|
146
|
+
# doc = Nokogiri::HTML5("<!DOCTYPE html><span>Hello world!</span>")
|
147
|
+
# puts doc.serialize
|
148
|
+
# # => <!DOCTYPE html><html><head></head><body><span>Hello world!</span></body></html>
|
149
|
+
#
|
150
|
+
# Due to quirks in how HTML is parsed and serialized, it's possible for a DOM tree to be
|
151
|
+
# serialized and then re-parsed, resulting in a different DOM. Mostly, this happens with DOMs
|
152
|
+
# produced from invalid HTML. Unfortunately, even valid HTML may not survive serialization and
|
153
|
+
# re-parsing.
|
154
|
+
#
|
155
|
+
# In particular, a newline at the start of +pre+, +listing+, and +textarea+ elements is ignored by
|
156
|
+
# the parser.
|
157
|
+
#
|
158
|
+
# doc = Nokogiri::HTML5(<<-EOF)
|
159
|
+
# <!DOCTYPE html>
|
160
|
+
# <pre>
|
161
|
+
# Content</pre>
|
162
|
+
# EOF
|
163
|
+
# puts doc.at('/html/body/pre').serialize
|
164
|
+
# # => <pre>Content</pre>
|
165
|
+
#
|
166
|
+
# In this case, the original HTML is semantically equivalent to the serialized version. If the
|
167
|
+
# +pre+, +listing+, or +textarea+ content starts with two newlines, the first newline will be
|
168
|
+
# stripped on the first parse and the second newline will be stripped on the second, leading to
|
169
|
+
# semantically different DOMs. Passing the parameter <tt>preserve_newline: true</tt> will cause
|
170
|
+
# two or more newlines to be preserved. (A single leading newline will still be removed.)
|
171
|
+
#
|
172
|
+
# doc = Nokogiri::HTML5(<<-EOF)
|
173
|
+
# <!DOCTYPE html>
|
174
|
+
# <listing>
|
175
|
+
#
|
176
|
+
# Content</listing>
|
177
|
+
# EOF
|
178
|
+
# puts doc.at('/html/body/listing').serialize(preserve_newline: true)
|
179
|
+
# # => <listing>
|
180
|
+
# #
|
181
|
+
# # Content</listing>
|
182
|
+
#
|
183
|
+
# == Encodings
|
184
|
+
#
|
185
|
+
# Nokogiri always parses HTML5 using {https://en.wikipedia.org/wiki/UTF-8 UTF-8}; however, the
|
186
|
+
# encoding of the input can be explicitly selected via the optional +encoding+ parameter. This is
|
187
|
+
# most useful when the input comes not from a string but from an IO object.
|
188
|
+
#
|
189
|
+
# When serializing a document or node, the encoding of the output string can be specified via the
|
190
|
+
# +:encoding+ options. Characters that cannot be encoded in the selected encoding will be encoded
|
191
|
+
# as {https://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references HTML numeric
|
192
|
+
# entities}.
|
193
|
+
#
|
194
|
+
# frag = Nokogiri::HTML5.fragment('<span>아는 길도 물어가라</span>')
|
195
|
+
# html = frag.serialize(encoding: 'US-ASCII')
|
196
|
+
# puts html
|
197
|
+
# # => <span>아는 길도 물어가라</span>
|
198
|
+
# frag = Nokogiri::HTML5.fragment(html)
|
199
|
+
# puts frag.serialize
|
200
|
+
# # => <span>아는 길도 물어가라</span>
|
201
|
+
#
|
202
|
+
# (There's a {https://bugs.ruby-lang.org/issues/15033 bug} in all current versions of Ruby that
|
203
|
+
# can cause the entity encoding to fail. Of the mandated supported encodings for HTML, the only
|
204
|
+
# encoding I'm aware of that has this bug is <tt>'ISO-2022-JP'</tt>. We recommend avoiding this
|
205
|
+
# encoding.)
|
206
|
+
#
|
207
|
+
# == Notes
|
208
|
+
#
|
209
|
+
# * The {Nokogiri::HTML5.fragment} function takes a string and parses it
|
210
|
+
# as a HTML5 document. The +<html>+, +<head>+, and +<body>+ elements are
|
211
|
+
# removed from this document, and any children of these elements that remain
|
212
|
+
# are returned as a {Nokogiri::HTML5::DocumentFragment}.
|
213
|
+
#
|
214
|
+
# * The {Nokogiri::HTML5.parse} function takes a string and passes it to the
|
215
|
+
# <code>gumbo_parse_with_options</code> method, using the default options.
|
216
|
+
# The resulting Gumbo parse tree is then walked.
|
217
|
+
#
|
218
|
+
# * Instead of uppercase element names, lowercase element names are produced.
|
219
|
+
#
|
220
|
+
# * Instead of returning +unknown+ as the element name for unknown tags, the
|
221
|
+
# original tag name is returned verbatim.
|
222
|
+
#
|
223
|
+
# @since v1.12.0
|
224
|
+
# @note HTML5 functionality is not available when running JRuby.
|
225
|
+
module HTML5
|
226
|
+
# HTML uses the XHTML namespace.
|
227
|
+
HTML_NAMESPACE = 'http://www.w3.org/1999/xhtml'.freeze
|
228
|
+
MATHML_NAMESPACE = 'http://www.w3.org/1998/Math/MathML'.freeze
|
229
|
+
SVG_NAMESPACE = 'http://www.w3.org/2000/svg'.freeze
|
230
|
+
XLINK_NAMESPACE = 'http://www.w3.org/1999/xlink'.freeze
|
231
|
+
XML_NAMESPACE = 'http://www.w3.org/XML/1998/namespace'.freeze
|
232
|
+
XMLNS_NAMESPACE = 'http://www.w3.org/2000/xmlns/'.freeze
|
233
|
+
|
234
|
+
# Parse an HTML 5 document. Convenience method for {Nokogiri::HTML5::Document.parse}
|
235
|
+
def self.parse(string, url = nil, encoding = nil, **options, &block)
|
236
|
+
Document.parse(string, url, encoding, **options, &block)
|
237
|
+
end
|
238
|
+
|
239
|
+
# Parse a fragment from +string+. Convenience method for
|
240
|
+
# {Nokogiri::HTML5::DocumentFragment.parse}.
|
241
|
+
def self.fragment(string, encoding = nil, **options)
|
242
|
+
DocumentFragment.parse(string, encoding, options)
|
243
|
+
end
|
244
|
+
|
245
|
+
# Fetch and parse a HTML document from the web, following redirects,
|
246
|
+
# handling https, and determining the character encoding using HTML5
|
247
|
+
# rules. +uri+ may be a +String+ or a +URI+. +options+ contains
|
248
|
+
# http headers and special options. Everything which is not a
|
249
|
+
# special option is considered a header. Special options include:
|
250
|
+
# * :follow_limit => number of redirects which are followed
|
251
|
+
# * :basic_auth => [username, password]
|
252
|
+
def self.get(uri, options={})
|
253
|
+
warn("Nokogiri::HTML5.get is deprecated and will be removed in a future version of Nokogiri.",
|
254
|
+
uplevel: 1, category: :deprecated)
|
255
|
+
get_impl(uri, options)
|
256
|
+
end
|
257
|
+
|
258
|
+
private
|
259
|
+
|
260
|
+
def self.get_impl(uri, options={})
|
261
|
+
headers = options.clone
|
262
|
+
headers = {:follow_limit => headers} if Numeric === headers # deprecated
|
263
|
+
limit=headers[:follow_limit] ? headers.delete(:follow_limit).to_i : 10
|
264
|
+
|
265
|
+
require 'net/http'
|
266
|
+
uri = URI(uri) unless URI === uri
|
267
|
+
|
268
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
269
|
+
|
270
|
+
# TLS / SSL support
|
271
|
+
http.use_ssl = true if uri.scheme == 'https'
|
272
|
+
|
273
|
+
# Pass through Net::HTTP override values, which currently include:
|
274
|
+
# :ca_file, :ca_path, :cert, :cert_store, :ciphers,
|
275
|
+
# :close_on_empty_response, :continue_timeout, :key, :open_timeout,
|
276
|
+
# :read_timeout, :ssl_timeout, :ssl_version, :use_ssl,
|
277
|
+
# :verify_callback, :verify_depth, :verify_mode
|
278
|
+
options.each do |key, value|
|
279
|
+
http.send "#{key}=", headers.delete(key) if http.respond_to? "#{key}="
|
280
|
+
end
|
281
|
+
|
282
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
283
|
+
|
284
|
+
# basic authentication
|
285
|
+
auth = headers.delete(:basic_auth)
|
286
|
+
auth ||= [uri.user, uri.password] if uri.user && uri.password
|
287
|
+
request.basic_auth auth.first, auth.last if auth
|
288
|
+
|
289
|
+
# remaining options are treated as headers
|
290
|
+
headers.each {|key, value| request[key.to_s] = value.to_s}
|
291
|
+
|
292
|
+
response = http.request(request)
|
293
|
+
|
294
|
+
case response
|
295
|
+
when Net::HTTPSuccess
|
296
|
+
doc = parse(reencode(response.body, response['content-type']), options)
|
297
|
+
doc.instance_variable_set('@response', response)
|
298
|
+
doc.class.send(:attr_reader, :response)
|
299
|
+
doc
|
300
|
+
when Net::HTTPRedirection
|
301
|
+
response.value if limit <= 1
|
302
|
+
location = URI.join(uri, response['location'])
|
303
|
+
get_impl(location, options.merge(:follow_limit => limit-1))
|
304
|
+
else
|
305
|
+
response.value
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
def self.read_and_encode(string, encoding)
|
310
|
+
# Read the string with the given encoding.
|
311
|
+
if string.respond_to?(:read)
|
312
|
+
if encoding.nil?
|
313
|
+
string = string.read
|
314
|
+
else
|
315
|
+
string = string.read(encoding: encoding)
|
316
|
+
end
|
317
|
+
else
|
318
|
+
# Otherwise the string has the given encoding.
|
319
|
+
string = string.to_s
|
320
|
+
if encoding
|
321
|
+
string = string.dup
|
322
|
+
string.force_encoding(encoding)
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
# convert to UTF-8
|
327
|
+
if string.encoding != Encoding::UTF_8
|
328
|
+
string = reencode(string)
|
329
|
+
end
|
330
|
+
string
|
331
|
+
end
|
332
|
+
|
333
|
+
# Charset sniffing is a complex and controversial topic that understandably isn't done _by
|
334
|
+
# default_ by the Ruby Net::HTTP library. This being said, it is a very real problem for
|
335
|
+
# consumers of HTML as the default for HTML is iso-8859-1, most "good" producers use utf-8, and
|
336
|
+
# the Gumbo parser *only* supports utf-8.
|
337
|
+
#
|
338
|
+
# Accordingly, Nokogiri::HTML4::Document.parse provides limited encoding detection. Following
|
339
|
+
# this lead, Nokogiri::HTML5 attempts to do likewise, while attempting to more closely follow
|
340
|
+
# the HTML5 standard.
|
341
|
+
#
|
342
|
+
# http://bugs.ruby-lang.org/issues/2567
|
343
|
+
# http://www.w3.org/TR/html5/syntax.html#determining-the-character-encoding
|
344
|
+
#
|
345
|
+
def self.reencode(body, content_type=nil)
|
346
|
+
if body.encoding == Encoding::ASCII_8BIT
|
347
|
+
encoding = nil
|
348
|
+
|
349
|
+
# look for a Byte Order Mark (BOM)
|
350
|
+
initial_bytes = body[0..2].bytes
|
351
|
+
if initial_bytes[0..2] == [0xEF, 0xBB, 0xBF]
|
352
|
+
encoding = Encoding::UTF_8
|
353
|
+
elsif initial_bytes[0..1] == [0xFE, 0xFF]
|
354
|
+
encoding = Encoding::UTF_16BE
|
355
|
+
elsif initial_bytes[0..1] == [0xFF, 0xFE]
|
356
|
+
encoding = Encoding::UTF_16LE
|
357
|
+
end
|
358
|
+
|
359
|
+
# look for a charset in a content-encoding header
|
360
|
+
if content_type
|
361
|
+
encoding ||= content_type[/charset=["']?(.*?)($|["';\s])/i, 1]
|
362
|
+
end
|
363
|
+
|
364
|
+
# look for a charset in a meta tag in the first 1024 bytes
|
365
|
+
if not encoding
|
366
|
+
data = body[0..1023].gsub(/<!--.*?(-->|\Z)/m, '')
|
367
|
+
data.scan(/<meta.*?>/m).each do |meta|
|
368
|
+
encoding ||= meta[/charset=["']?([^>]*?)($|["'\s>])/im, 1]
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
# if all else fails, default to the official default encoding for HTML
|
373
|
+
encoding ||= Encoding::ISO_8859_1
|
374
|
+
|
375
|
+
# change the encoding to match the detected or inferred encoding
|
376
|
+
body = body.dup
|
377
|
+
begin
|
378
|
+
body.force_encoding(encoding)
|
379
|
+
rescue ArgumentError
|
380
|
+
body.force_encoding(Encoding::ISO_8859_1)
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
body.encode(Encoding::UTF_8)
|
385
|
+
end
|
386
|
+
|
387
|
+
def self.serialize_node_internal(current_node, io, encoding, options)
|
388
|
+
case current_node.type
|
389
|
+
when XML::Node::ELEMENT_NODE
|
390
|
+
ns = current_node.namespace
|
391
|
+
ns_uri = ns.nil? ? nil : ns.href
|
392
|
+
# XXX(sfc): attach namespaces to all nodes, even html?
|
393
|
+
if ns_uri.nil? || ns_uri == HTML_NAMESPACE || ns_uri == MATHML_NAMESPACE || ns_uri == SVG_NAMESPACE
|
394
|
+
tagname = current_node.name
|
395
|
+
else
|
396
|
+
tagname = "#{ns.prefix}:#{current_node.name}"
|
397
|
+
end
|
398
|
+
io << '<' << tagname
|
399
|
+
current_node.attribute_nodes.each do |attr|
|
400
|
+
attr_ns = attr.namespace
|
401
|
+
if attr_ns.nil?
|
402
|
+
attr_name = attr.name
|
403
|
+
else
|
404
|
+
ns_uri = attr_ns.href
|
405
|
+
if ns_uri == XML_NAMESPACE
|
406
|
+
attr_name = 'xml:' + attr.name.sub(/^[^:]*:/, '')
|
407
|
+
elsif ns_uri == XMLNS_NAMESPACE && attr.name.sub(/^[^:]*:/, '') == 'xmlns'
|
408
|
+
attr_name = 'xmlns'
|
409
|
+
elsif ns_uri == XMLNS_NAMESPACE
|
410
|
+
attr_name = 'xmlns:' + attr.name.sub(/^[^:]*:/, '')
|
411
|
+
elsif ns_uri == XLINK_NAMESPACE
|
412
|
+
attr_name = 'xlink:' + attr.name.sub(/^[^:]*:/, '')
|
413
|
+
else
|
414
|
+
attr_name = "#{attr_ns.prefix}:#{attr.name}"
|
415
|
+
end
|
416
|
+
end
|
417
|
+
io << ' ' << attr_name << '="' << escape_text(attr.content, encoding, true) << '"'
|
418
|
+
end
|
419
|
+
io << '>'
|
420
|
+
if !%w[area base basefont bgsound br col embed frame hr img input keygen
|
421
|
+
link meta param source track wbr].include?(current_node.name)
|
422
|
+
io << "\n" if options[:preserve_newline] && prepend_newline?(current_node)
|
423
|
+
current_node.children.each do |child|
|
424
|
+
# XXX(sfc): Templates handled specially?
|
425
|
+
serialize_node_internal(child, io, encoding, options)
|
426
|
+
end
|
427
|
+
io << '</' << tagname << '>'
|
428
|
+
end
|
429
|
+
when XML::Node::TEXT_NODE
|
430
|
+
parent = current_node.parent
|
431
|
+
if parent.element? && %w[style script xmp iframe noembed noframes plaintext noscript].include?(parent.name)
|
432
|
+
io << current_node.content
|
433
|
+
else
|
434
|
+
io << escape_text(current_node.content, encoding, false)
|
435
|
+
end
|
436
|
+
when XML::Node::CDATA_SECTION_NODE
|
437
|
+
io << '<![CDATA[' << current_node.content << ']]>'
|
438
|
+
when XML::Node::COMMENT_NODE
|
439
|
+
io << '<!--' << current_node.content << '-->'
|
440
|
+
when XML::Node::PI_NODE
|
441
|
+
io << '<?' << current_node.content << '>'
|
442
|
+
when XML::Node::DOCUMENT_TYPE_NODE, XML::Node::DTD_NODE
|
443
|
+
io << '<!DOCTYPE ' << current_node.name << '>'
|
444
|
+
when XML::Node::HTML_DOCUMENT_NODE, XML::Node::DOCUMENT_FRAG_NODE
|
445
|
+
current_node.children.each do |child|
|
446
|
+
serialize_node_internal(child, io, encoding, options)
|
447
|
+
end
|
448
|
+
else
|
449
|
+
raise "Unexpected node '#{current_node.name}' of type #{current_node.type}"
|
450
|
+
end
|
451
|
+
end
|
452
|
+
|
453
|
+
def self.escape_text(text, encoding, attribute_mode)
|
454
|
+
if attribute_mode
|
455
|
+
text = text.gsub(/[&\u00a0"]/,
|
456
|
+
'&' => '&', "\u00a0" => ' ', '"' => '"')
|
457
|
+
else
|
458
|
+
text = text.gsub(/[&\u00a0<>]/,
|
459
|
+
'&' => '&', "\u00a0" => ' ', '<' => '<', '>' => '>')
|
460
|
+
end
|
461
|
+
# Not part of the standard
|
462
|
+
text.encode(encoding, fallback: lambda { |c| "&\#x#{c.ord.to_s(16)};" })
|
463
|
+
end
|
464
|
+
|
465
|
+
def self.prepend_newline?(node)
|
466
|
+
return false unless %w[pre textarea listing].include?(node.name) && !node.children.empty?
|
467
|
+
first_child = node.children[0]
|
468
|
+
first_child.text? && first_child.content.start_with?("\n")
|
469
|
+
end
|
470
|
+
end
|
471
|
+
end
|
472
|
+
|
473
|
+
require_relative 'gumbo'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
# The line below caused a problem on non-GAE rack environment.
|
3
|
+
# unless defined?(JRuby::Rack::VERSION) || defined?(AppEngine::ApiProxy)
|
4
|
+
#
|
5
|
+
# However, simply cutting defined?(JRuby::Rack::VERSION) off resulted in
|
6
|
+
# an unable-to-load-nokogiri problem. Thus, now, Nokogiri checks the presense
|
7
|
+
# of appengine-rack.jar in $LOAD_PATH. If Nokogiri is on GAE, Nokogiri
|
8
|
+
# should skip loading xml jars. This is because those are in WEB-INF/lib and
|
9
|
+
# already set in the classpath.
|
10
|
+
unless $LOAD_PATH.to_s.include?("appengine-rack")
|
11
|
+
require 'stringio'
|
12
|
+
require 'isorelax.jar'
|
13
|
+
require 'jing.jar'
|
14
|
+
require 'nekohtml.jar'
|
15
|
+
require 'nekodtd.jar'
|
16
|
+
require 'xercesImpl.jar'
|
17
|
+
require 'serializer.jar'
|
18
|
+
require 'xalan.jar'
|
19
|
+
require 'xml-apis.jar'
|
20
|
+
end
|