nokogiri 1.13.0-aarch64-linux
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 +7 -0
- data/Gemfile +5 -0
- data/LICENSE-DEPENDENCIES.md +1903 -0
- data/LICENSE.md +9 -0
- data/README.md +280 -0
- data/bin/nokogiri +131 -0
- data/dependencies.yml +73 -0
- data/ext/nokogiri/depend +38 -0
- data/ext/nokogiri/extconf.rb +1000 -0
- 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/include/libexslt/exslt.h +102 -0
- data/ext/nokogiri/include/libexslt/exsltconfig.h +70 -0
- data/ext/nokogiri/include/libexslt/exsltexports.h +140 -0
- data/ext/nokogiri/include/libxml2/libxml/DOCBparser.h +96 -0
- data/ext/nokogiri/include/libxml2/libxml/HTMLparser.h +306 -0
- data/ext/nokogiri/include/libxml2/libxml/HTMLtree.h +147 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX.h +173 -0
- data/ext/nokogiri/include/libxml2/libxml/SAX2.h +178 -0
- data/ext/nokogiri/include/libxml2/libxml/c14n.h +128 -0
- data/ext/nokogiri/include/libxml2/libxml/catalog.h +182 -0
- data/ext/nokogiri/include/libxml2/libxml/chvalid.h +230 -0
- data/ext/nokogiri/include/libxml2/libxml/debugXML.h +217 -0
- data/ext/nokogiri/include/libxml2/libxml/dict.h +79 -0
- data/ext/nokogiri/include/libxml2/libxml/encoding.h +245 -0
- data/ext/nokogiri/include/libxml2/libxml/entities.h +151 -0
- data/ext/nokogiri/include/libxml2/libxml/globals.h +508 -0
- data/ext/nokogiri/include/libxml2/libxml/hash.h +236 -0
- data/ext/nokogiri/include/libxml2/libxml/list.h +137 -0
- data/ext/nokogiri/include/libxml2/libxml/nanoftp.h +163 -0
- data/ext/nokogiri/include/libxml2/libxml/nanohttp.h +81 -0
- data/ext/nokogiri/include/libxml2/libxml/parser.h +1243 -0
- data/ext/nokogiri/include/libxml2/libxml/parserInternals.h +644 -0
- data/ext/nokogiri/include/libxml2/libxml/pattern.h +100 -0
- data/ext/nokogiri/include/libxml2/libxml/relaxng.h +217 -0
- data/ext/nokogiri/include/libxml2/libxml/schemasInternals.h +958 -0
- data/ext/nokogiri/include/libxml2/libxml/schematron.h +142 -0
- data/ext/nokogiri/include/libxml2/libxml/threads.h +89 -0
- data/ext/nokogiri/include/libxml2/libxml/tree.h +1311 -0
- data/ext/nokogiri/include/libxml2/libxml/uri.h +94 -0
- data/ext/nokogiri/include/libxml2/libxml/valid.h +458 -0
- data/ext/nokogiri/include/libxml2/libxml/xinclude.h +129 -0
- data/ext/nokogiri/include/libxml2/libxml/xlink.h +189 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlIO.h +368 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlautomata.h +146 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlerror.h +946 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlexports.h +77 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmemory.h +224 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlmodule.h +57 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlreader.h +428 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlregexp.h +222 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlsave.h +88 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlschemas.h +246 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlschemastypes.h +151 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlstring.h +140 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlunicode.h +202 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlversion.h +485 -0
- data/ext/nokogiri/include/libxml2/libxml/xmlwriter.h +488 -0
- data/ext/nokogiri/include/libxml2/libxml/xpath.h +564 -0
- data/ext/nokogiri/include/libxml2/libxml/xpathInternals.h +632 -0
- data/ext/nokogiri/include/libxml2/libxml/xpointer.h +114 -0
- data/ext/nokogiri/include/libxslt/attributes.h +38 -0
- data/ext/nokogiri/include/libxslt/documents.h +93 -0
- data/ext/nokogiri/include/libxslt/extensions.h +262 -0
- data/ext/nokogiri/include/libxslt/extra.h +72 -0
- data/ext/nokogiri/include/libxslt/functions.h +78 -0
- data/ext/nokogiri/include/libxslt/imports.h +75 -0
- data/ext/nokogiri/include/libxslt/keys.h +53 -0
- data/ext/nokogiri/include/libxslt/namespaces.h +68 -0
- data/ext/nokogiri/include/libxslt/numbersInternals.h +73 -0
- data/ext/nokogiri/include/libxslt/pattern.h +84 -0
- data/ext/nokogiri/include/libxslt/preproc.h +43 -0
- data/ext/nokogiri/include/libxslt/security.h +104 -0
- data/ext/nokogiri/include/libxslt/templates.h +77 -0
- data/ext/nokogiri/include/libxslt/transform.h +207 -0
- data/ext/nokogiri/include/libxslt/variables.h +118 -0
- data/ext/nokogiri/include/libxslt/xslt.h +110 -0
- data/ext/nokogiri/include/libxslt/xsltInternals.h +1978 -0
- data/ext/nokogiri/include/libxslt/xsltconfig.h +180 -0
- data/ext/nokogiri/include/libxslt/xsltexports.h +142 -0
- data/ext/nokogiri/include/libxslt/xsltlocale.h +76 -0
- data/ext/nokogiri/include/libxslt/xsltutils.h +313 -0
- data/ext/nokogiri/libxml2_backwards_compat.c +121 -0
- data/ext/nokogiri/nokogiri.c +278 -0
- data/ext/nokogiri/nokogiri.h +223 -0
- data/ext/nokogiri/test_global_handlers.c +40 -0
- data/ext/nokogiri/xml_attr.c +103 -0
- data/ext/nokogiri/xml_attribute_decl.c +70 -0
- data/ext/nokogiri/xml_cdata.c +57 -0
- data/ext/nokogiri/xml_comment.c +62 -0
- data/ext/nokogiri/xml_document.c +680 -0
- data/ext/nokogiri/xml_document_fragment.c +44 -0
- data/ext/nokogiri/xml_dtd.c +208 -0
- data/ext/nokogiri/xml_element_content.c +128 -0
- data/ext/nokogiri/xml_element_decl.c +69 -0
- data/ext/nokogiri/xml_encoding_handler.c +104 -0
- data/ext/nokogiri/xml_entity_decl.c +112 -0
- data/ext/nokogiri/xml_entity_reference.c +50 -0
- data/ext/nokogiri/xml_namespace.c +120 -0
- data/ext/nokogiri/xml_node.c +2144 -0
- data/ext/nokogiri/xml_node_set.c +498 -0
- data/ext/nokogiri/xml_processing_instruction.c +54 -0
- data/ext/nokogiri/xml_reader.c +719 -0
- data/ext/nokogiri/xml_relax_ng.c +185 -0
- data/ext/nokogiri/xml_sax_parser.c +310 -0
- data/ext/nokogiri/xml_sax_parser_context.c +281 -0
- data/ext/nokogiri/xml_sax_push_parser.c +168 -0
- data/ext/nokogiri/xml_schema.c +284 -0
- data/ext/nokogiri/xml_syntax_error.c +85 -0
- data/ext/nokogiri/xml_text.c +48 -0
- data/ext/nokogiri/xml_xpath_context.c +406 -0
- data/ext/nokogiri/xslt_stylesheet.c +264 -0
- data/gumbo-parser/CHANGES.md +63 -0
- data/gumbo-parser/Makefile +101 -0
- data/gumbo-parser/THANKS +27 -0
- data/lib/nokogiri/2.6/nokogiri.so +0 -0
- data/lib/nokogiri/2.7/nokogiri.so +0 -0
- data/lib/nokogiri/3.0/nokogiri.so +0 -0
- data/lib/nokogiri/3.1/nokogiri.so +0 -0
- data/lib/nokogiri/class_resolver.rb +67 -0
- data/lib/nokogiri/css/node.rb +54 -0
- data/lib/nokogiri/css/parser.rb +759 -0
- data/lib/nokogiri/css/parser.y +280 -0
- data/lib/nokogiri/css/parser_extras.rb +94 -0
- data/lib/nokogiri/css/syntax_error.rb +9 -0
- data/lib/nokogiri/css/tokenizer.rb +155 -0
- data/lib/nokogiri/css/tokenizer.rex +56 -0
- data/lib/nokogiri/css/xpath_visitor.rb +359 -0
- data/lib/nokogiri/css.rb +60 -0
- data/lib/nokogiri/decorators/slop.rb +44 -0
- data/lib/nokogiri/extension.rb +31 -0
- data/lib/nokogiri/gumbo.rb +15 -0
- data/lib/nokogiri/html.rb +48 -0
- data/lib/nokogiri/html4/builder.rb +37 -0
- data/lib/nokogiri/html4/document.rb +331 -0
- data/lib/nokogiri/html4/document_fragment.rb +54 -0
- data/lib/nokogiri/html4/element_description.rb +25 -0
- data/lib/nokogiri/html4/element_description_defaults.rb +578 -0
- data/lib/nokogiri/html4/entity_lookup.rb +15 -0
- data/lib/nokogiri/html4/sax/parser.rb +61 -0
- data/lib/nokogiri/html4/sax/parser_context.rb +20 -0
- data/lib/nokogiri/html4/sax/push_parser.rb +37 -0
- data/lib/nokogiri/html4.rb +46 -0
- data/lib/nokogiri/html5/document.rb +88 -0
- data/lib/nokogiri/html5/document_fragment.rb +83 -0
- data/lib/nokogiri/html5/node.rb +96 -0
- data/lib/nokogiri/html5.rb +477 -0
- data/lib/nokogiri/jruby/dependencies.rb +21 -0
- data/lib/nokogiri/syntax_error.rb +6 -0
- data/lib/nokogiri/version/constant.rb +6 -0
- data/lib/nokogiri/version/info.rb +221 -0
- data/lib/nokogiri/version.rb +4 -0
- data/lib/nokogiri/xml/attr.rb +17 -0
- data/lib/nokogiri/xml/attribute_decl.rb +20 -0
- data/lib/nokogiri/xml/builder.rb +485 -0
- data/lib/nokogiri/xml/cdata.rb +13 -0
- data/lib/nokogiri/xml/character_data.rb +9 -0
- data/lib/nokogiri/xml/document.rb +418 -0
- data/lib/nokogiri/xml/document_fragment.rb +162 -0
- data/lib/nokogiri/xml/dtd.rb +34 -0
- data/lib/nokogiri/xml/element_content.rb +38 -0
- data/lib/nokogiri/xml/element_decl.rb +15 -0
- data/lib/nokogiri/xml/entity_decl.rb +21 -0
- data/lib/nokogiri/xml/entity_reference.rb +20 -0
- data/lib/nokogiri/xml/namespace.rb +16 -0
- data/lib/nokogiri/xml/node/save_options.rb +65 -0
- data/lib/nokogiri/xml/node.rb +1402 -0
- data/lib/nokogiri/xml/node_set.rb +364 -0
- data/lib/nokogiri/xml/notation.rb +19 -0
- data/lib/nokogiri/xml/parse_options.rb +133 -0
- data/lib/nokogiri/xml/pp/character_data.rb +21 -0
- data/lib/nokogiri/xml/pp/node.rb +55 -0
- data/lib/nokogiri/xml/pp.rb +4 -0
- data/lib/nokogiri/xml/processing_instruction.rb +10 -0
- data/lib/nokogiri/xml/reader.rb +107 -0
- data/lib/nokogiri/xml/relax_ng.rb +38 -0
- data/lib/nokogiri/xml/sax/document.rb +167 -0
- data/lib/nokogiri/xml/sax/parser.rb +125 -0
- data/lib/nokogiri/xml/sax/parser_context.rb +21 -0
- data/lib/nokogiri/xml/sax/push_parser.rb +61 -0
- data/lib/nokogiri/xml/sax.rb +6 -0
- data/lib/nokogiri/xml/schema.rb +73 -0
- data/lib/nokogiri/xml/searchable.rb +259 -0
- data/lib/nokogiri/xml/syntax_error.rb +71 -0
- data/lib/nokogiri/xml/text.rb +11 -0
- data/lib/nokogiri/xml/xpath/syntax_error.rb +13 -0
- data/lib/nokogiri/xml/xpath.rb +21 -0
- data/lib/nokogiri/xml/xpath_context.rb +16 -0
- data/lib/nokogiri/xml.rb +75 -0
- data/lib/nokogiri/xslt/stylesheet.rb +27 -0
- data/lib/nokogiri/xslt.rb +58 -0
- data/lib/nokogiri.rb +128 -0
- data/lib/xsd/xmlparser/nokogiri.rb +104 -0
- metadata +539 -0
@@ -0,0 +1,418 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "pathname"
|
5
|
+
|
6
|
+
module Nokogiri
|
7
|
+
module XML
|
8
|
+
# Nokogiri::XML::Document is the main entry point for dealing with XML documents. The Document
|
9
|
+
# is created by parsing an XML document. See Nokogiri::XML::Document.parse for more information
|
10
|
+
# on parsing.
|
11
|
+
#
|
12
|
+
# For searching a Document, see Nokogiri::XML::Searchable#css and
|
13
|
+
# Nokogiri::XML::Searchable#xpath
|
14
|
+
class Document < Nokogiri::XML::Node
|
15
|
+
# See http://www.w3.org/TR/REC-xml-names/#ns-decl for more details. Note that we're not
|
16
|
+
# attempting to handle unicode characters partly because libxml2 doesn't handle unicode
|
17
|
+
# characters in NCNAMEs.
|
18
|
+
NCNAME_START_CHAR = "A-Za-z_"
|
19
|
+
NCNAME_CHAR = NCNAME_START_CHAR + "\\-\\.0-9"
|
20
|
+
NCNAME_RE = /^xmlns(?::([#{NCNAME_START_CHAR}][#{NCNAME_CHAR}]*))?$/
|
21
|
+
|
22
|
+
##
|
23
|
+
# Parse an XML file.
|
24
|
+
#
|
25
|
+
# +string_or_io+ may be a String, or any object that responds to
|
26
|
+
# _read_ and _close_ such as an IO, or StringIO.
|
27
|
+
#
|
28
|
+
# +url+ (optional) is the URI where this document is located.
|
29
|
+
#
|
30
|
+
# +encoding+ (optional) is the encoding that should be used when processing
|
31
|
+
# the document.
|
32
|
+
#
|
33
|
+
# +options+ (optional) is a configuration object that sets options during
|
34
|
+
# parsing, such as Nokogiri::XML::ParseOptions::RECOVER. See the
|
35
|
+
# Nokogiri::XML::ParseOptions for more information.
|
36
|
+
#
|
37
|
+
# +block+ (optional) is passed a configuration object on which
|
38
|
+
# parse options may be set.
|
39
|
+
#
|
40
|
+
# By default, Nokogiri treats documents as untrusted, and so
|
41
|
+
# does not attempt to load DTDs or access the network. See
|
42
|
+
# Nokogiri::XML::ParseOptions for a complete list of options;
|
43
|
+
# and that module's DEFAULT_XML constant for what's set (and not
|
44
|
+
# set) by default.
|
45
|
+
#
|
46
|
+
# Nokogiri.XML() is a convenience method which will call this method.
|
47
|
+
#
|
48
|
+
def self.parse(string_or_io, url = nil, encoding = nil, options = ParseOptions::DEFAULT_XML)
|
49
|
+
options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
|
50
|
+
yield options if block_given?
|
51
|
+
|
52
|
+
url ||= string_or_io.respond_to?(:path) ? string_or_io.path : nil
|
53
|
+
|
54
|
+
if empty_doc?(string_or_io)
|
55
|
+
if options.strict?
|
56
|
+
raise Nokogiri::XML::SyntaxError, "Empty document"
|
57
|
+
else
|
58
|
+
return encoding ? new.tap { |i| i.encoding = encoding } : new
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
doc = if string_or_io.respond_to?(:read)
|
63
|
+
if string_or_io.is_a?(Pathname)
|
64
|
+
# resolve the Pathname to the file and open it as an IO object, see #2110
|
65
|
+
string_or_io = string_or_io.expand_path.open
|
66
|
+
url ||= string_or_io.path
|
67
|
+
end
|
68
|
+
|
69
|
+
read_io(string_or_io, url, encoding, options.to_i)
|
70
|
+
else
|
71
|
+
# read_memory pukes on empty docs
|
72
|
+
read_memory(string_or_io, url, encoding, options.to_i)
|
73
|
+
end
|
74
|
+
|
75
|
+
# do xinclude processing
|
76
|
+
doc.do_xinclude(options) if options.xinclude?
|
77
|
+
|
78
|
+
doc
|
79
|
+
end
|
80
|
+
|
81
|
+
##
|
82
|
+
# :singleton-method: wrap
|
83
|
+
# :call-seq: wrap(java_document) → Nokogiri::XML::Document
|
84
|
+
#
|
85
|
+
# ⚠ This method is only available when running JRuby.
|
86
|
+
#
|
87
|
+
# Create a Document using an existing Java DOM document object.
|
88
|
+
#
|
89
|
+
# The returned Document shares the same underlying data structure as the Java object, so
|
90
|
+
# changes in one are reflected in the other.
|
91
|
+
#
|
92
|
+
# [Parameters]
|
93
|
+
# - `java_document` (Java::OrgW3cDom::Document)
|
94
|
+
# (The class `Java::OrgW3cDom::Document` is also accessible as `org.w3c.dom.Document`.)
|
95
|
+
#
|
96
|
+
# [Returns] Nokogiri::XML::Document
|
97
|
+
#
|
98
|
+
# See also \#to_java
|
99
|
+
|
100
|
+
# :method: to_java
|
101
|
+
# :call-seq: to_java() → Java::OrgW3cDom::Document
|
102
|
+
#
|
103
|
+
# ⚠ This method is only available when running JRuby.
|
104
|
+
#
|
105
|
+
# Returns the underlying Java DOM document object for this document.
|
106
|
+
#
|
107
|
+
# The returned Java object shares the same underlying data structure as this document, so
|
108
|
+
# changes in one are reflected in the other.
|
109
|
+
#
|
110
|
+
# [Returns]
|
111
|
+
# Java::OrgW3cDom::Document
|
112
|
+
# (The class `Java::OrgW3cDom::Document` is also accessible as `org.w3c.dom.Document`.)
|
113
|
+
#
|
114
|
+
# See also Document.wrap
|
115
|
+
|
116
|
+
# The errors found while parsing a document.
|
117
|
+
#
|
118
|
+
# [Returns] Array<Nokogiri::XML::SyntaxError>
|
119
|
+
attr_accessor :errors
|
120
|
+
|
121
|
+
# When `true`, reparented elements without a namespace will inherit their new parent's
|
122
|
+
# namespace (if one exists). Defaults to `false`.
|
123
|
+
#
|
124
|
+
# [Returns] Boolean
|
125
|
+
#
|
126
|
+
# *Example:* Default behavior of namespace inheritance
|
127
|
+
#
|
128
|
+
# xml = <<~EOF
|
129
|
+
# <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
|
130
|
+
# <foo:parent>
|
131
|
+
# </foo:parent>
|
132
|
+
# </root>
|
133
|
+
# EOF
|
134
|
+
# doc = Nokogiri::XML(xml)
|
135
|
+
# parent = doc.at_xpath("//foo:parent", "foo" => "http://nokogiri.org/default_ns/test/foo")
|
136
|
+
# parent.add_child("<child></child>")
|
137
|
+
# doc.to_xml
|
138
|
+
# # => <?xml version="1.0"?>
|
139
|
+
# # <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
|
140
|
+
# # <foo:parent>
|
141
|
+
# # <child/>
|
142
|
+
# # </foo:parent>
|
143
|
+
# # </root>
|
144
|
+
#
|
145
|
+
# *Example:* Setting namespace inheritance to `true`
|
146
|
+
#
|
147
|
+
# xml = <<~EOF
|
148
|
+
# <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
|
149
|
+
# <foo:parent>
|
150
|
+
# </foo:parent>
|
151
|
+
# </root>
|
152
|
+
# EOF
|
153
|
+
# doc = Nokogiri::XML(xml)
|
154
|
+
# doc.namespace_inheritance = true
|
155
|
+
# parent = doc.at_xpath("//foo:parent", "foo" => "http://nokogiri.org/default_ns/test/foo")
|
156
|
+
# parent.add_child("<child></child>")
|
157
|
+
# doc.to_xml
|
158
|
+
# # => <?xml version="1.0"?>
|
159
|
+
# # <root xmlns:foo="http://nokogiri.org/default_ns/test/foo">
|
160
|
+
# # <foo:parent>
|
161
|
+
# # <foo:child/>
|
162
|
+
# # </foo:parent>
|
163
|
+
# # </root>
|
164
|
+
#
|
165
|
+
# Since v1.12.4
|
166
|
+
attr_accessor :namespace_inheritance
|
167
|
+
|
168
|
+
def initialize(*args) # :nodoc:
|
169
|
+
@errors = []
|
170
|
+
@decorators = nil
|
171
|
+
@namespace_inheritance = false
|
172
|
+
end
|
173
|
+
|
174
|
+
# :call-seq:
|
175
|
+
# create_element(name, *contents_or_attrs, &block) → Nokogiri::XML::Element
|
176
|
+
#
|
177
|
+
# Create a new Element with `name` belonging to this document, optionally setting contents or
|
178
|
+
# attributes.
|
179
|
+
#
|
180
|
+
# This method is _not_ the most user-friendly option if your intention is to add a node to the
|
181
|
+
# document tree. Prefer one of the Nokogiri::XML::Node methods like Node#add_child,
|
182
|
+
# Node#add_next_sibling, Node#replace, etc. which will both create an element (or subtree) and
|
183
|
+
# place it in the document tree.
|
184
|
+
#
|
185
|
+
# Arguments may be passed to initialize the element:
|
186
|
+
#
|
187
|
+
# - a Hash argument will be used to set attributes
|
188
|
+
# - a non-Hash object that responds to \#to_s will be used to set the new node's contents
|
189
|
+
#
|
190
|
+
# A block may be passed to mutate the node.
|
191
|
+
#
|
192
|
+
# [Parameters]
|
193
|
+
# - `name` (String)
|
194
|
+
# - `contents_or_attrs` (\#to_s, Hash)
|
195
|
+
# [Yields] `node` (Nokogiri::XML::Element)
|
196
|
+
# [Returns] Nokogiri::XML::Element
|
197
|
+
#
|
198
|
+
# *Example:* An empty element without attributes
|
199
|
+
#
|
200
|
+
# doc.create_element("div")
|
201
|
+
# # => <div></div>
|
202
|
+
#
|
203
|
+
# *Example:* An element with contents
|
204
|
+
#
|
205
|
+
# doc.create_element("div", "contents")
|
206
|
+
# # => <div>contents</div>
|
207
|
+
#
|
208
|
+
# *Example:* An element with attributes
|
209
|
+
#
|
210
|
+
# doc.create_element("div", {"class" => "container"})
|
211
|
+
# # => <div class='container'></div>
|
212
|
+
#
|
213
|
+
# *Example:* An element with contents and attributes
|
214
|
+
#
|
215
|
+
# doc.create_element("div", "contents", {"class" => "container"})
|
216
|
+
# # => <div class='container'>contents</div>
|
217
|
+
#
|
218
|
+
# *Example:* Passing a block to mutate the element
|
219
|
+
#
|
220
|
+
# doc.create_element("div") { |node| node["class"] = "blue" if before_noon? }
|
221
|
+
#
|
222
|
+
def create_element(name, *contents_or_attrs, &block)
|
223
|
+
elm = Nokogiri::XML::Element.new(name, self, &block)
|
224
|
+
contents_or_attrs.each do |arg|
|
225
|
+
case arg
|
226
|
+
when Hash
|
227
|
+
arg.each do |k, v|
|
228
|
+
key = k.to_s
|
229
|
+
if key =~ NCNAME_RE
|
230
|
+
ns_name = Regexp.last_match(1)
|
231
|
+
elm.add_namespace_definition(ns_name, v)
|
232
|
+
else
|
233
|
+
elm[k.to_s] = v.to_s
|
234
|
+
end
|
235
|
+
end
|
236
|
+
else
|
237
|
+
elm.content = arg
|
238
|
+
end
|
239
|
+
end
|
240
|
+
if (ns = elm.namespace_definitions.find { |n| n.prefix.nil? || (n.prefix == "") })
|
241
|
+
elm.namespace = ns
|
242
|
+
end
|
243
|
+
elm
|
244
|
+
end
|
245
|
+
|
246
|
+
# Create a Text Node with +string+
|
247
|
+
def create_text_node(string, &block)
|
248
|
+
Nokogiri::XML::Text.new(string.to_s, self, &block)
|
249
|
+
end
|
250
|
+
|
251
|
+
# Create a CDATA Node containing +string+
|
252
|
+
def create_cdata(string, &block)
|
253
|
+
Nokogiri::XML::CDATA.new(self, string.to_s, &block)
|
254
|
+
end
|
255
|
+
|
256
|
+
# Create a Comment Node containing +string+
|
257
|
+
def create_comment(string, &block)
|
258
|
+
Nokogiri::XML::Comment.new(self, string.to_s, &block)
|
259
|
+
end
|
260
|
+
|
261
|
+
# The name of this document. Always returns "document"
|
262
|
+
def name
|
263
|
+
"document"
|
264
|
+
end
|
265
|
+
|
266
|
+
# A reference to +self+
|
267
|
+
def document
|
268
|
+
self
|
269
|
+
end
|
270
|
+
|
271
|
+
# :call-seq:
|
272
|
+
# collect_namespaces() → Hash<String(Namespace#prefix) ⇒ String(Namespace#href)>
|
273
|
+
#
|
274
|
+
# Recursively get all namespaces from this node and its subtree and return them as a
|
275
|
+
# hash.
|
276
|
+
#
|
277
|
+
# ⚠ This method will not handle duplicate namespace prefixes, since the return value is a hash.
|
278
|
+
#
|
279
|
+
# Note that this method does an xpath lookup for nodes with namespaces, and as a result the
|
280
|
+
# order (and which duplicate prefix "wins") may be dependent on the implementation of the
|
281
|
+
# underlying XML library.
|
282
|
+
#
|
283
|
+
# *Example:* Basic usage
|
284
|
+
#
|
285
|
+
# Given this document:
|
286
|
+
#
|
287
|
+
# <root xmlns="default" xmlns:foo="bar">
|
288
|
+
# <bar xmlns:hello="world" />
|
289
|
+
# </root>
|
290
|
+
#
|
291
|
+
# This method will return:
|
292
|
+
#
|
293
|
+
# {"xmlns:foo"=>"bar", "xmlns"=>"default", "xmlns:hello"=>"world"}
|
294
|
+
#
|
295
|
+
# *Example:* Duplicate prefixes
|
296
|
+
#
|
297
|
+
# Given this document:
|
298
|
+
#
|
299
|
+
# <root xmlns:foo="bar">
|
300
|
+
# <bar xmlns:foo="baz" />
|
301
|
+
# </root>
|
302
|
+
#
|
303
|
+
# The hash returned will be something like:
|
304
|
+
#
|
305
|
+
# {"xmlns:foo" => "baz"}
|
306
|
+
#
|
307
|
+
def collect_namespaces
|
308
|
+
xpath("//namespace::*").each_with_object({}) do |ns, hash|
|
309
|
+
hash[["xmlns", ns.prefix].compact.join(":")] = ns.href if ns.prefix != "xml"
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
# Get the list of decorators given +key+
|
314
|
+
def decorators(key)
|
315
|
+
@decorators ||= {}
|
316
|
+
@decorators[key] ||= []
|
317
|
+
end
|
318
|
+
|
319
|
+
##
|
320
|
+
# Validate this Document against it's DTD. Returns a list of errors on
|
321
|
+
# the document or +nil+ when there is no DTD.
|
322
|
+
def validate
|
323
|
+
return nil unless internal_subset
|
324
|
+
internal_subset.validate(self)
|
325
|
+
end
|
326
|
+
|
327
|
+
##
|
328
|
+
# Explore a document with shortcut methods. See Nokogiri::Slop for details.
|
329
|
+
#
|
330
|
+
# Note that any nodes that have been instantiated before #slop!
|
331
|
+
# is called will not be decorated with sloppy behavior. So, if you're in
|
332
|
+
# irb, the preferred idiom is:
|
333
|
+
#
|
334
|
+
# irb> doc = Nokogiri::Slop my_markup
|
335
|
+
#
|
336
|
+
# and not
|
337
|
+
#
|
338
|
+
# irb> doc = Nokogiri::HTML my_markup
|
339
|
+
# ... followed by irb's implicit inspect (and therefore instantiation of every node) ...
|
340
|
+
# irb> doc.slop!
|
341
|
+
# ... which does absolutely nothing.
|
342
|
+
#
|
343
|
+
def slop!
|
344
|
+
unless decorators(XML::Node).include?(Nokogiri::Decorators::Slop)
|
345
|
+
decorators(XML::Node) << Nokogiri::Decorators::Slop
|
346
|
+
decorate!
|
347
|
+
end
|
348
|
+
|
349
|
+
self
|
350
|
+
end
|
351
|
+
|
352
|
+
##
|
353
|
+
# Apply any decorators to +node+
|
354
|
+
def decorate(node)
|
355
|
+
return unless @decorators
|
356
|
+
@decorators.each do |klass, list|
|
357
|
+
next unless node.is_a?(klass)
|
358
|
+
list.each { |moodule| node.extend(moodule) }
|
359
|
+
end
|
360
|
+
end
|
361
|
+
|
362
|
+
alias_method :to_xml, :serialize
|
363
|
+
alias_method :clone, :dup
|
364
|
+
|
365
|
+
# Get the hash of namespaces on the root Nokogiri::XML::Node
|
366
|
+
def namespaces
|
367
|
+
root ? root.namespaces : {}
|
368
|
+
end
|
369
|
+
|
370
|
+
##
|
371
|
+
# Create a Nokogiri::XML::DocumentFragment from +tags+
|
372
|
+
# Returns an empty fragment if +tags+ is nil.
|
373
|
+
def fragment(tags = nil)
|
374
|
+
DocumentFragment.new(self, tags, root)
|
375
|
+
end
|
376
|
+
|
377
|
+
undef_method :swap, :parent, :namespace, :default_namespace=
|
378
|
+
undef_method :add_namespace_definition, :attributes
|
379
|
+
undef_method :namespace_definitions, :line, :add_namespace
|
380
|
+
|
381
|
+
def add_child(node_or_tags)
|
382
|
+
raise "A document may not have multiple root nodes." if (root && root.name != "nokogiri_text_wrapper") && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
383
|
+
node_or_tags = coerce(node_or_tags)
|
384
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
385
|
+
raise "A document may not have multiple root nodes." if node_or_tags.size > 1
|
386
|
+
super(node_or_tags.first)
|
387
|
+
else
|
388
|
+
super
|
389
|
+
end
|
390
|
+
end
|
391
|
+
alias_method :<<, :add_child
|
392
|
+
|
393
|
+
# :call-seq:
|
394
|
+
# xpath_doctype() → Nokogiri::CSS::XPathVisitor::DoctypeConfig
|
395
|
+
#
|
396
|
+
# [Returns] The document type which determines CSS-to-XPath translation.
|
397
|
+
#
|
398
|
+
# See XPathVisitor for more information.
|
399
|
+
def xpath_doctype
|
400
|
+
Nokogiri::CSS::XPathVisitor::DoctypeConfig::XML
|
401
|
+
end
|
402
|
+
|
403
|
+
private
|
404
|
+
|
405
|
+
def self.empty_doc?(string_or_io)
|
406
|
+
string_or_io.nil? ||
|
407
|
+
(string_or_io.respond_to?(:empty?) && string_or_io.empty?) ||
|
408
|
+
(string_or_io.respond_to?(:eof?) && string_or_io.eof?)
|
409
|
+
end
|
410
|
+
|
411
|
+
IMPLIED_XPATH_CONTEXTS = ["//"].freeze # :nodoc:
|
412
|
+
|
413
|
+
def inspect_attributes
|
414
|
+
[:name, :children]
|
415
|
+
end
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class DocumentFragment < Nokogiri::XML::Node
|
6
|
+
####
|
7
|
+
# Create a Nokogiri::XML::DocumentFragment from +tags+
|
8
|
+
def self.parse(tags, options = ParseOptions::DEFAULT_XML, &block)
|
9
|
+
new(XML::Document.new, tags, nil, options, &block)
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Create a new DocumentFragment from +tags+.
|
14
|
+
#
|
15
|
+
# If +ctx+ is present, it is used as a context node for the
|
16
|
+
# subtree created, e.g., namespaces will be resolved relative
|
17
|
+
# to +ctx+.
|
18
|
+
def initialize(document, tags = nil, ctx = nil, options = ParseOptions::DEFAULT_XML)
|
19
|
+
return self unless tags
|
20
|
+
|
21
|
+
options = Nokogiri::XML::ParseOptions.new(options) if Integer === options
|
22
|
+
yield options if block_given?
|
23
|
+
|
24
|
+
children = if ctx
|
25
|
+
# Fix for issue#490
|
26
|
+
if Nokogiri.jruby?
|
27
|
+
# fix for issue #770
|
28
|
+
ctx.parse("<root #{namespace_declarations(ctx)}>#{tags}</root>", options).children
|
29
|
+
else
|
30
|
+
ctx.parse(tags, options)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
wrapper_doc = XML::Document.parse("<root>#{tags}</root>", nil, nil, options)
|
34
|
+
self.errors = wrapper_doc.errors
|
35
|
+
wrapper_doc.xpath("/root/node()")
|
36
|
+
end
|
37
|
+
children.each { |child| child.parent = self }
|
38
|
+
end
|
39
|
+
|
40
|
+
if Nokogiri.uses_libxml?
|
41
|
+
def dup
|
42
|
+
new_document = document.dup
|
43
|
+
new_fragment = self.class.new(new_document)
|
44
|
+
children.each do |child|
|
45
|
+
child.dup(1, new_document).parent = new_fragment
|
46
|
+
end
|
47
|
+
new_fragment
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
###
|
52
|
+
# return the name for DocumentFragment
|
53
|
+
def name
|
54
|
+
"#document-fragment"
|
55
|
+
end
|
56
|
+
|
57
|
+
###
|
58
|
+
# Convert this DocumentFragment to a string
|
59
|
+
def to_s
|
60
|
+
children.to_s
|
61
|
+
end
|
62
|
+
|
63
|
+
###
|
64
|
+
# Convert this DocumentFragment to html
|
65
|
+
# See Nokogiri::XML::NodeSet#to_html
|
66
|
+
def to_html(*args)
|
67
|
+
if Nokogiri.jruby?
|
68
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
69
|
+
unless options[:save_with]
|
70
|
+
options[:save_with] = Node::SaveOptions::NO_DECLARATION | Node::SaveOptions::NO_EMPTY_TAGS | Node::SaveOptions::AS_HTML
|
71
|
+
end
|
72
|
+
args.insert(0, options)
|
73
|
+
end
|
74
|
+
children.to_html(*args)
|
75
|
+
end
|
76
|
+
|
77
|
+
###
|
78
|
+
# Convert this DocumentFragment to xhtml
|
79
|
+
# See Nokogiri::XML::NodeSet#to_xhtml
|
80
|
+
def to_xhtml(*args)
|
81
|
+
if Nokogiri.jruby?
|
82
|
+
options = args.first.is_a?(Hash) ? args.shift : {}
|
83
|
+
unless options[:save_with]
|
84
|
+
options[:save_with] = Node::SaveOptions::NO_DECLARATION | Node::SaveOptions::NO_EMPTY_TAGS | Node::SaveOptions::AS_XHTML
|
85
|
+
end
|
86
|
+
args.insert(0, options)
|
87
|
+
end
|
88
|
+
children.to_xhtml(*args)
|
89
|
+
end
|
90
|
+
|
91
|
+
###
|
92
|
+
# Convert this DocumentFragment to xml
|
93
|
+
# See Nokogiri::XML::NodeSet#to_xml
|
94
|
+
def to_xml(*args)
|
95
|
+
children.to_xml(*args)
|
96
|
+
end
|
97
|
+
|
98
|
+
###
|
99
|
+
# call-seq: css *rules, [namespace-bindings, custom-pseudo-class]
|
100
|
+
#
|
101
|
+
# Search this fragment for CSS +rules+. +rules+ must be one or more CSS
|
102
|
+
# selectors. For example:
|
103
|
+
#
|
104
|
+
# For more information see Nokogiri::XML::Searchable#css
|
105
|
+
def css(*args)
|
106
|
+
if children.any?
|
107
|
+
children.css(*args) # 'children' is a smell here
|
108
|
+
else
|
109
|
+
NodeSet.new(document)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# NOTE that we don't delegate #xpath to children ... another smell.
|
115
|
+
# def xpath ; end
|
116
|
+
#
|
117
|
+
|
118
|
+
###
|
119
|
+
# call-seq: search *paths, [namespace-bindings, xpath-variable-bindings, custom-handler-class]
|
120
|
+
#
|
121
|
+
# Search this fragment for +paths+. +paths+ must be one or more XPath or CSS queries.
|
122
|
+
#
|
123
|
+
# For more information see Nokogiri::XML::Searchable#search
|
124
|
+
def search(*rules)
|
125
|
+
rules, handler, ns, binds = extract_params(rules)
|
126
|
+
|
127
|
+
rules.inject(NodeSet.new(document)) do |set, rule|
|
128
|
+
set + if Searchable::LOOKS_LIKE_XPATH.match?(rule)
|
129
|
+
xpath(*[rule, ns, handler, binds].compact)
|
130
|
+
else
|
131
|
+
children.css(*[rule, ns, handler].compact) # 'children' is a smell here
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
alias_method :serialize, :to_s
|
137
|
+
|
138
|
+
# A list of Nokogiri::XML::SyntaxError found when parsing a document
|
139
|
+
def errors
|
140
|
+
document.errors
|
141
|
+
end
|
142
|
+
|
143
|
+
def errors=(things) # :nodoc:
|
144
|
+
document.errors = things
|
145
|
+
end
|
146
|
+
|
147
|
+
def fragment(data)
|
148
|
+
document.fragment(data)
|
149
|
+
end
|
150
|
+
|
151
|
+
private
|
152
|
+
|
153
|
+
# fix for issue 770
|
154
|
+
def namespace_declarations(ctx)
|
155
|
+
ctx.namespace_scopes.map do |namespace|
|
156
|
+
prefix = namespace.prefix.nil? ? "" : ":#{namespace.prefix}"
|
157
|
+
%{xmlns#{prefix}="#{namespace.href}"}
|
158
|
+
end.join(" ")
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class DTD < Nokogiri::XML::Node
|
6
|
+
undef_method :attribute_nodes
|
7
|
+
undef_method :values
|
8
|
+
undef_method :content
|
9
|
+
undef_method :namespace
|
10
|
+
undef_method :namespace_definitions
|
11
|
+
undef_method :line if method_defined?(:line)
|
12
|
+
|
13
|
+
def keys
|
14
|
+
attributes.keys
|
15
|
+
end
|
16
|
+
|
17
|
+
def each
|
18
|
+
attributes.each do |key, value|
|
19
|
+
yield([key, value])
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def html_dtd?
|
24
|
+
name.casecmp("html").zero?
|
25
|
+
end
|
26
|
+
|
27
|
+
def html5_dtd?
|
28
|
+
html_dtd? &&
|
29
|
+
external_id.nil? &&
|
30
|
+
(system_id.nil? || system_id == "about:legacy-compat")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
###
|
6
|
+
# Represents the allowed content in an Element Declaration inside a DTD:
|
7
|
+
#
|
8
|
+
# <?xml version="1.0"?><?TEST-STYLE PIDATA?>
|
9
|
+
# <!DOCTYPE staff SYSTEM "staff.dtd" [
|
10
|
+
# <!ELEMENT div1 (head, (p | list | note)*, div2*)>
|
11
|
+
# ]>
|
12
|
+
# </root>
|
13
|
+
#
|
14
|
+
# ElementContent represents the tree inside the <!ELEMENT> tag shown above
|
15
|
+
# that lists the possible content for the div1 tag.
|
16
|
+
class ElementContent
|
17
|
+
# Possible definitions of type
|
18
|
+
PCDATA = 1
|
19
|
+
ELEMENT = 2
|
20
|
+
SEQ = 3
|
21
|
+
OR = 4
|
22
|
+
|
23
|
+
# Possible content occurrences
|
24
|
+
ONCE = 1
|
25
|
+
OPT = 2
|
26
|
+
MULT = 3
|
27
|
+
PLUS = 4
|
28
|
+
|
29
|
+
attr_reader :document
|
30
|
+
|
31
|
+
###
|
32
|
+
# Get the children of this ElementContent node
|
33
|
+
def children
|
34
|
+
[c1, c2].compact
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class ElementDecl < Nokogiri::XML::Node
|
6
|
+
undef_method :namespace
|
7
|
+
undef_method :namespace_definitions
|
8
|
+
undef_method :line if method_defined?(:line)
|
9
|
+
|
10
|
+
def inspect
|
11
|
+
"#<#{self.class.name}:#{format("0x%x", object_id)} #{to_s.inspect}>"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nokogiri
|
4
|
+
module XML
|
5
|
+
class EntityDecl < Nokogiri::XML::Node
|
6
|
+
undef_method :attribute_nodes
|
7
|
+
undef_method :attributes
|
8
|
+
undef_method :namespace
|
9
|
+
undef_method :namespace_definitions
|
10
|
+
undef_method :line if method_defined?(:line)
|
11
|
+
|
12
|
+
def self.new(name, doc, *args)
|
13
|
+
doc.create_entity(name, *args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def inspect
|
17
|
+
"#<#{self.class.name}:#{format("0x%x", object_id)} #{to_s.inspect}>"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|