Class: Moxml::Adapter::Oga
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Oga
show all
- Defined in:
- lib/moxml/adapter/oga.rb
Defined Under Namespace
Classes: OgaSAXBridge
Constant Summary
Constants included
from XmlUtils
XmlUtils::VALID_ELEMENT_NAMES
Class Method Summary
collapse
-
.add_child(element, child_or_text) ⇒ 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, **_entity_bearing) ⇒ Object
-
.comment_content(node) ⇒ Object
-
.create_document(_native_doc = nil) ⇒ 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, _owner_doc = nil) ⇒ 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 Note: Oga stores SYSTEM identifier in public_id for SYSTEM doctypes.
-
.doctype_system_id(native) ⇒ Object
-
.document(node) ⇒ Object
-
.duplicate_node(node) ⇒ Object
Oga's Node#dup shallow-copies the ivars, leaving attributes and children shared with the original; the public dup contract needs a full deep copy.
-
.entity_ref?(node) ⇒ Boolean
-
.entity_reference_name(node) ⇒ Object
-
.get_attribute(element, name) ⇒ Object
-
.get_attribute_value(element, name) ⇒ Object
-
.has_declaration?(native_doc, _wrapper) ⇒ Boolean
-
.indents_output? ⇒ Boolean
Whether this adapter's oga serialization honors the indent option (the customized generator currently does not; the probe keeps the indentation spec pending-or-live per environment).
-
.inner_text(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
-
.parse_fragment(xml, _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_attribute_native(attr) ⇒ 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 Oga.
-
.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_or_string) ⇒ 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, bare_get_qname_safe?, bare_set_qname_safe?, bulk_materialize?, children_accepts_entity_flag?, create_cdata, create_comment, create_declaration, create_doctype, create_element, create_entity_reference, create_namespace, create_processing_instruction, create_text, decode_entities, digest, entity_bearing?, expanded_attr_reads?, expanded_attr_value, free_document, in_scope_namespaces, iterparse, iterparse_file, line_number, materialize_fields, native_identity_stable?, native_inclusive10, parse_errors, parse_html, patch_node, patches_children?, plan_rows, preprocess_entities, restore_entities, same_node?, sax_supported?, serialize_generation, set_attribute_name, set_attribute_value, source_position, walk_descendants, wrap_native, 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_or_text) ⇒ Object
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
# File 'lib/moxml/adapter/oga.rb', line 380
def add_child(element, child_or_text)
child =
if child_or_text.is_a?(String)
create_native_text(child_or_text)
else
child_or_text
end
if element.is_a?(::Oga::XML::Document) &&
child.is_a?(::Oga::XML::XmlDeclaration)
attachments.set(element, :xml_declaration, child)
return
end
if element.is_a?(::Oga::XML::Document) && child.is_a?(::Oga::XML::Doctype)
root_idx = nil
element.children.each_with_index do |n, i|
if n.is_a?(::Oga::XML::Element)
root_idx = i
break
end
end
if root_idx
element.children.insert(root_idx, child)
return
end
end
element.children << child
end
|
.add_next_sibling(node, sibling) ⇒ Object
423
424
425
426
427
428
429
430
431
432
|
# File 'lib/moxml/adapter/oga.rb', line 423
def add_next_sibling(node, sibling)
if node.parent == sibling.parent
dup_sibling = node.node_set.delete(sibling)
index = node.node_set.index(node) + 1
node.node_set.insert(index, dup_sibling)
else
node.after(sibling)
end
end
|
.add_previous_sibling(node, sibling) ⇒ Object
412
413
414
415
416
417
418
419
420
421
|
# File 'lib/moxml/adapter/oga.rb', line 412
def add_previous_sibling(node, sibling)
if node.parent == sibling.parent
dup_sibling = node.node_set.delete(sibling)
index = node.node_set.index(node)
node.node_set.insert(index, dup_sibling)
else
node.before(sibling)
end
end
|
.adjacent_to_entity_reference?(node) ⇒ Boolean
259
260
261
|
# File 'lib/moxml/adapter/oga.rb', line 259
def adjacent_to_entity_reference?(node)
entity_ref?(node.previous) || entity_ref?(node.next)
end
|
.at_xpath(node, expression, namespaces = nil) ⇒ Object
553
554
555
556
557
558
559
560
561
562
|
# File 'lib/moxml/adapter/oga.rb', line 553
def at_xpath(node, expression, namespaces = nil)
node.at_xpath(expression, namespaces: namespaces)
rescue ::Oga::XPath::Error => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "Oga",
node: node,
)
end
|
.attribute_element(attr) ⇒ Object
328
329
330
|
# File 'lib/moxml/adapter/oga.rb', line 328
def attribute_element(attr)
attr.element
end
|
.attributes(element) ⇒ Object
332
333
334
335
336
337
338
339
|
# File 'lib/moxml/adapter/oga.rb', line 332
def attributes(element)
return [] unless element.is_a?(::Oga::XML::Element)
element.attributes.reject do |attr|
attr.name == ::Oga::XML::Element::XMLNS_PREFIX || attr.namespace_name == ::Oga::XML::Element::XMLNS_PREFIX
end
end
|
.cdata_content(node) ⇒ Object
475
476
477
|
# File 'lib/moxml/adapter/oga.rb', line 475
def cdata_content(node)
node.text
end
|
.children(node, **_entity_bearing) ⇒ Object
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
|
# File 'lib/moxml/adapter/oga.rb', line 235
def children(node, **_entity_bearing)
all_children = []
if node.is_a?(::Oga::XML::Document)
all_children += [node.xml_declaration,
node.doctype].compact
end
return all_children unless node.is_a?(::Oga::XML::Node) || node.is_a?(::Oga::XML::Document)
child_nodes = node.children.to_a
if node.is_a?(::Oga::XML::Document)
child_nodes = child_nodes.reject do |child|
child.is_a?(::Oga::XML::Text) && child.text.strip.empty?
end
end
all_children + child_nodes
end
|
483
484
485
|
# File 'lib/moxml/adapter/oga.rb', line 483
def (node)
node.text
end
|
.create_document(_native_doc = nil) ⇒ Object
87
88
89
|
# File 'lib/moxml/adapter/oga.rb', line 87
def create_document(_native_doc = nil)
::Oga::XML::Document.new
end
|
.create_native_cdata(content, _owner_doc = nil) ⇒ Object
117
118
119
|
# File 'lib/moxml/adapter/oga.rb', line 117
def create_native_cdata(content, _owner_doc = nil)
::Oga::XML::Cdata.new(text: content)
end
|
121
122
123
|
# File 'lib/moxml/adapter/oga.rb', line 121
def (content, _owner_doc = nil)
::Oga::XML::Comment.new(text: content)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
136
137
138
139
140
141
142
143
|
# File 'lib/moxml/adapter/oga.rb', line 136
def create_native_declaration(version, encoding, standalone)
attrs = {
version: version,
encoding: encoding,
standalone: standalone,
}.compact
::Moxml::Adapter::CustomizedOga::XmlDeclaration.new(attrs)
end
|
.create_native_doctype(name, external_id, system_id) ⇒ Object
125
126
127
128
129
130
|
# File 'lib/moxml/adapter/oga.rb', line 125
def create_native_doctype(name, external_id, system_id)
::Oga::XML::Doctype.new(
name: name, public_id: external_id, system_id: system_id,
type: external_id ? "PUBLIC" : "SYSTEM"
)
end
|
.create_native_element(name, _owner_doc = nil) ⇒ Object
91
92
93
|
# File 'lib/moxml/adapter/oga.rb', line 91
def create_native_element(name, _owner_doc = nil)
::Oga::XML::Element.new(name: name)
end
|
.create_native_entity_reference(name, _owner_doc = nil) ⇒ Object
106
107
108
109
110
111
|
# File 'lib/moxml/adapter/oga.rb', line 106
def create_native_entity_reference(name, _owner_doc = nil)
text = ::Oga::XML::Text.new
text.text = "#{Entity::MARKER}#{name};"
attachments.set(text, :entity_name, name)
text
end
|
.create_native_namespace(element, prefix, uri) ⇒ Object
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/moxml/adapter/oga.rb', line 161
def create_native_namespace(element, prefix, uri)
ns = element.available_namespaces[prefix]
return ns unless ns.nil?
set_attribute(element,
[::Oga::XML::Element::XMLNS_PREFIX, prefix].compact.join(":"), uri)
element.register_namespace(prefix, uri)
::Oga::XML::Namespace.new(name: prefix, uri: uri)
end
|
.create_native_processing_instruction(target, content) ⇒ Object
132
133
134
|
# File 'lib/moxml/adapter/oga.rb', line 132
def create_native_processing_instruction(target, content)
::Oga::XML::ProcessingInstruction.new(name: target, text: content)
end
|
.create_native_text(content, _owner_doc = nil) ⇒ Object
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/moxml/adapter/oga.rb', line 95
def create_native_text(content, _owner_doc = nil)
processed = preprocess_entities(content)
text = ::Oga::XML::Text.new(text: processed)
attachments.set(text, :raw_text, processed)
text
end
|
.declaration_attribute(declaration, attr_name) ⇒ Object
145
146
147
148
149
150
151
|
# File 'lib/moxml/adapter/oga.rb', line 145
def declaration_attribute(declaration, attr_name)
unless ::Moxml::Declaration::ALLOWED_ATTRIBUTES.include?(attr_name.to_s)
return
end
declaration.public_send(attr_name)
end
|
.doctype_external_id(native) ⇒ Object
523
524
525
526
527
528
529
|
# File 'lib/moxml/adapter/oga.rb', line 523
def doctype_external_id(native)
if native.type == "SYSTEM"
nil
else
native.public_id
end
end
|
.doctype_name(native) ⇒ Object
Doctype accessor methods
Note: Oga stores SYSTEM identifier in public_id for SYSTEM doctypes.
See: Oga::XML::Doctype puts SYSTEM dtd in public_id, system_id is nil.
519
520
521
|
# File 'lib/moxml/adapter/oga.rb', line 519
def doctype_name(native)
native.name
end
|
.doctype_system_id(native) ⇒ Object
531
532
533
534
535
536
537
|
# File 'lib/moxml/adapter/oga.rb', line 531
def doctype_system_id(native)
if native.type == "SYSTEM"
native.public_id
else
native.system_id
end
end
|
.document(node) ⇒ Object
312
313
314
315
316
317
|
# File 'lib/moxml/adapter/oga.rb', line 312
def document(node)
current = node
current = current.parent while parent(current)
current
end
|
.duplicate_node(node) ⇒ Object
Oga's Node#dup shallow-copies the ivars, leaving attributes
and children shared with the original; the public dup
contract needs a full deep copy.
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
# File 'lib/moxml/adapter/oga.rb', line 275
def duplicate_node(node)
case node
when ::Oga::XML::Element
copy = ::Oga::XML::Element.new(name: node.name)
copy.namespace_name = node.namespace_name if node.namespace_name
node.attributes.each do |attr|
copy.add_attribute(::Oga::XML::Attribute.new(
name: attr.name,
namespace_name: attr.namespace_name,
value: attr.value,
))
end
node.children.each do |child|
copy.children << duplicate_node(child)
end
copy
when ::Oga::XML::Text
::Oga::XML::Text.new(text: node.text)
when ::Oga::XML::Cdata
::Oga::XML::Cdata.new(text: node.text)
when ::Oga::XML::Comment
::Oga::XML::Comment.new(text: node.text)
when ::Oga::XML::ProcessingInstruction
::Oga::XML::ProcessingInstruction.new(name: node.name, text: node.text)
else
node.dup
end
end
|
.entity_ref?(node) ⇒ Boolean
263
264
265
266
|
# File 'lib/moxml/adapter/oga.rb', line 263
def entity_ref?(node)
node.is_a?(::Oga::XML::Text) &&
attachments.get(node, :entity_name)
end
|
.entity_reference_name(node) ⇒ Object
113
114
115
|
# File 'lib/moxml/adapter/oga.rb', line 113
def entity_reference_name(node)
attachments.get(node, :entity_name)
end
|
.get_attribute(element, name) ⇒ Object
360
361
362
|
# File 'lib/moxml/adapter/oga.rb', line 360
def get_attribute(element, name)
element.attribute(name.to_s)
end
|
.get_attribute_value(element, name) ⇒ Object
364
365
366
367
|
# File 'lib/moxml/adapter/oga.rb', line 364
def get_attribute_value(element, name)
attr = element.attribute(name.to_s)
attr&.value
end
|
.has_declaration?(native_doc, _wrapper) ⇒ Boolean
583
584
585
586
587
588
589
590
|
# File 'lib/moxml/adapter/oga.rb', line 583
def has_declaration?(native_doc, _wrapper)
decl = attachments.get(native_doc, :xml_declaration)
if decl.nil? && !attachments.key?(native_doc, :xml_declaration)
native_doc.is_a?(::Oga::XML::Document) && !native_doc.xml_declaration.nil?
else
!decl.nil?
end
end
|
.indents_output? ⇒ Boolean
Whether this adapter's oga serialization honors the
indent option (the customized generator currently does
not; the probe keeps the indentation spec pending-or-live
per environment).
568
569
570
571
572
573
574
575
576
577
|
# File 'lib/moxml/adapter/oga.rb', line 568
def indents_output?
return @indents_output unless @indents_output.nil?
probe = ::Oga::XML::Document.new
el = ::Oga::XML::Element.new(name: "r")
el.children << ::Oga::XML::Text.new(text: "t")
probe.children << el
@indents_output = serialize(probe, indent: 2).include?("
")
end
|
.inner_text(node) ⇒ Object
458
459
460
461
462
463
464
|
# File 'lib/moxml/adapter/oga.rb', line 458
def inner_text(node)
if node.is_a?(::Oga::XML::Element)
node.inner_text
else
node.text
end
end
|
.namespace(element) ⇒ Object
192
193
194
195
196
197
198
199
200
201
|
# File 'lib/moxml/adapter/oga.rb', line 192
def namespace(element)
case element
when ::Oga::XML::Element, ::Oga::XML::Attribute
element.namespace
end
rescue NoMethodError
nil
end
|
.namespace_definitions(node) ⇒ Object
510
511
512
513
514
|
# File 'lib/moxml/adapter/oga.rb', line 510
def namespace_definitions(node)
return [] unless node.is_a?(::Oga::XML::Element)
node.namespaces.values
end
|
.namespace_prefix(namespace) ⇒ Object
499
500
501
502
503
504
|
# File 'lib/moxml/adapter/oga.rb', line 499
def namespace_prefix(namespace)
return if namespace.name == ::Oga::XML::Element::XMLNS_PREFIX
namespace.name
end
|
.namespace_uri(namespace) ⇒ Object
506
507
508
|
# File 'lib/moxml/adapter/oga.rb', line 506
def namespace_uri(namespace)
namespace.uri
end
|
.next_sibling(node) ⇒ Object
304
305
306
|
# File 'lib/moxml/adapter/oga.rb', line 304
def next_sibling(node)
node.next
end
|
.node_name(node) ⇒ Object
227
228
229
|
# File 'lib/moxml/adapter/oga.rb', line 227
def node_name(node)
node.name
end
|
.node_type(node) ⇒ Object
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/moxml/adapter/oga.rb', line 207
def node_type(node)
case node
when ::Oga::XML::Element then :element
when ::Oga::XML::Text
if attachments.key?(node, :entity_name)
:entity_reference
else
:text
end
when ::Oga::XML::Cdata then :cdata
when ::Oga::XML::Comment then :comment
when ::Oga::XML::Attribute then :attribute
when ::Oga::XML::Namespace then :namespace
when ::Oga::XML::ProcessingInstruction then :processing_instruction
when ::Oga::XML::Document then :document
when ::Oga::XML::Doctype then :doctype
else :unknown
end
end
|
.parent(node) ⇒ Object
268
269
270
|
# File 'lib/moxml/adapter/oga.rb', line 268
def parent(node)
node.parent if node.is_a?(::Oga::XML::Node)
end
|
.parse(xml, options = {}, _context = nil) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/moxml/adapter/oga.rb', line 31
def parse(xml, options = {}, _context = nil)
processed_xml = preprocess_entities(xml)
native_doc = begin
::Oga.parse_xml(processed_xml, strict: options[:strict])
rescue LL::ParserError => e
raise Moxml::ParseError.new(
e.message,
source: xml.is_a?(String) ? xml[0..100] : nil,
)
end
roots = native_doc.children.to_a.count do |child|
child.instance_of?(::Oga::XML::Element)
end
if roots > 1
raise Moxml::ParseError.new(
"multiple root elements",
source: xml.is_a?(String) ? xml[0..100] : nil,
)
end
ctx = _context || Context.new(:oga)
doc = Wrappers::Document.new(native_doc, ctx)
Entity::Restorer.new(doc).run if ctx.config.restore_entities
doc
end
|
.parse_fragment(xml, _context = nil) ⇒ Object
319
320
321
322
|
# File 'lib/moxml/adapter/oga.rb', line 319
def parse_fragment(xml, _context = nil)
doc = parse("<m>#{xml}</m>").native
children(root(doc))
end
|
.previous_sibling(node) ⇒ Object
308
309
310
|
# File 'lib/moxml/adapter/oga.rb', line 308
def previous_sibling(node)
node.previous
end
|
.processing_instruction_content(node) ⇒ Object
491
492
493
|
# File 'lib/moxml/adapter/oga.rb', line 491
def processing_instruction_content(node)
node.text
end
|
.processing_instruction_target(node) ⇒ Object
203
204
205
|
# File 'lib/moxml/adapter/oga.rb', line 203
def processing_instruction_target(node)
node.name
end
|
.remove(node) ⇒ Object
434
435
436
437
438
439
440
441
442
443
|
# File 'lib/moxml/adapter/oga.rb', line 434
def remove(node)
if node.is_a?(::Oga::XML::XmlDeclaration) &&
node.parent.is_a?(::Oga::XML::Document)
attachments.set(node.parent, :xml_declaration, nil)
end
node.remove
end
|
.remove_attribute(element, name) ⇒ Object
369
370
371
372
|
# File 'lib/moxml/adapter/oga.rb', line 369
def remove_attribute(element, name)
attr = element.attribute(name.to_s)
element.attributes.delete(attr) if attr
end
|
.remove_attribute_native(attr) ⇒ Object
374
375
376
377
378
|
# File 'lib/moxml/adapter/oga.rb', line 374
def remove_attribute_native(attr)
element = attr.element
element&.attributes&.delete(attr)
attr
end
|
.remove_declaration(native_doc) ⇒ Object
592
593
594
|
# File 'lib/moxml/adapter/oga.rb', line 592
def remove_declaration(native_doc)
attachments.set(native_doc, :xml_declaration, nil)
end
|
.replace(node, new_node) ⇒ Object
445
446
447
|
# File 'lib/moxml/adapter/oga.rb', line 445
def replace(node, new_node)
node.replace(new_node)
end
|
.replace_children(node, new_children) ⇒ Object
449
450
451
452
|
# File 'lib/moxml/adapter/oga.rb', line 449
def replace_children(node, new_children)
node.children = []
new_children.each { |child| add_child(node, child) }
end
|
.root(document) ⇒ Object
324
325
326
|
# File 'lib/moxml/adapter/oga.rb', line 324
def root(document)
document.children.find { |node| node.is_a?(::Oga::XML::Element) }
end
|
.sax_parse(xml, handler) ⇒ void
This method returns an undefined value.
SAX parsing implementation for Oga.
Driven off Oga.parse_xml plus a DOM walk because Oga 3.4's
native SAX parser decodes on_text content before delivery and
exposes no hook to override. The prepended EntityDecoderOverride
ensures node.text invoked during the walk produces the correct
single-pass decode, so no separate entity pass is needed here.
Trade-off: peak memory scales with document size (Oga 3.4 is the
final release of an unmaintained gem; the trade is accepted).
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/moxml/adapter/oga.rb', line 75
def sax_parse(xml, handler)
xml_string = xml.is_a?(IO) || xml.is_a?(StringIO) ? xml.read : xml.to_s
native_doc = ::Oga.parse_xml(xml_string)
bridge = OgaSAXBridge.new(handler)
bridge.on_start_document
native_doc.children.each { |child| bridge.emit(child) }
bridge.on_end_document
rescue StandardError => e
handler.on_error(Moxml::ParseError.new(e.message))
end
|
.serialize(node, options = {}) ⇒ Object
579
580
581
|
# File 'lib/moxml/adapter/oga.rb', line 579
def serialize(node, options = {})
serialize_without_entity_processing(node, options)
end
|
.set_attribute(element, name, value) ⇒ Object
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
# File 'lib/moxml/adapter/oga.rb', line 341
def set_attribute(element, name, value)
namespace_name = nil
if name.to_s.include?(":")
namespace_name, name = name.to_s.split(":",
2)
end
processed = preprocess_entities(value.to_s)
attr = ::Oga::XML::Attribute.new(
name: name.to_s,
namespace_name: namespace_name,
value: processed,
)
attachments.set(attr, :raw_value, processed)
element.add_attribute(attr)
end
|
.set_cdata_content(node, content) ⇒ Object
479
480
481
|
# File 'lib/moxml/adapter/oga.rb', line 479
def set_cdata_content(node, content)
node.text = content
end
|
487
488
489
|
# File 'lib/moxml/adapter/oga.rb', line 487
def (node, content)
node.text = content
end
|
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
153
154
155
156
157
158
159
|
# File 'lib/moxml/adapter/oga.rb', line 153
def set_declaration_attribute(declaration, attr_name, value)
unless ::Moxml::Declaration::ALLOWED_ATTRIBUTES.include?(attr_name.to_s)
return
end
declaration.public_send("#{attr_name}=", value)
end
|
.set_namespace(element, ns_or_string) ⇒ Object
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/moxml/adapter/oga.rb', line 172
def set_namespace(element, ns_or_string)
if ns_or_string.nil?
element.namespace_name = nil
if element.respond_to?(:name=) && element.name.to_s.include?(":")
element.name = element.name.split(":", 2)[-1]
end
set_attribute(element, "xmlns", "")
return element
end
if element.respond_to?(:name=) && element.name.to_s.include?(":")
element.name = element.name.split(":", 2)[-1]
end
element.namespace_name = ns_or_string.to_s
element
end
|
.set_node_name(node, name) ⇒ Object
231
232
233
|
# File 'lib/moxml/adapter/oga.rb', line 231
def set_node_name(node, name)
node.name = name
end
|
.set_processing_instruction_content(node, content) ⇒ Object
495
496
497
|
# File 'lib/moxml/adapter/oga.rb', line 495
def set_processing_instruction_content(node, content)
node.text = content
end
|
.set_root(doc, element) ⇒ Object
23
24
25
26
27
28
29
|
# File 'lib/moxml/adapter/oga.rb', line 23
def set_root(doc, element)
elements_to_remove = doc.children.grep(::Oga::XML::Element)
elements_to_remove.each { |elem| doc.children.delete(elem) }
doc.children << element
end
|
.set_text_content(node, content) ⇒ Object
466
467
468
469
470
471
472
473
|
# File 'lib/moxml/adapter/oga.rb', line 466
def set_text_content(node, content)
processed = preprocess_entities(content)
if node.is_a?(::Oga::XML::Element)
node.inner_text = processed
else
node.text = processed
end
end
|
.text_content(node) ⇒ Object
454
455
456
|
# File 'lib/moxml/adapter/oga.rb', line 454
def text_content(node)
node.text
end
|
.xpath(node, expression, namespaces = nil) ⇒ Object
539
540
541
542
543
544
545
546
547
548
549
550
551
|
# File 'lib/moxml/adapter/oga.rb', line 539
def xpath(node, expression, namespaces = nil)
result = node.xpath(expression, {},
namespaces: namespaces&.transform_keys(&:to_s))
result.is_a?(Enumerable) ? result.to_a : result
rescue ::LL::ParserError => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "Oga",
node: node,
)
end
|