Class: REXML::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/xmpp4r/rexmladdons.rb

Class Method Summary collapse

Class Method Details

.normalize(input, doctype = nil, entity_filter = nil) ⇒ Object

Escapes all possible entities



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/xmpp4r/rexmladdons.rb', line 841

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