Module: Ayril::XMLElement::ElementManipulation

Included in:
Ayril::XMLElement
Defined in:
lib/ayril/xml_element/element_manipulation.rb

Instance Method Summary collapse

Instance Method Details

#append(e) ⇒ Object



58
# File 'lib/ayril/xml_element/element_manipulation.rb', line 58

def append(e) self.insert :bottom => e end

#identifyObject



74
75
76
77
78
79
80
81
82
# File 'lib/ayril/xml_element/element_manipulation.rb', line 74

def identify
  id = self.read_attribute "id"
  return id unless id.nil?
  begin
    id = "anonymous_element_#{XMLElement::id_counter += 1}"
  end while self.rootDocument.select("##{id}").any?
  self.write_attribute "id", id
  id
end

#insert(insertions) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ayril/xml_element/element_manipulation.rb', line 24

def insert(insertions)
  insertions = { :bottom => insertions } if insertions.kind_of? String or
    insertions.kind_of? Integer or insertions.kind_of? XMLElement or 
    (insertions.respond_to? :to_element or insertions.respond_to? :to_html)

  insertions.each do |position, content|
    position.downcase! if position.kind_of? String
    insert = {
      :before => lambda { |element, node|
        element.parent.insertChild node, atIndex: element.index
      },
      :top => lambda { |element, node|
        element.insertChild node, atIndex: 0
      },
      :bottom => lambda { |element, node|
        element.addChild node
      },
      :after => lambda { |element, node|
        element.parent.insertChild node, atIndex: element.index + 1
      }
    }[position = position.to_sym]

    content = content.to_element if content.respond_to? :to_element
    (insert.call(self, content); next) if content.kind_of? XMLElement

    content = content.respond_to?(:to_html) ? content.to_html : content.to_s
    children = XMLElement.alloc.initWithXMLString("<root>#{content}</root>", error: nil).children

    children.reverse! if position == :top or position == :after
    children.each { |child| child.detach; insert.call self, child }
  end
  self
end

#prepend(e) ⇒ Object



59
# File 'lib/ayril/xml_element/element_manipulation.rb', line 59

def prepend(e) self.insert :top => e end

#wrap(wrapper = nil, attributes = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ayril/xml_element/element_manipulation.rb', line 61

def wrap(wrapper=nil, attributes={})
  if wrapper.kind_of? XMLElement
    wrapper.write_attribute attributes
  elsif wrapper.kind_of? String
    wrapper = XMLElement.new wrapper, attributes
  else
    wrapper = XMLElement.new 'div', attributes
  end
  self.replace wrapper if self.parent
  wrapper.addChild self
  wrapper
end