Module: Ayril::XMLNode::NodeManipulation

Included in:
Ayril::XMLDTD, Ayril::XMLDTDNode, Ayril::XMLDocument, Ayril::XMLElement, Ayril::XMLNode
Defined in:
lib/ayril/xml_node/node_manipulation.rb

Instance Method Summary collapse

Instance Method Details

#clean_whitespaceObject

Clean the whitespace of the node, i.e. remove all of its children text nodes that contain only whitespace.



54
55
56
57
58
59
60
61
62
# File 'lib/ayril/xml_node/node_manipulation.rb', line 54

def clean_whitespace
  node = self.childAtIndex 0
  while node
    next_node = node.nextSibling
    node.remove if node.kind == NSXMLTextKind and node.stringValue =~ /\S/
    node = next_node
  end
  self
end

#inner_htmlObject



70
# File 'lib/ayril/xml_node/node_manipulation.rb', line 70

def inner_html; self.children.invoke(:XMLString).join end

#removeObject

Removes node from parent and return the removed node itself.



25
26
27
# File 'lib/ayril/xml_node/node_manipulation.rb', line 25

def remove
  self.tap { |e| e.parent.removeChildAtIndex e.index }
end

#replace(content) ⇒ Object Also known as: swap

Replaces a node with another node and returns the old node.



45
46
47
48
49
# File 'lib/ayril/xml_node/node_manipulation.rb', line 45

def replace(content)
  content = content.to_elem if content.respond_to? :to_elem
  self.parent.replaceChildAtIndex self.index, withNode: content
  self
end

#update(content = '') ⇒ Object

Updates the content of node. If passed nothing or a blank string, the children are cleared. If passed a string, the string is parsed into element(s). Passing an element or an array of elements is good. All elements are first detached if possible.



33
34
35
36
37
38
39
40
41
42
# File 'lib/ayril/xml_node/node_manipulation.rb', line 33

def update(content='')
  if content == ''; children = nil
  elsif content.respond_to? :to_s
    children = XMLElement.alloc.initWithXMLString("<r>#{content}</r>", 
      error: nil).children
  elsif content.kind_of? XMLElement; children = [content]
  end
  children.each { |child| child.detach } if not children.nil?
  self.setChildren children
end