lobzique 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/Cargo.lock +520 -0
- data/Cargo.toml +11 -0
- data/LICENSE-NOKOGIRI.md +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +144 -0
- data/crates/lobzique-core/Cargo.toml +14 -0
- data/crates/lobzique-core/src/css/mod.rs +127 -0
- data/crates/lobzique-core/src/css/parser.rs +805 -0
- data/crates/lobzique-core/src/css/tables.rs +282 -0
- data/crates/lobzique-core/src/css/tests.rs +476 -0
- data/crates/lobzique-core/src/css/tokenizer.rs +215 -0
- data/crates/lobzique-core/src/css/translate.rs +806 -0
- data/crates/lobzique-core/src/css/write.rs +370 -0
- data/crates/lobzique-core/src/dom/attribute.rs +144 -0
- data/crates/lobzique-core/src/dom/check.rs +160 -0
- data/crates/lobzique-core/src/dom/copy.rs +229 -0
- data/crates/lobzique-core/src/dom/mod.rs +298 -0
- data/crates/lobzique-core/src/dom/namespace.rs +134 -0
- data/crates/lobzique-core/src/dom/node.rs +129 -0
- data/crates/lobzique-core/src/dom/path.rs +116 -0
- data/crates/lobzique-core/src/dom/reparent.rs +308 -0
- data/crates/lobzique-core/src/dom/tests.rs +1057 -0
- data/crates/lobzique-core/src/dom/text.rs +159 -0
- data/crates/lobzique-core/src/dom/tree.rs +446 -0
- data/crates/lobzique-core/src/html5/mod.rs +28 -0
- data/crates/lobzique-core/src/html5/parse.rs +838 -0
- data/crates/lobzique-core/src/html5/serialize.rs +181 -0
- data/crates/lobzique-core/src/html5/tests.rs +548 -0
- data/crates/lobzique-core/src/lib.rs +12 -0
- data/crates/lobzique-core/src/work.rs +26 -0
- data/crates/lobzique-core/src/xml/mod.rs +8 -0
- data/crates/lobzique-core/src/xml/serialize.rs +681 -0
- data/crates/lobzique-core/src/xml/tests.rs +174 -0
- data/crates/lobzique-core/src/xpath/ast.rs +415 -0
- data/crates/lobzique-core/src/xpath/chars.rs +139 -0
- data/crates/lobzique-core/src/xpath/eval.rs +1305 -0
- data/crates/lobzique-core/src/xpath/mod.rs +118 -0
- data/crates/lobzique-core/src/xpath/parse.rs +729 -0
- data/crates/lobzique-core/src/xpath/tests.rs +921 -0
- data/ext/lobzique/Cargo.toml +22 -0
- data/ext/lobzique/build.rs +5 -0
- data/ext/lobzique/clippy.toml +15 -0
- data/ext/lobzique/extconf.rb +6 -0
- data/ext/lobzique/src/css/mod.rs +9 -0
- data/ext/lobzique/src/css/parser.rs +44 -0
- data/ext/lobzique/src/css/selector.rs +101 -0
- data/ext/lobzique/src/html4/document.rs +31 -0
- data/ext/lobzique/src/html4/mod.rs +7 -0
- data/ext/lobzique/src/html5/mod.rs +16 -0
- data/ext/lobzique/src/html5ever.rs +278 -0
- data/ext/lobzique/src/lib.rs +45 -0
- data/ext/lobzique/src/support.rs +156 -0
- data/ext/lobzique/src/xml/attr.rs +27 -0
- data/ext/lobzique/src/xml/cdata.rs +33 -0
- data/ext/lobzique/src/xml/comment.rs +34 -0
- data/ext/lobzique/src/xml/document.rs +199 -0
- data/ext/lobzique/src/xml/document_fragment.rs +27 -0
- data/ext/lobzique/src/xml/dtd.rs +37 -0
- data/ext/lobzique/src/xml/mod.rs +44 -0
- data/ext/lobzique/src/xml/namespace.rs +56 -0
- data/ext/lobzique/src/xml/node.rs +944 -0
- data/ext/lobzique/src/xml/node_set.rs +20 -0
- data/ext/lobzique/src/xml/syntax_error.rs +33 -0
- data/ext/lobzique/src/xml/text.rs +44 -0
- data/ext/lobzique/src/xml/xpath_context.rs +305 -0
- data/lib/lobzique/action_text.rb +29 -0
- data/lib/lobzique/class_resolver.rb +33 -0
- data/lib/lobzique/css/selector_cache.rb +36 -0
- data/lib/lobzique/css/syntax_error.rb +12 -0
- data/lib/lobzique/css/xpath_visitor.rb +60 -0
- data/lib/lobzique/css.rb +57 -0
- data/lib/lobzique/extension.rb +31 -0
- data/lib/lobzique/html.rb +14 -0
- data/lib/lobzique/html4/document.rb +235 -0
- data/lib/lobzique/html4/document_fragment.rb +75 -0
- data/lib/lobzique/html4/encoding_reader.rb +374 -0
- data/lib/lobzique/html4.rb +35 -0
- data/lib/lobzique/html5/document.rb +106 -0
- data/lib/lobzique/html5/document_fragment.rb +113 -0
- data/lib/lobzique/html5/node.rb +66 -0
- data/lib/lobzique/html5.rb +92 -0
- data/lib/lobzique/html5ever.rb +14 -0
- data/lib/lobzique/rails_dom_testing.rb +95 -0
- data/lib/lobzique/sanitizer/concerns.rb +110 -0
- data/lib/lobzique/sanitizer/document.rb +14 -0
- data/lib/lobzique/sanitizer/document_fragment.rb +12 -0
- data/lib/lobzique/sanitizer/escape_raw_text_in_select.rb +31 -0
- data/lib/lobzique/sanitizer/scrubbers.rb +46 -0
- data/lib/lobzique/sanitizer.rb +96 -0
- data/lib/lobzique/syntax_error.rb +8 -0
- data/lib/lobzique/version.rb +6 -0
- data/lib/lobzique/xml/attr.rb +23 -0
- data/lib/lobzique/xml/cdata.rb +13 -0
- data/lib/lobzique/xml/character_data.rb +11 -0
- data/lib/lobzique/xml/document.rb +149 -0
- data/lib/lobzique/xml/document_fragment.rb +109 -0
- data/lib/lobzique/xml/dtd.rb +28 -0
- data/lib/lobzique/xml/namespace.rb +19 -0
- data/lib/lobzique/xml/node/save_options.rb +52 -0
- data/lib/lobzique/xml/node.rb +629 -0
- data/lib/lobzique/xml/node_set.rb +329 -0
- data/lib/lobzique/xml/parse_options.rb +89 -0
- data/lib/lobzique/xml/pp/character_data.rb +22 -0
- data/lib/lobzique/xml/pp/node.rb +73 -0
- data/lib/lobzique/xml/pp.rb +6 -0
- data/lib/lobzique/xml/searchable.rb +123 -0
- data/lib/lobzique/xml/syntax_error.rb +77 -0
- data/lib/lobzique/xml/text.rb +14 -0
- data/lib/lobzique/xml/xpath/syntax_error.rb +20 -0
- data/lib/lobzique/xml/xpath.rb +16 -0
- data/lib/lobzique/xml/xpath_context.rb +27 -0
- data/lib/lobzique/xml.rb +22 -0
- data/lib/lobzique.rb +15 -0
- metadata +177 -0
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Adapted from Nokogiri 1.19.7, lib/nokogiri/xml/node.rb (MIT, see LICENSE-NOKOGIRI.md).
|
|
4
|
+
|
|
5
|
+
require "stringio"
|
|
6
|
+
|
|
7
|
+
module Lobzique
|
|
8
|
+
module XML
|
|
9
|
+
class Node
|
|
10
|
+
include PP::Node
|
|
11
|
+
include Searchable
|
|
12
|
+
include ClassResolver
|
|
13
|
+
include Enumerable
|
|
14
|
+
|
|
15
|
+
# libxml2's node types, which `#type` returns
|
|
16
|
+
ELEMENT_NODE = 1
|
|
17
|
+
ATTRIBUTE_NODE = 2
|
|
18
|
+
TEXT_NODE = 3
|
|
19
|
+
CDATA_SECTION_NODE = 4
|
|
20
|
+
ENTITY_REF_NODE = 5
|
|
21
|
+
ENTITY_NODE = 6
|
|
22
|
+
PI_NODE = 7
|
|
23
|
+
COMMENT_NODE = 8
|
|
24
|
+
DOCUMENT_NODE = 9
|
|
25
|
+
DOCUMENT_TYPE_NODE = 10
|
|
26
|
+
DOCUMENT_FRAG_NODE = 11
|
|
27
|
+
NOTATION_NODE = 12
|
|
28
|
+
HTML_DOCUMENT_NODE = 13
|
|
29
|
+
DTD_NODE = 14
|
|
30
|
+
ELEMENT_DECL = 15
|
|
31
|
+
ATTRIBUTE_DECL = 16
|
|
32
|
+
ENTITY_DECL = 17
|
|
33
|
+
NAMESPACE_DECL = 18
|
|
34
|
+
XINCLUDE_START = 19
|
|
35
|
+
XINCLUDE_END = 20
|
|
36
|
+
DOCB_DOCUMENT_NODE = 21
|
|
37
|
+
|
|
38
|
+
# Runs after the native `Node.new` has made the node, so that subclasses
|
|
39
|
+
# can override it. Intentionally empty.
|
|
40
|
+
def initialize(name, document)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# A copy of the node: 0 copies the node alone, 2 also its attributes,
|
|
44
|
+
# and 1, the default, the whole subtree.
|
|
45
|
+
def dup(level = 1, new_parent_doc = document)
|
|
46
|
+
super().initialize_copy_with_args(self, level, new_parent_doc)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def clone(level = 1, new_parent_doc = document)
|
|
50
|
+
super().initialize_copy_with_args(self, level, new_parent_doc)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def decorate!
|
|
54
|
+
document.decorate(self)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# :section: Manipulating Document Structure
|
|
58
|
+
|
|
59
|
+
def add_child(node_or_tags)
|
|
60
|
+
node_or_tags = coerce(node_or_tags)
|
|
61
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
|
62
|
+
node_or_tags.each { |n| add_child_node_and_reparent_attrs(n) }
|
|
63
|
+
else
|
|
64
|
+
add_child_node_and_reparent_attrs(node_or_tags)
|
|
65
|
+
end
|
|
66
|
+
node_or_tags
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def prepend_child(node_or_tags)
|
|
70
|
+
if (first = children.first)
|
|
71
|
+
# Mimic the error add_child would raise.
|
|
72
|
+
raise "Document already has a root node" if document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
|
73
|
+
|
|
74
|
+
first.__send__(:add_sibling, :previous, node_or_tags)
|
|
75
|
+
else
|
|
76
|
+
add_child(node_or_tags)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def wrap(node_or_tags)
|
|
81
|
+
case node_or_tags
|
|
82
|
+
when String
|
|
83
|
+
context_node = parent || document
|
|
84
|
+
new_parent = context_node.coerce(node_or_tags).first
|
|
85
|
+
if new_parent.nil?
|
|
86
|
+
raise "Failed to parse '#{node_or_tags}' in the context of a '#{context_node.name}' element"
|
|
87
|
+
end
|
|
88
|
+
when Node
|
|
89
|
+
new_parent = node_or_tags.dup
|
|
90
|
+
else
|
|
91
|
+
raise ArgumentError, "Requires a String or Node argument, and cannot accept a #{node_or_tags.class}"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
if parent
|
|
95
|
+
add_next_sibling(new_parent)
|
|
96
|
+
else
|
|
97
|
+
new_parent.unlink
|
|
98
|
+
end
|
|
99
|
+
new_parent.add_child(self)
|
|
100
|
+
|
|
101
|
+
self
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def <<(node_or_tags)
|
|
105
|
+
add_child(node_or_tags)
|
|
106
|
+
self
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def add_previous_sibling(node_or_tags)
|
|
110
|
+
if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
|
111
|
+
raise ArgumentError, "A document may not have multiple root nodes."
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
add_sibling(:previous, node_or_tags)
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def add_next_sibling(node_or_tags)
|
|
118
|
+
if parent&.document? && !(node_or_tags.comment? || node_or_tags.processing_instruction?)
|
|
119
|
+
raise ArgumentError, "A document may not have multiple root nodes."
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
add_sibling(:next, node_or_tags)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def before(node_or_tags)
|
|
126
|
+
add_previous_sibling(node_or_tags)
|
|
127
|
+
self
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def after(node_or_tags)
|
|
131
|
+
add_next_sibling(node_or_tags)
|
|
132
|
+
self
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def inner_html=(node_or_tags)
|
|
136
|
+
self.children = node_or_tags
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def children=(node_or_tags)
|
|
140
|
+
node_or_tags = coerce(node_or_tags)
|
|
141
|
+
children.unlink
|
|
142
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
|
143
|
+
node_or_tags.each { |n| add_child_node_and_reparent_attrs(n) }
|
|
144
|
+
else
|
|
145
|
+
add_child_node_and_reparent_attrs(node_or_tags)
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def replace(node_or_tags)
|
|
150
|
+
raise("Cannot replace a node with no parent") unless parent
|
|
151
|
+
|
|
152
|
+
# We cannot replace a text node directly, otherwise libxml will return
|
|
153
|
+
# an internal error at parser.c:13031, I don't know exactly why
|
|
154
|
+
# libxml is trying to find a parent node that is an element or document
|
|
155
|
+
# so I can't tell if this is bug in libxml or not. issue #775.
|
|
156
|
+
if text?
|
|
157
|
+
replacee = XML::Node.new("dummy", document)
|
|
158
|
+
add_previous_sibling_node(replacee)
|
|
159
|
+
unlink
|
|
160
|
+
return replacee.replace(node_or_tags)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
node_or_tags = parent.coerce(node_or_tags)
|
|
164
|
+
|
|
165
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
|
166
|
+
node_or_tags.each { |n| add_previous_sibling(n) }
|
|
167
|
+
unlink
|
|
168
|
+
else
|
|
169
|
+
replace_node(node_or_tags)
|
|
170
|
+
end
|
|
171
|
+
node_or_tags
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def swap(node_or_tags)
|
|
175
|
+
replace(node_or_tags)
|
|
176
|
+
self
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
# Sets the content to +string+ as text, escaped for the native setter,
|
|
180
|
+
# which unescapes it.
|
|
181
|
+
def content=(string)
|
|
182
|
+
self.native_content = encode_special_chars(string.to_s)
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def parent=(parent_node)
|
|
186
|
+
parent_node.add_child(self)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
# lobzique: default_namespace=, namespace= and do_xinclude need namespace
|
|
190
|
+
# definitions and XInclude, which are out of scope.
|
|
191
|
+
|
|
192
|
+
alias_method :next, :next_sibling
|
|
193
|
+
alias_method :previous, :previous_sibling
|
|
194
|
+
alias_method :next=, :add_next_sibling
|
|
195
|
+
alias_method :previous=, :add_previous_sibling
|
|
196
|
+
alias_method :remove, :unlink
|
|
197
|
+
alias_method :name=, :node_name=
|
|
198
|
+
alias_method :inner_text, :content
|
|
199
|
+
alias_method :text, :content
|
|
200
|
+
alias_method :to_str, :content
|
|
201
|
+
alias_method :name, :node_name
|
|
202
|
+
alias_method :type, :node_type
|
|
203
|
+
alias_method :elements, :element_children
|
|
204
|
+
|
|
205
|
+
# :section: Working With Node Attributes
|
|
206
|
+
|
|
207
|
+
def [](name)
|
|
208
|
+
get(name.to_s)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def []=(name, value)
|
|
212
|
+
set(name.to_s, value.to_s)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def attributes
|
|
216
|
+
attribute_nodes.each_with_object({}) do |node, hash|
|
|
217
|
+
hash[node.node_name] = node
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
|
|
221
|
+
def values
|
|
222
|
+
attribute_nodes.map(&:value)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def value?(value)
|
|
226
|
+
values.include?(value)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def keys
|
|
230
|
+
attribute_nodes.map(&:node_name)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def each
|
|
234
|
+
attribute_nodes.each do |node|
|
|
235
|
+
yield [node.node_name, node.value]
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def remove_attribute(name)
|
|
240
|
+
attributes[name].remove if key?(name)
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
def classes
|
|
244
|
+
kwattr_values("class")
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
def add_class(names)
|
|
248
|
+
kwattr_add("class", names)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
def append_class(names)
|
|
252
|
+
kwattr_append("class", names)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def remove_class(names = nil)
|
|
256
|
+
kwattr_remove("class", names)
|
|
257
|
+
end
|
|
258
|
+
|
|
259
|
+
def kwattr_values(attribute_name)
|
|
260
|
+
keywordify(get_attribute(attribute_name) || [])
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def kwattr_add(attribute_name, keywords)
|
|
264
|
+
keywords = keywordify(keywords)
|
|
265
|
+
current_kws = kwattr_values(attribute_name)
|
|
266
|
+
new_kws = (current_kws + (keywords - current_kws)).join(" ")
|
|
267
|
+
set_attribute(attribute_name, new_kws)
|
|
268
|
+
self
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def kwattr_append(attribute_name, keywords)
|
|
272
|
+
keywords = keywordify(keywords)
|
|
273
|
+
current_kws = kwattr_values(attribute_name)
|
|
274
|
+
new_kws = (current_kws + keywords).join(" ")
|
|
275
|
+
set_attribute(attribute_name, new_kws)
|
|
276
|
+
self
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def kwattr_remove(attribute_name, keywords)
|
|
280
|
+
if keywords.nil?
|
|
281
|
+
remove_attribute(attribute_name)
|
|
282
|
+
return self
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
keywords = keywordify(keywords)
|
|
286
|
+
current_kws = kwattr_values(attribute_name)
|
|
287
|
+
new_kws = current_kws - keywords
|
|
288
|
+
if new_kws.empty?
|
|
289
|
+
remove_attribute(attribute_name)
|
|
290
|
+
else
|
|
291
|
+
set_attribute(attribute_name, new_kws.join(" "))
|
|
292
|
+
end
|
|
293
|
+
self
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
alias_method :delete, :remove_attribute
|
|
297
|
+
alias_method :get_attribute, :[]
|
|
298
|
+
alias_method :attr, :[]
|
|
299
|
+
alias_method :set_attribute, :[]=
|
|
300
|
+
alias_method :has_attribute?, :key?
|
|
301
|
+
|
|
302
|
+
def matches?(selector)
|
|
303
|
+
ancestors.last.search(selector).include?(self)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
def fragment(tags)
|
|
307
|
+
document.related_class("DocumentFragment").new(document, tags, self)
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
def parse(string_or_io, options = nil)
|
|
311
|
+
##
|
|
312
|
+
# When the current node is unparented and not an element node, use the
|
|
313
|
+
# document as the parsing context instead. Otherwise, the in-context
|
|
314
|
+
# parser cannot find an element or a document node.
|
|
315
|
+
# Document Fragments are also not usable by the in-context parser.
|
|
316
|
+
if !element? && !document? && (!parent || parent.fragment?)
|
|
317
|
+
return document.parse(string_or_io, options)
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
options ||= (document.html? ? ParseOptions::DEFAULT_HTML : ParseOptions::DEFAULT_XML)
|
|
321
|
+
options = ParseOptions.new(options) if Integer === options
|
|
322
|
+
yield options if block_given?
|
|
323
|
+
|
|
324
|
+
contents = if string_or_io.respond_to?(:read)
|
|
325
|
+
string_or_io.read
|
|
326
|
+
else
|
|
327
|
+
string_or_io
|
|
328
|
+
end
|
|
329
|
+
|
|
330
|
+
return NodeSet.new(document) if contents.empty?
|
|
331
|
+
|
|
332
|
+
# lobzique: in_context parses HTML only. XML parsing is out of scope
|
|
333
|
+
# (plan.md decision 4).
|
|
334
|
+
raise NotImplementedError, "#{XML::Document}.parse isn't supported" unless document.html?
|
|
335
|
+
|
|
336
|
+
error_count = document.errors.length
|
|
337
|
+
node_set = in_context(contents, options.to_i)
|
|
338
|
+
|
|
339
|
+
if document.errors.length > error_count
|
|
340
|
+
raise document.errors[error_count] unless options.recover?
|
|
341
|
+
|
|
342
|
+
# lobzique: the fallback of Nokogiri to a fragment parsed
|
|
343
|
+
# without the context is dropped. It emulates recovery for libxml2
|
|
344
|
+
# < 2.13, and the HTML5 parser always recovers.
|
|
345
|
+
end
|
|
346
|
+
|
|
347
|
+
node_set
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
def namespaces
|
|
351
|
+
namespace_scopes.each_with_object({}) do |ns, hash|
|
|
352
|
+
prefix = ns.prefix
|
|
353
|
+
key = prefix ? "xmlns:#{prefix}" : "xmlns"
|
|
354
|
+
hash[key] = ns.href
|
|
355
|
+
end
|
|
356
|
+
end
|
|
357
|
+
|
|
358
|
+
def comment?
|
|
359
|
+
type == COMMENT_NODE
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
def cdata?
|
|
363
|
+
type == CDATA_SECTION_NODE
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
def xml?
|
|
367
|
+
type == DOCUMENT_NODE
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
def html?
|
|
371
|
+
type == HTML_DOCUMENT_NODE
|
|
372
|
+
end
|
|
373
|
+
|
|
374
|
+
def document?
|
|
375
|
+
is_a?(XML::Document)
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def processing_instruction?
|
|
379
|
+
type == PI_NODE
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
def text?
|
|
383
|
+
type == TEXT_NODE
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def fragment?
|
|
387
|
+
type == DOCUMENT_FRAG_NODE
|
|
388
|
+
end
|
|
389
|
+
|
|
390
|
+
def read_only?
|
|
391
|
+
[NOTATION_NODE, ENTITY_NODE, ENTITY_DECL].include?(type)
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def element?
|
|
395
|
+
type == ELEMENT_NODE
|
|
396
|
+
end
|
|
397
|
+
|
|
398
|
+
alias_method :elem?, :element?
|
|
399
|
+
|
|
400
|
+
def to_s
|
|
401
|
+
document.xml? ? to_xml : to_html
|
|
402
|
+
end
|
|
403
|
+
|
|
404
|
+
def inner_html(*args)
|
|
405
|
+
children.map { |x| x.to_html(*args) }.join
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def css_path
|
|
409
|
+
path.split(%r{/}).filter_map do |part|
|
|
410
|
+
part.empty? ? nil : part.gsub(/\[(\d+)\]/, ':nth-of-type(\1)')
|
|
411
|
+
end.join(" > ")
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
def ancestors(selector = nil)
|
|
415
|
+
return NodeSet.new(document) unless respond_to?(:parent)
|
|
416
|
+
return NodeSet.new(document) unless parent
|
|
417
|
+
|
|
418
|
+
parents = [parent]
|
|
419
|
+
|
|
420
|
+
while parents.last.respond_to?(:parent)
|
|
421
|
+
break unless (ctx_parent = parents.last.parent)
|
|
422
|
+
|
|
423
|
+
parents << ctx_parent
|
|
424
|
+
end
|
|
425
|
+
|
|
426
|
+
return NodeSet.new(document, parents) unless selector
|
|
427
|
+
|
|
428
|
+
root = parents.last
|
|
429
|
+
search_results = root.search(selector)
|
|
430
|
+
|
|
431
|
+
NodeSet.new(document, parents.find_all do |parent|
|
|
432
|
+
search_results.include?(parent)
|
|
433
|
+
end)
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
def traverse(&block)
|
|
437
|
+
children.each { |j| j.traverse(&block) }
|
|
438
|
+
yield(self)
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
def accept(visitor)
|
|
442
|
+
visitor.visit(self)
|
|
443
|
+
end
|
|
444
|
+
|
|
445
|
+
def ==(other)
|
|
446
|
+
return false unless other
|
|
447
|
+
return false unless other.respond_to?(:pointer_id)
|
|
448
|
+
|
|
449
|
+
pointer_id == other.pointer_id
|
|
450
|
+
end
|
|
451
|
+
|
|
452
|
+
def <=>(other)
|
|
453
|
+
return unless other.is_a?(XML::Node)
|
|
454
|
+
return unless document == other.document
|
|
455
|
+
|
|
456
|
+
compare(other)
|
|
457
|
+
end
|
|
458
|
+
|
|
459
|
+
# :section: Serialization and Generating Output
|
|
460
|
+
|
|
461
|
+
# Save options can also be set with a block, which gets a SaveOptions.
|
|
462
|
+
def serialize(*args, &block)
|
|
463
|
+
options = if args.first.is_a?(Hash)
|
|
464
|
+
args.shift
|
|
465
|
+
else
|
|
466
|
+
{
|
|
467
|
+
encoding: args[0],
|
|
468
|
+
save_with: args[1]
|
|
469
|
+
}
|
|
470
|
+
end
|
|
471
|
+
|
|
472
|
+
options[:encoding] ||= document.encoding
|
|
473
|
+
encoding = Encoding.find(options[:encoding] || "UTF-8")
|
|
474
|
+
|
|
475
|
+
io = StringIO.new(String.new(encoding: encoding))
|
|
476
|
+
|
|
477
|
+
write_to(io, options, &block)
|
|
478
|
+
io.string
|
|
479
|
+
end
|
|
480
|
+
|
|
481
|
+
def to_html(options = {})
|
|
482
|
+
to_format(SaveOptions::DEFAULT_HTML, options)
|
|
483
|
+
end
|
|
484
|
+
|
|
485
|
+
def to_xml(options = {})
|
|
486
|
+
options[:save_with] ||= SaveOptions::DEFAULT_XML
|
|
487
|
+
serialize(options)
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
def to_xhtml(options = {})
|
|
491
|
+
to_format(SaveOptions::DEFAULT_XHTML, options)
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Options: :encoding (defaults to the document's), :indent_text (" "),
|
|
495
|
+
# :indent (2) and :save_with (SaveOptions::FORMAT).
|
|
496
|
+
def write_to(io, *options)
|
|
497
|
+
options = options.first.is_a?(Hash) ? options.shift : {}
|
|
498
|
+
encoding = options[:encoding] || options[0] || document.encoding
|
|
499
|
+
save_options = options[:save_with] || options[1] || SaveOptions::FORMAT
|
|
500
|
+
indent_times = options[:indent] || 2
|
|
501
|
+
indent_text = options[:indent_text] || " "
|
|
502
|
+
|
|
503
|
+
# Any string times 0 returns an empty string. Therefore, use the same
|
|
504
|
+
# string instead of generating a new empty string for every node with
|
|
505
|
+
# zero indentation.
|
|
506
|
+
indentation = indent_times.zero? ? "" : (indent_text * indent_times)
|
|
507
|
+
|
|
508
|
+
config = SaveOptions.new(save_options.to_i)
|
|
509
|
+
yield config if block_given?
|
|
510
|
+
|
|
511
|
+
encoding = encoding.is_a?(Encoding) ? encoding.name : encoding
|
|
512
|
+
|
|
513
|
+
native_write_to(io, encoding, indentation, config.options)
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
def write_html_to(io, options = {})
|
|
517
|
+
write_format_to(SaveOptions::DEFAULT_HTML, io, options)
|
|
518
|
+
end
|
|
519
|
+
|
|
520
|
+
def write_xhtml_to(io, options = {})
|
|
521
|
+
write_format_to(SaveOptions::DEFAULT_XHTML, io, options)
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
def write_xml_to(io, options = {})
|
|
525
|
+
options[:save_with] ||= SaveOptions::DEFAULT_XML
|
|
526
|
+
write_to(io, options)
|
|
527
|
+
end
|
|
528
|
+
|
|
529
|
+
DECONSTRUCT_KEYS = [:name, :attributes, :children, :namespace, :content, :elements, :inner_html].freeze # :nodoc:
|
|
530
|
+
DECONSTRUCT_METHODS = {attributes: :attribute_nodes}.freeze # :nodoc:
|
|
531
|
+
|
|
532
|
+
def deconstruct_keys(keys)
|
|
533
|
+
requested_keys = DECONSTRUCT_KEYS & keys
|
|
534
|
+
{}.tap do |values|
|
|
535
|
+
requested_keys.each do |key|
|
|
536
|
+
method = DECONSTRUCT_METHODS[key] || key
|
|
537
|
+
values[key] = send(method)
|
|
538
|
+
end
|
|
539
|
+
end
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
protected
|
|
543
|
+
|
|
544
|
+
# The nodes +data+ stands for: markup is parsed in the context of
|
|
545
|
+
# this node.
|
|
546
|
+
def coerce(data)
|
|
547
|
+
case data
|
|
548
|
+
when XML::NodeSet
|
|
549
|
+
return data
|
|
550
|
+
when XML::DocumentFragment
|
|
551
|
+
return data.children
|
|
552
|
+
when String
|
|
553
|
+
return fragment(data).children
|
|
554
|
+
when Document, XML::Attr
|
|
555
|
+
# unacceptable
|
|
556
|
+
when XML::Node
|
|
557
|
+
return data
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
raise ArgumentError, <<~EOERR
|
|
561
|
+
Requires a Node, NodeSet or String argument, and cannot accept a #{data.class}.
|
|
562
|
+
(You probably want to select a node from the Document with at() or search(), or create a new Node via Node.new().)
|
|
563
|
+
EOERR
|
|
564
|
+
end
|
|
565
|
+
|
|
566
|
+
private
|
|
567
|
+
|
|
568
|
+
def keywordify(keywords)
|
|
569
|
+
case keywords
|
|
570
|
+
when Enumerable
|
|
571
|
+
keywords
|
|
572
|
+
when String
|
|
573
|
+
keywords.scan(/\S+/)
|
|
574
|
+
else
|
|
575
|
+
raise ArgumentError,
|
|
576
|
+
"Keyword attributes must be passed as either a String or an Enumerable, but received #{keywords.class}"
|
|
577
|
+
end
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
def add_sibling(next_or_previous, node_or_tags)
|
|
581
|
+
raise("Cannot add sibling to a node with no parent") unless parent
|
|
582
|
+
|
|
583
|
+
impl = (next_or_previous == :next) ? :add_next_sibling_node : :add_previous_sibling_node
|
|
584
|
+
iter = (next_or_previous == :next) ? :reverse_each : :each
|
|
585
|
+
|
|
586
|
+
node_or_tags = parent.coerce(node_or_tags)
|
|
587
|
+
if node_or_tags.is_a?(XML::NodeSet)
|
|
588
|
+
if text?
|
|
589
|
+
pivot = XML::Node.new("dummy", document)
|
|
590
|
+
send(impl, pivot)
|
|
591
|
+
else
|
|
592
|
+
pivot = self
|
|
593
|
+
end
|
|
594
|
+
node_or_tags.send(iter) { |n| pivot.send(impl, n) }
|
|
595
|
+
pivot.unlink if text?
|
|
596
|
+
else
|
|
597
|
+
send(impl, node_or_tags)
|
|
598
|
+
end
|
|
599
|
+
node_or_tags
|
|
600
|
+
end
|
|
601
|
+
|
|
602
|
+
def to_format(save_option, options)
|
|
603
|
+
options[:save_with] = save_option unless options[:save_with]
|
|
604
|
+
serialize(options)
|
|
605
|
+
end
|
|
606
|
+
|
|
607
|
+
def write_format_to(save_option, io, options)
|
|
608
|
+
options[:save_with] ||= save_option
|
|
609
|
+
write_to(io, options)
|
|
610
|
+
end
|
|
611
|
+
|
|
612
|
+
def inspect_attributes
|
|
613
|
+
[:name, :namespace, :attribute_nodes, :children]
|
|
614
|
+
end
|
|
615
|
+
|
|
616
|
+
IMPLIED_XPATH_CONTEXTS = [".//"].freeze
|
|
617
|
+
|
|
618
|
+
def add_child_node_and_reparent_attrs(node)
|
|
619
|
+
add_child_node(node)
|
|
620
|
+
node.attribute_nodes.find_all { |a| a.name.include?(":") }.each do |attr_node|
|
|
621
|
+
attr_node.remove
|
|
622
|
+
node[attr_node.name] = attr_node.value
|
|
623
|
+
end
|
|
624
|
+
end
|
|
625
|
+
end
|
|
626
|
+
end
|
|
627
|
+
end
|
|
628
|
+
|
|
629
|
+
require_relative "node/save_options"
|