Class: Moxml::Adapter::Nokogiri
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Nokogiri
show all
- Defined in:
- lib/moxml/adapter/nokogiri.rb
Defined Under Namespace
Classes: NokogiriSAXBridge
Class Method Summary
collapse
-
.add_child(element, child) ⇒ 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(_native_doc = nil) ⇒ Object
-
.create_fragment ⇒ 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
-
.sax_parse(xml, handler) ⇒ void
SAX parsing implementation for Nokogiri.
-
.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) ⇒ 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, patch_node, prepare_for_new_document, sax_supported?, 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) ⇒ Object
223
224
225
226
227
228
229
230
231
232
|
# File 'lib/moxml/adapter/nokogiri.rb', line 223
def add_child(element, child)
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
238
239
240
|
# File 'lib/moxml/adapter/nokogiri.rb', line 238
def add_next_sibling(node, sibling)
node.add_next_sibling(sibling)
end
|
.add_previous_sibling(node, sibling) ⇒ Object
234
235
236
|
# File 'lib/moxml/adapter/nokogiri.rb', line 234
def add_previous_sibling(node, sibling)
node.add_previous_sibling(sibling)
end
|
.at_xpath(node, expression, namespaces = nil) ⇒ Object
310
311
312
313
314
315
316
317
318
319
|
# File 'lib/moxml/adapter/nokogiri.rb', line 310
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
|
.attribute_element(attr) ⇒ Object
197
198
199
|
# File 'lib/moxml/adapter/nokogiri.rb', line 197
def attribute_element(attr)
attr.parent
end
|
.attributes(element) ⇒ Object
201
202
203
|
# File 'lib/moxml/adapter/nokogiri.rb', line 201
def attributes(element)
element.attributes.values
end
|
.cdata_content(node) ⇒ Object
263
264
265
|
# File 'lib/moxml/adapter/nokogiri.rb', line 263
def cdata_content(node)
node.content
end
|
.children(node) ⇒ Object
165
166
167
168
169
170
|
# File 'lib/moxml/adapter/nokogiri.rb', line 165
def children(node)
node.children.reject do |child|
child.text? && child.content.strip.empty? &&
!(child.previous_sibling.nil? && child.next_sibling.nil?)
end
end
|
271
272
273
|
# File 'lib/moxml/adapter/nokogiri.rb', line 271
def (node)
node.content
end
|
.create_document(_native_doc = nil) ⇒ Object
59
60
61
|
# File 'lib/moxml/adapter/nokogiri.rb', line 59
def create_document(_native_doc = nil)
::Nokogiri::XML::Document.new
end
|
.create_fragment ⇒ Object
63
64
65
66
67
68
69
|
# File 'lib/moxml/adapter/nokogiri.rb', line 63
def create_fragment
::Nokogiri::XML::DocumentFragment.new(
::Nokogiri::XML::Document.new,
)
end
|
.create_native_cdata(content) ⇒ Object
79
80
81
|
# File 'lib/moxml/adapter/nokogiri.rb', line 79
def create_native_cdata(content)
::Nokogiri::XML::CDATA.new(create_document, content)
end
|
83
84
85
|
# File 'lib/moxml/adapter/nokogiri.rb', line 83
def (content)
::Nokogiri::XML::.new(create_document, content)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
99
100
101
102
103
104
105
|
# File 'lib/moxml/adapter/nokogiri.rb', line 99
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
87
88
89
90
91
|
# File 'lib/moxml/adapter/nokogiri.rb', line 87
def create_native_doctype(name, external_id, system_id)
create_document.create_internal_subset(
name, external_id, system_id
)
end
|
.create_native_element(name) ⇒ Object
71
72
73
|
# File 'lib/moxml/adapter/nokogiri.rb', line 71
def create_native_element(name)
::Nokogiri::XML::Element.new(name, create_document)
end
|
.create_native_namespace(element, prefix, uri) ⇒ Object
138
139
140
|
# File 'lib/moxml/adapter/nokogiri.rb', line 138
def create_native_namespace(element, prefix, uri)
element.add_namespace_definition(prefix, uri)
end
|
.create_native_processing_instruction(target, content) ⇒ Object
93
94
95
96
97
|
# File 'lib/moxml/adapter/nokogiri.rb', line 93
def create_native_processing_instruction(target, content)
::Nokogiri::XML::ProcessingInstruction.new(
::Nokogiri::XML::Document.new, target, content
)
end
|
.create_native_text(content) ⇒ Object
75
76
77
|
# File 'lib/moxml/adapter/nokogiri.rb', line 75
def create_native_text(content)
::Nokogiri::XML::Text.new(content, create_document)
end
|
.declaration_attribute(declaration, attr_name) ⇒ Object
107
108
109
110
111
112
|
# File 'lib/moxml/adapter/nokogiri.rb', line 107
def declaration_attribute(declaration, attr_name)
return nil unless declaration.content
match = declaration.content.match(/#{attr_name}="([^"]*)"/)
match && match[1]
end
|
.document(node) ⇒ Object
189
190
191
|
# File 'lib/moxml/adapter/nokogiri.rb', line 189
def document(node)
node.document
end
|
.get_attribute(element, name) ⇒ Object
209
210
211
212
|
# File 'lib/moxml/adapter/nokogiri.rb', line 209
def get_attribute(element, name)
element.attributes[name.to_s]
end
|
.get_attribute_value(element, name) ⇒ Object
214
215
216
217
|
# File 'lib/moxml/adapter/nokogiri.rb', line 214
def get_attribute_value(element, name)
element[name.to_s]
end
|
.inner_text(node) ⇒ Object
254
255
256
257
|
# File 'lib/moxml/adapter/nokogiri.rb', line 254
def inner_text(node)
text_children = node.children - node.element_children
text_children.map(&:content).join
end
|
.namespace(element) ⇒ Object
130
131
132
|
# File 'lib/moxml/adapter/nokogiri.rb', line 130
def namespace(element)
element.namespace
end
|
.namespace_definitions(node) ⇒ Object
295
296
297
|
# File 'lib/moxml/adapter/nokogiri.rb', line 295
def namespace_definitions(node)
node.namespace_definitions
end
|
.namespace_prefix(namespace) ⇒ Object
287
288
289
|
# File 'lib/moxml/adapter/nokogiri.rb', line 287
def namespace_prefix(namespace)
namespace.prefix
end
|
.namespace_uri(namespace) ⇒ Object
291
292
293
|
# File 'lib/moxml/adapter/nokogiri.rb', line 291
def namespace_uri(namespace)
namespace.href
end
|
.next_sibling(node) ⇒ Object
181
182
183
|
# File 'lib/moxml/adapter/nokogiri.rb', line 181
def next_sibling(node)
node.next_sibling
end
|
.node_name(node) ⇒ Object
157
158
159
|
# File 'lib/moxml/adapter/nokogiri.rb', line 157
def node_name(node)
node.name
end
|
.node_type(node) ⇒ Object
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/moxml/adapter/nokogiri.rb', line 142
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:: 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
else :unknown
end
end
|
.parent(node) ⇒ Object
177
178
179
|
# File 'lib/moxml/adapter/nokogiri.rb', line 177
def parent(node)
node.parent
end
|
.parse(xml, options = {}) ⇒ Object
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/moxml/adapter/nokogiri.rb', line 14
def parse(xml, options = {})
native_doc = begin
if options[:fragment]
::Nokogiri::XML::DocumentFragment.parse(xml) do |config|
config.strict.nonet
config.recover unless options[:strict]
end
else
::Nokogiri::XML(xml, nil, options[:encoding]) do |config|
config.strict.nonet
config.recover unless options[:strict]
end
end
rescue ::Nokogiri::XML::SyntaxError => e
raise Moxml::ParseError.new(e.message, line: e.line,
column: e.column)
end
DocumentBuilder.new(Context.new(:nokogiri)).build(native_doc)
end
|
.previous_sibling(node) ⇒ Object
185
186
187
|
# File 'lib/moxml/adapter/nokogiri.rb', line 185
def previous_sibling(node)
node.previous_sibling
end
|
.processing_instruction_content(node) ⇒ Object
279
280
281
|
# File 'lib/moxml/adapter/nokogiri.rb', line 279
def processing_instruction_content(node)
node.content
end
|
.processing_instruction_target(node) ⇒ Object
134
135
136
|
# File 'lib/moxml/adapter/nokogiri.rb', line 134
def processing_instruction_target(node)
node.name
end
|
.remove(node) ⇒ Object
242
243
244
|
# File 'lib/moxml/adapter/nokogiri.rb', line 242
def remove(node)
node.remove
end
|
.remove_attribute(element, name) ⇒ Object
219
220
221
|
# File 'lib/moxml/adapter/nokogiri.rb', line 219
def remove_attribute(element, name)
element.remove_attribute(name.to_s)
end
|
.replace(node, new_node) ⇒ Object
246
247
248
|
# File 'lib/moxml/adapter/nokogiri.rb', line 246
def replace(node, new_node)
node.replace(new_node)
end
|
.replace_children(node, new_children) ⇒ Object
172
173
174
175
|
# File 'lib/moxml/adapter/nokogiri.rb', line 172
def replace_children(node, new_children)
node.children.unlink
new_children.each { |child| add_child(node, child) }
end
|
.root(document) ⇒ Object
193
194
195
|
# File 'lib/moxml/adapter/nokogiri.rb', line 193
def root(document)
document.respond_to?(:root) ? document.root : document.children.first
end
|
.sax_parse(xml, handler) ⇒ void
This method returns an undefined value.
SAX parsing implementation for Nokogiri
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/moxml/adapter/nokogiri.rb', line 40
def sax_parse(xml, handler)
bridge = NokogiriSAXBridge.new(handler)
parser = ::Nokogiri::XML::SAX::Parser.new(bridge)
if xml.respond_to?(:read)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/moxml/adapter/nokogiri.rb', line 321
def serialize(node, options = {})
save_options = ::Nokogiri::XML::Node::SaveOptions::AS_XML
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
if options[:no_declaration]
save_options |= ::Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
end
node.to_xml(
indent: options[:indent],
encoding: options[:encoding],
save_with: save_options,
)
end
|
.set_attribute(element, name, value) ⇒ Object
205
206
207
|
# File 'lib/moxml/adapter/nokogiri.rb', line 205
def set_attribute(element, name, value)
element[name.to_s] = value.to_s
end
|
.set_cdata_content(node, content) ⇒ Object
267
268
269
|
# File 'lib/moxml/adapter/nokogiri.rb', line 267
def set_cdata_content(node, content)
node.content = content
end
|
275
276
277
|
# File 'lib/moxml/adapter/nokogiri.rb', line 275
def (node, content)
node.native_content = content
end
|
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
|
# File 'lib/moxml/adapter/nokogiri.rb', line 114
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
126
127
128
|
# File 'lib/moxml/adapter/nokogiri.rb', line 126
def set_namespace(element, ns)
element.namespace = ns
end
|
.set_node_name(node, name) ⇒ Object
161
162
163
|
# File 'lib/moxml/adapter/nokogiri.rb', line 161
def set_node_name(node, name)
node.name = name
end
|
.set_processing_instruction_content(node, content) ⇒ Object
283
284
285
|
# File 'lib/moxml/adapter/nokogiri.rb', line 283
def set_processing_instruction_content(node, content)
node.native_content = content
end
|
.set_root(doc, element) ⇒ Object
10
11
12
|
# File 'lib/moxml/adapter/nokogiri.rb', line 10
def set_root(doc, element)
doc.root = element
end
|
.set_text_content(node, content) ⇒ Object
259
260
261
|
# File 'lib/moxml/adapter/nokogiri.rb', line 259
def set_text_content(node, content)
node.native_content = content
end
|
.text_content(node) ⇒ Object
250
251
252
|
# File 'lib/moxml/adapter/nokogiri.rb', line 250
def text_content(node)
node.text
end
|
.xpath(node, expression, namespaces = nil) ⇒ Object
299
300
301
302
303
304
305
306
307
308
|
# File 'lib/moxml/adapter/nokogiri.rb', line 299
def xpath(node, expression, namespaces = nil)
node.xpath(expression, namespaces).to_a
rescue ::Nokogiri::XML::XPath::SyntaxError => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "Nokogiri",
node: node,
)
end
|