Class: Moxml::Adapter::Oga
- Inherits:
-
Base
- Object
- Base
- Moxml::Adapter::Oga
show all
- Defined in:
- lib/moxml/adapter/oga.rb
Constant Summary
collapse
- STANDARD_XML_ENTITIES =
Standard XML entities handled natively by parsers
%w[amp lt gt quot apos].freeze
- ENTITY_PATTERN =
Shared entity name pattern (W3C: 2-31 chars, starts with alpha)
"([a-zA-Z][a-zA-Z0-9]{1,30})"
- ENTITY_MARKER =
Marker character for entity preservation through Oga’s parser. U+0001 is preserved literally by Oga through parse/serialize cycle.
"\x01"
- ENTITY_MARKER_REGEX =
Regular expression for entity marker post-processing
/#{ENTITY_MARKER}#{ENTITY_PATTERN};/
- ENTITY_REF_REGEX =
Simple entity-only regex with no nested quantifiers
/&#{ENTITY_PATTERN};/
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(_native_doc = nil) ⇒ 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
-
.doctype_external_id(native) ⇒ Object
-
.doctype_name(native) ⇒ Object
Doctype accessor methods.
-
.doctype_system_id(native) ⇒ 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 = {}, _context = nil) ⇒ 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 Oga.
-
.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_entity_reference, create_namespace, create_processing_instruction, create_text, duplicate_node, entity_reference_name, 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_entity_reference_name, #validate_pi_target, #validate_prefix, #validate_uri
Class Method Details
.add_child(element, child_or_text) ⇒ Object
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/moxml/adapter/oga.rb', line 254
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
if element.is_a?(::Oga::XML::Document) &&
child.is_a?(::Oga::XML::XmlDeclaration)
element.instance_variable_set(:@xml_declaration, child)
end
element.children << child
end
|
.add_next_sibling(node, sibling) ⇒ Object
283
284
285
286
287
288
289
290
291
292
|
# File 'lib/moxml/adapter/oga.rb', line 283
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
272
273
274
275
276
277
278
279
280
281
|
# File 'lib/moxml/adapter/oga.rb', line 272
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
403
404
405
406
407
408
409
410
411
412
|
# File 'lib/moxml/adapter/oga.rb', line 403
def at_xpath(node, expression, namespaces = nil)
node.at_xpath(expression, namespaces: namespaces)
rescue ::Oga::XPath::Error => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "Oga",
node: node,
)
end
|
.attribute_element(attr) ⇒ Object
213
214
215
|
# File 'lib/moxml/adapter/oga.rb', line 213
def attribute_element(attr)
attr.element
end
|
.attributes(element) ⇒ Object
217
218
219
220
221
222
223
224
|
# File 'lib/moxml/adapter/oga.rb', line 217
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
337
338
339
|
# File 'lib/moxml/adapter/oga.rb', line 337
def cdata_content(node)
node.text
end
|
.children(node) ⇒ Object
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/moxml/adapter/oga.rb', line 173
def children(node)
all_children = []
if node.is_a?(::Oga::XML::Document)
all_children += [node.xml_declaration,
node.doctype].compact
end
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
|
345
346
347
|
# File 'lib/moxml/adapter/oga.rb', line 345
def (node)
node.text
end
|
.create_document(_native_doc = nil) ⇒ Object
63
64
65
|
# File 'lib/moxml/adapter/oga.rb', line 63
def create_document(_native_doc = nil)
::Oga::XML::Document.new
end
|
.create_native_cdata(content) ⇒ Object
75
76
77
|
# File 'lib/moxml/adapter/oga.rb', line 75
def create_native_cdata(content)
::Oga::XML::Cdata.new(text: content)
end
|
79
80
81
|
# File 'lib/moxml/adapter/oga.rb', line 79
def (content)
::Oga::XML::Comment.new(text: content)
end
|
.create_native_declaration(version, encoding, standalone) ⇒ Object
94
95
96
97
98
99
100
101
|
# File 'lib/moxml/adapter/oga.rb', line 94
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
83
84
85
86
87
88
|
# File 'lib/moxml/adapter/oga.rb', line 83
def create_native_doctype(name, external_id, system_id)
::Oga::XML::Doctype.new(
name: name, public_id: external_id, system_id: system_id,
type: external_id ? "PUBLIC" : "SYSTEM"
)
end
|
.create_native_element(name) ⇒ Object
67
68
69
|
# File 'lib/moxml/adapter/oga.rb', line 67
def create_native_element(name)
::Oga::XML::Element.new(name: name)
end
|
.create_native_namespace(element, prefix, uri) ⇒ Object
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/moxml/adapter/oga.rb', line 119
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
90
91
92
|
# File 'lib/moxml/adapter/oga.rb', line 90
def create_native_processing_instruction(target, content)
::Oga::XML::ProcessingInstruction.new(name: target, text: content)
end
|
.create_native_text(content) ⇒ Object
71
72
73
|
# File 'lib/moxml/adapter/oga.rb', line 71
def create_native_text(content)
::Oga::XML::Text.new(text: encode_entity_markers(content))
end
|
.declaration_attribute(declaration, attr_name) ⇒ Object
103
104
105
106
107
108
109
|
# File 'lib/moxml/adapter/oga.rb', line 103
def declaration_attribute(declaration, attr_name)
unless ::Moxml::Declaration::ALLOWED_ATTRIBUTES.include?(attr_name.to_s)
return
end
declaration.public_send(attr_name)
end
|
.doctype_external_id(native) ⇒ Object
383
384
385
|
# File 'lib/moxml/adapter/oga.rb', line 383
def doctype_external_id(native)
native.public_id
end
|
.doctype_name(native) ⇒ Object
379
380
381
|
# File 'lib/moxml/adapter/oga.rb', line 379
def doctype_name(native)
native.name
end
|
.doctype_system_id(native) ⇒ Object
387
388
389
|
# File 'lib/moxml/adapter/oga.rb', line 387
def doctype_system_id(native)
native.system_id
end
|
.document(node) ⇒ Object
202
203
204
205
206
207
|
# File 'lib/moxml/adapter/oga.rb', line 202
def document(node)
current = node
current = current.parent while parent(current)
current
end
|
.get_attribute(element, name) ⇒ Object
241
242
243
|
# File 'lib/moxml/adapter/oga.rb', line 241
def get_attribute(element, name)
element.attribute(name.to_s)
end
|
.get_attribute_value(element, name) ⇒ Object
245
246
247
|
# File 'lib/moxml/adapter/oga.rb', line 245
def get_attribute_value(element, name)
restore_entity_markers(element[name.to_s])
end
|
.inner_text(node) ⇒ Object
318
319
320
321
322
323
324
325
326
|
# File 'lib/moxml/adapter/oga.rb', line 318
def inner_text(node)
text = if node.respond_to?(:inner_text)
node.inner_text
else
node.text
end
restore_entity_markers(text)
end
|
.namespace(element) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/moxml/adapter/oga.rb', line 134
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
372
373
374
375
376
|
# File 'lib/moxml/adapter/oga.rb', line 372
def namespace_definitions(node)
return [] unless node.respond_to?(:namespaces)
node.namespaces.values
end
|
.namespace_prefix(namespace) ⇒ Object
361
362
363
364
365
366
|
# File 'lib/moxml/adapter/oga.rb', line 361
def namespace_prefix(namespace)
return if namespace.name == ::Oga::XML::Element::XMLNS_PREFIX
namespace.name
end
|
.namespace_uri(namespace) ⇒ Object
368
369
370
|
# File 'lib/moxml/adapter/oga.rb', line 368
def namespace_uri(namespace)
namespace.uri
end
|
.next_sibling(node) ⇒ Object
194
195
196
|
# File 'lib/moxml/adapter/oga.rb', line 194
def next_sibling(node)
node.next
end
|
.node_name(node) ⇒ Object
165
166
167
|
# File 'lib/moxml/adapter/oga.rb', line 165
def node_name(node)
node.name
end
|
.node_type(node) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/moxml/adapter/oga.rb', line 150
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::Attribute then :attribute
when ::Oga::XML::Namespace then :namespace
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
190
191
192
|
# File 'lib/moxml/adapter/oga.rb', line 190
def parent(node)
node.parent if node.respond_to?(:parent)
end
|
.parse(xml, options = {}, _context = nil) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/moxml/adapter/oga.rb', line 23
def parse(xml, options = {}, _context = nil)
processed_xml = preprocess_named_entities(xml)
native_doc = begin
::Oga.parse_xml(processed_xml, strict: options[:strict])
rescue LL::ParserError => e
raise Moxml::ParseError.new(
e.message,
source: xml.is_a?(String) ? xml[0..100] : nil,
)
end
ctx = _context || Context.new(:oga)
DocumentBuilder.new(ctx).build(native_doc)
end
|
.previous_sibling(node) ⇒ Object
198
199
200
|
# File 'lib/moxml/adapter/oga.rb', line 198
def previous_sibling(node)
node.previous
end
|
.processing_instruction_content(node) ⇒ Object
353
354
355
|
# File 'lib/moxml/adapter/oga.rb', line 353
def processing_instruction_content(node)
node.text
end
|
.processing_instruction_target(node) ⇒ Object
146
147
148
|
# File 'lib/moxml/adapter/oga.rb', line 146
def processing_instruction_target(node)
node.name
end
|
.remove(node) ⇒ Object
294
295
296
297
298
299
300
301
302
303
|
# File 'lib/moxml/adapter/oga.rb', line 294
def remove(node)
if node.is_a?(::Oga::XML::XmlDeclaration) &&
node.parent.is_a?(::Oga::XML::Document)
node.parent.instance_variable_set(:@xml_declaration, nil)
end
node.remove
end
|
.remove_attribute(element, name) ⇒ Object
249
250
251
252
|
# File 'lib/moxml/adapter/oga.rb', line 249
def remove_attribute(element, name)
attr = element.attribute(name.to_s)
element.attributes.delete(attr) if attr
end
|
.replace(node, new_node) ⇒ Object
305
306
307
|
# File 'lib/moxml/adapter/oga.rb', line 305
def replace(node, new_node)
node.replace(new_node)
end
|
.replace_children(node, new_children) ⇒ Object
309
310
311
312
|
# File 'lib/moxml/adapter/oga.rb', line 309
def replace_children(node, new_children)
node.children = []
new_children.each { |child| add_child(node, child) }
end
|
.root(document) ⇒ Object
209
210
211
|
# File 'lib/moxml/adapter/oga.rb', line 209
def root(document)
document.children.find { |node| node.is_a?(::Oga::XML::Element) }
end
|
.sax_parse(xml, handler) ⇒ void
This method returns an undefined value.
SAX parsing implementation for Oga
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/moxml/adapter/oga.rb', line 46
def sax_parse(xml, handler)
bridge = OgaSAXBridge.new(handler)
xml_string = xml.respond_to?(:read) ? xml.read : xml.to_s
handler.on_start_document
::Oga.sax_parse_xml(bridge, xml_string)
handler.on_end_document
rescue StandardError => e
error = Moxml::ParseError.new(e.message)
handler.on_error(error)
end
|
.serialize(node, options = {}) ⇒ Object
414
415
416
417
418
|
# File 'lib/moxml/adapter/oga.rb', line 414
def serialize(node, options = {})
output = serialize_without_entity_processing(node, options)
output.gsub(ENTITY_MARKER_REGEX, '&\1;')
end
|
.set_attribute(element, name, value) ⇒ Object
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/moxml/adapter/oga.rb', line 226
def set_attribute(element, name, value)
namespace_name = nil
if name.to_s.include?(":")
namespace_name, name = name.to_s.split(":",
2)
end
attr = ::Oga::XML::Attribute.new(
name: name.to_s,
namespace_name: namespace_name,
value: encode_entity_markers(value.to_s),
)
element.add_attribute(attr)
end
|
.set_cdata_content(node, content) ⇒ Object
341
342
343
|
# File 'lib/moxml/adapter/oga.rb', line 341
def set_cdata_content(node, content)
node.text = content
end
|
349
350
351
|
# File 'lib/moxml/adapter/oga.rb', line 349
def (node, content)
node.text = content
end
|
.set_declaration_attribute(declaration, attr_name, value) ⇒ Object
111
112
113
114
115
116
117
|
# File 'lib/moxml/adapter/oga.rb', line 111
def set_declaration_attribute(declaration, attr_name, value)
unless ::Moxml::Declaration::ALLOWED_ATTRIBUTES.include?(attr_name.to_s)
return
end
declaration.public_send("#{attr_name}=", value)
end
|
.set_namespace(element, ns_or_string) ⇒ Object
130
131
132
|
# File 'lib/moxml/adapter/oga.rb', line 130
def set_namespace(element, ns_or_string)
element.namespace_name = ns_or_string.to_s
end
|
.set_node_name(node, name) ⇒ Object
169
170
171
|
# File 'lib/moxml/adapter/oga.rb', line 169
def set_node_name(node, name)
node.name = name
end
|
.set_processing_instruction_content(node, content) ⇒ Object
357
358
359
|
# File 'lib/moxml/adapter/oga.rb', line 357
def set_processing_instruction_content(node, content)
node.text = content
end
|
.set_root(doc, element) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/moxml/adapter/oga.rb', line 15
def set_root(doc, element)
elements_to_remove = doc.children.grep(::Oga::XML::Element)
elements_to_remove.each { |elem| doc.children.delete(elem) }
doc.children << element
end
|
.set_text_content(node, content) ⇒ Object
328
329
330
331
332
333
334
335
|
# File 'lib/moxml/adapter/oga.rb', line 328
def set_text_content(node, content)
encoded = encode_entity_markers(content)
if node.respond_to?(:inner_text=)
node.inner_text = encoded
else
node.text = encoded
end
end
|
.text_content(node) ⇒ Object
314
315
316
|
# File 'lib/moxml/adapter/oga.rb', line 314
def text_content(node)
restore_entity_markers(node.text)
end
|
.xpath(node, expression, namespaces = nil) ⇒ Object
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/moxml/adapter/oga.rb', line 391
def xpath(node, expression, namespaces = nil)
node.xpath(expression, {},
namespaces: namespaces&.transform_keys(&:to_s)).to_a
rescue ::LL::ParserError => e
raise Moxml::XPathError.new(
e.message,
expression: expression,
adapter: "Oga",
node: node,
)
end
|