Ticket #131: rexml19.diff

File rexml19.diff, 23.5 kB (added by rubys, 4 months ago)
  • test/xpath_test.rb

     
    335335                        fail "'bar' should match nothing in this case" 
    336336                } 
    337337 
    338                 namespace = {"t","this"} 
     338                namespace = {"t"=>"this"} 
    339339                results = XPath.first( doc, "//t:bar", namespace ) 
    340340                assert_equal "this bar", results.text 
    341341        end 
     
    937937    sum = Document.new(File.new("test/yahoo.xml")).elements.to_a("//item").size 
    938938    assert_equal( 10, sum ) 
    939939 
    940     text = Document.new(File.new("test/yahoo.xml")).elements.to_a(%Q{//title[contains(text(), "'")]}).collect{|e| e.text}.to_s 
     940    text = Document.new(File.new("test/yahoo.xml")).elements.to_a(%Q{//title[contains(text(), "'")]}).collect{|e| e.text}.join 
    941941    assert_equal( "Broward labor market's a solid performer (Miami Herald)", text ) 
    942942  end 
    943943 
  • test/changing_encoding.rb

     
    11#!/usr/bin/ruby -Ku 
     2# -*- coding: utf-8 -*- 
    23 
    34require 'kconv' 
    45require 'iconv' 
  • test/contrib_test.rb

     
    9090                assert_equal "myprog-config", doc.root.name 
    9191                count = 0 
    9292                REXML::XPath.each(doc, "x:myprog-config/x:main/x:parameter",  
    93                         {"x","http://someurl/program/version"}) { |element| 
     93                        {"x"=>"http://someurl/program/version"}) { |element| 
    9494                                assert_equal "name", element.attributes["name"] 
    9595                        count += 1; 
    9696                } 
     
    227227  def test_umlaut 
    228228                koln_iso = 'Köln' 
    229229                koln_utf = 'Köln' 
     230                if koln_utf.respond_to? :force_encoding 
     231                  koln_utf.force_encoding(Encoding::UTF_8) 
     232                end 
    230233    source_iso = "<?xml version='1.0' encoding='ISO-8859-1'?><test>#{koln_iso}</test>" 
    231234    source_utf = "<?xml version='1.0' encoding='UTF-8'?><test>#{koln_utf}</test>" 
    232235                doc = REXML::Document.new(source_iso) 
     
    255258</intranet> 
    256259EOF 
    257260                tn = XPath.first(doc, "//nebenspalte/text()[2]") 
    258                 assert_equal("Nützliches von Flashern für Flasher.".unpack('C*').pack('U*'), tn.to_s.strip) 
     261                expected_iso = "Nützliches von Flashern für Flasher." 
     262                expected_utf = expected_iso.unpack('C*').pack('U*') 
     263                if expected_utf.respond_to? :encode 
     264                  expected_iso.force_encoding("iso-8859-1") 
     265                  expected_utf.force_encoding(Encoding::UTF_8) 
     266                end 
     267                assert_equal(expected_utf, tn.to_s.strip) 
    259268    f = REXML::Formatters::Default.new 
    260269    f.write( tn, Output.new(o = "", "ISO-8859-1") ) 
    261                 assert_equal("Nützliches von Flashern für Flasher.", o.strip) 
     270                assert_equal(expected_iso, o.strip) 
    262271 
    263272                doc = Document.new File.new('test/xmlfile-bug.xml') 
    264273                tn = XPath.first(doc, "//nebenspalte/text()[2]") 
    265                 assert_equal("Nützliches von Flashern für Flasher.".unpack('C*').pack('U*'), tn.to_s.strip) 
     274                assert_equal(expected_utf, tn.to_s.strip) 
    266275    f.write( tn, Output.new(o = "", "ISO-8859-1") ) 
    267                 assert_equal("Nützliches von Flashern für Flasher.", o.strip) 
     276                assert_equal(expected_iso, o.strip) 
    268277  end 
    269278 
    270279        def test_element_cloning_namespace_Chris 
  • test/jaxen.rb

     
    6565    got = XPath.match( ctx, valueOfElement.attributes["select"], namespaces, variables )[0] 
    6666    assert_true( (got.nil? && expected.nil?) || !got.nil? ) 
    6767    case got.class 
    68     when Element          : assert_equal( got.class, Element ) 
     68    when Element 
     69      assert_equal( got.class, Element ) 
    6970    when Attribute, Text, Comment, TrueClass, FalseClass 
    7071      assert_equal( expected, got.to_s ) 
    7172    when Instruction 
  • test/entity.rb

     
    141141                assert_equal("&amp;", REXML::Text.new("&amp;", false, nil, true).to_s) 
    142142                #assert_equal("&", REXML::Text.new("&amp;", false, false).to_s) 
    143143        end 
     144 
     145        def test_single_pass_unnormalization 
     146                assert_equal '&amp;&', REXML::Text::unnormalize('&#38;amp;&amp;') 
     147        end 
    144148end 
  • test/preceding-sibling.rb

     
    2525  def test_Dd_preceding_sibling_children 
    2626    arr = [] 
    2727    XPath.each(@@docDd, "//b[@x='ab02A']/preceding-sibling::b/child::*") do |cell| 
    28       arr << cell.texts.to_s 
     28      arr << cell.texts.join 
    2929    end 
    3030    assert_equal( 'Success', arr.join ) 
    3131  end 
  • test/listenertest.rb

     
    8383<f  a="é" /> 
    8484</g>' 
    8585                doc = REXML::Document.new( source ) 
    86                 assert_equal( 'é', doc.elements['/g/f'].attributes['a'] ) 
     86                a = doc.elements['/g/f'].attribute('a') 
     87                if a.value.respond_to? :force_encoding 
     88                  a.value.force_encoding('binary') 
     89                end 
     90                assert_equal( 'é', a.value) 
    8791                doc = REXML::Document.parse_stream( 
    8892                        File::new("test/stream_accents.xml"), 
    8993                        AccentListener::new 
  • test/attributes.rb

     
    187187    d.root.context[:attribute_quote] = :quote 
    188188    assert_equal( %q{<a x="1" y="2"><b z="3"/></a>}, d.to_s ) 
    189189  end 
     190 
     191  def test_ticket_127 
     192    doc = Document.new 
     193    doc.add_element 'a', { 'v' => 'x & y' } 
     194    assert doc.to_s.index(';') 
     195  end 
    190196end 
  • test/encoding.rb

     
    1717        def test_encoded_in_encoded_out 
    1818    doc = Document.new( @encoded ) 
    1919    doc.write( out="" ) 
     20    out.force_encoding('binary') if out.respond_to? :force_encoding 
    2021    assert_equal( @encoded, out ) 
    2122  end 
    2223 
     
    2829    REXML::Formatters::Default.new.write( doc.root, out="" ) 
    2930    assert_equal( @not_encoded, out ) 
    3031    char = XPath.first( doc, "/a/b/text()" ).to_s 
     32    char.force_encoding('binary') if char.respond_to? :force_encoding 
    3133    assert_equal( "ĉ", char ) 
    3234  end 
    3335 
     
    4446    doc.xml_decl.encoding = "ISO-8859-3" 
    4547    assert_equal( doc.encoding, "ISO-8859-3" ) 
    4648    doc.write( out="" ) 
     49    out.force_encoding('binary') if out.respond_to? :force_encoding 
    4750    assert_equal( @encoded, out ) 
    4851  end 
    4952 
     
    5154  def test_in_different_out 
    5255    doc = Document.new( @not_encoded ) 
    5356    doc.write( Output.new( out="", "ISO-8859-3" ) ) 
     57    out.force_encoding('binary') if out.respond_to? :force_encoding 
    5458    assert_equal( @encoded, out ) 
    5559  end 
    5660 
     
    6165    <?xml version='1.0' encoding='ISO-8859-1'?> 
    6266    <a a="ÿ">ÿ</a> 
    6367    EOL 
    64     assert_equal( doc.elements['a'].attributes['a'], "\303\277" ) 
    65     assert_equal( doc.elements['a'].text, "\303\277" ) 
     68    expect = "\303\277" 
     69    expect.force_encoding('UTF-8') if expect.respond_to? :force_encoding 
     70    assert_equal( expect, doc.elements['a'].attributes['a'] ) 
     71    assert_equal( expect, doc.elements['a'].text ) 
    6672  end 
    6773 
    6874 
  • test/core_test.rb

     
    656656    koln_utf8 = "K\xc3\xb6ln" 
    657657    source = Source.new( koln_iso_8859_1, 'iso-8859-1' ) 
    658658    results = source.scan(/.*/)[0] 
     659    koln_utf8.force_encoding('UTF-8') if koln_utf8.respond_to?(:force_encoding) 
    659660    assert_equal koln_utf8, results 
    660661    output << results 
     662    if koln_iso_8859_1.respond_to?(:force_encoding) 
     663      koln_iso_8859_1.force_encoding('ISO-8859-1') 
     664    end 
    661665    assert_equal koln_iso_8859_1, out 
    662666  end 
    663667 
  • test/sax.rb

     
    229229        Thread.stop 
    230230      end 
    231231    } 
     232    sleep 1 #to be sure that server is running 
    232233    @socket = TCPSocket.new('127.0.0.1',$port) 
    233234 
    234235    ok = false   
  • src/rexml/parsers/xpathparser.rb

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: test/xml/ticket_110_utf16.xml
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
     
    332332          predicates << expr[1..-2] if expr 
    333333        end 
    334334        #puts "PREDICATES = #{predicates.inspect}" 
    335         predicates.each{ |expr|  
    336           #puts "ORING #{expr}" 
     335        predicates.each{ |pred|  
     336          #puts "ORING #{pred}" 
    337337          preds = [] 
    338338          parsed << :predicate 
    339339          parsed << preds 
    340           OrExpr(expr, preds)  
     340          OrExpr(pred, preds)  
    341341        } 
    342342        #puts "PREDICATES = #{predicates.inspect}" 
    343343        path 
  • src/rexml/parsers/baseparser.rb

     
    242242            @document_status = :after_doctype 
    243243            @source.read if @source.buffer.size<2 
    244244            md = @source.match(/\s*/um, true) 
     245            if @source.encoding == "UTF-8" 
     246              if @source.buffer.respond_to? :force_encoding 
     247                @source.buffer.force_encoding(Encoding::UTF_8) 
     248              end 
     249            end 
    245250          end 
    246251        end 
    247252        if @document_status == :in_doctype 
  • src/rexml/parsers/sax2parser.rb

     
    149149                                                procs = get_procs( :end_prefix_mapping, event[1] ) 
    150150                                                listeners = get_listeners( :end_prefix_mapping, event[1] ) 
    151151                                                if procs or listeners 
    152                                                         namespace_mapping.each do |prefix, uri| 
     152                                                        namespace_mapping.each do |ns_prefix, ns_uri| 
    153153                                                                # notify observers of namespaces 
    154                                                                 procs.each { |ob| ob.call( prefix ) } if procs 
    155                                                                 listeners.each { |ob| ob.end_prefix_mapping(prefix) } if listeners 
     154                                                                procs.each { |ob| ob.call( ns_prefix ) } if procs 
     155                                                                listeners.each { |ob| ob.end_prefix_mapping(ns_prefix) } if listeners 
    156156                                                        end 
    157157                                                end 
    158158                                        when :text 
    159159            #normalized = @parser.normalize( event[1] ) 
    160160            #handle( :characters, normalized ) 
    161161            copy = event[1].clone 
    162             @entities.each { |key, value| copy = copy.gsub("&#{key};", value) } 
     162 
     163            esub = proc { |match|  
     164              if @entities.has_key?($1) 
     165                @entities[$1].gsub(Text::REFERENCE, &esub) 
     166              else 
     167                match 
     168              end 
     169            } 
     170 
     171            copy.gsub!( Text::REFERENCE, &esub ) 
    163172            copy.gsub!( Text::NUMERICENTITY ) {|m| 
    164173              m=$1 
    165174              m = "0#{m}" if m[0] == ?x 
  • src/rexml/parsers/treeparser.rb

     
    3030              return 
    3131            when :start_element 
    3232              tag_stack.push(event[1]) 
    33               el = @build_context = @build_context.add_element( event[1], event[2] ) 
     33              el = @build_context = @build_context.add_element( event[1] ) 
     34              event[2].each do |key, value| 
     35                el.attributes[key]=Attribute.new(key,value,self) 
     36              end 
    3437            when :end_element 
    3538              tag_stack.pop 
    3639              @build_context = @build_context.parent 
  • src/rexml/validation/validation.rb

     
    3333            sattr = [:start_attribute, nil] 
    3434            eattr = [:end_attribute] 
    3535            text = [:text, nil] 
    36             k,v = event[2].find { |k,v|  
    37               sattr[1] = k 
     36            k,v = event[2].find { |key,value|  
     37              sattr[1] = key 
    3838              #puts "Looking for #{sattr.inspect}" 
    3939              m = @current.next( sattr ) 
    4040              #puts "Got #{m.inspect}" 
     
    4747                  @current = m 
    4848                else 
    4949                  #puts "Didn't get end" 
    50                   text[1] = v 
     50                  text[1] = value 
    5151                  #puts "Looking for #{text.inspect}" 
    5252                  m = m.next( text ) 
    5353                  #puts "Got #{m.inspect}" 
  • src/rexml/element.rb

     
    296296      raise "First argument must be either an element name, or an Element object" if element.nil? 
    297297      el = @elements.add(element) 
    298298      attrs.each do |key, value| 
    299         el.attributes[key]=Attribute.new(key,value,self) 
     299        el.attributes[key]=value 
    300300      end       if attrs.kind_of? Hash 
    301301      el 
    302302    end 
     
    552552 
    553553    def attribute( name, namespace=nil ) 
    554554      prefix = nil 
    555       prefix = namespaces.index(namespace) if namespace 
     555      if namespaces.respond_to? :key 
     556        prefix = namespaces.key(namespace) if namespace 
     557      else 
     558        prefix = namespaces.index(namespace) if namespace 
     559      end 
    556560      prefix = nil if prefix == 'xmlns' 
    557561      attributes.get_attribute( "#{prefix ? prefix + ':' : ''}#{name}" ) 
    558562    end 
     
    704708    # A private helper method 
    705709    def each_with_something( test, max=0, name=nil ) 
    706710      num = 0 
    707       child=nil 
    708711      @elements.each( name ){ |child| 
    709712        yield child if test.call(child) and num += 1 
    710713        return if max>0 and num == max 
     
    754757        raise "index (#{index}) must be >= 1" if index < 1 
    755758        name = literalize(name) if name 
    756759        num = 0 
    757         child = nil 
    758760        @element.find { |child| 
    759761          child.kind_of? Element and 
    760762          (name.nil? ? true : child.has_name?( name )) and  
     
    12171219    def get_attribute_ns(namespace, name) 
    12181220      each_attribute() { |attribute| 
    12191221        if name == attribute.name && 
    1220           namespace == attribute.namespace() 
     1222          namespace == attribute.namespace() && 
     1223          ( !namespace.empty? || !attribute.fully_expanded_name.index(':') ) 
    12211224          return attribute 
    12221225        end 
    12231226      } 
  • src/rexml/source.rb

     
    5858        @to_utf = true 
    5959      else 
    6060        @to_utf = false 
     61        if @buffer.respond_to? :force_encoding 
     62          @buffer.force_encoding Encoding::UTF_8 
     63        end 
    6164      end 
    6265    end 
    6366 
     
    146149      str = @source.read( 2 ) 
    147150      if encoding 
    148151        self.encoding = encoding 
    149       elsif 0xfe == str[0] && 0xff == str[1] 
     152      elsif str[0,2] == "\xfe\xff" 
    150153        @line_break = "\000>" 
    151       elsif 0xff == str[0] && 0xfe == str[1] 
     154      elsif str[0,2] == "\xff\xfe" 
    152155        @line_break = ">\000" 
    153       elsif 0xef == str[0] && 0xbb == str[1] 
     156      elsif str[0,2] == "\xef\xbb" 
    154157        str += @source.read(1) 
    155         str = '' if (0xbf == str[2]) 
     158        str = '' if (str[2,1] == "\xBF") 
    156159        @line_break = ">" 
    157160      else 
    158161        @line_break = ">" 
     
    192195        str = @source.readline(@line_break) 
    193196        str = decode(str) if @to_utf and str  
    194197        @buffer << str 
     198        if not @to_utf and @buffer.respond_to? :force_encoding 
     199          @buffer.force_encoding Encoding::UTF_8 
     200        end 
    195201      rescue Exception, NameError 
    196202        @source = nil 
    197203      end 
  • src/rexml/doctype.rb

     
    117117      unless @children.empty? 
    118118        next_indent = indent + 1 
    119119        output << ' [' 
    120         child = nil    # speed 
    121120        @children.each { |child| 
    122121          output << "\n" 
    123122          f.write( child, output ) 
  • src/rexml/functions.rb

     
    256256        end 
    257257      } 
    258258 
    259       string(string).unpack('U*').collect { |c| 
    260         if map.has_key? c then map[c] else c end 
    261       }.compact.pack('U*') 
     259      if ''.respond_to? :chars 
     260        string(string).chars.collect { |c| 
     261          if map.has_key? c then map[c] else c end 
     262        }.compact.join 
     263      else 
     264        string(string).unpack('U*').collect { |c| 
     265          if map.has_key? c then map[c] else c end 
     266        }.compact.pack('U*') 
     267      end 
    262268    end 
    263269 
    264270    # UNTESTED 
  • src/rexml/entity.rb

     
    139139                                if @parent 
    140140                                        matches.each do |entity_reference| 
    141141                                                entity_value = @parent.entity( entity_reference[0] ) 
    142                                                 rv.gsub!( /%#{entity_reference};/um, entity_value ) 
     142                                                rv.gsub!( /%#{entity_reference.join};/um, entity_value ) 
    143143                                        end 
    144144                                end 
    145145                                return rv 
  • src/rexml/syncenumerator.rb

     
    66    # Enumerable objects. 
    77    def initialize(*enums) 
    88      @gens = enums 
    9       @biggest = @gens[0] 
    10       @gens.each {|x| @biggest = x if x.size > @biggest.size } 
     9      @length = @gens.collect {|x| x.size }.max 
    1110    end 
    1211 
    1312    # Returns the number of enumerated Enumerable objects, i.e. the size 
     
    2423 
    2524    # Enumerates rows of the Enumerable objects. 
    2625    def each 
    27       @biggest.zip( *@gens ) {|a| 
    28         yield(*a[1..-1]) 
     26      @length.times {|i| 
     27        yield @gens.collect {|x| x[i]} 
    2928      } 
    3029      self 
    3130    end 
  • src/rexml/text.rb

     
    308308 
    309309    # Unescapes all possible entities 
    310310    def Text::unnormalize( string, doctype=nil, filter=nil, illegal=nil ) 
    311       rv = string.clone 
    312       rv.gsub!( /\r\n?/, "\n" ) 
    313       matches = rv.scan( REFERENCE ) 
    314       return rv if matches.size == 0 
    315       rv.gsub!( NUMERICENTITY ) {|m| 
    316         m=$1 
    317         m = "0#{m}" if m[0] == ?x 
    318         [Integer(m)].pack('U*') 
    319       } 
    320       matches.collect!{|x|x[0]}.compact! 
    321       if matches.size > 0 
    322         if doctype 
    323           matches.each do |entity_reference| 
    324             unless filter and filter.include?(entity_reference) 
    325               entity_value = doctype.entity( entity_reference ) 
    326               re = /&#{entity_reference};/ 
    327               rv.gsub!( re, entity_value ) if entity_value 
    328             end 
     311      string.gsub( /\r\n?/, "\n" ).gsub( REFERENCE ) { |ref| 
     312        if ref[1] == ?# 
     313          if ref[2] == ?x 
     314            [ref[3...-1].to_i(16)].pack('U*') 
     315          else 
     316            [ref[2...-1].to_i].pack('U*') 
    329317          end 
     318        elsif ref == '&amp;' 
     319          '&' 
     320        elsif filter and filter.include?( ref[1...-1] ) 
     321          ref 
     322        elsif doctype 
     323          doctype.entity( ref[1...-1] ) or ref 
    330324        else 
    331           matches.each do |entity_reference| 
    332             unless filter and filter.include?(entity_reference) 
    333               entity_value = DocType::DEFAULT_ENTITIES[ entity_reference ] 
    334               re = /&#{entity_reference};/ 
    335               rv.gsub!( re, entity_value.value ) if entity_value 
    336             end 
    337           end 
     325          entity_value = DocType::DEFAULT_ENTITIES[ ref[1...-1] ] 
     326          entity_value ? entity_value.value : ref 
    338327        end 
    339         rv.gsub!( /&amp;/, '&' ) 
    340       end 
    341       rv 
     328      } 
    342329    end 
    343330  end 
    344331end 
  • src/rexml/formatters/pretty.rb

     
    3131        @level = 0 
    3232        @ie_hack = ie_hack 
    3333        @width = 80 
     34        @compact = false 
    3435      end 
    3536 
    3637      protected 
  • src/rexml/encoding.rb

     
    5656 
    5757    def check_encoding str 
    5858      # We have to recognize UTF-16, LSB UTF-16, and UTF-8 
    59       if str[0] == 0xfe && str[1] == 0xff 
     59      if str[0,2] == "\xfe\xff" 
    6060        str[0,2] = "" 
    6161        return UTF_16 
    62       elsif str[0] == 0xff && str[1] == 0xfe 
     62      elsif str[0,2] == "\xff\xfe" 
    6363        str[0,2] = "" 
    6464        return UNILE 
    6565      end 
    66       str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/um 
     66      str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/m 
    6767      return $3.upcase if $3 
    6868      return UTF_8 
    6969    end 
  • src/rexml/xpath_parser.rb

     
    222222        when :child 
    223223          new_nodeset = [] 
    224224          nt = nil 
    225           for node in nodeset 
     225          nodeset.each do |node| 
    226226            nt = node.node_type 
    227227            new_nodeset += node.children if nt == :element or nt == :document 
    228228          end 
     
    266266 
    267267        when :ancestor 
    268268          new_nodeset = [] 
    269           for node in nodeset 
     269          nodeset.each do |node| 
    270270            while node.parent 
    271271              node = node.parent 
    272272              new_nodeset << node unless new_nodeset.include? node 
     
    277277 
    278278        when :ancestor_or_self 
    279279          new_nodeset = [] 
    280           for node in nodeset 
     280          nodeset.each do |node| 
    281281            if node.node_type == :element 
    282282              new_nodeset << node 
    283283              while ( node.parent ) 
     
    341341        when :descendant 
    342342          results = [] 
    343343          nt = nil 
    344           for node in nodeset 
     344          nodeset.each do |node| 
    345345            nt = node.node_type 
    346346            results += expr( path_stack.dclone.unshift( :descendant_or_self ), 
    347347              node.children ) if nt == :element or nt == :document 
     
    376376 
    377377        when :preceding 
    378378          new_nodeset = [] 
    379           for node in nodeset 
     379          nodeset.each do |node| 
    380380            new_nodeset += preceding( node ) 
    381381          end 
    382382          #puts "NEW NODESET => #{new_nodeset.inspect}" 
     
    385385 
    386386        when :following 
    387387          new_nodeset = [] 
    388           for node in nodeset 
     388          nodeset.each do |node| 
    389389            new_nodeset += following( node ) 
    390390          end 
    391391          nodeset = new_nodeset 
     
    395395          #puts "In :namespace" 
    396396          new_nodeset = [] 
    397397          prefix = path_stack.shift 
    398           for node in nodeset 
     398          nodeset.each do |node| 
    399399            if (node.node_type == :element or node.node_type == :attribute) 
    400400              if @namespaces 
    401401                namespaces = @namespaces