Class: Moxml::Adapter::Nokogiri
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Nokogiri
show all
- Defined in:
- lib/moxml/adapter/nokogiri.rb
Defined Under Namespace
Classes: NokogiriSAXBridge
Class Method Summary
collapse
-
.add_child(element, child) ⇒ Object
-
.add_next_sibling(node, sibling) ⇒ Object
-
.add_previous_sibling(node, sibling) ⇒ Object
-
.adjacent_to_entity_reference?(node) ⇒ Boolean
-
.at_xpath(node, expression, namespaces = nil) ⇒ Object
-
.attachments ⇒ Object
-
.attribute_element(attr) ⇒ Object
-
.attributes(element) ⇒ Object
-
.cdata_content(node) ⇒ Object
-
.children(node) ⇒ Object
-
.comment_content(node) ⇒ Object
-
.create_document(_native_doc = nil) ⇒ Object
-
.create_fragment ⇒ Object
-
.create_native_cdata(content, owner_doc = nil) ⇒ Object
-
.create_native_comment(content, owner_doc = nil) ⇒ Object
-
.create_native_declaration(version, encoding, standalone) ⇒ Object
-
.create_native_doctype(name, external_id, system_id) ⇒ Object
-
.create_native_element(name, owner_doc = nil) ⇒ Object
-
.create_native_entity_reference(name) ⇒ Object
-
.create_native_namespace(element, prefix, uri) ⇒ Object
-
.create_native_processing_instruction(target, content) ⇒ Object
-
.create_native_text(content, owner_doc = nil) ⇒ Object
-
.declaration_attribute(declaration, attr_name) ⇒ Object
-
.doctype_external_id(native) ⇒ Object
-
.doctype_name(native) ⇒ Object
Doctype accessor methods.
-
.doctype_system_id(native) ⇒ Object
-
.document(node) ⇒ Object
-
.entity_reference_name(node) ⇒ Object
-
.get_attribute(element, name) ⇒ Object
-
.get_attribute_value(element, name) ⇒ Object
-
.has_declaration?(native_doc, wrapper) ⇒ Boolean
-
.in_scope_namespaces(element) ⇒ Object
-
.inner_text(node) ⇒ Object
-
.line_number(node) ⇒ Object
-
.namespace(element) ⇒ Object
-
.namespace_definitions(node) ⇒ Object
-
.namespace_prefix(namespace) ⇒ Object
-
.namespace_uri(namespace) ⇒ Object
-
.next_sibling(node) ⇒ Object
-
.node_name(node) ⇒ Object
-
.node_type(node) ⇒ Object
-
.parent(node) ⇒ Object
-
.parse(xml, options = {}, _context = nil) ⇒ Object
-
.previous_sibling(node) ⇒ Object
-
.processing_instruction_content(node) ⇒ Object
-
.processing_instruction_target(node) ⇒ Object
-
.remove(node) ⇒ Object
-
.remove_attribute(element, name) ⇒ Object
-
.remove_declaration(native_doc) ⇒ Object
-
.replace(node, new_node) ⇒ Object
-
.replace_children(node, new_children) ⇒ Object
-
.root(document) ⇒ Object
-
.sax_parse(xml, handler) ⇒ void
SAX parsing implementation for Nokogiri.
-
.serialize(node, options = {}) ⇒ Object
-
.set_attribute(element, name, value) ⇒ Object
-
.set_cdata_content(node, content) ⇒ Object
-
.set_comment_content(node, content) ⇒ Object
-
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
-
.set_namespace(element, ns) ⇒ Object
-
.set_node_name(node, name) ⇒ Object
-
.set_processing_instruction_content(node, content) ⇒ Object
-
.set_root(doc, element) ⇒ Object
-
.set_text_content(node, content) ⇒ Object
-
.text_content(node) ⇒ Object
-
.xpath(node, expression, namespaces = nil) ⇒ Object
Methods inherited from Base
actual_native, attribute_name, bulk_materialize?, create_cdata, create_comment, create_declaration, create_doctype, create_element, create_entity_reference, create_namespace, create_processing_instruction, create_text, decode_entities, duplicate_node, entity_bearing?, patch_node, patches_children?, preprocess_entities, remove_attribute_native, restore_entities, sax_supported?, set_attribute_name, set_attribute_value, wrappers_recyclable?
Methods included from XmlUtils
#encode_entities, #normalize_xml_value, #validate_comment_content, #validate_declaration_encoding, #validate_declaration_standalone, #validate_declaration_version, #validate_element_name, #validate_entity_reference_name, #validate_pi_target, #validate_prefix, #validate_uri
Class Method Details
.add_child(element, child) ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
# File 'lib/moxml/adapter/nokogiri.rb', line 261
def add_child(element, child)
if element.is_a?(::Nokogiri::XML::Document) &&
child.is_a?(::Nokogiri::XML::ProcessingInstruction) &&
child.name == "xml"
version = declaration_attribute(child, "version") || "1.0"
encoding = declaration_attribute(child, "encoding")
standalone = declaration_attribute(child, "standalone")
attachments.set(element, :xml_decl, {
version: version,
encoding: encoding,
standalone: standalone,
}.compact)
return
end
if node_type(child) == :doctype
element.create_internal_subset(
child.name, child.external_id, child.system_id
)
else
element.add_child(child)
end
end
|
.add_next_sibling(node, sibling) ⇒ Object
290
291
292
|
# File 'lib/moxml/adapter/nokogiri.rb', line 290
def add_next_sibling(node, sibling)
node.add_next_sibling(sibling)
end
|
.add_previous_sibling(node, sibling) ⇒ Object
286
287
288
|
# File 'lib/moxml/adapter/nokogiri.rb', line 286
def add_previous_sibling(node, sibling)
node.add_previous_sibling(sibling)
end
|
.adjacent_to_entity_reference?(node) ⇒ Boolean
190
191
192
193
|
# File 'lib/moxml/adapter/nokogiri.rb', line 190
def adjacent_to_entity_reference?(node)
node.previous_sibling.is_a?(::Nokogiri::XML::EntityReference) ||
node.next_sibling.is_a?(::Nokogiri::XML::EntityReference)
end
|
.at_xpath(node, expression, namespaces = nil) ⇒ Object
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/moxml/adapter/nokogiri.rb', line 392
def at_xpath(node, expression, namespaces = nil)
node.at_xpath(expression, namespaces)
rescue ::Nokogiri::XML::XPath::SyntaxError => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "Nokogiri",
node: node,
)
end
|
.attachments ⇒ Object
11
12
13
|
# File 'lib/moxml/adapter/nokogiri.rb', line 11
def attachments
@attachments ||= Moxml::NativeAttachment.new
end
|
.attribute_element(attr) ⇒ Object
224
225
226
|
# File 'lib/moxml/adapter/nokogiri.rb', line 224
def attribute_element(attr)
attr.parent
end
|
.attributes(element) ⇒ Object
232
233
234
235
236
237
|
# File 'lib/moxml/adapter/nokogiri.rb', line 232
def attributes(element)
element.attribute_nodes
end
|
.cdata_content(node) ⇒ Object
325
326
327
|
# File 'lib/moxml/adapter/nokogiri.rb', line 325
def cdata_content(node)
node.content
end
|
.children(node) ⇒ Object
186
187
188
|
# File 'lib/moxml/adapter/nokogiri.rb', line 186
def children(node)
node.children
end
|
333
334
335
|
# File 'lib/moxml/adapter/nokogiri.rb', line 333
def (node)
node.content
end
|
.create_document(_native_doc = nil) ⇒ Object
70
71
72
|
# File 'lib/moxml/adapter/nokogiri.rb', line 70
def create_document(_native_doc = nil)
::Nokogiri::XML::Document.new
end
|
.create_fragment ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/moxml/adapter/nokogiri.rb', line 74
def create_fragment
::Nokogiri::XML::DocumentFragment.new(
::Nokogiri::XML::Document.new,
)
end
|
.create_native_cdata(content, owner_doc = nil) ⇒ Object
90
91
92
|
# File 'lib/moxml/adapter/nokogiri.rb', line 90
def create_native_cdata(content, owner_doc = nil)
::Nokogiri::XML::CDATA.new(owner_doc || create_document, content)
end
|
94
95
96
|
# File 'lib/moxml/adapter/nokogiri.rb', line 94
def (content, owner_doc = nil)
::Nokogiri::XML::Comment.new(owner_doc || create_document, content)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
110
111
112
113
114
115
116
|
# File 'lib/moxml/adapter/nokogiri.rb', line 110
def create_native_declaration(version, encoding, standalone)
::Nokogiri::XML::ProcessingInstruction.new(
create_document,
"xml",
build_declaration_attrs(version, encoding, standalone),
)
end
|
.create_native_doctype(name, external_id, system_id) ⇒ Object
98
99
100
101
102
|
# File 'lib/moxml/adapter/nokogiri.rb', line 98
def create_native_doctype(name, external_id, system_id)
create_document.create_internal_subset(
name, external_id, system_id
)
end
|
.create_native_element(name, owner_doc = nil) ⇒ Object
82
83
84
|
# File 'lib/moxml/adapter/nokogiri.rb', line 82
def create_native_element(name, owner_doc = nil)
::Nokogiri::XML::Element.new(name, owner_doc || create_document)
end
|
.create_native_entity_reference(name) ⇒ Object
118
119
120
|
# File 'lib/moxml/adapter/nokogiri.rb', line 118
def create_native_entity_reference(name)
::Nokogiri::XML::EntityReference.new(create_document, name)
end
|
.create_native_namespace(element, prefix, uri) ⇒ Object
158
159
160
|
# File 'lib/moxml/adapter/nokogiri.rb', line 158
def create_native_namespace(element, prefix, uri)
element.add_namespace_definition(prefix, uri)
end
|
.create_native_processing_instruction(target, content) ⇒ Object
104
105
106
107
108
|
# File 'lib/moxml/adapter/nokogiri.rb', line 104
def create_native_processing_instruction(target, content)
::Nokogiri::XML::ProcessingInstruction.new(
::Nokogiri::XML::Document.new, target, content
)
end
|
.create_native_text(content, owner_doc = nil) ⇒ Object
86
87
88
|
# File 'lib/moxml/adapter/nokogiri.rb', line 86
def create_native_text(content, owner_doc = nil)
::Nokogiri::XML::Text.new(content, owner_doc || create_document)
end
|
.declaration_attribute(declaration, attr_name) ⇒ Object
126
127
128
129
130
131
|
# File 'lib/moxml/adapter/nokogiri.rb', line 126
def declaration_attribute(declaration, attr_name)
return nil unless declaration.content
match = declaration.content.match(/#{attr_name}="([^"]*)"/)
match && match[1]
end
|
.doctype_external_id(native) ⇒ Object
370
371
372
|
# File 'lib/moxml/adapter/nokogiri.rb', line 370
def doctype_external_id(native)
native.external_id
end
|
.doctype_name(native) ⇒ Object
366
367
368
|
# File 'lib/moxml/adapter/nokogiri.rb', line 366
def doctype_name(native)
native.name
end
|
.doctype_system_id(native) ⇒ Object
374
375
376
|
# File 'lib/moxml/adapter/nokogiri.rb', line 374
def doctype_system_id(native)
native.system_id
end
|
.document(node) ⇒ Object
212
213
214
|
# File 'lib/moxml/adapter/nokogiri.rb', line 212
def document(node)
node.document
end
|
.entity_reference_name(node) ⇒ Object
122
123
124
|
# File 'lib/moxml/adapter/nokogiri.rb', line 122
def entity_reference_name(node)
node.name
end
|
.get_attribute(element, name) ⇒ Object
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/moxml/adapter/nokogiri.rb', line 239
def get_attribute(element, name)
name_s = name.to_s
if name_s.include?(":")
prefix, local = name_s.split(":", 2)
href = element.namespace_definitions
.find { |ns| ns.prefix == prefix }&.href
element.attribute_with_ns(local, href)
else
element.attribute_with_ns(name_s, nil) ||
element.attributes[name_s]
end
end
|
.get_attribute_value(element, name) ⇒ Object
252
253
254
255
|
# File 'lib/moxml/adapter/nokogiri.rb', line 252
def get_attribute_value(element, name)
element[name.to_s]
end
|
.has_declaration?(native_doc, wrapper) ⇒ Boolean
438
439
440
441
442
443
444
|
# File 'lib/moxml/adapter/nokogiri.rb', line 438
def has_declaration?(native_doc, wrapper)
if attachments.key?(native_doc, :xml_decl)
!attachments.get(native_doc, :xml_decl).nil?
else
wrapper.has_xml_declaration
end
end
|
.in_scope_namespaces(element) ⇒ Object
361
362
363
|
# File 'lib/moxml/adapter/nokogiri.rb', line 361
def in_scope_namespaces(element)
element.namespace_scopes
end
|
.inner_text(node) ⇒ Object
314
315
316
317
318
319
|
# File 'lib/moxml/adapter/nokogiri.rb', line 314
def inner_text(node)
text_children = node.children.reject do |c|
c.element? || c.
end
text_children.map(&:content).join
end
|
.line_number(node) ⇒ Object
220
221
222
|
# File 'lib/moxml/adapter/nokogiri.rb', line 220
def line_number(node)
node.line
end
|
.namespace(element) ⇒ Object
150
151
152
|
# File 'lib/moxml/adapter/nokogiri.rb', line 150
def namespace(element)
element.namespace
end
|
.namespace_definitions(node) ⇒ Object
357
358
359
|
# File 'lib/moxml/adapter/nokogiri.rb', line 357
def namespace_definitions(node)
node.namespace_definitions
end
|
.namespace_prefix(namespace) ⇒ Object
349
350
351
|
# File 'lib/moxml/adapter/nokogiri.rb', line 349
def namespace_prefix(namespace)
namespace.prefix
end
|
.namespace_uri(namespace) ⇒ Object
353
354
355
|
# File 'lib/moxml/adapter/nokogiri.rb', line 353
def namespace_uri(namespace)
namespace.href
end
|
.next_sibling(node) ⇒ Object
204
205
206
|
# File 'lib/moxml/adapter/nokogiri.rb', line 204
def next_sibling(node)
node.next_sibling
end
|
.node_name(node) ⇒ Object
178
179
180
|
# File 'lib/moxml/adapter/nokogiri.rb', line 178
def node_name(node)
node.name
end
|
.node_type(node) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/moxml/adapter/nokogiri.rb', line 162
def node_type(node)
case node
when ::Nokogiri::XML::Element then :element
when ::Nokogiri::XML::CDATA then :cdata
when ::Nokogiri::XML::Text then :text
when ::Nokogiri::XML::Comment then :comment
when ::Nokogiri::XML::Attr then :attribute
when ::Nokogiri::XML::Namespace then :namespace
when ::Nokogiri::XML::ProcessingInstruction then :processing_instruction
when ::Nokogiri::XML::Document, ::Nokogiri::XML::DocumentFragment then :document
when ::Nokogiri::XML::DTD then :doctype
when ::Nokogiri::XML::EntityReference then :entity_reference
else :unknown
end
end
|
.parent(node) ⇒ Object
200
201
202
|
# File 'lib/moxml/adapter/nokogiri.rb', line 200
def parent(node)
node.parent
end
|
.parse(xml, options = {}, _context = nil) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/moxml/adapter/nokogiri.rb', line 19
def parse(xml, options = {}, _context = nil)
processed_xml = preprocess_entities(xml)
native_doc = begin
if options[:fragment]
::Nokogiri::XML::DocumentFragment.parse(processed_xml) do |config|
config.strict.nonet
config.recover unless options[:strict]
end
else
::Nokogiri::XML(processed_xml, nil, "UTF-8") do |config|
config.strict.nonet
config.recover unless options[:strict]
end
end
rescue ::Nokogiri::XML::SyntaxError => e
raise Moxml::ParseError.new(e.message, line: e.line,
column: e.column)
end
ctx = _context || Context.new(:nokogiri)
Document.new(native_doc, ctx)
end
|
.previous_sibling(node) ⇒ Object
208
209
210
|
# File 'lib/moxml/adapter/nokogiri.rb', line 208
def previous_sibling(node)
node.previous_sibling
end
|
.processing_instruction_content(node) ⇒ Object
341
342
343
|
# File 'lib/moxml/adapter/nokogiri.rb', line 341
def processing_instruction_content(node)
node.content
end
|
.processing_instruction_target(node) ⇒ Object
154
155
156
|
# File 'lib/moxml/adapter/nokogiri.rb', line 154
def processing_instruction_target(node)
node.name
end
|
.remove(node) ⇒ Object
294
295
296
297
298
299
300
301
302
303
304
|
# File 'lib/moxml/adapter/nokogiri.rb', line 294
def remove(node)
if node.is_a?(::Nokogiri::XML::ProcessingInstruction) &&
node.name == "xml" &&
node.parent.is_a?(::Nokogiri::XML::Document)
attachments.set(node.parent, :xml_decl, nil)
end
node.remove
end
|
.remove_attribute(element, name) ⇒ Object
257
258
259
|
# File 'lib/moxml/adapter/nokogiri.rb', line 257
def remove_attribute(element, name)
element.remove_attribute(name.to_s)
end
|
.remove_declaration(native_doc) ⇒ Object
446
447
448
|
# File 'lib/moxml/adapter/nokogiri.rb', line 446
def remove_declaration(native_doc)
attachments.set(native_doc, :xml_decl, nil)
end
|
.replace(node, new_node) ⇒ Object
306
307
308
|
# File 'lib/moxml/adapter/nokogiri.rb', line 306
def replace(node, new_node)
node.replace(new_node)
end
|
.replace_children(node, new_children) ⇒ Object
195
196
197
198
|
# File 'lib/moxml/adapter/nokogiri.rb', line 195
def replace_children(node, new_children)
node.children.unlink
new_children.each { |child| add_child(node, child) }
end
|
.root(document) ⇒ Object
216
217
218
|
# File 'lib/moxml/adapter/nokogiri.rb', line 216
def root(document)
document.is_a?(::Nokogiri::XML::Document) ? document.root : document.children.first
end
|
.sax_parse(xml, handler) ⇒ void
This method returns an undefined value.
SAX parsing implementation for Nokogiri
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/moxml/adapter/nokogiri.rb', line 51
def sax_parse(xml, handler)
bridge = NokogiriSAXBridge.new(handler)
parser = ::Nokogiri::XML::SAX::Parser.new(bridge)
if xml.is_a?(IO) || xml.is_a?(StringIO)
parser.parse(xml)
else
parser.parse(xml.to_s)
end
rescue ::Nokogiri::XML::SyntaxError => e
error = Moxml::ParseError.new(e.message, line: e.line,
column: e.column)
handler.on_error(error)
end
|
.serialize(node, options = {}) ⇒ Object
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
|
# File 'lib/moxml/adapter/nokogiri.rb', line 403
def serialize(node, options = {})
save_options = ::Nokogiri::XML::Node::SaveOptions::AS_XML
if options[:expand_empty]
save_options |= ::Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS
end
if options[:indent].to_i.positive?
save_options |= ::Nokogiri::XML::Node::SaveOptions::FORMAT
end
custom_decl = nil
if options[:no_declaration]
save_options |= ::Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
elsif attachments.key?(node, :xml_decl) && (xml_decl = attachments.get(node, :xml_decl))
save_options |= ::Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
attrs = ["version=\"#{xml_decl[:version]}\""]
attrs << "encoding=\"#{xml_decl[:encoding]}\"" if xml_decl[:encoding]
attrs << "standalone=\"#{xml_decl[:standalone]}\"" if xml_decl[:standalone]
custom_decl = "<?xml #{attrs.join(' ')}?>"
end
result = node.to_xml(
indent: options[:indent],
encoding: options[:encoding],
save_with: save_options,
)
if custom_decl
result = "#{custom_decl}\n#{result}"
end
result
end
|
.set_attribute(element, name, value) ⇒ Object
228
229
230
|
# File 'lib/moxml/adapter/nokogiri.rb', line 228
def set_attribute(element, name, value)
element[name.to_s] = value.to_s
end
|
.set_cdata_content(node, content) ⇒ Object
329
330
331
|
# File 'lib/moxml/adapter/nokogiri.rb', line 329
def set_cdata_content(node, content)
node.content = content
end
|
337
338
339
|
# File 'lib/moxml/adapter/nokogiri.rb', line 337
def (node, content)
node.native_content = content
end
|
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/moxml/adapter/nokogiri.rb', line 133
def set_declaration_attribute(declaration, attr_name, value)
attrs = current_declaration_attributes(declaration)
if value.nil?
attrs.delete(attr_name)
else
attrs[attr_name] = value
end
declaration.native_content =
attrs.map { |k, v| %(#{k}="#{v}") }.join(" ")
end
|
.set_namespace(element, ns) ⇒ Object
145
146
147
148
|
# File 'lib/moxml/adapter/nokogiri.rb', line 145
def set_namespace(element, ns)
element.namespace = ns
element
end
|
.set_node_name(node, name) ⇒ Object
182
183
184
|
# File 'lib/moxml/adapter/nokogiri.rb', line 182
def set_node_name(node, name)
node.name = name
end
|
.set_processing_instruction_content(node, content) ⇒ Object
345
346
347
|
# File 'lib/moxml/adapter/nokogiri.rb', line 345
def set_processing_instruction_content(node, content)
node.native_content = content
end
|
.set_root(doc, element) ⇒ Object
15
16
17
|
# File 'lib/moxml/adapter/nokogiri.rb', line 15
def set_root(doc, element)
doc.root = element
end
|
.set_text_content(node, content) ⇒ Object
321
322
323
|
# File 'lib/moxml/adapter/nokogiri.rb', line 321
def set_text_content(node, content)
node.native_content = content
end
|
.text_content(node) ⇒ Object
310
311
312
|
# File 'lib/moxml/adapter/nokogiri.rb', line 310
def text_content(node)
node.text.to_s
end
|
.xpath(node, expression, namespaces = nil) ⇒ Object
378
379
380
381
382
383
384
385
386
387
388
389
390
|
# File 'lib/moxml/adapter/nokogiri.rb', line 378
def xpath(node, expression, namespaces = nil)
result = node.xpath(expression, namespaces)
result.is_a?(Enumerable) ? result.to_a : result
rescue ::Nokogiri::XML::XPath::SyntaxError => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "Nokogiri",
node: node,
)
end
|