Class: Plurimath::XmlEngine::Oga::Node

Inherits:
Wrapper
  • Object
show all
Defined in:
lib/plurimath/xml_engine/oga/node.rb

Direct Known Subclasses

Document

Instance Method Summary collapse

Methods inherited from Wrapper

#==, #initialize, #unwrap

Constructor Details

This class inherits a constructor from Plurimath::XmlEngine::Oga::Wrapper

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
    # Here we tap into the internal representation due to some likely
    # bug in Oga
    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)
  # Here we tap into the internal representation due to some likely
  # bug in Oga
  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

#attributesObject



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

#eachObject



96
97
98
# File 'lib/plurimath/xml_engine/oga/node.rb', line 96

def each
  nodes.each
end

#insert_in_nodes(index, element) ⇒ Object



112
113
114
# File 'lib/plurimath/xml_engine/oga/node.rb', line 112

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

#nameObject



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

#nodesObject

Ox removes text nodes that are whitespace-only. There exists a weird edge case on which Plurimath depends: <mi> <!– xxx –> &#x3C0;<!–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



116
117
118
# File 'lib/plurimath/xml_engine/oga/node.rb', line 116

def remove_attr(attr_key)
  @wrapped.unset(attr_key)
end

#set_attr(attr_hash) ⇒ Object



100
101
102
# File 'lib/plurimath/xml_engine/oga/node.rb', line 100

def set_attr(attr_hash)
  Array(attr_hash)&.each { |key, value| @wrapped.set(key.to_s, encode_entities((value.to_s))) }
end

#xml_node?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/plurimath/xml_engine/oga/node.rb', line 104

def xml_node?
  true
end

#xml_nodesObject



108
109
110
# File 'lib/plurimath/xml_engine/oga/node.rb', line 108

def xml_nodes
  self
end