Class: Moxml::Node
Direct Known Subclasses
Attribute, Cdata, Comment, Declaration, Doctype, Document, Element, EntityReference, Namespace, ProcessingInstruction, Text
Constant Summary
collapse
- TYPES =
%i[
element text cdata comment processing_instruction document
declaration doctype namespace attribute unknown entity_reference
].freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Object
-
#[](name) ⇒ Object
Attribute accessor - only works on Element nodes Returns nil for non-element nodes.
-
#add_child(node) ⇒ Object
-
#add_next_sibling(node) ⇒ Object
-
#add_previous_sibling(node) ⇒ Object
-
#at_xpath(expression, namespaces = {}) ⇒ Object
-
#children ⇒ Object
-
#clone ⇒ Object
(also: #dup)
-
#document ⇒ Object
-
#each_node(&block) ⇒ Object
Recursively yield all descendant nodes Used by XPath descendant-or-self and descendant axes.
-
#find(xpath_expression, namespaces = {}) ⇒ Object
Convenience find methods (aliases for xpath methods).
-
#find_all(xpath_expression, namespaces = {}) ⇒ Object
-
#first_child ⇒ Object
-
#has_children? ⇒ Boolean
Check if node has any children.
-
#identifier ⇒ String?
Returns the primary identifier for this node type For Element: the tag name For Attribute: the attribute name For ProcessingInstruction: the target For content nodes (Text, Comment, Cdata, Declaration): nil (no identifier) For Doctype: nil (not fully implemented across adapters).
-
#initialize(native, context) ⇒ Node
constructor
-
#last_child ⇒ Object
-
#namespace ⇒ Object
Returns the namespace of this node Only applicable to Element nodes, returns nil for others.
-
#namespaces ⇒ Object
Returns all namespace definitions on this node Only applicable to Element nodes, returns empty array for others.
-
#next_sibling ⇒ Object
-
#parent ⇒ Object
-
#previous_sibling ⇒ Object
-
#remove ⇒ Object
-
#replace(node) ⇒ Object
-
#text ⇒ Object
Returns the text content of this node Subclasses should override this method Element and Text have their own implementations.
-
#to_xml(options = {}) ⇒ Object
-
#xpath(expression, namespaces = {}) ⇒ Object
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
Constructor Details
#initialize(native, context) ⇒ Node
Returns a new instance of Node.
17
18
19
20
21
|
# File 'lib/moxml/node.rb', line 17
def initialize(native, context)
@context = context
@native = native
@parent_node = nil
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
15
16
17
|
# File 'lib/moxml/node.rb', line 15
def context
@context
end
|
#native ⇒ Object
Returns the value of attribute native.
15
16
17
|
# File 'lib/moxml/node.rb', line 15
def native
@native
end
|
Class Method Details
.adapter(context) ⇒ Object
237
238
239
|
# File 'lib/moxml/node.rb', line 237
def self.adapter(context)
context.config.adapter
end
|
.wrap(node, context) ⇒ Object
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/moxml/node.rb', line 211
def self.wrap(node, context)
return nil if node.nil?
klass = case adapter(context).node_type(node)
when :element then Element
when :text then Text
when :cdata then Cdata
when :comment then Comment
when :processing_instruction then ProcessingInstruction
when :document then Document
when :declaration then Declaration
when :doctype then Doctype
when :attribute then Attribute
when :entity_reference then EntityReference
else self
end
klass.new(node, context)
end
|
Instance Method Details
#==(other) ⇒ Object
189
190
191
|
# File 'lib/moxml/node.rb', line 189
def ==(other)
self.class == other.class && @native == other.native
end
|
#[](name) ⇒ Object
Attribute accessor - only works on Element nodes Returns nil for non-element nodes
148
149
150
151
152
153
|
# File 'lib/moxml/node.rb', line 148
def [](name)
return nil unless respond_to?(:attribute)
attr = attribute(name)
attr&.value if attr.respond_to?(:value)
end
|
#add_child(node) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/moxml/node.rb', line 47
def add_child(node)
node = prepare_node(node)
adapter.add_child(@native, node.native)
node.instance_variable_set(:@parent_node, self)
invalidate_children_cache!
self
end
|
#add_next_sibling(node) ⇒ Object
62
63
64
65
66
67
|
# File 'lib/moxml/node.rb', line 62
def add_next_sibling(node)
node = prepare_node(node)
adapter.add_next_sibling(@native, node.native)
invalidate_parent_children_cache!
self
end
|
#add_previous_sibling(node) ⇒ Object
55
56
57
58
59
60
|
# File 'lib/moxml/node.rb', line 55
def add_previous_sibling(node)
node = prepare_node(node)
adapter.add_previous_sibling(@native, node.native)
invalidate_parent_children_cache!
self
end
|
#at_xpath(expression, namespaces = {}) ⇒ Object
98
99
100
101
|
# File 'lib/moxml/node.rb', line 98
def at_xpath(expression, namespaces = {})
Moxml::Node.wrap(adapter.at_xpath(@native, expression, namespaces),
context)
end
|
#children ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/moxml/node.rb', line 31
def children
@children ||= NodeSet.new(
adapter.children(@native).map { adapter.patch_node(_1, @native) },
context,
self,
)
end
|
#clone ⇒ Object
Also known as:
dup
184
185
186
|
# File 'lib/moxml/node.rb', line 184
def clone
Moxml::Node.wrap(adapter.dup(@native), context)
end
|
#document ⇒ Object
23
24
25
|
# File 'lib/moxml/node.rb', line 23
def document
Document.wrap(adapter.document(@native), context)
end
|
#each_node(&block) ⇒ Object
Recursively yield all descendant nodes Used by XPath descendant-or-self and descendant axes
176
177
178
179
180
181
|
# File 'lib/moxml/node.rb', line 176
def each_node(&block)
children.each do |child|
yield child
child.each_node(&block) if child.respond_to?(:each_node)
end
end
|
#find(xpath_expression, namespaces = {}) ⇒ Object
Convenience find methods (aliases for xpath methods)
104
105
106
|
# File 'lib/moxml/node.rb', line 104
def find(xpath_expression, namespaces = {})
at_xpath(xpath_expression, namespaces)
end
|
#find_all(xpath_expression, namespaces = {}) ⇒ Object
108
109
110
|
# File 'lib/moxml/node.rb', line 108
def find_all(xpath_expression, namespaces = {})
xpath(xpath_expression, namespaces).to_a
end
|
#first_child ⇒ Object
118
119
120
|
# File 'lib/moxml/node.rb', line 118
def first_child
children.first
end
|
#has_children? ⇒ Boolean
Check if node has any children
113
114
115
|
# File 'lib/moxml/node.rb', line 113
def has_children?
!children.empty?
end
|
#identifier ⇒ String?
Returns the primary identifier for this node type For Element: the tag name For Attribute: the attribute name For ProcessingInstruction: the target For content nodes (Text, Comment, Cdata, Declaration): nil (no identifier) For Doctype: nil (not fully implemented across adapters)
207
208
209
|
# File 'lib/moxml/node.rb', line 207
def identifier
nil
end
|
#last_child ⇒ Object
122
123
124
|
# File 'lib/moxml/node.rb', line 122
def last_child
children.last
end
|
#namespace ⇒ Object
Returns the namespace of this node Only applicable to Element nodes, returns nil for others
157
158
159
160
161
162
|
# File 'lib/moxml/node.rb', line 157
def namespace
return nil unless element?
ns = adapter.namespace(@native)
ns && Namespace.new(ns, context)
end
|
#namespaces ⇒ Object
Returns all namespace definitions on this node Only applicable to Element nodes, returns empty array for others
166
167
168
169
170
171
172
|
# File 'lib/moxml/node.rb', line 166
def namespaces
return [] unless element?
adapter.namespace_definitions(@native).map do |ns|
Namespace.new(ns, context)
end
end
|
#next_sibling ⇒ Object
39
40
41
|
# File 'lib/moxml/node.rb', line 39
def next_sibling
Moxml::Node.wrap(adapter.next_sibling(@native), context)
end
|
#parent ⇒ Object
27
28
29
|
# File 'lib/moxml/node.rb', line 27
def parent
Moxml::Node.wrap(adapter.parent(@native), context)
end
|
#previous_sibling ⇒ Object
43
44
45
|
# File 'lib/moxml/node.rb', line 43
def previous_sibling
Moxml::Node.wrap(adapter.previous_sibling(@native), context)
end
|
#remove ⇒ Object
69
70
71
72
73
74
|
# File 'lib/moxml/node.rb', line 69
def remove
invalidate_parent_children_cache!
adapter.remove(@native)
invalidate_children_cache!
self
end
|
#replace(node) ⇒ Object
76
77
78
79
80
81
82
|
# File 'lib/moxml/node.rb', line 76
def replace(node)
node = prepare_node(node)
invalidate_parent_children_cache!
adapter.replace(@native, node.native)
invalidate_children_cache!
self
end
|
#text ⇒ Object
Returns the text content of this node Subclasses should override this method Element and Text have their own implementations
129
130
131
132
133
134
135
136
137
|
# File 'lib/moxml/node.rb', line 129
def text
if respond_to?(:content)
content
elsif respond_to?(:children)
children.grep(Text).map(&:content).join
else
""
end
end
|
#to_xml(options = {}) ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/moxml/node.rb', line 84
def to_xml(options = {})
serialize_options = default_options.merge(options)
serialize_options[:no_declaration] = !should_include_declaration?(options)
adapter.serialize(@native, serialize_options)
end
|
#xpath(expression, namespaces = {}) ⇒ Object
94
95
96
|
# File 'lib/moxml/node.rb', line 94
def xpath(expression, namespaces = {})
NodeSet.new(adapter.xpath(@native, expression, namespaces), context)
end
|