Class: Moxml::Adapter::Oga
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Oga
show all
- Defined in:
- lib/moxml/adapter/oga.rb
Class Method Summary
collapse
-
.add_child(element, child_or_text) ⇒ Object
-
.add_next_sibling(node, sibling) ⇒ Object
-
.add_previous_sibling(node, sibling) ⇒ Object
-
.at_xpath(node, expression, namespaces = nil) ⇒ Object
-
.attribute_element(attr) ⇒ Object
-
.attributes(element) ⇒ Object
-
.cdata_content(node) ⇒ Object
-
.children(node) ⇒ Object
-
.comment_content(node) ⇒ Object
-
.create_document ⇒ Object
-
.create_native_cdata(content) ⇒ Object
-
.create_native_comment(content) ⇒ Object
-
.create_native_declaration(version, encoding, standalone) ⇒ Object
-
.create_native_doctype(name, external_id, system_id) ⇒ Object
-
.create_native_element(name) ⇒ Object
-
.create_native_namespace(element, prefix, uri) ⇒ Object
-
.create_native_processing_instruction(target, content) ⇒ Object
-
.create_native_text(content) ⇒ Object
-
.declaration_attribute(declaration, attr_name) ⇒ Object
-
.document(node) ⇒ Object
-
.get_attribute(element, name) ⇒ Object
-
.get_attribute_value(element, name) ⇒ Object
-
.inner_text(node) ⇒ Object
-
.namespace(element) ⇒ Object
-
.namespace_definitions(node) ⇒ Object
-
.namespace_prefix(namespace) ⇒ Object
-
.namespace_uri(namespace) ⇒ Object
-
.next_sibling(node) ⇒ Object
-
.node_name(node) ⇒ Object
-
.node_type(node) ⇒ Object
-
.parent(node) ⇒ Object
-
.parse(xml, options = {}) ⇒ Object
-
.previous_sibling(node) ⇒ Object
-
.processing_instruction_content(node) ⇒ Object
-
.processing_instruction_target(node) ⇒ Object
-
.remove(node) ⇒ Object
-
.remove_attribute(element, name) ⇒ Object
-
.replace(node, new_node) ⇒ Object
-
.replace_children(node, new_children) ⇒ Object
-
.root(document) ⇒ Object
-
.serialize(node, _options = {}) ⇒ Object
-
.set_attribute(element, name, value) ⇒ Object
-
.set_cdata_content(node, content) ⇒ Object
-
.set_comment_content(node, content) ⇒ Object
-
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
-
.set_namespace(element, ns_or_string) ⇒ 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
-
.text_content(node) ⇒ Object
-
.xpath(node, expression, namespaces = nil) ⇒ Object
Methods inherited from Base
create_cdata, create_comment, create_declaration, create_doctype, create_element, create_namespace, create_processing_instruction, create_text, duplicate_node, set_attribute_name, set_attribute_value
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_pi_target, #validate_prefix, #validate_uri
Class Method Details
.add_child(element, child_or_text) ⇒ Object
204
205
206
207
208
209
210
211
212
213
|
# File 'lib/moxml/adapter/oga.rb', line 204
def add_child(element, child_or_text)
child =
if child_or_text.is_a?(String)
create_native_text(child_or_text)
else
child_or_text
end
element.children << child
end
|
.add_next_sibling(node, sibling) ⇒ Object
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/moxml/adapter/oga.rb', line 226
def add_next_sibling(node, sibling)
if node.parent == sibling.parent
dup_sibling = node.node_set.delete(sibling)
index = node.node_set.index(node) + 1
node.node_set.insert(index, dup_sibling)
else
node.after(sibling)
end
end
|
.add_previous_sibling(node, sibling) ⇒ Object
215
216
217
218
219
220
221
222
223
224
|
# File 'lib/moxml/adapter/oga.rb', line 215
def add_previous_sibling(node, sibling)
if node.parent == sibling.parent
dup_sibling = node.node_set.delete(sibling)
index = node.node_set.index(node)
node.node_set.insert(index, dup_sibling)
else
node.before(sibling)
end
end
|
.at_xpath(node, expression, namespaces = nil) ⇒ Object
319
320
321
322
323
|
# File 'lib/moxml/adapter/oga.rb', line 319
def at_xpath(node, expression, namespaces = nil)
node.at_xpath(expression, namespaces: namespaces)
rescue ::Oga::XPath::Error => e
raise Moxml::XPathError, e.message
end
|
.attribute_element(attr) ⇒ Object
166
167
168
|
# File 'lib/moxml/adapter/oga.rb', line 166
def attribute_element(attr)
attr.element
end
|
.attributes(element) ⇒ Object
170
171
172
173
174
175
176
177
|
# File 'lib/moxml/adapter/oga.rb', line 170
def attributes(element)
return [] unless element.respond_to?(:attributes)
element.attributes.reject do |attr|
attr.name == ::Oga::XML::Element::XMLNS_PREFIX || attr.namespace_name == ::Oga::XML::Element::XMLNS_PREFIX
end
end
|
.cdata_content(node) ⇒ Object
272
273
274
|
# File 'lib/moxml/adapter/oga.rb', line 272
def cdata_content(node)
node.text
end
|
.children(node) ⇒ Object
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/moxml/adapter/oga.rb', line 129
def children(node)
all_children = []
all_children += [node.xml_declaration, node.doctype].compact if node.is_a?(::Oga::XML::Document)
return all_children unless node.respond_to?(:children)
all_children + node.children.reject do |child|
child.is_a?(::Oga::XML::Text) &&
child.text.strip.empty? &&
!(child.previous.nil? && child.next.nil?)
end
end
|
280
281
282
|
# File 'lib/moxml/adapter/oga.rb', line 280
def (node)
node.text
end
|
.create_document ⇒ Object
27
28
29
|
# File 'lib/moxml/adapter/oga.rb', line 27
def create_document
::Oga::XML::Document.new
end
|
.create_native_cdata(content) ⇒ Object
39
40
41
|
# File 'lib/moxml/adapter/oga.rb', line 39
def create_native_cdata(content)
::Oga::XML::Cdata.new(text: content)
end
|
43
44
45
|
# File 'lib/moxml/adapter/oga.rb', line 43
def (content)
::Oga::XML::Comment.new(text: content)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/moxml/adapter/oga.rb', line 57
def create_native_declaration(version, encoding, standalone)
attrs = {
version: version,
encoding: encoding,
standalone: standalone
}.compact
::Moxml::Adapter::CustomizedOga::XmlDeclaration.new(attrs)
end
|
.create_native_doctype(name, external_id, system_id) ⇒ Object
47
48
49
50
51
|
# File 'lib/moxml/adapter/oga.rb', line 47
def create_native_doctype(name, external_id, system_id)
::Oga::XML::Doctype.new(
name: name, public_id: external_id, system_id: system_id, type: "PUBLIC"
)
end
|
.create_native_element(name) ⇒ Object
31
32
33
|
# File 'lib/moxml/adapter/oga.rb', line 31
def create_native_element(name)
::Oga::XML::Element.new(name: name)
end
|
.create_native_namespace(element, prefix, uri) ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/moxml/adapter/oga.rb', line 78
def create_native_namespace(element, prefix, uri)
ns = element.available_namespaces[prefix]
return ns unless ns.nil?
set_attribute(element, [::Oga::XML::Element::XMLNS_PREFIX, prefix].compact.join(":"), uri)
element.register_namespace(prefix, uri)
::Oga::XML::Namespace.new(name: prefix, uri: uri)
end
|
.create_native_processing_instruction(target, content) ⇒ Object
53
54
55
|
# File 'lib/moxml/adapter/oga.rb', line 53
def create_native_processing_instruction(target, content)
::Oga::XML::ProcessingInstruction.new(name: target, text: content)
end
|
.create_native_text(content) ⇒ Object
35
36
37
|
# File 'lib/moxml/adapter/oga.rb', line 35
def create_native_text(content)
::Oga::XML::Text.new(text: content)
end
|
.declaration_attribute(declaration, attr_name) ⇒ Object
66
67
68
69
70
|
# File 'lib/moxml/adapter/oga.rb', line 66
def declaration_attribute(declaration, attr_name)
return unless ::Moxml::Declaration::ALLOWED_ATTRIBUTES.include?(attr_name.to_s)
declaration.public_send(attr_name)
end
|
.document(node) ⇒ Object
155
156
157
158
159
160
|
# File 'lib/moxml/adapter/oga.rb', line 155
def document(node)
current = node
current = current.parent while parent(current)
current
end
|
.get_attribute(element, name) ⇒ Object
191
192
193
|
# File 'lib/moxml/adapter/oga.rb', line 191
def get_attribute(element, name)
element.attribute(name.to_s)
end
|
.get_attribute_value(element, name) ⇒ Object
195
196
197
|
# File 'lib/moxml/adapter/oga.rb', line 195
def get_attribute_value(element, name)
element[name.to_s]
end
|
.inner_text(node) ⇒ Object
254
255
256
257
258
259
260
261
|
# File 'lib/moxml/adapter/oga.rb', line 254
def inner_text(node)
if node.respond_to?(:inner_text)
node.inner_text
else
node.text
end
end
|
.namespace(element) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/moxml/adapter/oga.rb', line 92
def namespace(element)
if element.respond_to?(:namespace)
element.namespace
elsif element.respond_to?(:namespaces)
element.namespaces.values.last
end
rescue NoMethodError
nil
end
|
.namespace_definitions(node) ⇒ Object
307
308
309
310
311
|
# File 'lib/moxml/adapter/oga.rb', line 307
def namespace_definitions(node)
return [] unless node.respond_to?(:namespaces)
node.namespaces.values
end
|
.namespace_prefix(namespace) ⇒ Object
296
297
298
299
300
301
|
# File 'lib/moxml/adapter/oga.rb', line 296
def namespace_prefix(namespace)
return if namespace.name == ::Oga::XML::Element::XMLNS_PREFIX
namespace.name
end
|
.namespace_uri(namespace) ⇒ Object
303
304
305
|
# File 'lib/moxml/adapter/oga.rb', line 303
def namespace_uri(namespace)
namespace.uri
end
|
.next_sibling(node) ⇒ Object
147
148
149
|
# File 'lib/moxml/adapter/oga.rb', line 147
def next_sibling(node)
node.next
end
|
.node_name(node) ⇒ Object
121
122
123
|
# File 'lib/moxml/adapter/oga.rb', line 121
def node_name(node)
node.name
end
|
.node_type(node) ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
119
|
# File 'lib/moxml/adapter/oga.rb', line 108
def node_type(node)
case node
when ::Oga::XML::Element then :element
when ::Oga::XML::Text then :text
when ::Oga::XML::Cdata then :cdata
when ::Oga::XML::Comment then :comment
when ::Oga::XML::ProcessingInstruction then :processing_instruction
when ::Oga::XML::Document then :document
when ::Oga::XML::Doctype then :doctype
else :unknown
end
end
|
.parent(node) ⇒ Object
143
144
145
|
# File 'lib/moxml/adapter/oga.rb', line 143
def parent(node)
node.parent if node.respond_to?(:parent)
end
|
.parse(xml, options = {}) ⇒ Object
17
18
19
20
21
22
23
24
25
|
# File 'lib/moxml/adapter/oga.rb', line 17
def parse(xml, options = {})
native_doc = begin
::Oga.parse_xml(xml, strict: options[:strict])
rescue LL::ParserError => e
raise Moxml::ParseError, e.message
end
DocumentBuilder.new(Context.new(:oga)).build(native_doc)
end
|
.previous_sibling(node) ⇒ Object
151
152
153
|
# File 'lib/moxml/adapter/oga.rb', line 151
def previous_sibling(node)
node.previous
end
|
.processing_instruction_content(node) ⇒ Object
288
289
290
|
# File 'lib/moxml/adapter/oga.rb', line 288
def processing_instruction_content(node)
node.text
end
|
.processing_instruction_target(node) ⇒ Object
104
105
106
|
# File 'lib/moxml/adapter/oga.rb', line 104
def processing_instruction_target(node)
node.name
end
|
.remove(node) ⇒ Object
237
238
239
|
# File 'lib/moxml/adapter/oga.rb', line 237
def remove(node)
node.remove
end
|
.remove_attribute(element, name) ⇒ Object
199
200
201
202
|
# File 'lib/moxml/adapter/oga.rb', line 199
def remove_attribute(element, name)
attr = element.attribute(name.to_s)
element.attributes.delete(attr) if attr
end
|
.replace(node, new_node) ⇒ Object
241
242
243
|
# File 'lib/moxml/adapter/oga.rb', line 241
def replace(node, new_node)
node.replace(new_node)
end
|
.replace_children(node, new_children) ⇒ Object
245
246
247
248
|
# File 'lib/moxml/adapter/oga.rb', line 245
def replace_children(node, new_children)
node.children = []
new_children.each { |child| add_child(node, child) }
end
|
.root(document) ⇒ Object
162
163
164
|
# File 'lib/moxml/adapter/oga.rb', line 162
def root(document)
document.children.find { |node| node.is_a?(::Oga::XML::Element) }
end
|
.serialize(node, _options = {}) ⇒ Object
.set_attribute(element, name, value) ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/moxml/adapter/oga.rb', line 179
def set_attribute(element, name, value)
namespace_name = nil
namespace_name, name = name.to_s.split(":", 2) if name.to_s.include?(":")
attr = ::Oga::XML::Attribute.new(
name: name.to_s,
namespace_name: namespace_name,
value: value.to_s
)
element.add_attribute(attr)
end
|
.set_cdata_content(node, content) ⇒ Object
276
277
278
|
# File 'lib/moxml/adapter/oga.rb', line 276
def set_cdata_content(node, content)
node.text = content
end
|
284
285
286
|
# File 'lib/moxml/adapter/oga.rb', line 284
def (node, content)
node.text = content
end
|
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
72
73
74
75
76
|
# File 'lib/moxml/adapter/oga.rb', line 72
def set_declaration_attribute(declaration, attr_name, value)
return unless ::Moxml::Declaration::ALLOWED_ATTRIBUTES.include?(attr_name.to_s)
declaration.public_send("#{attr_name}=", value)
end
|
.set_namespace(element, ns_or_string) ⇒ Object
88
89
90
|
# File 'lib/moxml/adapter/oga.rb', line 88
def set_namespace(element, ns_or_string)
element.namespace_name = ns_or_string.to_s
end
|
.set_node_name(node, name) ⇒ Object
125
126
127
|
# File 'lib/moxml/adapter/oga.rb', line 125
def set_node_name(node, name)
node.name = name
end
|
.set_processing_instruction_content(node, content) ⇒ Object
292
293
294
|
# File 'lib/moxml/adapter/oga.rb', line 292
def set_processing_instruction_content(node, content)
node.text = content
end
|
.set_root(doc, element) ⇒ Object
12
13
14
15
|
# File 'lib/moxml/adapter/oga.rb', line 12
def set_root(doc, element)
doc.children.clear doc.children << element
end
|
.set_text_content(node, content) ⇒ Object
263
264
265
266
267
268
269
270
|
# File 'lib/moxml/adapter/oga.rb', line 263
def set_text_content(node, content)
if node.respond_to?(:inner_text)
node.inner_text = content
else
node.text = content
end
end
|
.text_content(node) ⇒ Object
250
251
252
|
# File 'lib/moxml/adapter/oga.rb', line 250
def text_content(node)
node.text
end
|
.xpath(node, expression, namespaces = nil) ⇒ Object
313
314
315
316
317
|
# File 'lib/moxml/adapter/oga.rb', line 313
def xpath(node, expression, namespaces = nil)
node.xpath(expression, {}, namespaces: namespaces&.transform_keys(&:to_s)).to_a
rescue ::LL::ParserError => e
raise Moxml::XPathError, e.message
end
|