Class: Moxml::Adapter::Oga

Inherits:
Base
  • Object
show all
Defined in:
lib/moxml/adapter/oga.rb

Constant Summary

Constants inherited from Base

Base::ENTITY_MARKER, Base::ENTITY_MARKER_RE, Base::ENTITY_NAME_PATTERN, Base::ENTITY_NAME_RE, Base::SERIALIZED_ENTITY_MARKER_RE, Base::STANDARD_ENTITIES

Class Method Summary collapse

Methods inherited from Base

actual_native, create_cdata, create_comment, create_declaration, create_doctype, create_element, create_entity_reference, create_namespace, create_processing_instruction, create_text, duplicate_node, in_scope_namespaces, patch_node, prepare_for_new_document, preprocess_entities, restore_entities, sax_supported?, set_attribute_name, set_attribute_value

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



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/moxml/adapter/oga.rb', line 281

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

  # Insert doctype before root element in document
  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



324
325
326
327
328
329
330
331
332
333
# File 'lib/moxml/adapter/oga.rb', line 324

def add_next_sibling(node, sibling)
  if node.parent == sibling.parent
    # Oga doesn't manipulate children of the same 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



313
314
315
316
317
318
319
320
321
322
# File 'lib/moxml/adapter/oga.rb', line 313

def add_previous_sibling(node, sibling)
  if node.parent == sibling.parent
    # Oga doesn't manipulate children of the same 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

Returns:

  • (Boolean)


208
209
210
# File 'lib/moxml/adapter/oga.rb', line 208

def adjacent_to_entity_reference?(node)
  entity_ref?(node.previous) || entity_ref?(node.next)
end

.at_xpath(node, expression, namespaces = nil) ⇒ Object



452
453
454
455
456
457
458
459
460
461
# File 'lib/moxml/adapter/oga.rb', line 452

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

.attachmentsObject



9
10
11
# File 'lib/moxml/adapter/oga.rb', line 9

def attachments
  @attachments ||= Moxml::NativeAttachment.new
end

.attribute_element(attr) ⇒ Object



240
241
242
# File 'lib/moxml/adapter/oga.rb', line 240

def attribute_element(attr)
  attr.element
end

.attributes(element) ⇒ Object



244
245
246
247
248
249
250
251
# File 'lib/moxml/adapter/oga.rb', line 244

def attributes(element)
  return [] unless element.is_a?(::Oga::XML::Element)

  # remove attributes-namespaces
  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



376
377
378
# File 'lib/moxml/adapter/oga.rb', line 376

def cdata_content(node)
  node.text
end

.children(node) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/moxml/adapter/oga.rb', line 184

def children(node)
  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
  # Filter out whitespace-only text nodes at document level only.
  # Document-level whitespace (between <?xml?> and <root>) is
  # formatting, not content, and differs across adapters.
  # Whitespace inside elements (e.g. "FigureA.1" spacing) is
  # meaningful and must be preserved.
  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

.comment_content(node) ⇒ Object



384
385
386
# File 'lib/moxml/adapter/oga.rb', line 384

def comment_content(node)
  node.text
end

.create_document(_native_doc = nil) ⇒ Object



59
60
61
# File 'lib/moxml/adapter/oga.rb', line 59

def create_document(_native_doc = nil)
  ::Oga::XML::Document.new
end

.create_native_cdata(content, _owner_doc = nil) ⇒ Object



82
83
84
# File 'lib/moxml/adapter/oga.rb', line 82

def create_native_cdata(content, _owner_doc = nil)
  ::Oga::XML::Cdata.new(text: content)
end

.create_native_comment(content, _owner_doc = nil) ⇒ Object



86
87
88
# File 'lib/moxml/adapter/oga.rb', line 86

def create_native_comment(content, _owner_doc = nil)
  ::Oga::XML::Comment.new(text: content)
end

.create_native_declaration(version, encoding, standalone) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/moxml/adapter/oga.rb', line 101

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



90
91
92
93
94
95
# File 'lib/moxml/adapter/oga.rb', line 90

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



63
64
65
# File 'lib/moxml/adapter/oga.rb', line 63

def create_native_element(name, _owner_doc = nil)
  ::Oga::XML::Element.new(name: name)
end

.create_native_entity_reference(name) ⇒ Object



71
72
73
74
75
76
# File 'lib/moxml/adapter/oga.rb', line 71

def create_native_entity_reference(name)
  text = ::Oga::XML::Text.new
  text.text = "#{self::ENTITY_MARKER}#{name};"
  attachments.set(text, :entity_name, name)
  text
end

.create_native_namespace(element, prefix, uri) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/moxml/adapter/oga.rb', line 126

def create_native_namespace(element, prefix, uri)
  ns = element.available_namespaces[prefix]
  return ns unless ns.nil?

  # Oga creates an attribute and registers a namespace
  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



97
98
99
# File 'lib/moxml/adapter/oga.rb', line 97

def create_native_processing_instruction(target, content)
  ::Oga::XML::ProcessingInstruction.new(name: target, text: content)
end

.create_native_text(content, _owner_doc = nil) ⇒ Object



67
68
69
# File 'lib/moxml/adapter/oga.rb', line 67

def create_native_text(content, _owner_doc = nil)
  ::Oga::XML::Text.new(text: preprocess_entities(content))
end

.declaration_attribute(declaration, attr_name) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/moxml/adapter/oga.rb', line 110

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



424
425
426
427
428
429
430
# File 'lib/moxml/adapter/oga.rb', line 424

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.



420
421
422
# File 'lib/moxml/adapter/oga.rb', line 420

def doctype_name(native)
  native.name
end

.doctype_system_id(native) ⇒ Object



432
433
434
435
436
437
438
# File 'lib/moxml/adapter/oga.rb', line 432

def doctype_system_id(native)
  if native.type == "SYSTEM"
    native.public_id
  else
    native.system_id
  end
end

.document(node) ⇒ Object



229
230
231
232
233
234
# File 'lib/moxml/adapter/oga.rb', line 229

def document(node)
  current = node
  current = current.parent while parent(current)

  current
end

.entity_ref?(node) ⇒ Boolean

Returns:

  • (Boolean)


212
213
214
215
# File 'lib/moxml/adapter/oga.rb', line 212

def entity_ref?(node)
  node.is_a?(::Oga::XML::Text) &&
    attachments.get(node, :entity_name)
end

.entity_reference_name(node) ⇒ Object



78
79
80
# File 'lib/moxml/adapter/oga.rb', line 78

def entity_reference_name(node)
  attachments.get(node, :entity_name)
end

.get_attribute(element, name) ⇒ Object



268
269
270
# File 'lib/moxml/adapter/oga.rb', line 268

def get_attribute(element, name)
  element.attribute(name.to_s)
end

.get_attribute_value(element, name) ⇒ Object



272
273
274
# File 'lib/moxml/adapter/oga.rb', line 272

def get_attribute_value(element, name)
  element[name.to_s]
end

.has_declaration?(native_doc, _wrapper) ⇒ Boolean

Returns:

  • (Boolean)


467
468
469
470
471
472
473
474
# File 'lib/moxml/adapter/oga.rb', line 467

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

.inner_text(node) ⇒ Object



359
360
361
362
363
364
365
# File 'lib/moxml/adapter/oga.rb', line 359

def inner_text(node)
  if node.is_a?(::Oga::XML::Element)
    node.inner_text
  else
    node.text
  end
end

.namespace(element) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/moxml/adapter/oga.rb', line 141

def namespace(element)
  case element
  when ::Oga::XML::Element, ::Oga::XML::Attribute
    element.namespace
  end
rescue NoMethodError
  # Oga attributes fail with NoMethodError:
  # undefined method `available_namespaces' for nil:NilClass
  nil
end

.namespace_definitions(node) ⇒ Object



411
412
413
414
415
# File 'lib/moxml/adapter/oga.rb', line 411

def namespace_definitions(node)
  return [] unless node.is_a?(::Oga::XML::Element)

  node.namespaces.values
end

.namespace_prefix(namespace) ⇒ Object



400
401
402
403
404
405
# File 'lib/moxml/adapter/oga.rb', line 400

def namespace_prefix(namespace)
  # nil for the default namespace
  return if namespace.name == ::Oga::XML::Element::XMLNS_PREFIX

  namespace.name
end

.namespace_uri(namespace) ⇒ Object



407
408
409
# File 'lib/moxml/adapter/oga.rb', line 407

def namespace_uri(namespace)
  namespace.uri
end

.next_sibling(node) ⇒ Object



221
222
223
# File 'lib/moxml/adapter/oga.rb', line 221

def next_sibling(node)
  node.next
end

.node_name(node) ⇒ Object



176
177
178
# File 'lib/moxml/adapter/oga.rb', line 176

def node_name(node)
  node.name
end

.node_type(node) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/moxml/adapter/oga.rb', line 156

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



217
218
219
# File 'lib/moxml/adapter/oga.rb', line 217

def parent(node)
  node.parent if node.is_a?(::Oga::XML::Node)
end

.parse(xml, options = {}, _context = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moxml/adapter/oga.rb', line 21

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

  ctx = _context || Context.new(:oga)
  DocumentBuilder.new(ctx).build(native_doc)
end

.previous_sibling(node) ⇒ Object



225
226
227
# File 'lib/moxml/adapter/oga.rb', line 225

def previous_sibling(node)
  node.previous
end

.processing_instruction_content(node) ⇒ Object



392
393
394
# File 'lib/moxml/adapter/oga.rb', line 392

def processing_instruction_content(node)
  node.text
end

.processing_instruction_target(node) ⇒ Object



152
153
154
# File 'lib/moxml/adapter/oga.rb', line 152

def processing_instruction_target(node)
  node.name
end

.remove(node) ⇒ Object



335
336
337
338
339
340
341
342
343
344
# File 'lib/moxml/adapter/oga.rb', line 335

def remove(node)
  # Special handling for declarations on Oga documents
  if node.is_a?(::Oga::XML::XmlDeclaration) &&
      node.parent.is_a?(::Oga::XML::Document)
    # Clear declaration state in attachment map
    attachments.set(node.parent, :xml_declaration, nil)
  end

  node.remove
end

.remove_attribute(element, name) ⇒ Object



276
277
278
279
# File 'lib/moxml/adapter/oga.rb', line 276

def remove_attribute(element, name)
  attr = element.attribute(name.to_s)
  element.attributes.delete(attr) if attr
end

.remove_declaration(native_doc) ⇒ Object



476
477
478
# File 'lib/moxml/adapter/oga.rb', line 476

def remove_declaration(native_doc)
  attachments.set(native_doc, :xml_declaration, nil)
end

.replace(node, new_node) ⇒ Object



346
347
348
# File 'lib/moxml/adapter/oga.rb', line 346

def replace(node, new_node)
  node.replace(new_node)
end

.replace_children(node, new_children) ⇒ Object



350
351
352
353
# File 'lib/moxml/adapter/oga.rb', line 350

def replace_children(node, new_children)
  node.children = []
  new_children.each { |child| add_child(node, child) }
end

.root(document) ⇒ Object



236
237
238
# File 'lib/moxml/adapter/oga.rb', line 236

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

Parameters:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/moxml/adapter/oga.rb', line 42

def sax_parse(xml, handler)
  bridge = OgaSAXBridge.new(handler)

  xml_string = xml.is_a?(IO) || xml.is_a?(StringIO) ? xml.read : xml.to_s

  # Manually call start_document (Oga doesn't)
  handler.on_start_document

  ::Oga.sax_parse_xml(bridge, xml_string)

  # Manually call end_document (Oga doesn't)
  handler.on_end_document
rescue StandardError => e
  error = Moxml::ParseError.new(e.message)
  handler.on_error(error)
end

.serialize(node, options = {}) ⇒ Object



463
464
465
# File 'lib/moxml/adapter/oga.rb', line 463

def serialize(node, options = {})
  serialize_without_entity_processing(node, options)
end

.set_attribute(element, name, value) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/moxml/adapter/oga.rb', line 253

def set_attribute(element, name, value)
  namespace_name = nil
  if name.to_s.include?(":")
    namespace_name, name = name.to_s.split(":",
                                           2)
  end

  attr = ::Oga::XML::Attribute.new(
    name: name.to_s,
    namespace_name: namespace_name,
    value: preprocess_entities(value.to_s),
  )
  element.add_attribute(attr)
end

.set_cdata_content(node, content) ⇒ Object



380
381
382
# File 'lib/moxml/adapter/oga.rb', line 380

def set_cdata_content(node, content)
  node.text = content
end

.set_comment_content(node, content) ⇒ Object



388
389
390
# File 'lib/moxml/adapter/oga.rb', line 388

def set_comment_content(node, content)
  node.text = content
end

.set_declaration_attribute(declaration, attr_name, value) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/moxml/adapter/oga.rb', line 118

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



137
138
139
# File 'lib/moxml/adapter/oga.rb', line 137

def set_namespace(element, ns_or_string)
  element.namespace_name = ns_or_string.to_s
end

.set_node_name(node, name) ⇒ Object



180
181
182
# File 'lib/moxml/adapter/oga.rb', line 180

def set_node_name(node, name)
  node.name = name
end

.set_processing_instruction_content(node, content) ⇒ Object



396
397
398
# File 'lib/moxml/adapter/oga.rb', line 396

def set_processing_instruction_content(node, content)
  node.text = content
end

.set_root(doc, element) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/moxml/adapter/oga.rb', line 13

def set_root(doc, element)
  # Clear existing root element if any - Oga's NodeSet needs special handling
  # We need to manually remove elements since NodeSet doesn't support clear or delete_if
  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



367
368
369
370
371
372
373
374
# File 'lib/moxml/adapter/oga.rb', line 367

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



355
356
357
# File 'lib/moxml/adapter/oga.rb', line 355

def text_content(node)
  node.text
end

.xpath(node, expression, namespaces = nil) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
# File 'lib/moxml/adapter/oga.rb', line 440

def xpath(node, expression, namespaces = nil)
  node.xpath(expression, {},
             namespaces: namespaces&.transform_keys(&:to_s)).to_a
rescue ::LL::ParserError => e
  raise Moxml::XPathError.new(
    e.message,
    expression: expression,
    adapter: "Oga",
    node: node,
  )
end