Class: Plurimath::XmlEngine::Oga::Node
- Inherits:
-
Wrapper
- Object
- Wrapper
- Plurimath::XmlEngine::Oga::Node
show all
- Defined in:
- lib/plurimath/xml_engine/oga/node.rb
Instance Method Summary
collapse
Methods inherited from Wrapper
#==, #initialize, #unwrap
Instance Method Details
#<<(other) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 51
def <<(other)
other = other.unwrap if other.respond_to? :unwrap
case other
when String
text = other
other = ::Oga::XML::Text.new
other.instance_variable_set(:@from_plurimath, true)
other.instance_variable_set(:@text, text)
other.instance_variable_set(:@decoded, true)
end
@wrapped.children << other.dup
self
end
|
#[](attr) ⇒ Object
31
32
33
34
35
36
37
38
39
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 31
def [](attr)
attr = attr.to_s
@wrapped.attributes.each do |e|
return e.value if [e.name, e.name.split(":").last].include? attr
end
nil
end
|
#[]=(attr, value) ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 41
def []=(attr, value)
attr = ::Oga::XML::Attribute.new(name: attr.to_s)
attr.element = @wrapped
attr.instance_variable_set(:@value, encode_entities(value.to_s))
attr.instance_variable_set(:@decoded, true)
@wrapped.attributes << attr
end
|
#attributes ⇒ Object
69
70
71
72
73
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 69
def attributes
@wrapped.attributes.to_h do |e|
[e.name.split(":").last, e.value]
end
end
|
#each ⇒ Object
96
97
98
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 96
def each
nodes.each
end
|
#insert_in_nodes(index, element) ⇒ Object
114
115
116
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 114
def insert_in_nodes(index, element)
@wrapped.children.insert(index, element.unwrap)
end
|
#locate(xpath) ⇒ Object
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 75
def locate(xpath)
@wrapped.xpath(xpath).map do |i|
case i
when ::Oga::XML::Text
i.text
when ::Oga::XML::Attribute
i.value
else
Node.new(i)
end
end
end
|
#name ⇒ Object
88
89
90
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 88
def name
@wrapped.name
end
|
#name=(new_name) ⇒ Object
92
93
94
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 92
def name=(new_name)
@wrapped.name = new_name
end
|
#nodes ⇒ Object
Ox removes text nodes that are whitespace-only. There exists a weird edge case on which Plurimath depends: <mi> <!– xxx –> π<!–GREEK SMALL LETTER PI–> </mi> If the last text node of an element that does not contain other elements is a whitespace, it preserves it. The first one can be safely removed.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 13
def nodes
children = @wrapped.children
length = children.length
preserve_last = true
children.map.with_index do |i, idx|
if preserve_last && idx == length - 1 && i.is_a?(::Oga::XML::Text)
i.text
elsif i.is_a? ::Oga::XML::Text
remove_indentation(i)
elsif i.is_a? ::Oga::XML::Comment
Node.new(i)
else
preserve_last = false
Node.new(i)
end
end.compact
end
|
#remove_attr(attr_key) ⇒ Object
118
119
120
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 118
def remove_attr(attr_key)
@wrapped.unset(attr_key)
end
|
#set_attr(attr_hash) ⇒ Object
100
101
102
103
104
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 100
def set_attr(attr_hash)
Array(attr_hash)&.each do |key, value|
@wrapped.set(key.to_s, encode_entities(value.to_s))
end
end
|
#xml_node? ⇒ Boolean
106
107
108
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 106
def xml_node?
true
end
|
#xml_nodes ⇒ Object
110
111
112
|
# File 'lib/plurimath/xml_engine/oga/node.rb', line 110
def xml_nodes
self
end
|