Class: Moxml::Adapter::Leptris
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Leptris
show all
- Defined in:
- lib/moxml/adapter/leptris.rb
Overview
Adapter over the leptris FFI binding (libleptris C library).
libleptris provides DOM parsing, a native XPath 1.0 engine, SAX,
and serialization. It has no first-class XML declaration or
programmatic DOCTYPE, so those live in CustomizedLeptris value
objects stored through NativeAttachment.
Defined Under Namespace
Classes: LeptrisSAXBridge
Constant Summary
collapse
- DOC_NODE_SUPPORTED =
libleptris 1.9.7 (leptris-ruby 1.9.26): Document#node exposes
the libxml2-model document node — prolog/epilog parts in
document order. Older bindings keep the flat pre-root PI path.
::Leptris::XML::Document.method_defined?(:node)
- NATIVE_XPATH_CACHE =
XPath prefers libleptris' native C engine (three orders of
magnitude faster than the Ruby engine) for document-context
queries, with a conservative gate: the Ruby engine handles
element-context evaluation (native relative-context
semantics are not contract-verified), variable references,
and expressions whose results are attribute nodes (native
returns those without name accessors). Both engines resolve
expression prefixes against document-declared namespaces.
XPath::Cache.new(100)
- NATIVE_GATE_CACHE =
The gate decision is expression-intrinsic; re-walking the
cached AST three times per xpath call cost ~23% of repeated
selective queries, so the boolean caches beside it.
XPath::Cache.new(100)
- LITERAL_REGIONS =
moxml canonical serialization: apostrophes stay literal
(only & < > " are escaped) and empty elements expand when the
caller asked for it — matching the other adapters' contract.
Runs segment-aware: CDATA content is literal and must not be
touched.
Regions whose content is literal and must never be rewritten.
Scanned positionally (String#index), not with a regex: the
scan is linear in the input length, so pathological document
content cannot blow up the serializer.
{
"<!--" => "-->",
"<![CDATA[" => "]]>",
"<?" => "?>",
}.freeze
- EMPTY_ELEMENT_RE =
All quantifiers are possessive and the bare-part class
excludes "/" and ">": the possessive groups can never consume
the closing delimiter, so no backtracking is possible and the
match is linear even on malformed tags.
%r{<([A-Za-z_][\w.:-]*+)((?:"[^"]*+"|'[^']*+'|[^<>"'/]++)*+)/>}
Class Method Summary
collapse
-
.add_child(parent, child) ⇒ Object
-
.add_next_sibling(node, new_node) ⇒ Object
-
.add_previous_sibling(node, new_node) ⇒ Object
-
.ast_contains_type?(ast, type) ⇒ Boolean
-
.at_xpath(node, expression, namespaces = {}) ⇒ Object
-
.attachments ⇒ Object
-
.attr_matches?(attr_name) ⇒ Boolean
-
.attribute_element(attr) ⇒ Object
-
.attribute_name(attr) ⇒ Object
-
.attributes(element) ⇒ Object
-
.bulk_materialize? ⇒ Boolean
Bulk materialization (issue #132): leptris_node_traverse walks the subtree with one FFI call; the only per-node cost is the C->Ruby callback and the property reads below.
-
.cdata_content(node) ⇒ Object
-
.children(node) ⇒ Object
-
.comment_content(node) ⇒ Object
-
.contains_parent_axis?(ast) ⇒ Boolean
-
.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) ⇒ 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
-
.default_declaration_xml(doc, options) ⇒ Object
-
.doctype_external_id(node) ⇒ Object
-
.doctype_name(node) ⇒ Object
-
.doctype_system_id(node) ⇒ Object
-
.document(node) ⇒ Object
-
.duplicate_node(node) ⇒ Object
-
.element_material_record(node, depth_memo) ⇒ Object
-
.engine_xpath(node, expression, namespaces = {}) ⇒ Object
-
.entity_bearing?(native) ⇒ Boolean
Marker presence is a document-level fact: parse records it, the ER builder path flips it, and the split/restore scans consult it.
-
.entity_reference_name(node) ⇒ Object
-
.expression_uses_parent_axis?(expression) ⇒ Boolean
-
.get_attribute(element, name) ⇒ Object
-
.get_attribute_value(element, name) ⇒ Object
-
.has_declaration?(native_doc, _wrapper) ⇒ Boolean
-
.inner_text(node) ⇒ Object
-
.line_number(node) ⇒ Object
-
.marker_text_for(parent, name) ⇒ Object
-
.material_depth(node, memo) ⇒ Object
Binding wrappers are address-stable and #parent is memoized, so each edge is fetched once; depths resolve through the identity-keyed memo.
-
.materialize_records(native, &block) ⇒ Object
-
.namespace(node) ⇒ Object
-
.namespace_definitions(element) ⇒ Object
-
.namespace_prefix(namespace) ⇒ Object
-
.namespace_uri(namespace) ⇒ Object
-
.native_context_node?(node) ⇒ Boolean
-
.native_doctype_xml(doc) ⇒ Object
-
.native_expression?(expression) ⇒ Boolean
Document-context queries without variable references, attribute-node results, or the nokogiri-compat xmlns: reserved prefix convention.
-
.native_xpath(node, expression, namespaces, first_only: false) ⇒ Array, ...
Native results, or nil when the query must run on the Ruby engine.
-
.next_literal_region(xml, from) ⇒ Object
Nearest literal region at/after from: [position, terminator].
-
.next_sibling(node) ⇒ Object
-
.node_name(node) ⇒ Object
-
.node_type(node) ⇒ Object
-
.normalize_markup(markup, needs_apos, needs_expand) ⇒ Object
-
.normalize_serialization(xml, options) ⇒ Object
-
.opener_at_offset(terminator) ⇒ Object
Search for a terminator past its opener's overlap-safe offset ("-->" cannot start inside "<!--").
-
.parent(node) ⇒ Object
-
.parse(xml, options = {}, _context = nil) ⇒ Object
-
.prefix_part(name) ⇒ Object
-
.previous_sibling(node) ⇒ Object
-
.processing_instruction_content(node) ⇒ Object
-
.processing_instruction_target(node) ⇒ Object
-
.raw_serialize(node, options) ⇒ 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
-
.resolve_prefix_ns(owner, prefix) ⇒ Object
Nearest in-scope declaration of prefix, walking owner then ancestors.
-
.root(document) ⇒ Object
-
.sax_parse(xml, handler) ⇒ Object
-
.selects_attribute_results?(ast) ⇒ Boolean
-
.serialize(node, options = {}) ⇒ Object
-
.serialize_document(doc, options) ⇒ Object
Documents compose from their parts: the native serializer only walks the root subtree, so declaration, DOCTYPE, PIs and document-level text are assembled around it explicitly.
-
.set_attribute(element, name, value) ⇒ Object
-
.set_attribute_name(attr, name) ⇒ Object
-
.set_attribute_namespace(attr, namespace) ⇒ Object
Attr is an immutable value object: namespace changes go through the owning element, recreating the attribute with a qualified name, and yield a fresh native for the wrapper.
-
.set_attribute_value(attr, value) ⇒ Object
-
.set_cdata_content(node, content) ⇒ Object
-
.set_comment_content(node, content) ⇒ Object
-
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
-
.set_namespace(node, namespace) ⇒ 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
-
.split_entity_markers(natives, parent) ⇒ Object
Expand marker-bearing text nodes into the child sequence the moxml contract exposes: text, EntityReference, text, ...
-
.text_content(node) ⇒ Object
-
.text_material_record(kind, text, node, depth_memo) ⇒ Object
-
.uses_xmlns_prefix?(ast) ⇒ Boolean
xmlns:name is a nokogiri-compat convention addressing elements in the default namespace; only the Ruby engine implements it.
-
.xpath(node, expression, namespaces = {}) ⇒ Object
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, decode_entities, in_scope_namespaces, patch_node, patches_children?, preprocess_entities, restore_entities, sax_supported?, 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(parent, child) ⇒ Object
521
522
523
524
525
526
527
528
529
530
531
532
533
534
|
# File 'lib/moxml/adapter/leptris.rb', line 521
def add_child(parent, child)
case parent
when ::Leptris::XML::Document then add_document_child(parent, child)
else
if child.is_a?(CustomizedLeptris::EntityReference)
marker = parent.document.create_text_node("#{Entity::MARKER}#{child.name};")
parent.add_child(marker)
attachments.set(parent.document, :entity_markers, true)
return child
end
child = parent.document.create_text_node(child) if child.is_a?(String)
parent.add_child(child)
end
end
|
.add_next_sibling(node, new_node) ⇒ Object
547
548
549
|
# File 'lib/moxml/adapter/leptris.rb', line 547
def add_next_sibling(node, new_node)
node.add_next_sibling(new_node)
end
|
.add_previous_sibling(node, new_node) ⇒ Object
536
537
538
539
540
541
542
543
544
545
|
# File 'lib/moxml/adapter/leptris.rb', line 536
def add_previous_sibling(node, new_node)
if new_node.is_a?(::Leptris::XML::ProcessingInstruction) &&
node.document&.root == node
node.document.add_pi(new_node.target, new_node.content.to_s)
return new_node
end
node.add_previous_sibling(new_node)
end
|
.ast_contains_type?(ast, type) ⇒ Boolean
788
789
790
791
792
793
794
|
# File 'lib/moxml/adapter/leptris.rb', line 788
def ast_contains_type?(ast, type)
return true if ast.type == type
ast.children.any? do |child|
child.is_a?(XPath::AST::Node) && ast_contains_type?(child, type)
end
end
|
.at_xpath(node, expression, namespaces = {}) ⇒ Object
708
709
710
711
712
713
714
|
# File 'lib/moxml/adapter/leptris.rb', line 708
def at_xpath(node, expression, namespaces = {})
native = native_xpath(node, expression, namespaces, first_only: true)
return native unless native.nil?
result = engine_xpath(node, expression, namespaces)
result.is_a?(Array) ? result.first : result
end
|
.attachments ⇒ Object
28
29
30
|
# File 'lib/moxml/adapter/leptris.rb', line 28
def attachments
@attachments ||= Moxml::NativeAttachment.new
end
|
.attr_matches?(attr_name) ⇒ Boolean
113
114
115
|
# File 'lib/moxml/adapter/leptris.rb', line 113
def attr_matches?(attr_name)
%w[version encoding standalone].include?(attr_name.to_s)
end
|
.attribute_element(attr) ⇒ Object
473
474
475
|
# File 'lib/moxml/adapter/leptris.rb', line 473
def attribute_element(attr)
attr.element
end
|
.attribute_name(attr) ⇒ Object
477
478
479
480
481
482
483
|
# File 'lib/moxml/adapter/leptris.rb', line 477
def attribute_name(attr)
return attr.name.split(":", 2)[1] if attr.name.include?(":")
attr.name.to_s.dup.force_encoding("UTF-8")
end
|
.attributes(element) ⇒ Object
469
470
471
|
# File 'lib/moxml/adapter/leptris.rb', line 469
def attributes(element)
element.attribute_nodes
end
|
.bulk_materialize? ⇒ Boolean
Bulk materialization (issue #132): leptris_node_traverse
walks the subtree with one FFI call; the only per-node cost
is the C->Ruby callback and the property reads below. No
Moxml::Node or Attribute wrapper is allocated.
241
242
243
|
# File 'lib/moxml/adapter/leptris.rb', line 241
def bulk_materialize?
true
end
|
.cdata_content(node) ⇒ Object
632
633
634
|
# File 'lib/moxml/adapter/leptris.rb', line 632
def cdata_content(node)
node.content
end
|
.children(node) ⇒ 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
399
400
401
|
# File 'lib/moxml/adapter/leptris.rb', line 375
def children(node)
case node
when ::Leptris::XML::Document
assemble_document_children(node)
when CustomizedLeptris::Declaration, CustomizedLeptris::Doctype,
CustomizedLeptris::EntityReference, CustomizedLeptris::TextSegment,
CustomizedLeptris::DocumentPI,
::Leptris::XML::Text, ::Leptris::XML::Comment,
::Leptris::XML::CDATA, ::Leptris::XML::ProcessingInstruction,
::Leptris::XML::Attr
[]
else
natives = node.children.to_a
return natives if attachments.get(node.document, :entity_markers) == false
split_entity_markers(natives, node)
end
end
|
640
641
642
|
# File 'lib/moxml/adapter/leptris.rb', line 640
def (node)
node.content
end
|
.contains_parent_axis?(ast) ⇒ Boolean
764
765
766
767
768
769
770
771
|
# File 'lib/moxml/adapter/leptris.rb', line 764
def contains_parent_axis?(ast)
return true if ast.type == :parent
return true if ast.type == :axis && ast.children.first == "parent"
ast.children.any? do |child|
child.is_a?(XPath::AST::Node) && contains_parent_axis?(child)
end
end
|
.create_document(_native_doc = nil) ⇒ Object
64
65
66
|
# File 'lib/moxml/adapter/leptris.rb', line 64
def create_document(_native_doc = nil)
::Leptris::XML::Document.create
end
|
.create_native_cdata(content, owner_doc = nil) ⇒ Object
76
77
78
|
# File 'lib/moxml/adapter/leptris.rb', line 76
def create_native_cdata(content, owner_doc = nil)
(owner_doc || create_document).create_cdata(content)
end
|
80
81
82
|
# File 'lib/moxml/adapter/leptris.rb', line 80
def (content, owner_doc = nil)
(owner_doc || create_document).(content)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
93
94
95
|
# File 'lib/moxml/adapter/leptris.rb', line 93
def create_native_declaration(version, encoding, standalone)
CustomizedLeptris::Declaration.new(version, encoding, standalone)
end
|
.create_native_doctype(name, external_id, system_id) ⇒ Object
89
90
91
|
# File 'lib/moxml/adapter/leptris.rb', line 89
def create_native_doctype(name, external_id, system_id)
CustomizedLeptris::Doctype.new(name, external_id, system_id)
end
|
.create_native_element(name, owner_doc = nil) ⇒ Object
68
69
70
|
# File 'lib/moxml/adapter/leptris.rb', line 68
def create_native_element(name, owner_doc = nil)
(owner_doc || create_document).create_element(name.to_s)
end
|
.create_native_entity_reference(name) ⇒ Object
.create_native_namespace(element, prefix, uri) ⇒ Object
130
131
132
|
# File 'lib/moxml/adapter/leptris.rb', line 130
def create_native_namespace(element, prefix, uri)
element.add_namespace_definition(prefix, uri)
end
|
.create_native_processing_instruction(target, content) ⇒ Object
84
85
86
87
|
# File 'lib/moxml/adapter/leptris.rb', line 84
def create_native_processing_instruction(target, content)
doc = create_document
doc.create_processing_instruction(target.to_s, content.to_s)
end
|
.create_native_text(content, owner_doc = nil) ⇒ Object
72
73
74
|
# File 'lib/moxml/adapter/leptris.rb', line 72
def create_native_text(content, owner_doc = nil)
(owner_doc || create_document).create_text_node(content)
end
|
.declaration_attribute(declaration, attr_name) ⇒ Object
105
106
107
|
# File 'lib/moxml/adapter/leptris.rb', line 105
def declaration_attribute(declaration, attr_name)
declaration.public_send(attr_name) if attr_matches?(attr_name)
end
|
.default_declaration_xml(doc, options) ⇒ Object
1009
1010
1011
1012
1013
|
# File 'lib/moxml/adapter/leptris.rb', line 1009
def default_declaration_xml(doc, options)
encoding = options[:encoding] || doc.encoding
encoding = "UTF-8" if encoding.to_s.empty?
XmlEmitter.declaration_xml("1.0", encoding, nil)
end
|
.doctype_external_id(node) ⇒ Object
679
680
681
|
# File 'lib/moxml/adapter/leptris.rb', line 679
def doctype_external_id(node)
node.is_a?(CustomizedLeptris::Doctype) ? node.external_id : node.public_id
end
|
.doctype_name(node) ⇒ Object
675
676
677
|
# File 'lib/moxml/adapter/leptris.rb', line 675
def doctype_name(node)
node.is_a?(CustomizedLeptris::Doctype) ? node.name : node.root_name
end
|
.doctype_system_id(node) ⇒ Object
683
684
685
|
# File 'lib/moxml/adapter/leptris.rb', line 683
def doctype_system_id(node)
node.system_id
end
|
.duplicate_node(node) ⇒ Object
.element_material_record(node, depth_memo) ⇒ Object
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
|
# File 'lib/moxml/adapter/leptris.rb', line 276
def element_material_record(node, depth_memo)
attributes = node.each_attribute.map do |attr|
name = attr.name
prefix = attr.prefix
name = name.split(":", 2)[1] || name if prefix
[name, attr.value, attr.namespace_uri, prefix]
end
ns = node.namespace
{
kind: :element,
qname: node.name,
prefix: node.prefix,
namespace_uri: ns&.href,
namespaces: begin
decls = node.namespace_definitions.map { |d| [d.prefix, d.href] }
decls.empty? ? Materializer::EMPTY_ATTRIBUTES : decls
end,
attributes: attributes,
text: nil,
depth: material_depth(node, depth_memo),
}
end
|
.engine_xpath(node, expression, namespaces = {}) ⇒ Object
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
|
# File 'lib/moxml/adapter/leptris.rb', line 820
def engine_xpath(node, expression, namespaces = {})
unless node.is_a?(Moxml::Node)
node = Moxml::Node.wrap(node, Context.new(:leptris))
end
ast = XPath::Parser.parse(expression)
proc = XPath::Compiler.compile_with_cache(ast, namespaces: namespaces)
result = proc.call(node)
case result
when Array, NodeSet
nodes = result.is_a?(NodeSet) ? result.to_a : result
seen = {}.compare_by_identity
nodes.map { |n| n.is_a?(Moxml::Node) ? n.native : n }
.select do |native|
if seen.key?(native)
false
else
seen[native] = true
true
end
end
else
result
end
end
|
.entity_bearing?(native) ⇒ Boolean
Marker presence is a document-level fact: parse records it,
the ER builder path flips it, and the split/restore scans
consult it. Customized natives only exist as split products
of marker-bearing text, so they always report true.
.entity_reference_name(node) ⇒ Object
.expression_uses_parent_axis?(expression) ⇒ Boolean
757
758
759
760
761
762
|
# File 'lib/moxml/adapter/leptris.rb', line 757
def expression_uses_parent_axis?(expression)
ast = XPath::Parser.parse_with_cache(expression)
contains_parent_axis?(ast)
rescue XPath::SyntaxError
true
end
|
.get_attribute(element, name) ⇒ Object
504
505
506
|
# File 'lib/moxml/adapter/leptris.rb', line 504
def get_attribute(element, name)
element.attribute_nodes.find { |attr| attr.name == name.to_s }
end
|
.get_attribute_value(element, name) ⇒ Object
508
509
510
|
# File 'lib/moxml/adapter/leptris.rb', line 508
def get_attribute_value(element, name)
element[name.to_s]
end
|
.has_declaration?(native_doc, _wrapper) ⇒ Boolean
118
119
120
121
122
123
|
# File 'lib/moxml/adapter/leptris.rb', line 118
def has_declaration?(native_doc, _wrapper)
return true if attachments.get(native_doc, :declaration)
attachments.key?(native_doc, :had_source_declaration) &&
attachments.get(native_doc, :had_source_declaration)
end
|
.inner_text(node) ⇒ Object
609
610
611
612
613
614
615
616
617
618
619
620
621
|
# File 'lib/moxml/adapter/leptris.rb', line 609
def inner_text(node)
children(node).filter_map do |child|
case child
when CustomizedLeptris::EntityReference then "&#{child.name};"
when ::Leptris::XML::CDATA then child.content
when ::Leptris::XML::Text, CustomizedLeptris::TextSegment
child.content.to_s.dup.force_encoding("UTF-8")
end
end.join
end
|
.line_number(node) ⇒ Object
462
463
464
465
466
467
|
# File 'lib/moxml/adapter/leptris.rb', line 462
def line_number(node)
return nil unless node.is_a?(::Leptris::XML::Node)
line = node.line
line.zero? ? nil : line
end
|
.marker_text_for(parent, name) ⇒ Object
1015
1016
1017
1018
1019
1020
1021
1022
|
# File 'lib/moxml/adapter/leptris.rb', line 1015
def marker_text_for(parent, name)
return nil unless parent.is_a?(::Leptris::XML::Element)
marker = "#{Entity::MARKER}#{name};"
parent.children.to_a.find do |child|
child.is_a?(::Leptris::XML::Text) && child.content == marker
end
end
|
.material_depth(node, memo) ⇒ Object
Binding wrappers are address-stable and #parent is memoized,
so each edge is fetched once; depths resolve through the
identity-keyed memo.
319
320
321
322
323
324
|
# File 'lib/moxml/adapter/leptris.rb', line 319
def material_depth(node, memo)
memo[node] ||= begin
parent = node.parent
parent.nil? || parent.is_a?(::Leptris::XML::Document) ? 0 : material_depth(parent, memo) + 1
end
end
|
.materialize_records(native, &block) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
# File 'lib/moxml/adapter/leptris.rb', line 245
def materialize_records(native, &block)
doc = native.is_a?(::Leptris::XML::Document) ? native : native.document
return nil if doc.nil? || attachments.get(doc, :entity_markers)
root = native.is_a?(::Leptris::XML::Document) ? native.root : native
return nil if root.nil?
depth_memo = {}.compare_by_identity
root.traverse do |node|
record = case node
when ::Leptris::XML::Element
element_material_record(node, depth_memo)
when ::Leptris::XML::CDATA
text_material_record(:cdata, node.content, node, depth_memo)
when ::Leptris::XML::Text
text_material_record(:text, node.content, node, depth_memo)
when ::Leptris::XML::Comment
text_material_record(:comment, node.content, node, depth_memo)
when ::Leptris::XML::ProcessingInstruction
text_material_record(:processing_instruction, node.content, node, depth_memo)
.merge(qname: node.target)
end
yield(record) if record
end
true
end
|
.namespace(node) ⇒ Object
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/moxml/adapter/leptris.rb', line 170
def namespace(node)
if node.is_a?(::Leptris::XML::Attr) &&
(prefix = node.prefix || prefix_part(node.name))
resolved = resolve_prefix_ns(node.element, prefix)
return resolved if resolved
end
ns = node.namespace
return ns unless ns.nil?
owner = node.is_a?(::Leptris::XML::Attr) ? node.element : node
prefix = if node.is_a?(::Leptris::XML::Attr)
node.prefix || prefix_part(node.name)
else
prefix_part(node.name)
end
return nil if prefix.nil?
resolve_prefix_ns(owner, prefix)
end
|
.namespace_definitions(element) ⇒ Object
671
672
673
|
# File 'lib/moxml/adapter/leptris.rb', line 671
def namespace_definitions(element)
element.namespace_definitions
end
|
.namespace_prefix(namespace) ⇒ Object
656
657
658
659
660
661
662
663
|
# File 'lib/moxml/adapter/leptris.rb', line 656
def namespace_prefix(namespace)
return nil if namespace.is_a?(String)
namespace.prefix
end
|
.namespace_uri(namespace) ⇒ Object
665
666
667
668
669
|
# File 'lib/moxml/adapter/leptris.rb', line 665
def namespace_uri(namespace)
return namespace if namespace.is_a?(String)
namespace.href
end
|
.native_context_node?(node) ⇒ Boolean
752
753
754
755
|
# File 'lib/moxml/adapter/leptris.rb', line 752
def native_context_node?(node)
node.is_a?(::Leptris::XML::Document) ||
node.is_a?(::Leptris::XML::Element)
end
|
.native_doctype_xml(doc) ⇒ Object
1024
1025
1026
1027
1028
1029
|
# File 'lib/moxml/adapter/leptris.rb', line 1024
def native_doctype_xml(doc)
dt = doc.doctype
return nil unless dt
XmlEmitter.doctype_xml(dt.root_name, dt.public_id, dt.system_id)
end
|
.native_expression?(expression) ⇒ Boolean
Document-context queries without variable references,
attribute-node results, or the nokogiri-compat xmlns:
reserved prefix convention.
776
777
778
779
780
781
782
783
784
785
786
|
# File 'lib/moxml/adapter/leptris.rb', line 776
def native_expression?(expression)
NATIVE_GATE_CACHE.get_or_set(expression) do
ast = XPath::Parser.parse_with_cache(expression)
next false if ast_contains_type?(ast, :variable)
next false if uses_xmlns_prefix?(ast)
!selects_attribute_results?(ast)
end
rescue XPath::SyntaxError
false
end
|
.native_xpath(node, expression, namespaces, first_only: false) ⇒ Array, ...
Returns native results, or nil when the
query must run on the Ruby engine.
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
|
# File 'lib/moxml/adapter/leptris.rb', line 718
def native_xpath(node, expression, namespaces, first_only: false)
return nil unless native_context_node?(node)
return nil unless native_expression?(expression)
if node.is_a?(::Leptris::XML::Element) &&
node.document&.root.equal?(node) &&
expression_uses_parent_axis?(expression)
return nil
end
compiled = NATIVE_XPATH_CACHE.get_or_set(expression) do
::Leptris::XML::XPath.compile(expression)
end
result = if namespaces && !namespaces.empty?
compiled.eval(node, namespaces)
else
compiled.eval(node)
end
case result
when ::Leptris::XML::NodeSet
nodes = result.to_a
first_only ? nodes.first : nodes
else
result
end
rescue ::Leptris::XML::XPathError
nil
end
|
.next_literal_region(xml, from) ⇒ Object
Nearest literal region at/after from: [position, terminator].
924
925
926
927
928
929
930
931
932
933
934
935
936
937
|
# File 'lib/moxml/adapter/leptris.rb', line 924
def next_literal_region(xml, from)
best = nil
best_terminator = nil
LITERAL_REGIONS.each do |opener, terminator|
idx = xml.index(opener, from)
next if idx.nil?
if best.nil? || idx < best
best = idx
best_terminator = terminator
end
end
best.nil? ? nil : [best, best_terminator]
end
|
.next_sibling(node) ⇒ Object
442
443
444
|
# File 'lib/moxml/adapter/leptris.rb', line 442
def next_sibling(node)
node.next_sibling if node.is_a?(::Leptris::XML::Node)
end
|
.node_name(node) ⇒ Object
346
347
348
349
350
351
|
# File 'lib/moxml/adapter/leptris.rb', line 346
def node_name(node)
return node.root_name if node.is_a?(::Leptris::XML::DocType)
return node.target if node.is_a?(CustomizedLeptris::DocumentPI)
node.name.to_s.dup.force_encoding("UTF-8")
end
|
.node_type(node) ⇒ Object
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
# File 'lib/moxml/adapter/leptris.rb', line 326
def node_type(node)
case node
when ::Leptris::XML::Element then :element
when ::Leptris::XML::CDATA then :cdata
when ::Leptris::XML::Text, CustomizedLeptris::TextSegment then :text
when ::Leptris::XML::Attr then :attribute
when ::Leptris::XML::Comment then :comment
when ::Leptris::XML::ProcessingInstruction, CustomizedLeptris::DocumentPI then :processing_instruction
when ::Leptris::XML::Document then :document
when ::Leptris::XML::DocType, CustomizedLeptris::Doctype then :doctype
when CustomizedLeptris::Declaration then :declaration
when CustomizedLeptris::EntityReference then :entity_reference
else :unknown
end
end
|
.normalize_markup(markup, needs_apos, needs_expand) ⇒ Object
951
952
953
954
955
956
957
958
959
|
# File 'lib/moxml/adapter/leptris.rb', line 951
def normalize_markup(markup, needs_apos, needs_expand)
markup = markup.gsub("'", "'") if needs_apos
if needs_expand
markup = markup.gsub(EMPTY_ELEMENT_RE) do
"<#{Regexp.last_match(1)}#{Regexp.last_match(2)}></#{Regexp.last_match(1)}>"
end
end
markup
end
|
.normalize_serialization(xml, options) ⇒ Object
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
|
# File 'lib/moxml/adapter/leptris.rb', line 899
def normalize_serialization(xml, options)
needs_apos = xml.include?("'")
needs_expand = options[:expand_empty] && xml.include?("/>")
return xml unless needs_apos || needs_expand
out = +""
pos = 0
while pos < xml.length
opener_at, terminator = next_literal_region(xml, pos)
if opener_at.nil?
out << normalize_markup(xml[pos..], needs_apos, needs_expand)
break
end
out << normalize_markup(xml[pos...opener_at], needs_apos, needs_expand)
search_from = opener_at + opener_at_offset(terminator)
close = xml.index(terminator, search_from)
close_end = close.nil? ? xml.length : close + terminator.length
out << xml[opener_at...close_end]
pos = close_end
end
out
end
|
.opener_at_offset(terminator) ⇒ Object
Search for a terminator past its opener's overlap-safe offset
("-->" cannot start inside "<!--").
941
942
943
|
# File 'lib/moxml/adapter/leptris.rb', line 941
def opener_at_offset(terminator)
terminator == "-->" ? 4 : 0
end
|
.parse(xml, options = {}, _context = nil) ⇒ Object
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
61
62
|
# File 'lib/moxml/adapter/leptris.rb', line 36
def parse(xml, options = {}, _context = nil)
xml_string = xml.is_a?(IO) || xml.is_a?(StringIO) ? xml.read : xml.to_s
processed, entity_markers = Entity.preprocess_with_marker_flag(xml_string)
native_doc = begin
::Leptris::XML::Document.parse(processed, readonly: options[:readonly] == true)
rescue ::Leptris::XML::ParseError => e
raise Moxml::ParseError.new(e.message) if options[:strict]
create_document
end
ctx = _context || Context.new(:leptris)
doc = Document.new(native_doc, ctx)
record_source_declaration(native_doc, processed)
attachments.set(native_doc, :entity_markers, entity_markers)
doc
end
|
.prefix_part(name) ⇒ Object
213
214
215
|
# File 'lib/moxml/adapter/leptris.rb', line 213
def prefix_part(name)
name.include?(":") ? name.split(":", 2)[0] : nil
end
|
.previous_sibling(node) ⇒ Object
446
447
448
|
# File 'lib/moxml/adapter/leptris.rb', line 446
def previous_sibling(node)
node.previous_sibling if node.is_a?(::Leptris::XML::Node)
end
|
.processing_instruction_content(node) ⇒ Object
648
649
650
|
# File 'lib/moxml/adapter/leptris.rb', line 648
def processing_instruction_content(node)
node.content
end
|
.processing_instruction_target(node) ⇒ Object
217
218
219
|
# File 'lib/moxml/adapter/leptris.rb', line 217
def processing_instruction_target(node)
node.target
end
|
.raw_serialize(node, options) ⇒ Object
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
|
# File 'lib/moxml/adapter/leptris.rb', line 854
def raw_serialize(node, options)
case node
when CustomizedLeptris::Declaration, CustomizedLeptris::Doctype,
CustomizedLeptris::EntityReference, CustomizedLeptris::DocumentPI
return node.to_xml
when ::Leptris::XML::CDATA
return XmlEmitter.cdata(node.content)
when ::Leptris::XML::Comment
return "<!--#{node.content}-->"
when ::Leptris::XML::ProcessingInstruction
content = node.content.to_s
return content.empty? ? "<?#{node.target}?>" : "<?#{node.target} #{content}?>"
when ::Leptris::XML::Text, CustomizedLeptris::TextSegment
return XmlEmitter.escape_text(node.content.to_s)
when ::Leptris::XML::Document
return serialize_document(node, options)
end
include_decl = options.fetch(:declaration) do
options[:no_declaration] ? false : document_has_declaration?(node)
end
node.to_xml(
indent: options.fetch(:indent, 0),
no_decl: !include_decl,
encoding: options[:encoding],
)
end
|
.remove_attribute(element, name) ⇒ Object
512
513
514
|
# File 'lib/moxml/adapter/leptris.rb', line 512
def remove_attribute(element, name)
element.remove_attribute(name.to_s)
end
|
.remove_attribute_native(attr) ⇒ Object
516
517
518
519
|
# File 'lib/moxml/adapter/leptris.rb', line 516
def remove_attribute_native(attr)
attr.element.remove_attribute(attr.name)
attr
end
|
.remove_declaration(native_doc) ⇒ Object
125
126
127
128
|
# File 'lib/moxml/adapter/leptris.rb', line 125
def remove_declaration(native_doc)
attachments.delete(native_doc, :declaration)
attachments.delete(native_doc, :had_source_declaration)
end
|
.replace(node, new_node) ⇒ Object
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
# File 'lib/moxml/adapter/leptris.rb', line 570
def replace(node, new_node)
return node.replace(new_node) if node.is_a?(::Leptris::XML::Element)
parent = node.parent
unless parent
raise Moxml::DocumentStructureError.new(
"cannot replace a detached node",
)
end
prev = node.previous_sibling
node.unlink
if prev.is_a?(::Leptris::XML::Element)
prev.add_next_sibling(new_node)
else
parent.add_child(new_node)
end
new_node
end
|
.replace_children(node, new_children) ⇒ Object
594
595
596
|
# File 'lib/moxml/adapter/leptris.rb', line 594
def replace_children(node, new_children)
node.children = new_children
end
|
.resolve_prefix_ns(owner, prefix) ⇒ Object
Nearest in-scope declaration of prefix, walking owner then
ancestors. Unbound prefix → nil. Returns the Namespace object
so wrappers keep prefix and uri.
202
203
204
205
206
207
208
209
210
211
|
# File 'lib/moxml/adapter/leptris.rb', line 202
def resolve_prefix_ns(owner, prefix)
current = owner
while current.is_a?(::Leptris::XML::Element)
hit = current.namespace_definitions.find { |ns| ns.prefix == prefix }
return hit if hit
current = current.parent
end
nil
end
|
.root(document) ⇒ Object
458
459
460
|
# File 'lib/moxml/adapter/leptris.rb', line 458
def root(document)
document.root
end
|
.sax_parse(xml, handler) ⇒ Object
1031
1032
1033
1034
1035
1036
1037
|
# File 'lib/moxml/adapter/leptris.rb', line 1031
def sax_parse(xml, handler)
bridge = LeptrisSAXBridge.new(handler)
xml_string = xml.is_a?(IO) || xml.is_a?(::StringIO) ? xml.read : xml.to_s
::Leptris::XML::SAX::Parser.new(bridge).parse(xml_string)
rescue ::Leptris::XML::ParseError, ::Leptris::XML::Error => e
handler.on_error(Moxml::ParseError.new(e.message))
end
|
.selects_attribute_results?(ast) ⇒ Boolean
807
808
809
810
811
812
813
814
815
816
817
818
|
# File 'lib/moxml/adapter/leptris.rb', line 807
def selects_attribute_results?(ast)
case ast.type
when :pipe, :union, :filter_expr
ast.children.any? { |child| selects_attribute_results?(child) }
when :absolute_path, :relative_path
step = ast.children.last
step = step.children.first if step.type == :step_with_predicates
step.type == :axis && step.children.first == "attribute"
else
false
end
end
|
.serialize(node, options = {}) ⇒ Object
847
848
849
850
851
852
|
# File 'lib/moxml/adapter/leptris.rb', line 847
def serialize(node, options = {})
normalize_serialization(raw_serialize(node, options), options)
end
|
.serialize_document(doc, options) ⇒ Object
Documents compose from their parts: the native serializer
only walks the root subtree, so declaration, DOCTYPE, PIs and
document-level text are assembled around it explicitly.
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
|
# File 'lib/moxml/adapter/leptris.rb', line 964
def serialize_document(doc, options)
parts = []
include_decl = !options[:no_declaration] && options.fetch(:declaration) do
document_has_declaration?(doc)
end
if include_decl
declaration = attachments.get(doc, :declaration)
parts << (declaration ? declaration.to_xml : default_declaration_xml(doc, options)) << "\n"
end
doctype = attachments.get(doc, :doctype)
parts << doctype.to_xml << "\n" if doctype
native = native_doctype_xml(doc)
parts << native << "\n" if native
if DOC_NODE_SUPPORTED
doc_children = document_node_children(doc)
if doc_children
doc_children.each { |child| parts << raw_serialize(child, options) << "\n" }
else
document_pi_nodes(doc).each { |pi| parts << pi.to_xml << "\n" }
parts << raw_serialize(doc.root, options) << "\n" if doc.root
end
else
document_pi_nodes(doc).each { |pi| parts << pi.to_xml << "\n" }
parts << raw_serialize(doc.root, options) << "\n" if doc.root
end
texts = attachments.get(doc, :document_text)
texts&.each { |text| parts << XmlEmitter.escape_text(text.content.to_s) }
parts.join
end
|
.set_attribute(element, name, value) ⇒ Object
485
486
487
|
# File 'lib/moxml/adapter/leptris.rb', line 485
def set_attribute(element, name, value)
element[name.to_s] = value.to_s
end
|
.set_attribute_name(attr, name) ⇒ Object
489
490
491
492
493
494
495
496
497
|
# File 'lib/moxml/adapter/leptris.rb', line 489
def set_attribute_name(attr, name)
element = attr.element
value = attr.value
element.remove_attribute(attr.name)
element[name.to_s] = value
element.attribute_nodes.reverse.find { |candidate| candidate.name == name.to_s }
end
|
.set_attribute_namespace(attr, namespace) ⇒ Object
Attr is an immutable value object: namespace changes go
through the owning element, recreating the attribute with a
qualified name, and yield a fresh native for the wrapper.
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/moxml/adapter/leptris.rb', line 155
def set_attribute_namespace(attr, namespace)
element = attr.element
local = attr.name.include?(":") ? attr.name.split(":", 2)[1] : attr.name
value = attr.value
element.remove_attribute(attr.name)
prefix = namespace.is_a?(String) ? nil : namespace.prefix
uri = namespace.is_a?(String) ? namespace : namespace.href
qualified = prefix.nil? || prefix.empty? ? local : "#{prefix}:#{local}"
element[qualified] = value
if prefix && !prefix.empty? && !uri.to_s.empty? && !resolve_prefix_ns(element, prefix)
element.add_namespace_definition(prefix, uri.to_s)
end
element.attribute_nodes.reverse.find { |candidate| candidate.name == qualified }
end
|
.set_attribute_value(attr, value) ⇒ Object
499
500
501
502
|
# File 'lib/moxml/adapter/leptris.rb', line 499
def set_attribute_value(attr, value)
attr.value = value.to_s
attr
end
|
.set_cdata_content(node, content) ⇒ Object
636
637
638
|
# File 'lib/moxml/adapter/leptris.rb', line 636
def set_cdata_content(node, content)
node.content = content.to_s
end
|
644
645
646
|
# File 'lib/moxml/adapter/leptris.rb', line 644
def (node, content)
node.content = content.to_s
end
|
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
109
110
111
|
# File 'lib/moxml/adapter/leptris.rb', line 109
def set_declaration_attribute(declaration, attr_name, value)
declaration.public_send("#{attr_name}=", value) if attr_matches?(attr_name)
end
|
.set_namespace(node, namespace) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/moxml/adapter/leptris.rb', line 134
def set_namespace(node, namespace)
return set_attribute_namespace(node, namespace) if node.is_a?(::Leptris::XML::Attr)
element = node
prefix = namespace.is_a?(String) ? nil : namespace.prefix
uri = namespace.is_a?(String) ? namespace : namespace.href
if prefix.nil? || prefix.empty?
element.default_namespace = uri.to_s
element.name = element.name.split(":", 2)[-1] if element.name.include?(":")
else
element.add_namespace_definition(prefix, uri.to_s)
element.name = "#{prefix}:#{element.name.split(':', 2)[-1]}"
end
element
end
|
.set_node_name(node, name) ⇒ Object
353
354
355
356
357
358
|
# File 'lib/moxml/adapter/leptris.rb', line 353
def set_node_name(node, name)
case node
when ::Leptris::XML::ProcessingInstruction, CustomizedLeptris::DocumentPI then node.target = name
else node.name = name
end
end
|
.set_processing_instruction_content(node, content) ⇒ Object
652
653
654
|
# File 'lib/moxml/adapter/leptris.rb', line 652
def set_processing_instruction_content(node, content)
node.data = content.to_s
end
|
.set_root(doc, element) ⇒ Object
32
33
34
|
# File 'lib/moxml/adapter/leptris.rb', line 32
def set_root(doc, element)
doc.root = element
end
|
.set_text_content(node, content) ⇒ Object
623
624
625
626
627
628
629
630
|
# File 'lib/moxml/adapter/leptris.rb', line 623
def set_text_content(node, content)
case node
when ::Leptris::XML::Document
node.root&.content = content.to_s
else
node.content = content.to_s
end
end
|
.split_entity_markers(natives, parent) ⇒ Object
Expand marker-bearing text nodes into the child sequence the
moxml contract exposes: text, EntityReference, text, ...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
# File 'lib/moxml/adapter/leptris.rb', line 405
def split_entity_markers(natives, parent)
result = []
natives.each do |child|
if child.is_a?(::Leptris::XML::Text)
content = child.content
content.force_encoding("UTF-8")
end
if content&.include?(Entity::MARKER)
content.scan(/([^#{Entity::MARKER}]*)(?:#{Entity::MARKER}([\w.:-]+);)?/o) do
text_part = Regexp.last_match(1)
name = Regexp.last_match(2)
result << CustomizedLeptris::TextSegment.new(text_part, parent) unless text_part.empty?
result << CustomizedLeptris::EntityReference.new(name) if name
end
else
result << child
end
end
result
end
|
.text_content(node) ⇒ Object
.text_material_record(kind, text, node, depth_memo) ⇒ Object
303
304
305
306
307
308
309
310
311
312
313
314
|
# File 'lib/moxml/adapter/leptris.rb', line 303
def text_material_record(kind, text, node, depth_memo)
{
kind: kind,
qname: nil,
prefix: nil,
namespace_uri: nil,
namespaces: Materializer::EMPTY_ATTRIBUTES,
attributes: Materializer::EMPTY_ATTRIBUTES,
text: text,
depth: material_depth(node, depth_memo),
}
end
|
.uses_xmlns_prefix?(ast) ⇒ Boolean
xmlns:name is a nokogiri-compat convention addressing
elements in the default namespace; only the Ruby engine
implements it.
799
800
801
802
803
804
805
|
# File 'lib/moxml/adapter/leptris.rb', line 799
def uses_xmlns_prefix?(ast)
return true if ast.type == :test && ast.value[:namespace] == "xmlns"
ast.children.any? do |child|
child.is_a?(XPath::AST::Node) && uses_xmlns_prefix?(child)
end
end
|
.xpath(node, expression, namespaces = {}) ⇒ Object
701
702
703
704
705
706
|
# File 'lib/moxml/adapter/leptris.rb', line 701
def xpath(node, expression, namespaces = {})
native = native_xpath(node, expression, namespaces)
return native unless native.nil?
engine_xpath(node, expression, namespaces)
end
|