lib/xmpp4r/rexmladdons.rb in xmpp4r-0.5 vs lib/xmpp4r/rexmladdons.rb in xmpp4r-0.5.5
- old
+ new
@@ -23,34 +23,74 @@
end
end
##
# Replaces or adds a child element of name <tt>e</tt> with text <tt>t</tt>.
- def replace_element_text(e, t)
- el = first_element(e)
+ def replace_element_text(e, t, namespace = nil)
+ el = first_element(e, namespace)
if el.nil?
el = REXML::Element.new(e)
+ el.add_namespace(namespace)
add_element(el)
end
if t
el.text = t
end
self
end
+
+ ##
+ # Replaces or adds a child element of name <tt>e</tt> with content of <tt>t</tt>.
+ def replace_element_content(e, c, namespace = nil)
+ el = first_element(e, namespace)
+ if el.nil?
+ el = REXML::Element.new(e)
+ el.add_namespace(namespace)
+ add_element(el)
+ end
+ if c
+ el.children.each do |ch|
+ ch.remove
+ end
+ c.root.children.each do |ch|
+ el.add ch
+ end
+ end
+ self
+ end
##
# Returns first element of name <tt>e</tt>
- def first_element(e)
- each_element(e) { |el| return el }
+ def first_element(e, namespace = nil)
+ if namespace
+ each_element_with_attribute("xmlns", namespace, 1, e) { |el| return el }
+ else
+ each_element(e) { |el| return el }
+ end
return nil
end
##
# Returns text of first element of name <tt>e</tt>
- def first_element_text(e)
- el = first_element(e)
+ def first_element_text(e, namespace = nil)
+ el = first_element(e, namespace)
if el
return el.text
+ else
+ return nil
+ end
+ end
+
+ ##
+ # This method works like <tt>first_element_text</tt> except that it
+ # returns content of all children, not just the value of the first
+ # child text element.
+ #
+ # Returns content of first element of name <tt>e</tt>
+ def first_element_content(e, namespace = nil)
+ el = first_element(e, namespace)
+ if el
+ return el.children.join
else
return nil
end
end