Class: Moxml::Adapter::Nokogiri

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

Defined Under Namespace

Classes: NokogiriSAXBridge

Constant Summary

Constants included from XmlUtils

XmlUtils::VALID_ELEMENT_NAMES

Class Method Summary collapse

Methods inherited from Base

actual_native, attribute_name, attribute_pairs, bulk_materialize?, children_accepts_entity_flag?, create_cdata, create_comment, create_declaration, create_doctype, create_element, create_element_with_attrs, create_entity_reference, create_namespace, create_processing_instruction, create_text, decode_entities, digest, duplicate_node, entity_bearing?, expanded_attr_reads?, expanded_attr_value, free_document, iterparse, iterparse_file, materialize_fields, patch_node, patches_children?, plan_structs, preprocess_entities, remove_attribute_native, 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) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/moxml/adapter/nokogiri.rb', line 375

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



404
405
406
# File 'lib/moxml/adapter/nokogiri.rb', line 404

def add_next_sibling(node, sibling)
  node.add_next_sibling(sibling)
end

.add_previous_sibling(node, sibling) ⇒ Object



400
401
402
# File 'lib/moxml/adapter/nokogiri.rb', line 400

def add_previous_sibling(node, sibling)
  node.add_previous_sibling(sibling)
end

.adjacent_to_entity_reference?(node) ⇒ Boolean

Returns:

  • (Boolean)


292
293
294
295
# File 'lib/moxml/adapter/nokogiri.rb', line 292

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



508
509
510
511
512
513
514
515
516
517
# File 'lib/moxml/adapter/nokogiri.rb', line 508

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



326
327
328
# File 'lib/moxml/adapter/nokogiri.rb', line 326

def attribute_element(attr)
  attr.parent
end

.attributes(element) ⇒ Object



346
347
348
349
350
351
# File 'lib/moxml/adapter/nokogiri.rb', line 346

def attributes(element)
  # attribute_nodes, not attributes.values: the attributes Hash
  # is keyed by local name, so xmi:type and type collide and
  # one is silently dropped (issue #94).
  element.attribute_nodes
end

.bare_attr_value(element, name) ⇒ Object

Fast bare-name read for Element#[]: the native call plus, only when the parse recorded entity markers, their restoration (the resolver path's other real semantic). The marker flag is constant per document — a WeakMap on the adapter beats the per-read document fetch + attachment hash chain. Raw qualified-name read; the entity-restore decision is the wrapper's (Element#[] rides its generation memo).



45
46
47
# File 'lib/moxml/adapter/nokogiri.rb', line 45

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

.bare_get_qname_safe? ⇒ Boolean

Qualified-name reads are clean on this engine (verified by the expanded-name specs); markers restore in-line when the parse recorded them.

Returns:

  • (Boolean)


22
23
24
# File 'lib/moxml/adapter/nokogiri.rb', line 22

def bare_get_qname_safe?
  true
end

.bare_set_qname_safe? ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/moxml/adapter/nokogiri.rb', line 15

def bare_set_qname_safe?
  true
end

.cdata_content(node) ⇒ Object



439
440
441
# File 'lib/moxml/adapter/nokogiri.rb', line 439

def cdata_content(node)
  node.content
end

.children(node, **_entity_bearing) ⇒ Object



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

def children(node, **_entity_bearing)
  node.children
end

.comment_content(node) ⇒ Object



447
448
449
# File 'lib/moxml/adapter/nokogiri.rb', line 447

def comment_content(node)
  node.content
end

.create_document(_native_doc = nil) ⇒ Object



164
165
166
# File 'lib/moxml/adapter/nokogiri.rb', line 164

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

.create_fragment ⇒ Object



168
169
170
171
172
173
174
# File 'lib/moxml/adapter/nokogiri.rb', line 168

def create_fragment
  # document fragments are weird and should be used with caution:
  # https://github.com/sparklemotion/nokogiri/issues/572
  ::Nokogiri::XML::DocumentFragment.new(
    ::Nokogiri::XML::Document.new,
  )
end

.create_native_cdata(content, owner_doc = nil) ⇒ Object



184
185
186
# File 'lib/moxml/adapter/nokogiri.rb', line 184

def create_native_cdata(content, owner_doc = nil)
  ::Nokogiri::XML::CDATA.new(owner_doc || create_document, content)
end

.create_native_comment(content, owner_doc = nil) ⇒ Object



188
189
190
# File 'lib/moxml/adapter/nokogiri.rb', line 188

def create_native_comment(content, owner_doc = nil)
  ::Nokogiri::XML::Comment.new(owner_doc || create_document, content)
end

.create_native_declaration(version, encoding, standalone) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/moxml/adapter/nokogiri.rb', line 204

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



192
193
194
195
196
# File 'lib/moxml/adapter/nokogiri.rb', line 192

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



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

def create_native_element(name, owner_doc = nil)
  ::Nokogiri::XML::Element.new(name, owner_doc || create_document)
end

.create_native_entity_reference(name, _owner_doc = nil) ⇒ Object



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

def create_native_entity_reference(name, _owner_doc = nil)
  ::Nokogiri::XML::EntityReference.new(create_document, name)
end

.create_native_namespace(element, prefix, uri) ⇒ Object



260
261
262
# File 'lib/moxml/adapter/nokogiri.rb', line 260

def create_native_namespace(element, prefix, uri)
  element.add_namespace_definition(prefix, uri)
end

.create_native_processing_instruction(target, content) ⇒ Object



198
199
200
201
202
# File 'lib/moxml/adapter/nokogiri.rb', line 198

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



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

def create_native_text(content, owner_doc = nil)
  ::Nokogiri::XML::Text.new(content, owner_doc || create_document)
end

.declaration_attribute(declaration, attr_name) ⇒ Object



220
221
222
223
224
225
# File 'lib/moxml/adapter/nokogiri.rb', line 220

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



484
485
486
# File 'lib/moxml/adapter/nokogiri.rb', line 484

def doctype_external_id(native)
  native.external_id
end

.doctype_name(native) ⇒ Object

Doctype accessor methods



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

def doctype_name(native)
  native.name
end

.doctype_system_id(native) ⇒ Object



488
489
490
# File 'lib/moxml/adapter/nokogiri.rb', line 488

def doctype_system_id(native)
  native.system_id
end

.document(node) ⇒ Object



314
315
316
# File 'lib/moxml/adapter/nokogiri.rb', line 314

def document(node)
  node.document
end

.entity_reference_name(node) ⇒ Object



216
217
218
# File 'lib/moxml/adapter/nokogiri.rb', line 216

def entity_reference_name(node)
  node.name
end

.get_attribute(element, name) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/moxml/adapter/nokogiri.rb', line 353

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



366
367
368
369
# File 'lib/moxml/adapter/nokogiri.rb', line 366

def get_attribute_value(element, name)
  # get the attribute value by its name including a namespace
  element[name.to_s]
end

.has_declaration?(native_doc, wrapper) ⇒ Boolean

Returns:

  • (Boolean)


556
557
558
559
560
561
562
# File 'lib/moxml/adapter/nokogiri.rb', line 556

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



475
476
477
# File 'lib/moxml/adapter/nokogiri.rb', line 475

def in_scope_namespaces(element)
  element.namespace_scopes
end

.inner_text(node) ⇒ Object



428
429
430
431
432
433
# File 'lib/moxml/adapter/nokogiri.rb', line 428

def inner_text(node)
  text_children = node.children.reject do |c|
    c.element? || c.comment?
  end
  text_children.map(&:content).join
end

.line_number(node) ⇒ Object



322
323
324
# File 'lib/moxml/adapter/nokogiri.rb', line 322

def line_number(node)
  node.line
end

.namespace(element) ⇒ Object



252
253
254
# File 'lib/moxml/adapter/nokogiri.rb', line 252

def namespace(element)
  element.namespace
end

.namespace_definitions(node) ⇒ Object



471
472
473
# File 'lib/moxml/adapter/nokogiri.rb', line 471

def namespace_definitions(node)
  node.namespace_definitions
end

.namespace_prefix(namespace) ⇒ Object



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

def namespace_prefix(namespace)
  namespace.prefix
end

.namespace_uri(namespace) ⇒ Object



467
468
469
# File 'lib/moxml/adapter/nokogiri.rb', line 467

def namespace_uri(namespace)
  namespace.href
end

.native_c14n_byte_safe? ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/moxml/adapter/nokogiri.rb', line 68

def native_c14n_byte_safe?
  return @native_c14n_byte_safe unless @native_c14n_byte_safe.nil?

  @native_c14n_byte_safe = begin
    probe_xml = %(<?xml version="1.0"?><doc xmlns:p="urn:p" xmlns="urn:d" b="2" a="1"><e p:x="v" z="w">t &amp; u</e><!-- c --></doc>)
    native_doc = ::Nokogiri::XML(probe_xml) { |c| c.strict.nonet }
    native = native_doc.root.canonicalize(::Nokogiri::XML::XML_C14N_1_0)
    wrapper = Wrappers::Document.new(native_doc, Context.new(:nokogiri))
    reference = Moxml::C14n::Inclusive10.new.canonicalize(wrapper.root)
    native == reference
  rescue StandardError
    false
  end
end

.native_identity_stable? ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/moxml/adapter/nokogiri.rb', line 49

def native_identity_stable?
  true
end

.native_inclusive10(native) ⇒ Object

libxml2-native inclusive C14N: delegated for the default shape (Inclusive 1.0, no comments, no InclusiveNamespaces prefix list) when the probe below proves libxml2's output byte-identical to the Ruby reference (ported from canon). Every other combination keeps the Ruby engine. Lazy, not load-time: the probe exercises the wrapper layer (Document/C14n), which is circular while THIS adapter file is still loading.



61
62
63
64
65
66
# File 'lib/moxml/adapter/nokogiri.rb', line 61

def native_inclusive10(native)
  return nil if native.nil?
  return nil unless native_c14n_byte_safe?

  native.canonicalize(::Nokogiri::XML::XML_C14N_1_0)
end

.next_sibling(node) ⇒ Object



306
307
308
# File 'lib/moxml/adapter/nokogiri.rb', line 306

def next_sibling(node)
  node.next_sibling
end

.node_name(node) ⇒ Object



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

def node_name(node)
  node.name
end

.node_type(node) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/moxml/adapter/nokogiri.rb', line 264

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



302
303
304
# File 'lib/moxml/adapter/nokogiri.rb', line 302

def parent(node)
  node.parent
end

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/moxml/adapter/nokogiri.rb', line 87

def parse(xml, options = {}, _context = nil)
  processed_xml, entity_markers = Entity.preprocess_with_marker_flag(xml)

  # preprocess_entities always returns UTF-8, so tell Nokogiri to
  # parse as UTF-8 regardless of any original encoding option.
  native_doc = begin
    if options[:fragment]
      ::Nokogiri::XML::DocumentFragment.parse(processed_xml) do |config|
        config.strict.nonet
        config.recover unless options[:strict]
        config.noblanks if options[:noblanks]
        config.huge
      end
    else
      ::Nokogiri::XML(processed_xml, nil, "UTF-8") do |config|
        config.strict.nonet
        config.recover unless options[:strict]
        config.noblanks if options[:noblanks]
        # XML_PARSE_HUGE: lifts libxml2's arbitrary limits —
        # text nodes > 10MB are silently TRUNCATED at 10MB in
        # recover mode, so large documents lose data without
        # it (users parse >10MB XML).
        config.huge
      end
    end
  rescue ::Nokogiri::XML::SyntaxError => e
    raise Moxml::ParseError.new(e.message, line: e.line,
                                           column: e.column)
  end

  # Use provided context if available, otherwise create new one
  ctx = _context || Context.new(:nokogiri)
  attachments.set(native_doc, :entity_markers, entity_markers)
  Wrappers::Document.new(native_doc, ctx)
end

.parse_diagnostics(native_doc) ⇒ Object

Cross-adapter parity for Document#parse_diagnostics (#271): Nokogiri's doc.errors already carries recover- mode entries; mapped into the same { kind:, message: } shape. Kind is :error — Nokogiri does not classify.



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

def parse_diagnostics(native_doc)
  return [] unless native_doc.respond_to?(:errors)

  native_doc.errors.map do |e|
    { kind: :error, message: e.message.to_s }
  end
end

.parse_errors(native_doc) ⇒ Object

Nokogiri parses with config.recover unless strict, so recoverable syntax errors land on doc.errors instead of raising (issue #147).



136
137
138
# File 'lib/moxml/adapter/nokogiri.rb', line 136

def parse_errors(native_doc)
  native_doc.errors.map(&:message)
end

.parse_fragment(xml, _context = nil) ⇒ Object

Nokogiri has a real fragment node type — its children ARE the fragment's top-level nodes.



28
29
30
31
32
33
34
35
# File 'lib/moxml/adapter/nokogiri.rb', line 28

def parse_fragment(xml, _context = nil)
  processed = Entity.preprocess_entities(xml)
  ::Nokogiri::XML::DocumentFragment.parse(processed) do |config|
    config.strict.nonet
    config.recover
    config.huge
  end.children.to_a
end

.parse_html(html, _options = {}, _context = nil) ⇒ Object

Tolerant HTML parsing through libxml2's HTML mode: implied html/head/body structure, void elements, lowercased names, the HTML named-entity table. Recovery is inherent — malformed input never raises.



127
128
129
130
131
# File 'lib/moxml/adapter/nokogiri.rb', line 127

def parse_html(html, _options = {}, _context = nil)
  html_string = html.is_a?(IO) || html.is_a?(StringIO) ? html.read : html.to_s
  native_doc = ::Nokogiri::HTML(html_string, &:huge)
  Wrappers::Document.new(native_doc, _context || Context.new(:nokogiri))
end

.plan_rows(native) ⇒ Object

Plan row stream (Moxml::Plan contract): pre-order element rows |name, attrs_pairs, first-text, depth| straight off the libxml2-backed nodes — no wrapper minting.



571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/moxml/adapter/nokogiri.rb', line 571

def plan_rows(native)
  root = native.is_a?(::Nokogiri::XML::Document) ? native.root : native
  return nil unless root

  walk = lambda do |el, depth|
    attrs = []
    el.attribute_nodes.each { |a| attrs << a.name << a.value }
    first_text = nil
    el.children.each { |c| first_text ||= c.content if c.text? }
    yield(el.name, attrs, first_text, depth)
    el.element_children.each { |c| walk.call(c, depth + 1) }
  end
  walk.call(root, 0)
  true
end

.previous_sibling(node) ⇒ Object



310
311
312
# File 'lib/moxml/adapter/nokogiri.rb', line 310

def previous_sibling(node)
  node.previous_sibling
end

.processing_instruction_content(node) ⇒ Object



455
456
457
# File 'lib/moxml/adapter/nokogiri.rb', line 455

def processing_instruction_content(node)
  node.content
end

.processing_instruction_target(node) ⇒ Object



256
257
258
# File 'lib/moxml/adapter/nokogiri.rb', line 256

def processing_instruction_target(node)
  node.name
end

.remove(node) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
# File 'lib/moxml/adapter/nokogiri.rb', line 408

def remove(node)
  # Special handling for declarations on Nokogiri documents
  if node.is_a?(::Nokogiri::XML::ProcessingInstruction) &&
      node.name == "xml" &&
      node.parent.is_a?(::Nokogiri::XML::Document)
    # Clear document's xml_decl when removing declaration
    attachments.set(node.parent, :xml_decl, nil)
  end

  node.remove
end

.remove_attribute(element, name) ⇒ Object



371
372
373
# File 'lib/moxml/adapter/nokogiri.rb', line 371

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

.remove_declaration(native_doc) ⇒ Object



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

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

.replace(node, new_node) ⇒ Object



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

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

.replace_children(node, new_children) ⇒ Object



297
298
299
300
# File 'lib/moxml/adapter/nokogiri.rb', line 297

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

.root(document) ⇒ Object



318
319
320
# File 'lib/moxml/adapter/nokogiri.rb', line 318

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

Parameters:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/moxml/adapter/nokogiri.rb', line 145

def sax_parse(xml, handler)
  # Create bridge that translates Nokogiri SAX to Moxml SAX
  bridge = NokogiriSAXBridge.new(handler)

  # Create Nokogiri SAX parser
  parser = ::Nokogiri::XML::SAX::Parser.new(bridge)

  # Parse
  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



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'lib/moxml/adapter/nokogiri.rb', line 519

def serialize(node, options = {})
  save_options = ::Nokogiri::XML::Node::SaveOptions::AS_XML

  # Don't force expand empty elements if they're really empty
  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

  serialize_kwargs = {
    indent: options[:indent],
    encoding: options[:encoding],
    save_with: save_options,
  }
  serialize_kwargs[:indent_text] = options[:indent_text] if options[:indent_text]
  result = node.to_xml(**serialize_kwargs)

  if custom_decl
    result = "#{custom_decl}\n#{result}"
  end

  result
end

.set_attribute(element, name, value) ⇒ Object



330
331
332
# File 'lib/moxml/adapter/nokogiri.rb', line 330

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

.set_cdata_content(node, content) ⇒ Object



443
444
445
# File 'lib/moxml/adapter/nokogiri.rb', line 443

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

.set_comment_content(node, content) ⇒ Object



451
452
453
# File 'lib/moxml/adapter/nokogiri.rb', line 451

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

.set_declaration_attribute(declaration, attr_name, value) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
# File 'lib/moxml/adapter/nokogiri.rb', line 227

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



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/moxml/adapter/nokogiri.rb', line 239

def set_namespace(element, ns)
  # Strip any existing name prefix before binding: create_element
  # with a qname ("p:c") leaves name="p:c", and Nokogiri's
  # bare namespace= then serializes p: + p:c → p:p:c (issue
  # #208). Local-name-only + namespace= is the correct shape;
  # consumers that pass the qname get the same result.
  if ns && element.respond_to?(:name=) && element.name.to_s.include?(":")
    element.name = element.name.split(":", 2)[-1]
  end
  element.namespace = ns
  element
end

.set_node_name(node, name) ⇒ Object



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

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

.set_processing_instruction_content(node, content) ⇒ Object



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

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

.set_root(doc, element) ⇒ Object



83
84
85
# File 'lib/moxml/adapter/nokogiri.rb', line 83

def set_root(doc, element)
  doc.root = element
end

.set_text_content(node, content) ⇒ Object



435
436
437
# File 'lib/moxml/adapter/nokogiri.rb', line 435

def set_text_content(node, content)
  node.native_content = content
end

.text_content(node) ⇒ Object



424
425
426
# File 'lib/moxml/adapter/nokogiri.rb', line 424

def text_content(node)
  node.text.to_s
end

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



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/moxml/adapter/nokogiri.rb', line 492

def xpath(node, expression, namespaces = nil)
  result = node.xpath(expression, namespaces)
  # Adapter contract: Array<native> | LazyNodeSet | scalar
  # (count(), string-length(), boolean functions return
  # scalars). The native node-set passes through lazily —
  # to_a would materialize every result wrapper up front.
  result.is_a?(Enumerable) ? result.extend(Moxml::LazyNodeSet) : result
rescue ::Nokogiri::XML::XPath::SyntaxError => e
  raise Moxml::XPathError.new(
    e.message,
    expression: expression,
    adapter: "Nokogiri",
    node: node,
  )
end