Class: Leptris::XML::Element
- Inherits:
-
Node
- Object
- Node
- Leptris::XML::Element
show all
- Defined in:
- lib/leptris/xml/element.rb
Instance Attribute Summary
Attributes inherited from Node
#c_ptr, #document
Instance Method Summary
collapse
Methods inherited from Node
#<=>, #==, #cdata?, #child, #children, #comment?, #css_path, #element?, #element_children, #first_element_child, #initialize, #inspect, #last_element_child, #line, #next_element, #next_sibling, #parent, #path, #previous_element, #previous_sibling, #processing_instruction?, #text?, #traverse, #type, #unlink, wrap
Methods included from Searchable
#at, #at_css, #at_xpath, #css, #search, #xpath
Instance Method Details
#[](key) ⇒ Object
Also known as:
attr, get_attribute
26
27
28
|
# File 'lib/leptris/xml/element.rb', line 26
def [](key)
Leptris::XML::FFI.leptris_element_attribute(@c_ptr, key.to_s)
end
|
#[]=(key, value) ⇒ Object
Also known as:
set_attribute
32
33
34
35
36
|
# File 'lib/leptris/xml/element.rb', line 32
def []=(key, value)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_set_attribute(@c_ptr, key.to_s, value.to_s))
value
end
|
#add_child(node_or_markup) ⇒ Object
Also known as:
<<
#add_namespace_definition(prefix, href) ⇒ Object
Also known as:
add_namespace
231
232
233
234
235
236
|
# File 'lib/leptris/xml/element.rb', line 231
def add_namespace_definition(prefix, href)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_add_namespace_definition(
@c_ptr, prefix.to_s, href.to_s))
Leptris::XML::Namespace.new(self, href.to_s, prefix: prefix.nil? ? nil : prefix.to_s)
end
|
#add_next_sibling(node) ⇒ Object
87
88
89
90
91
|
# File 'lib/leptris/xml/element.rb', line 87
def add_next_sibling(node)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_insert_after(@c_ptr, node.c_ptr))
node
end
|
#add_previous_sibling(node) ⇒ Object
93
94
95
96
97
|
# File 'lib/leptris/xml/element.rb', line 93
def add_previous_sibling(node)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_insert_before(@c_ptr, node.c_ptr))
node
end
|
#attribute_nodes ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/leptris/xml/element.rb', line 72
def attribute_nodes
count = Leptris::XML::FFI.leptris_element_attribute_count(@c_ptr)
count.times.map do |i|
name = Leptris::XML::FFI.leptris_element_attribute_name_at(@c_ptr, i)
value = Leptris::XML::FFI.leptris_element_attribute_value_at(@c_ptr, i)
Leptris::XML::Attr.new(name, value, self)
end
end
|
#attributes ⇒ Object
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/leptris/xml/element.rb', line 61
def attributes
count = Leptris::XML::FFI.leptris_element_attribute_count(@c_ptr)
result = {}
count.times do |i|
name = Leptris::XML::FFI.leptris_element_attribute_name_at(@c_ptr, i)
value = Leptris::XML::FFI.leptris_element_attribute_value_at(@c_ptr, i)
result[name] = Leptris::XML::Attr.new(name, value, self)
end
result
end
|
#canonicalize(version = Leptris::XML::FFI::C14N_1_0, inclusive_namespaces = nil, with_comments: false, exclusive: false, mode: nil) ⇒ Object
Also known as:
c14n
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/leptris/xml/element.rb', line 216
def canonicalize(version = Leptris::XML::FFI::C14N_1_0,
inclusive_namespaces = nil,
with_comments: false,
exclusive: false,
mode: nil)
resolved_mode = mode || (exclusive ? Leptris::XML::FFI::C14N_MODE_EXCLUSIVE
: Leptris::XML::FFI::C14N_MODE_CANONICAL)
Leptris::XML::Serialization.canonicalize(
Leptris::XML::FFI.method(:leptris_c14n_canonicalize_subtree_ex), @c_ptr,
version: version, mode: resolved_mode,
inclusive_namespaces: inclusive_namespaces,
with_comments: )
end
|
#children=(node_or_nodes) ⇒ Object
105
106
107
108
109
110
|
# File 'lib/leptris/xml/element.rb', line 105
def children=(node_or_nodes)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_remove_children(@c_ptr))
Array(node_or_nodes).each { |n| add_child(n) }
end
|
#content ⇒ Object
16
17
18
|
# File 'lib/leptris/xml/element.rb', line 16
def content
Leptris::XML::FFI.leptris_element_text(@c_ptr)
end
|
#content=(new_content) ⇒ Object
20
21
22
23
24
|
# File 'lib/leptris/xml/element.rb', line 20
def content=(new_content)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_set_text(@c_ptr, new_content.to_s))
new_content
end
|
#default_namespace=(href) ⇒ Object
239
240
241
242
243
|
# File 'lib/leptris/xml/element.rb', line 239
def default_namespace=(href)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_set_default_namespace(@c_ptr, href.to_s))
href
end
|
#dup ⇒ Object
Also known as:
clone
154
155
156
157
158
|
# File 'lib/leptris/xml/element.rb', line 154
def dup
copy_ptr = Leptris::XML::FFI.leptris_element_copy(@c_ptr, @document.c_ptr)
raise Leptris::XML::Error, "leptris_element_copy failed" if copy_ptr.null?
Leptris::XML::Node.wrap(copy_ptr, @document)
end
|
#key?(name) ⇒ Boolean
Also known as:
has_attribute?
39
40
41
|
# File 'lib/leptris/xml/element.rb', line 39
def key?(name)
Leptris::XML::FFI.leptris_element_has_attribute(@c_ptr, name.to_s) != 0
end
|
#keys ⇒ Object
51
52
53
54
|
# File 'lib/leptris/xml/element.rb', line 51
def keys
count = Leptris::XML::FFI.leptris_element_attribute_count(@c_ptr)
count.times.map { |i| Leptris::XML::FFI.leptris_element_attribute_name_at(@c_ptr, i) }
end
|
#name ⇒ Object
Also known as:
node_name
4
5
6
|
# File 'lib/leptris/xml/element.rb', line 4
def name
Leptris::XML::FFI.leptris_element_name(@c_ptr)
end
|
#name=(new_name) ⇒ Object
Also known as:
node_name=
#namespace ⇒ Object
182
183
184
185
186
|
# File 'lib/leptris/xml/element.rb', line 182
def namespace
uri = Leptris::XML::FFI.leptris_element_namespace(@c_ptr)
return nil if uri.nil? || uri.empty?
Leptris::XML::Namespace.new(self, uri)
end
|
#namespace_definitions ⇒ Object
188
189
190
191
192
193
194
195
|
# File 'lib/leptris/xml/element.rb', line 188
def namespace_definitions
count = Leptris::XML::FFI.leptris_element_namespace_count(@c_ptr)
count.times.map do |i|
prefix = Leptris::XML::FFI.leptris_element_namespace_decl_prefix(@c_ptr, i)
uri = Leptris::XML::FFI.leptris_element_namespace_decl_uri(@c_ptr, i)
Leptris::XML::Namespace.new(self, uri, prefix: prefix)
end
end
|
#namespaces ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/leptris/xml/element.rb', line 197
def namespaces
scopes = {}
node = self
while node.is_a?(Leptris::XML::Element)
node.namespace_definitions.each do |ns|
key = ns.prefix ? "xmlns:#{ns.prefix}" : "xmlns"
scopes[key] ||= ns.href
end
node = node.parent
end
scopes
end
|
#prepend_child(node) ⇒ Object
81
82
83
84
85
|
# File 'lib/leptris/xml/element.rb', line 81
def prepend_child(node)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_prepend_child(@c_ptr, node.c_ptr))
node
end
|
#remove_attribute(name) ⇒ Object
Also known as:
delete
44
45
46
47
48
|
# File 'lib/leptris/xml/element.rb', line 44
def remove_attribute(name)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_remove_attribute(@c_ptr, name.to_s))
self
end
|
#remove_child(node) ⇒ Object
99
100
101
102
103
|
# File 'lib/leptris/xml/element.rb', line 99
def remove_child(node)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_remove_child(@c_ptr, node.c_ptr))
node
end
|
#remove_namespace_definition(prefix) ⇒ Object
245
246
247
248
249
|
# File 'lib/leptris/xml/element.rb', line 245
def remove_namespace_definition(prefix)
Leptris::XML::FFI.check_status(
Leptris::XML::FFI.leptris_element_remove_namespace_definition(@c_ptr, prefix.to_s))
self
end
|
#replace(new_node) ⇒ Object
Replace this element with new_node in the parent's child list.
new_node must belong to the same document. Returns new_node.
114
115
116
117
118
119
120
|
# File 'lib/leptris/xml/element.rb', line 114
def replace(new_node)
parent = self.parent
raise Leptris::XML::Error, "cannot replace a node with no parent" unless parent
add_next_sibling(new_node)
parent.remove_child(self)
new_node
end
|
#swap(new_node) ⇒ Object
Like #replace but returns self for chaining.
123
124
125
126
|
# File 'lib/leptris/xml/element.rb', line 123
def swap(new_node)
replace(new_node)
self
end
|
#to_xml(indent: 0, no_decl: false, encoding: nil) ⇒ Object
210
211
212
213
214
|
# File 'lib/leptris/xml/element.rb', line 210
def to_xml(indent: 0, no_decl: false, encoding: nil)
Leptris::XML::Serialization.to_xml(
Leptris::XML::FFI.method(:leptris_element_serialize), @c_ptr,
indent: indent, no_decl: no_decl, encoding: encoding)
end
|
#values ⇒ Object
56
57
58
59
|
# File 'lib/leptris/xml/element.rb', line 56
def values
count = Leptris::XML::FFI.leptris_element_attribute_count(@c_ptr)
count.times.map { |i| Leptris::XML::FFI.leptris_element_attribute_value_at(@c_ptr, i) }
end
|
#wrap(node_or_markup) ⇒ Object
Wrap this element in a new element parsed from markup or a dup of
node. The wrapper takes this element's place in the tree, and this
element becomes its only child. Returns self for chaining.
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/leptris/xml/element.rb', line 131
def wrap(node_or_markup)
wrapper =
case node_or_markup
when Leptris::XML::Element then node_or_markup.dup
when String
frag_doc = Leptris::XML::Document.parse(node_or_markup)
frag_doc.root or raise Leptris::XML::Error, "wrap markup has no root element"
else
raise ArgumentError, "wrap expects a String or Element, got #{node_or_markup.class}"
end
parent = self.parent
raise Leptris::XML::Error, "cannot wrap a node with no parent" unless parent
add_next_sibling(wrapper)
wrapper.add_child(self)
self
end
|