lib/xmpp4r/rexmladdons.rb in xmpp4r-0.3 vs lib/xmpp4r/rexmladdons.rb in xmpp4r-0.3.1
- old
+ new
@@ -8,12 +8,27 @@
# Turn $VERBOSE off to suppress warnings about redefinition
oldverbose = $VERBOSE
$VERBOSE = false
-# REXML module. This file only adds a few methods to the REXML module, to
-# ease the coding.
+# 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
+#
+# 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>.
@@ -114,11 +129,12 @@
@parser = REXML::Parsers::XPathParser.new
@namespaces = {}
@variables = {}
end
- def namespaces=( namespaces={} )
+ def namespaces=( namespaces )
+ namespaces ||= {}
Functions::namespace_context = namespaces
@namespaces = namespaces
end
def variables=( vars={} )
@@ -817,9 +833,55 @@
end
end
############################################################################
+
+ class Text
+ # Escapes all possible entities
+ def Text::normalize( input, doctype=nil, entity_filter=nil )
+ copy = input
+ # Doing it like this rather than in a loop improves the speed
+ if doctype
+ # Replace all ampersands that aren't part of an entity
+ copy = copy.gsub( EREFERENCE, '&' )
+ doctype.entities.each_value do |entity|
+ copy = copy.gsub( entity.value,
+ "&#{entity.name};" ) if entity.value and
+ not( entity_filter and entity_filter.include?(entity) )
+ end
+ else
+ # Replace all ampersands
+ copy = copy.gsub( '&', '&' )
+ DocType::DEFAULT_ENTITIES.each_value do |entity|
+ copy = copy.gsub(entity.value, "&#{entity.name};" )
+ end
+ end
+ copy
+ end
+ end
+
+ class Attribute
+ def initialize( first, second=nil, parent=nil )
+ @normalized = @unnormalized = @element = nil
+ if first.kind_of? Attribute
+ self.name = first.expanded_name
+ @value = first.value
+ if second.kind_of? Element
+ @element = second
+ else
+ @element = first.element
+ end
+ elsif first.kind_of? String
+ @element = parent if parent.kind_of? Element
+ self.name = first
+ @value = Text::normalize(second.to_s)
+ else
+ raise "illegal argument #{first.class.name} to Attribute constructor"
+ end
+ end
+ end
+
end
# Restore the old $VERBOSE setting
$VERBOSE = oldverbose