Class: Moxml::Adapter::Rexml

Inherits:
Base
  • Object
show all
Defined in:
lib/moxml/adapter/rexml.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, patch_node, prepare_for_new_document, preprocess_entities, restore_entities, sax_supported?

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



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/moxml/adapter/rexml.rb', line 292

def add_child(element, child)
  # Special handling for declarations on REXML documents
  if element.is_a?(::REXML::Document) && child.is_a?(::REXML::XMLDecl)
    # Track declaration state in attachment map
    attachments.set(element, :xml_declaration, child)
  end

  case child
  when String
    element.add_text(child)
    append_child_sequence(element, :native)
  when ::Moxml::Adapter::CustomizedRexml::EntityReference
    # REXML doesn't support custom node types in its tree.
    # Store alongside native children via attachment map.
    refs = attachments.get(element, :entity_refs) || []
    refs << child
    attachments.set(element, :entity_refs, refs)
    append_child_sequence(element, :eref)
  else
    element.add(child)
    append_child_sequence(element, :native)
  end
end

.add_next_sibling(node, sibling) ⇒ Object



332
333
334
335
# File 'lib/moxml/adapter/rexml.rb', line 332

def add_next_sibling(node, sibling)
  parent = node.parent
  parent.insert_after(node, sibling)
end

.add_previous_sibling(node, sibling) ⇒ Object



322
323
324
325
326
327
328
329
330
# File 'lib/moxml/adapter/rexml.rb', line 322

def add_previous_sibling(node, sibling)
  parent = node.parent
  # caveat: Rexml fails if children belong to the same parent and are already in a correct order
  # example: "<root><a/><b/></root>"
  # add_previous_sibling(node_b, node_a)
  # result: "<root><b/><a/></root>"
  # expected result: "<root><a/><b/></root>"
  parent.insert_before(node, sibling)
end

.append_child_sequence(element, type) ⇒ Object



316
317
318
319
320
# File 'lib/moxml/adapter/rexml.rb', line 316

def append_child_sequence(element, type)
  seq = attachments.get(element, :child_sequence) || []
  seq << type
  attachments.set(element, :child_sequence, seq)
end

.at_xpath(node, expression, namespaces = {}) ⇒ Object



563
564
565
566
# File 'lib/moxml/adapter/rexml.rb', line 563

def at_xpath(node, expression, namespaces = {})
  results = xpath(node, expression, namespaces)
  results.first
end

.attachmentsObject



12
13
14
# File 'lib/moxml/adapter/rexml.rb', line 12

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

.attribute_element(attribute) ⇒ Object



258
259
260
# File 'lib/moxml/adapter/rexml.rb', line 258

def attribute_element(attribute)
  attribute.element
end

.attributes(element) ⇒ Object



250
251
252
253
254
255
256
# File 'lib/moxml/adapter/rexml.rb', line 250

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

  # Only return non-namespace attributes
  element.attributes.values
    .reject { |attr| attr.prefix.to_s.start_with?("xmlns") }
end

.cdata_content(node) ⇒ Object



386
387
388
# File 'lib/moxml/adapter/rexml.rb', line 386

def cdata_content(node)
  node.value
end

.children(node) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/moxml/adapter/rexml.rb', line 180

def children(node)
  return [] unless node.is_a?(::REXML::Parent)

  # Return all children preserving whitespace text nodes
  result = node.children.dup

  # Include any EntityReference wrappers stored alongside native children
  entity_refs = attachments.get(node, :entity_refs)
  result.concat(entity_refs) if entity_refs

  # Ensure uniqueness by object_id to prevent duplicates
  result.uniq(&:object_id)
end

.comment_content(node) ⇒ Object



378
379
380
# File 'lib/moxml/adapter/rexml.rb', line 378

def comment_content(node)
  node.string
end

.create_document(_native_doc = nil) ⇒ Object



80
81
82
# File 'lib/moxml/adapter/rexml.rb', line 80

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

.create_native_cdata(content, _owner_doc = nil) ⇒ Object



100
101
102
# File 'lib/moxml/adapter/rexml.rb', line 100

def create_native_cdata(content, _owner_doc = nil)
  ::REXML::CData.new(content.to_s)
end

.create_native_comment(content, _owner_doc = nil) ⇒ Object



104
105
106
# File 'lib/moxml/adapter/rexml.rb', line 104

def create_native_comment(content, _owner_doc = nil)
  ::REXML::Comment.new(content.to_s)
end

.create_native_declaration(version, encoding, standalone) ⇒ Object



113
114
115
# File 'lib/moxml/adapter/rexml.rb', line 113

def create_native_declaration(version, encoding, standalone)
  ::REXML::XMLDecl.new(version, encoding&.downcase, standalone)
end

.create_native_doctype(name, external_id, system_id) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/moxml/adapter/rexml.rb', line 117

def create_native_doctype(name, external_id, system_id)
  return nil unless name

  parts = [name]
  if external_id
    parts.push("PUBLIC", %("#{external_id}"))
    parts << %("#{system_id}") if system_id
  elsif system_id
    parts.push("SYSTEM", %("#{system_id}"))
  end

  ::REXML::DocType.new(parts.join(" "))
end

.create_native_element(name, _owner_doc = nil) ⇒ Object



84
85
86
# File 'lib/moxml/adapter/rexml.rb', line 84

def create_native_element(name, _owner_doc = nil)
  ::REXML::Element.new(name.to_s)
end

.create_native_entity_reference(name) ⇒ Object



92
93
94
# File 'lib/moxml/adapter/rexml.rb', line 92

def create_native_entity_reference(name)
  ::Moxml::Adapter::CustomizedRexml::EntityReference.new(name)
end

.create_native_namespace(element, prefix, uri) ⇒ Object

add a namespace definition, keep the element name unchanged



459
460
461
462
# File 'lib/moxml/adapter/rexml.rb', line 459

def create_native_namespace(element, prefix, uri)
  element.add_namespace(prefix.to_s, uri)
  ::REXML::Attribute.new(prefix.to_s, uri, element)
end

.create_native_processing_instruction(target, content) ⇒ Object



108
109
110
111
# File 'lib/moxml/adapter/rexml.rb', line 108

def create_native_processing_instruction(target, content)
  # Clone strings to avoid frozen string errors
  ::REXML::Instruction.new(target.to_s.dup, content.to_s.dup)
end

.create_native_text(content, _owner_doc = nil) ⇒ Object



88
89
90
# File 'lib/moxml/adapter/rexml.rb', line 88

def create_native_text(content, _owner_doc = nil)
  ::REXML::Text.new(content.to_s, true, nil)
end

.declaration_attribute(node, name) ⇒ Object



356
357
358
359
360
361
362
363
364
365
# File 'lib/moxml/adapter/rexml.rb', line 356

def declaration_attribute(node, name)
  case name
  when "version"
    node.version
  when "encoding"
    node.encoding
  when "standalone"
    node.standalone
  end
end

.doctype_external_id(native) ⇒ Object



520
521
522
# File 'lib/moxml/adapter/rexml.rb', line 520

def doctype_external_id(native)
  native.public
end

.doctype_name(native) ⇒ Object

Doctype accessor methods



516
517
518
# File 'lib/moxml/adapter/rexml.rb', line 516

def doctype_name(native)
  native.name
end

.doctype_system_id(native) ⇒ Object



524
525
526
# File 'lib/moxml/adapter/rexml.rb', line 524

def doctype_system_id(native)
  native.system
end

.document(node) ⇒ Object



242
243
244
# File 'lib/moxml/adapter/rexml.rb', line 242

def document(node)
  node.document
end

.duplicate_node(node) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/moxml/adapter/rexml.rb', line 172

def duplicate_node(node)
  if node.is_a?(::REXML::Parent)
    node.deep_clone
  else
    Marshal.load(Marshal.dump(node))
  end
end

.entity_reference_name(node) ⇒ Object



96
97
98
# File 'lib/moxml/adapter/rexml.rb', line 96

def entity_reference_name(node)
  node.name if node.is_a?(::Moxml::Adapter::CustomizedRexml::EntityReference)
end

.extract_encoding_from_xml(xml) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/moxml/adapter/rexml.rb', line 46

def extract_encoding_from_xml(xml)
  return "UTF-8" unless xml.start_with?("<?xml")

  decl_end = xml.index("?>")
  return "UTF-8" unless decl_end

  decl = xml[0...decl_end]
  match = decl.match(/encoding\s*=\s*["']([^"']+)["']/i)
  match ? match[1] : "UTF-8"
end

.extract_text_recursively(element) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/moxml/adapter/rexml.rb', line 418

def extract_text_recursively(element)
  return "" unless element

  text = ""
  element.children.each do |child|
    case child
    when ::REXML::Text
      # Preserve original spacing from text nodes exactly including newlines and all whitespace
      text += child.value
    when ::REXML::Element
      # Extract text recursively from child element
      child_text = extract_text_recursively(child)
      # Concatenate directly like other adapters - NO SPACE INSERTION
      text += child_text
    end
  end
  # Don't strip - preserve original spacing including newlines
  text
end

.get_attribute(element, name) ⇒ Object



280
281
282
# File 'lib/moxml/adapter/rexml.rb', line 280

def get_attribute(element, name)
  element.attributes.get_attribute(name)
end

.get_attribute_value(element, name) ⇒ Object



284
285
286
# File 'lib/moxml/adapter/rexml.rb', line 284

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

.has_declaration?(native_doc, wrapper) ⇒ Boolean

Returns:

  • (Boolean)


617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/moxml/adapter/rexml.rb', line 617

def has_declaration?(native_doc, wrapper)
  xml_decl = attachments.get(native_doc, :xml_declaration)
  if xml_decl.nil?
    if attachments.key?(native_doc, :xml_declaration)
      false
    else
      wrapper.has_xml_declaration
    end
  else
    true
  end
end

.in_scope_namespaces(element) ⇒ Object



505
506
507
508
509
510
511
512
513
# File 'lib/moxml/adapter/rexml.rb', line 505

def in_scope_namespaces(element)
  namespaces = {}
  element.namespaces.each do |prefix, uri|
    key = prefix.to_s.empty? ? "xmlns" : prefix.to_s
    ns = ::REXML::Attribute.new(key, uri, element)
    namespaces[prefix] = ns
  end
  namespaces.values
end

.inner_text(node) ⇒ Object



438
439
440
441
442
443
444
# File 'lib/moxml/adapter/rexml.rb', line 438

def inner_text(node)
  # Get direct text children only, filter duplicates
  text_children = node.children
    .grep(::REXML::Text)
    .uniq(&:object_id)
  text_children.map(&:value).join
end

.namespace(node) ⇒ Object



484
485
486
487
488
489
490
491
# File 'lib/moxml/adapter/rexml.rb', line 484

def namespace(node)
  prefix = node.prefix
  uri = node.namespace(prefix)
  return if prefix.to_s.empty? && uri.to_s.empty?

  owner = node.is_a?(::REXML::Attribute) ? node.element : node
  ::REXML::Attribute.new(prefix, uri, owner)
end

.namespace_definitions(node) ⇒ Object



493
494
495
496
497
498
499
500
501
502
503
# File 'lib/moxml/adapter/rexml.rb', line 493

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

  result = []
  node.attributes.each_attribute do |attr|
    next unless attr.prefix == "xmlns" || (attr.name == "xmlns" && attr.prefix.to_s.empty?)

    result << attr
  end
  result
end

.namespace_prefix(node) ⇒ Object



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

def namespace_prefix(node)
  node.name unless node.name == "xmlns"
end

.namespace_uri(node) ⇒ Object



480
481
482
# File 'lib/moxml/adapter/rexml.rb', line 480

def namespace_uri(node)
  node.value
end

.next_sibling(node) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/moxml/adapter/rexml.rb', line 198

def next_sibling(node)
  current = node.next_sibling

  seen = {}
  while current
    if current.is_a?(::REXML::Text) && current.to_s.strip.empty?
      current = current.next_sibling
      next
    end

    if seen[current.object_id]
      current = current.next_sibling
      next
    end

    seen[current.object_id] = true
    break
  end

  current
end

.node_name(node) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/moxml/adapter/rexml.rb', line 161

def node_name(node)
  case node
  when ::REXML::Element, ::REXML::DocType
    node.name
  when ::REXML::XMLDecl
    "xml"
  when ::REXML::Instruction
    node.target
  end
end

.node_type(node) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/moxml/adapter/rexml.rb', line 135

def node_type(node)
  case node
  when ::REXML::Document then :document
  when ::REXML::Element then :element
  when ::REXML::CData then :cdata
  when ::REXML::Text then :text
  when ::REXML::Comment then :comment
  when ::REXML::Attribute then :attribute # but in fact it may be a namespace as well
  when ::REXML::Namespace then :namespace # we don't use this one
  when ::REXML::Instruction then :processing_instruction
  when ::REXML::DocType then :doctype
  when ::REXML::XMLDecl then :declaration
  when ::Moxml::Adapter::CustomizedRexml::EntityReference then :entity_reference
  else :unknown
  end
end

.parent(node) ⇒ Object



194
195
196
# File 'lib/moxml/adapter/rexml.rb', line 194

def parent(node)
  node.parent
end

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



16
17
18
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/rexml.rb', line 16

def parse(xml, options = {}, _context = nil)
  xml = "" if xml.nil?

  # Handle frozen strings by creating a mutable copy
  processed_xml = if xml.frozen?
                    xml.dup.force_encoding("UTF-8").encode("UTF-8")
                  else
                    xml.force_encoding("UTF-8").encode("UTF-8")
                  end

  # Preprocess entities to avoid double-escaping on output
  processed_xml = preprocess_entities(processed_xml)

  native_doc = begin
    ::REXML::Document.new(processed_xml)
  rescue ::REXML::ParseException => e
    if options[:strict]
      raise Moxml::ParseError.new(
        e.message,
        line: e.line,
        source: xml.is_a?(String) ? xml[0..100] : nil,
      )
    end
    create_document
  end

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

.prepare_xpath_namespaces(node) ⇒ Object

not used at the moment but may be useful when the xpath is upgraded to work with namespaces



530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/moxml/adapter/rexml.rb', line 530

def prepare_xpath_namespaces(node)
  ns = {}

  # Get all namespace definitions in scope
  all_ns = namespace_definitions(node)

  # Convert to XPath-friendly format
  all_ns.each do |prefix, uri|
    if prefix.to_s.empty?
      ns["xmlns"] = uri
    else
      ns[prefix] = uri
    end
  end

  ns
end

.previous_sibling(node) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/moxml/adapter/rexml.rb', line 220

def previous_sibling(node)
  current = node.previous_sibling

  seen = {}
  while current
    if current.is_a?(::REXML::Text) && current.to_s.strip.empty?
      current = current.previous_sibling
      next
    end

    if seen[current.object_id]
      current = current.previous_sibling
      next
    end

    seen[current.object_id] = true
    break
  end

  current
end

.processing_instruction_content(node) ⇒ Object



398
399
400
# File 'lib/moxml/adapter/rexml.rb', line 398

def processing_instruction_content(node)
  node.content
end

.processing_instruction_target(node) ⇒ Object



394
395
396
# File 'lib/moxml/adapter/rexml.rb', line 394

def processing_instruction_target(node)
  node.target
end

.remove(node) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/moxml/adapter/rexml.rb', line 337

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

  node.remove
end

.remove_attribute(element, name) ⇒ Object



288
289
290
# File 'lib/moxml/adapter/rexml.rb', line 288

def remove_attribute(element, name)
  element.delete_attribute(name.to_s)
end

.remove_declaration(native_doc) ⇒ Object



630
631
632
# File 'lib/moxml/adapter/rexml.rb', line 630

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

.replace(node, new_node) ⇒ Object



347
348
349
# File 'lib/moxml/adapter/rexml.rb', line 347

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

.replace_children(element, children) ⇒ Object



351
352
353
354
# File 'lib/moxml/adapter/rexml.rb', line 351

def replace_children(element, children)
  element.children.each(&:remove)
  children.each { |child| element.add(child) }
end

.root(document) ⇒ Object



246
247
248
# File 'lib/moxml/adapter/rexml.rb', line 246

def root(document)
  document.root
end

.sax_parse(xml, handler) ⇒ void

This method returns an undefined value.

SAX parsing implementation for REXML

Parameters:



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/moxml/adapter/rexml.rb', line 62

def sax_parse(xml, handler)
  require "rexml/parsers/sax2parser"
  require "rexml/source"
  require "stringio"

  bridge = REXMLSAX2Bridge.new(handler)

  xml_string = xml.is_a?(IO) || xml.is_a?(StringIO) ? xml.read : xml.to_s
  source = ::REXML::IOSource.new(StringIO.new(xml_string))

  parser = ::REXML::Parsers::SAX2Parser.new(source)
  parser.listen(bridge)
  parser.parse
rescue ::REXML::ParseException => e
  error = Moxml::ParseError.new(e.message, line: e.line)
  handler.on_error(error)
end

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



568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
# File 'lib/moxml/adapter/rexml.rb', line 568

def serialize(node, options = {})
  output = StringIO.new("") if RUBY_ENGINE == "opal"
  output ||= +""

  if node.is_a?(::REXML::Document)
    # Check if we should include declaration
    # Priority: explicit option > check if document has xml_decl
    should_include_decl = if options.key?(:no_declaration)
                            !options[:no_declaration]
                          else
                            # Include declaration only if document has xml_decl
                            !node.xml_decl.nil?
                          end

    # Include XML declaration only if should_include_decl and xml_decl exists
    if should_include_decl && node.xml_decl
      decl = node.xml_decl
      decl.encoding = options[:encoding] if options[:encoding]
      output << "<?xml"
      output << %( version="#{decl.version}") if decl.version
      output << %( encoding="#{decl.encoding}") if decl.encoding
      output << %( standalone="#{decl.standalone}") if decl.standalone
      output << "?>"
    end

    # output << "\n"
    node.doctype&.write(output)

    # Write processing instructions
    node.children.each do |child|
      next unless [::REXML::Instruction, ::REXML::CData,
                   ::REXML::Comment, ::REXML::Text].include?(child.class)

      write_with_formatter(child, output, options[:indent] || 2)
      # output << "\n"
    end

    if node.root
      write_with_formatter(node.root, output,
                           options[:indent] || 2)
    end
  else
    write_with_formatter(node, output, options[:indent] || 2)
  end

  result = output.is_a?(StringIO) ? output.string : output
  result.strip
end

.set_attribute(element, name, value) ⇒ Object



262
263
264
265
# File 'lib/moxml/adapter/rexml.rb', line 262

def set_attribute(element, name, value)
  element.attributes[name&.to_s] = value&.to_s
  element.attributes.get_attribute(name&.to_s)
end

.set_attribute_name(attribute, name) ⇒ Object



267
268
269
270
271
272
273
274
# File 'lib/moxml/adapter/rexml.rb', line 267

def set_attribute_name(attribute, name)
  old_name = attribute.expanded_name
  attribute.name = name
  # Rexml doesn't change the keys of the attributes hash
  element = attribute.element
  element.attributes.delete(old_name)
  element.attributes << attribute
end

.set_attribute_value(attribute, value) ⇒ Object



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

def set_attribute_value(attribute, value)
  attribute.normalized = value
end

.set_cdata_content(node, content) ⇒ Object



390
391
392
# File 'lib/moxml/adapter/rexml.rb', line 390

def set_cdata_content(node, content)
  node.value = content.to_s
end

.set_comment_content(node, content) ⇒ Object



382
383
384
# File 'lib/moxml/adapter/rexml.rb', line 382

def set_comment_content(node, content)
  node.string = content.to_s
end

.set_declaration_attribute(node, name, value) ⇒ Object



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

def set_declaration_attribute(node, name, value)
  case name
  when "version"
    node.version = value
  when "encoding"
    node.encoding = value
  when "standalone"
    node.standalone = value
  end
end

.set_namespace(element, ns) ⇒ Object

add a namespace prefix to the element name AND a namespace definition



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

def set_namespace(element, ns)
  prefix = ns.name.to_s.empty? ? "xmlns" : ns.name.to_s
  if element.is_a?(::REXML::Element)
    element.add_namespace(prefix,
                          ns.value)
  end
  element.name = "#{prefix}:#{element.name}"
  owner = element.is_a?(::REXML::Attribute) ? element.element : element
  ::REXML::Attribute.new(prefix, ns.value, owner)
end

.set_node_name(node, name) ⇒ Object



152
153
154
155
156
157
158
159
# File 'lib/moxml/adapter/rexml.rb', line 152

def set_node_name(node, name)
  case node
  when ::REXML::Element
    node.name = name.to_s
  when ::REXML::Instruction
    node.target = name.to_s
  end
end

.set_processing_instruction_content(node, content) ⇒ Object



402
403
404
# File 'lib/moxml/adapter/rexml.rb', line 402

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

.set_root(doc, element) ⇒ Object



131
132
133
# File 'lib/moxml/adapter/rexml.rb', line 131

def set_root(doc, element)
  doc.add_element(element)
end

.set_text_content(node, content) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
# File 'lib/moxml/adapter/rexml.rb', line 446

def set_text_content(node, content)
  case node
  when ::REXML::Text, ::REXML::CData
    node.value = content.to_s
  when ::REXML::Element
    # Remove existing text nodes to prevent duplicates
    node.texts.each(&:remove)
    # Add new text content
    node.add_text(content.to_s)
  end
end

.text_content(node) ⇒ Object



406
407
408
409
410
411
412
413
414
415
416
# File 'lib/moxml/adapter/rexml.rb', line 406

def text_content(node)
  case node
  when ::REXML::Text, ::REXML::CData
    node.value.to_s
  when ::Moxml::Adapter::CustomizedRexml::EntityReference
    ""
  when ::REXML::Element
    # Extract text recursively from all children to match other adapters
    extract_text_recursively(node)
  end.to_s
end

.xpath(node, expression, namespaces = {}) ⇒ Object



548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/moxml/adapter/rexml.rb', line 548

def xpath(node, expression, namespaces = {})
  if namespaces && !namespaces.empty?
    ::REXML::XPath.match(node, expression, namespaces)
  else
    node.get_elements(expression).to_a
  end
rescue ::REXML::ParseException => e
  raise Moxml::XPathError.new(
    e.message,
    expression: expression,
    adapter: "REXML",
    node: node,
  )
end