lib/xmpp4r/rexmladdons.rb in xmpp4r-0.3.2 vs lib/xmpp4r/rexmladdons.rb in xmpp4r-0.4

- old
+ new

@@ -8,36 +8,23 @@ # Turn $VERBOSE off to suppress warnings about redefinition oldverbose = $VERBOSE $VERBOSE = false -# REXML module. This file only adds the following methods to the REXML module, to -# ease the coding: -# * replace_element_text -# * first_element -# * first_element_text -# * typed_add -# * import -# * self.import -# * delete_elements +# REXML : Adds custom helper methods to the REXML module. # -# Further definitions are just copied from REXML out of Ruby-1.8.4 to solve issues -# with REXML in Ruby-1.8.2. -# -# The redefinitions of Text::normalize and Attribute#initialize address an issue -# where entities in element texts and attributes were not escaped. This modifies -# the behavious of REXML a bit but Sean Russell intends a similar behaviour for -# the future of REXML. module REXML + # this class adds a few helper methods to REXML::Element class Element + ## - # Replaces or add a child element of name <tt>e</tt> with text <tt>t</tt>. + # 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) if el.nil? - el = REXML::Element::new(e) + el = REXML::Element.new(e) add_element(el) end if t el.text = t end @@ -80,11 +67,13 @@ add_attributes(xmlelement.attributes.clone) @context = xmlelement.context xmlelement.each do |e| if e.kind_of? REXML::Element typed_add(e.deep_clone) - else # text element, probably. + elsif e.kind_of? REXML::Text + add_text(e.value) + else add(e.clone) end end self end @@ -97,27 +86,66 @@ # Deletes one or more children elements, # not just one like REXML::Element#delete_element def delete_elements(element) while(delete_element(element)) do end end - end -end -# very dirty fix for the :progress problem in REXML from Ruby 1.8.3 -# http://www.germane-software.com/projects/rexml/ticket/34 -# the fix proposed in REXML changeset 1145 only fixes this for pipes, not for -# TCP sockets, so we have to keep this. -module REXML + ## + # Test for equality of two elements, useful for assert_equal in + # test cases. Tries to parse String o as XML. + # + # See Test::Unit::Assertions + def ==(o) + return false unless self.kind_of? REXML::Element + if o.kind_of? REXML::Element + # Ok + elsif o.kind_of? String + # Parse o + begin + o = REXML::Document.new(o).root + rescue REXML::ParseException + return false + end + else + # Cannot compare with anything other than Elements or Strings + return false + end + + return false unless name == o.name + + attributes.each_attribute do |attr| + return false unless attr.value == o.attributes[attr.name] + end + + o.attributes.each_attribute do |attr| + return false unless attributes[attr.name] == attr.value + end + + children.each_with_index do |child,i| + return false unless child == o.children[i] + end + + return true + end + + end # class Element + + # FIXME : Is this still needed now that we're a bit past Ruby 1.8.3?? + # Very dirty fix for the :progress problem in REXML from Ruby 1.8.3 + # http://www.germane-software.com/projects/rexml/ticket/34 + # the fix proposed in REXML changeset 1145 only fixes this for pipes, not for + # TCP sockets, so we have to keep this. class IOSource def position 0 end def current_line [0, 0, ""] end - end -end + end # class IOSource + +end # module REXML # Restore the old $VERBOSE setting $VERBOSE = oldverbose