From 834e54f226c06d8adb684dbfc24733d6caf7dd59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Nie=C5=82acny?= Date: Sun, 27 May 2012 12:40:27 +0200 Subject: [PATCH 1/3] Update REXML (from ruby MRI) --- lib/18/rexml/element.rb | 2 +- lib/18/rexml/encodings/SHIFT-JIS.rb | 4 +- lib/18/rexml/rexml.rb | 4 +- lib/18/rexml/text.rb | 2 +- lib/19/rexml/attribute.rb | 2 +- lib/19/rexml/cdata.rb | 2 +- lib/19/rexml/child.rb | 4 +- lib/19/rexml/comment.rb | 12 +-- lib/19/rexml/doctype.rb | 11 ++- lib/19/rexml/document.rb | 7 +- lib/19/rexml/element.rb | 37 ++++---- lib/19/rexml/encoding.rb | 107 +++++++++++------------ lib/19/rexml/formatters/default.rb | 2 +- lib/19/rexml/formatters/pretty.rb | 14 +-- lib/19/rexml/formatters/transitive.rb | 1 - lib/19/rexml/functions.rb | 8 +- lib/19/rexml/light/node.rb | 13 ++- lib/19/rexml/node.rb | 12 +-- lib/19/rexml/output.rb | 2 +- lib/19/rexml/parseexception.rb | 2 +- lib/19/rexml/parsers/baseparser.rb | 29 +++--- lib/19/rexml/parsers/lightparser.rb | 8 +- lib/19/rexml/parsers/sax2parser.rb | 10 +-- lib/19/rexml/parsers/ultralightparser.rb | 8 +- lib/19/rexml/parsers/xpathparser.rb | 5 +- lib/19/rexml/quickpath.rb | 31 ++++--- lib/19/rexml/rexml.rb | 2 +- lib/19/rexml/source.rb | 6 +- lib/19/rexml/text.rb | 2 +- lib/19/rexml/validation/validation.rb | 2 +- lib/19/rexml/xpath.rb | 13 +-- lib/19/rexml/xpath_parser.rb | 15 +++- 32 files changed, 190 insertions(+), 189 deletions(-) diff --git a/lib/18/rexml/element.rb b/lib/18/rexml/element.rb index 3db87c6126..1f443956c8 100644 --- a/lib/18/rexml/element.rb +++ b/lib/18/rexml/element.rb @@ -492,7 +492,7 @@ def get_text path = nil def text=( text ) if text.kind_of? String text = Text.new( text, whitespace(), nil, raw() ) - elsif text and !text.kind_of? Text + elsif !text.nil? and !text.kind_of? Text text = Text.new( text.to_s, whitespace(), nil, raw() ) end old_text = get_text diff --git a/lib/18/rexml/encodings/SHIFT-JIS.rb b/lib/18/rexml/encodings/SHIFT-JIS.rb index 93c7877afd..9e0f4af20e 100644 --- a/lib/18/rexml/encodings/SHIFT-JIS.rb +++ b/lib/18/rexml/encodings/SHIFT-JIS.rb @@ -13,8 +13,8 @@ def encode_sjis(str) rescue LoadError require 'nkf' - SJISTOU8 = '-Swm0' - U8TOSJIS = '-Wsm0' + SJISTOU8 = '-Swm0x' + U8TOSJIS = '-Wsm0x' def decode_sjis(str) NKF.nkf(SJISTOU8, str) diff --git a/lib/18/rexml/rexml.rb b/lib/18/rexml/rexml.rb index 698e92b5d1..95bc2a7f6d 100644 --- a/lib/18/rexml/rexml.rb +++ b/lib/18/rexml/rexml.rb @@ -13,7 +13,7 @@ # Author:: Sean Russell # Version:: 3.1.7.2 # Date:: 2007/275 -# Revision:: $Revision: 22842 $ +# Revision:: $Revision$ # # This API documentation can be downloaded from the REXML home page, or can # be accessed online[http://www.germane-software.com/software/rexml_doc] @@ -25,7 +25,7 @@ module REXML COPYRIGHT = "Copyright \xC2\xA9 2001-2006 Sean Russell " VERSION = "3.1.7.3" DATE = "2007/275" - REVISION = "$Revision: 22842 $".gsub(/\$Revision:|\$/,'').strip + REVISION = "$Revision$".gsub(/\$Revision:|\$/,'').strip Copyright = COPYRIGHT Version = VERSION diff --git a/lib/18/rexml/text.rb b/lib/18/rexml/text.rb index 2bc00429b3..a4a30b6d54 100644 --- a/lib/18/rexml/text.rb +++ b/lib/18/rexml/text.rb @@ -286,7 +286,7 @@ def Text::read_with_substitution( input, illegal=nil ) EREFERENCE = /&(?!#{Entity::NAME};)/ # Escapes all possible entities def Text::normalize( input, doctype=nil, entity_filter=nil ) - copy = input + copy = input.to_s # Doing it like this rather than in a loop improves the speed #copy = copy.gsub( EREFERENCE, '&' ) copy = copy.gsub( "&", "&" ) diff --git a/lib/19/rexml/attribute.rb b/lib/19/rexml/attribute.rb index 28a5923608..e99927943f 100644 --- a/lib/19/rexml/attribute.rb +++ b/lib/19/rexml/attribute.rb @@ -115,7 +115,7 @@ def to_string def doctype if @element doc = @element.document - doctype = doc.doctype if doc + doc.doctype if doc end end diff --git a/lib/19/rexml/cdata.rb b/lib/19/rexml/cdata.rb index e1235d60f8..73358edc28 100644 --- a/lib/19/rexml/cdata.rb +++ b/lib/19/rexml/cdata.rb @@ -6,7 +6,7 @@ class CData < Text STOP = ']]>' ILLEGAL = /(\]\]>)/ - # Constructor. CData is data between + # Constructor. CData is data between # # _Examples_ # CData.new( source ) diff --git a/lib/19/rexml/child.rb b/lib/19/rexml/child.rb index fd59d7283a..bf97d5f903 100644 --- a/lib/19/rexml/child.rb +++ b/lib/19/rexml/child.rb @@ -7,7 +7,7 @@ module REXML # class directly. class Child include Node - attr_reader :parent # The Parent of this object + attr_reader :parent # The Parent of this object # Constructor. Any inheritors of this class should call super to make # sure this method is called. @@ -88,7 +88,7 @@ def document # This doesn't yet handle encodings def bytes - encoding = document.encoding + document.encoding to_s end diff --git a/lib/19/rexml/comment.rb b/lib/19/rexml/comment.rb index e401090376..42a040c456 100644 --- a/lib/19/rexml/comment.rb +++ b/lib/19/rexml/comment.rb @@ -38,15 +38,15 @@ def clone # See REXML::Formatters # # output:: - # Where to write the string + # Where to write the string # indent:: - # An integer. If -1, no indenting will be used; otherwise, the - # indentation will be this number of spaces, and children will be - # indented an additional amount. + # An integer. If -1, no indenting will be used; otherwise, the + # indentation will be this number of spaces, and children will be + # indented an additional amount. # transitive:: - # Ignored by this class. The contents of comments are never modified. + # Ignored by this class. The contents of comments are never modified. # ie_hack:: - # Needed for conformity to the child API, but not used by this class. + # Needed for conformity to the child API, but not used by this class. def write( output, indent=-1, transitive=false, ie_hack=false ) Kernel.warn("Comment.write is deprecated. See REXML::Formatters") indent( output, indent ) diff --git a/lib/19/rexml/doctype.rb b/lib/19/rexml/doctype.rb index 1a946a1587..0b3c533bb4 100644 --- a/lib/19/rexml/doctype.rb +++ b/lib/19/rexml/doctype.rb @@ -115,7 +115,6 @@ def write( output, indent=0, transitive=false, ie_hack=false ) output << " #{@long_name.inspect}" if @long_name output << " #{@uri.inspect}" if @uri unless @children.empty? - next_indent = indent + 1 output << ' [' @children.each { |child| output << "\n" @@ -249,11 +248,11 @@ def initialize name, middle, pub, sys end def to_s - "" + notation = "" + notation end def write( output, indent=-1 ) diff --git a/lib/19/rexml/document.rb b/lib/19/rexml/document.rb index 0337553a2e..96db53bdb0 100644 --- a/lib/19/rexml/document.rb +++ b/lib/19/rexml/document.rb @@ -131,7 +131,8 @@ def version xml_decl().version end - # @return the XMLDecl encoding of this document as a String. + # @return the XMLDecl encoding of this document as an + # Encoding object. # If no XMLDecl has been set, returns the default encoding. def encoding xml_decl().encoding @@ -164,7 +165,7 @@ def stand_alone? # Document.new("").serialize( tr ) # # output:: - # output an object which supports '<< string'; this is where the + # output an object which supports '<< string'; this is where the # document will be written. # indent:: # An integer. If -1, no indenting will be used; otherwise, the @@ -183,7 +184,7 @@ def stand_alone? # that IE's limited abilities can handle. This hack inserts a space # before the /> on empty tags. Defaults to false def write( output=$stdout, indent=-1, transitive=false, ie_hack=false ) - if xml_decl.encoding != "UTF-8" && !output.kind_of?(Output) + if xml_decl.encoding != 'UTF-8' && !output.kind_of?(Output) output = Output.new( output, xml_decl.encoding ) end formatter = if indent > -1 diff --git a/lib/19/rexml/element.rb b/lib/19/rexml/element.rb index f999b0b2b1..5991859a78 100644 --- a/lib/19/rexml/element.rb +++ b/lib/19/rexml/element.rb @@ -20,7 +20,7 @@ module REXML class Element < Parent include Namespace - UNDEFINED = "UNDEFINED"; # The default name + UNDEFINED = "UNDEFINED"; # The default name # Mechanisms for accessing attributes and child elements of this # element. @@ -31,17 +31,17 @@ class Element < Parent # Constructor # arg:: - # if not supplied, will be set to the default value. - # If a String, the name of this object will be set to the argument. - # If an Element, the object will be shallowly cloned; name, - # attributes, and namespaces will be copied. Children will +not+ be - # copied. + # if not supplied, will be set to the default value. + # If a String, the name of this object will be set to the argument. + # If an Element, the object will be shallowly cloned; name, + # attributes, and namespaces will be copied. Children will +not+ be + # copied. # parent:: - # if supplied, must be a Parent, and will be used as - # the parent of this object. + # if supplied, must be a Parent, and will be used as + # the parent of this object. # context:: - # If supplied, must be a hash containing context items. Context items - # include: + # If supplied, must be a hash containing context items. Context items + # include: # * :respect_whitespace the value of this is :+all+ or an array of # strings being the names of the elements to respect # whitespace for. Defaults to :+all+. @@ -297,7 +297,7 @@ def add_element element, attrs=nil el = @elements.add(element) attrs.each do |key, value| el.attributes[key]=value - end if attrs.kind_of? Hash + end if attrs.kind_of? Hash el end @@ -667,7 +667,7 @@ def texts # # Writes out this element, and recursively, all children. # output:: - # output an object which supports '<< string'; this is where the + # output an object which supports '<< string'; this is where the # document will be written. # indent:: # An integer. If -1, no indenting will be used; otherwise, the @@ -778,7 +778,7 @@ def []( index, name=nil) else return XPath::first( @element, index ) #{ |element| - # return element if element.kind_of? Element + # return element if element.kind_of? Element #} #return nil end @@ -874,7 +874,6 @@ def delete_all( xpath ) # a.elements.add(Element.new('b')) #-> # a.elements.add('c') #-> def add element=nil - rv = nil if element.nil? Element.new("", self, @element.context) elsif not element.kind_of?(Element) @@ -1087,12 +1086,12 @@ def get_attribute( name ) # doc.root.attributes['foo'] = '4' # doc.root.attributes['x:foo'] = nil def []=( name, value ) - if value.nil? # Delete the named attribute + if value.nil? # Delete the named attribute attr = get_attribute(name) delete attr return end - element_document = @element.document + unless value.kind_of? Attribute if @element.document and @element.document.doctype value = Text::normalize( value, @element.document.doctype ) @@ -1117,8 +1116,8 @@ def []=( name, value ) value.prefix != "xmlns" and old_attr.prefix != "xmlns" and @element.namespace( old_attr.prefix ) == @element.namespace( value.prefix ) - store value.name, { old_attr.prefix => old_attr, - value.prefix => value } + store value.name, { old_attr.prefix => old_attr, + value.prefix => value } else store value.name, value end @@ -1197,7 +1196,7 @@ def delete( attribute ) return @element else # the supplied attribute is a top-level one attr = old - res = super(name) + super(name) end @element end diff --git a/lib/19/rexml/encoding.rb b/lib/19/rexml/encoding.rb index 3feffb80f4..d1d5172841 100644 --- a/lib/19/rexml/encoding.rb +++ b/lib/19/rexml/encoding.rb @@ -1,71 +1,62 @@ -# -*- mode: ruby; ruby-indent-level: 2; indent-tabs-mode: t; tab-width: 2 -*- vim: sw=2 ts=2 module REXML module Encoding - @encoding_methods = {} - def self.register(enc, &block) - @encoding_methods[enc] = block - end - def self.apply(obj, enc) - @encoding_methods[enc][obj] - end - def self.encoding_method(enc) - @encoding_methods[enc] - end - - # Native, default format is UTF-8, so it is declared here rather than in - # an encodings/ definition. - UTF_8 = 'UTF-8' - UTF_16 = 'UTF-16' - UNILE = 'UNILE' - # ID ---> Encoding name attr_reader :encoding - def encoding=( enc ) - old_verbosity = $VERBOSE - begin - $VERBOSE = false - enc = enc.nil? ? nil : enc.upcase - return false if defined? @encoding and enc == @encoding - if enc and enc != UTF_8 - @encoding = enc - raise ArgumentError, "Bad encoding name #@encoding" unless @encoding =~ /^[\w-]+$/ - @encoding.untaint - begin - require 'rexml/encodings/ICONV.rb' - Encoding.apply(self, "ICONV") - rescue LoadError, Exception - begin - enc_file = File.join( "rexml", "encodings", "#@encoding.rb" ) - require enc_file - Encoding.apply(self, @encoding) - rescue LoadError => err - puts err.message - raise ArgumentError, "No decoder found for encoding #@encoding. Please install iconv." - end - end - else - @encoding = UTF_8 - require 'rexml/encodings/UTF-8.rb' - Encoding.apply(self, @encoding) + def encoding=(encoding) + encoding = encoding.name if encoding.is_a?(Encoding) + if encoding.is_a?(String) + original_encoding = encoding + encoding = find_encoding(encoding) + unless encoding + raise ArgumentError, "Bad encoding name #{original_encoding}" end - ensure - $VERBOSE = old_verbosity + end + return false if defined?(@encoding) and encoding == @encoding + if encoding + @encoding = encoding.upcase + else + @encoding = 'UTF-8' end true end - def check_encoding str - # We have to recognize UTF-16, LSB UTF-16, and UTF-8 - if str[0,2] == "\xfe\xff" - str[0,2] = "" - return UTF_16 - elsif str[0,2] == "\xff\xfe" - str[0,2] = "" - return UNILE + def check_encoding(xml) + # We have to recognize UTF-16BE, UTF-16LE, and UTF-8 + if xml[0, 2] == "\xfe\xff" + xml[0, 2] = "" + return 'UTF-16BE' + elsif xml[0, 2] == "\xff\xfe" + xml[0, 2] = "" + return 'UTF-16LE' + end + xml =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/m + return $3 ? $3.upcase : 'UTF-8' + end + + def encode(string) + string.encode(@encoding) + end + + def decode(string) + string.encode(::Encoding::UTF_8, @encoding) + end + + private + def find_encoding(name) + case name + when /\Ashift-jis\z/i + return "SHIFT_JIS" + when /\ACP-(\d+)\z/ + name = "CP#{$1}" + when /\AUTF-8\z/i + return name + end + begin + ::Encoding::Converter.search_convpath(name, 'UTF-8') + rescue ::Encoding::ConverterNotFoundError + return nil end - str =~ /^\s*<\?xml\s+version\s*=\s*(['"]).*?\1\s+encoding\s*=\s*(["'])(.*?)\2/m - return $3.upcase if $3 - return UTF_8 + name end end end diff --git a/lib/19/rexml/formatters/default.rb b/lib/19/rexml/formatters/default.rb index db44453e1e..574c821f96 100644 --- a/lib/19/rexml/formatters/default.rb +++ b/lib/19/rexml/formatters/default.rb @@ -22,7 +22,7 @@ def write( node, output ) case node when Document - if node.xml_decl.encoding != "UTF-8" && !output.kind_of?(Output) + if node.xml_decl.encoding != 'UTF-8' && !output.kind_of?(Output) output = Output.new( output, node.xml_decl.encoding ) end write_document( node, output ) diff --git a/lib/19/rexml/formatters/pretty.rb b/lib/19/rexml/formatters/pretty.rb index 17d217d1dc..63f726e8c3 100644 --- a/lib/19/rexml/formatters/pretty.rb +++ b/lib/19/rexml/formatters/pretty.rb @@ -88,7 +88,7 @@ def write_text( node, output ) s = node.to_s() s.gsub!(/\s/,' ') s.squeeze!(" ") - s = wrap(s, 80-@level) + s = wrap(s, @width - @level) s = indent_text(s, @level, " ", true) output << (' '*@level + s) end @@ -126,11 +126,13 @@ def indent_text(string, level=1, style="\t", indentfirstline=true) end def wrap(string, width) - # Recursively wrap string at width. - return string if string.length <= width - place = string.rindex(' ', width) # Position in string with last ' ' before cutoff - return string if place.nil? - return string[0,place] + "\n" + wrap(string[place+1..-1], width) + parts = [] + while string.length > width and place = string.rindex(' ', width) + parts << string[0...place] + string = string[place+1..-1] + end + parts << string + parts.join("\n") end end diff --git a/lib/19/rexml/formatters/transitive.rb b/lib/19/rexml/formatters/transitive.rb index 3a52e03f01..6cc690d922 100644 --- a/lib/19/rexml/formatters/transitive.rb +++ b/lib/19/rexml/formatters/transitive.rb @@ -37,7 +37,6 @@ def write_element( node, output ) # If compact and all children are text, and if the formatted output # is less than the specified width, then try to print everything on # one line - skip = false @level += @indentation node.children.each { |child| write( child, output ) diff --git a/lib/19/rexml/functions.rb b/lib/19/rexml/functions.rb index 2d30e5fe92..20c8961aee 100644 --- a/lib/19/rexml/functions.rb +++ b/lib/19/rexml/functions.rb @@ -28,6 +28,7 @@ def Functions::text( ) end end + # Returns the last node of the given list of nodes. def Functions::last( ) @@context[:size] end @@ -36,6 +37,7 @@ def Functions::position( ) @@context[:index] end + # Returns the size of the given list of nodes. def Functions::count( node_set ) node_set.size end @@ -129,6 +131,11 @@ def Functions::string( object=nil ) end end + # A node-set is converted to a string by + # returning the concatenation of the string-value + # of each of the children of the node in the + # node-set that is first in document order. + # If the node-set is empty, an empty string is returned. def Functions::string_value( o ) rv = "" o.children.each { |e| @@ -170,7 +177,6 @@ def Functions::substring_before( string, test ) # Kouhei fixed this too def Functions::substring_after( string, test ) ruby_string = string(string) - test_string = string(test) return $1 if ruby_string =~ /#{test}(.*)/ "" end diff --git a/lib/19/rexml/light/node.rb b/lib/19/rexml/light/node.rb index 9c90148c05..0a896c83dc 100644 --- a/lib/19/rexml/light/node.rb +++ b/lib/19/rexml/light/node.rb @@ -1,14 +1,13 @@ require 'rexml/xmltokens' -require 'rexml/light/node' # [ :element, parent, name, attributes, children* ] # a = Node.new - # a << "B" # => B - # a.b # => B - # a.b[1] # => B - # a.b[1]["x"] = "y" # => B - # a.b[0].c # => B - # a.b.c << "D" # => BD + # a << "B" # => B + # a.b # => B + # a.b[1] # => B + # a.b[1]["x"] = "y" # => B + # a.b[0].c # => B + # a.b.c << "D" # => BD module REXML module Light # Represents a tagged XML element. Elements are characterized by diff --git a/lib/19/rexml/node.rb b/lib/19/rexml/node.rb index 85457f1ad0..cab6e9fddb 100644 --- a/lib/19/rexml/node.rb +++ b/lib/19/rexml/node.rb @@ -36,12 +36,12 @@ def to_s indent=nil end def indent to, ind - if @parent and @parent.context and not @parent.context[:indentstyle].nil? then - indentstyle = @parent.context[:indentstyle] - else - indentstyle = ' ' - end - to << indentstyle*ind unless ind<1 + if @parent and @parent.context and not @parent.context[:indentstyle].nil? then + indentstyle = @parent.context[:indentstyle] + else + indentstyle = ' ' + end + to << indentstyle*ind unless ind<1 end def parent? diff --git a/lib/19/rexml/output.rb b/lib/19/rexml/output.rb index b7f17b9dff..50333ba177 100644 --- a/lib/19/rexml/output.rb +++ b/lib/19/rexml/output.rb @@ -10,7 +10,7 @@ def initialize real_IO, encd="iso-8859-1" @output = real_IO self.encoding = encd - @to_utf = encd == UTF_8 ? false : true + @to_utf = encd != 'UTF-8' end def <<( content ) diff --git a/lib/19/rexml/parseexception.rb b/lib/19/rexml/parseexception.rb index 0481f72818..0c4d55abda 100644 --- a/lib/19/rexml/parseexception.rb +++ b/lib/19/rexml/parseexception.rb @@ -28,7 +28,7 @@ def to_s err << "\nLine: #{line}\n" err << "Position: #{position}\n" err << "Last 80 unconsumed characters:\n" - err << @source.buffer[0..80].gsub(/\n/, ' ') + err << @source.buffer[0..80].force_encoding("ASCII-8BIT").gsub(/\n/, ' ') end err diff --git a/lib/19/rexml/parsers/baseparser.rb b/lib/19/rexml/parsers/baseparser.rb index ade35d7921..95afd7add5 100644 --- a/lib/19/rexml/parsers/baseparser.rb +++ b/lib/19/rexml/parsers/baseparser.rb @@ -114,22 +114,10 @@ class BaseParser def initialize( source ) self.stream = source + @listeners = [] end def add_listener( listener ) - if !defined?(@listeners) or !@listeners - @listeners = [] - instance_eval <<-EOL - alias :_old_pull :pull - def pull - event = _old_pull - @listeners.each do |listener| - listener.receive event - end - event - end - EOL - end @listeners << listener end @@ -192,6 +180,14 @@ def peek depth=0 # Returns the next event. This is a +PullEvent+ object. def pull + pull_event.tap do |event| + @listeners.each do |listener| + listener.receive event + end + end + end + + def pull_event if @closed x, @closed = @closed, nil return [ :end_element, x ] @@ -249,9 +245,7 @@ def pull @source.read if @source.buffer.size<2 md = @source.match(/\s*/um, true) if @source.encoding == "UTF-8" - if @source.buffer.respond_to? :force_encoding - @source.buffer.force_encoding(Encoding::UTF_8) - end + @source.buffer.force_encoding(::Encoding::UTF_8) end end end @@ -382,7 +376,7 @@ def pull attrs.each { |a,b,c,d,e| if b == "xmlns" if c == "xml" - if d != "http://www.w3.org/XML/1998/namespace" + if e != "http://www.w3.org/XML/1998/namespace" msg = "The 'xml' prefix must not be bound to any other namespace "+ "(http://www.w3.org/TR/REC-xml-names/#ns-decl)" raise REXML::ParseException.new( msg, @source, self ) @@ -442,6 +436,7 @@ def pull end return [ :dummy ] end + private :pull_event def entity( reference, entities ) value = nil diff --git a/lib/19/rexml/parsers/lightparser.rb b/lib/19/rexml/parsers/lightparser.rb index ca9692c449..81041681c2 100644 --- a/lib/19/rexml/parsers/lightparser.rb +++ b/lib/19/rexml/parsers/lightparser.rb @@ -44,10 +44,10 @@ def parse end # An element is an array. The array contains: - # 0 The parent element - # 1 The tag name - # 2 A hash of attributes - # 3..-1 The child elements + # 0 The parent element + # 1 The tag name + # 2 A hash of attributes + # 3..-1 The child elements # An element is an array of size > 3 # Text is a String # PIs are [ :processing_instruction, target, data ] diff --git a/lib/19/rexml/parsers/sax2parser.rb b/lib/19/rexml/parsers/sax2parser.rb index d0f0c5155e..0661af37aa 100644 --- a/lib/19/rexml/parsers/sax2parser.rb +++ b/lib/19/rexml/parsers/sax2parser.rb @@ -28,15 +28,15 @@ def add_listener( listener ) # Listen arguments: # # Symbol, Array, Block - # Listen to Symbol events on Array elements + # Listen to Symbol events on Array elements # Symbol, Block # Listen to Symbol events # Array, Listener - # Listen to all events on Array elements + # Listen to all events on Array elements # Array, Block - # Listen to :start_element events on Array elements + # Listen to :start_element events on Array elements # Listener - # Listen to All events + # Listen to All events # # Symbol can be one of: :start_element, :end_element, # :start_prefix_mapping, :end_prefix_mapping, :characters, @@ -87,7 +87,7 @@ def parse @listeners.each { |sym,match,block| block.start_document if sym == :start_document or sym.nil? } - root = context = [] + context = [] while true event = @parser.pull case event[0] diff --git a/lib/19/rexml/parsers/ultralightparser.rb b/lib/19/rexml/parsers/ultralightparser.rb index 96c55d837e..7dd8172802 100644 --- a/lib/19/rexml/parsers/ultralightparser.rb +++ b/lib/19/rexml/parsers/ultralightparser.rb @@ -42,10 +42,10 @@ def parse end # An element is an array. The array contains: - # 0 The parent element - # 1 The tag name - # 2 A hash of attributes - # 3..-1 The child elements + # 0 The parent element + # 1 The tag name + # 2 A hash of attributes + # 3..-1 The child elements # An element is an array of size > 3 # Text is a String # PIs are [ :processing_instruction, target, data ] diff --git a/lib/19/rexml/parsers/xpathparser.rb b/lib/19/rexml/parsers/xpathparser.rb index 2db37a2578..e643d11511 100644 --- a/lib/19/rexml/parsers/xpathparser.rb +++ b/lib/19/rexml/parsers/xpathparser.rb @@ -17,8 +17,9 @@ def namespaces=( namespaces ) end def parse path + path = path.dup path.gsub!(/([\(\[])\s+/, '\1') # Strip ignorable spaces - path.gsub!( /\s+([\]\)])/, '\1' ) + path.gsub!( /\s+([\]\)])/, '\1') parsed = [] path = OrExpr(path, parsed) parsed @@ -282,7 +283,6 @@ def RelativeLocationPath path, parsed PI = /^processing-instruction\(/ def NodeTest path, parsed #puts "NodeTest with #{path}" - res = nil case path when /^\*/ path = $' @@ -578,7 +578,6 @@ def FilterExpr path, parsed NUMBER = /^(\d*\.?\d+)/ NT = /^comment|text|processing-instruction|node$/ def PrimaryExpr path, parsed - arry = [] case path when VARIABLE_REFERENCE varname = $1 diff --git a/lib/19/rexml/quickpath.rb b/lib/19/rexml/quickpath.rb index 3ad8023470..9bec2158dd 100644 --- a/lib/19/rexml/quickpath.rb +++ b/lib/19/rexml/quickpath.rb @@ -6,6 +6,8 @@ class QuickPath include Functions include XMLTokens + # A base Hash object to be used when initializing a + # default empty namespaces set. EMPTY_HASH = {} def QuickPath::first element, path, namespaces=EMPTY_HASH @@ -33,7 +35,6 @@ def QuickPath::match element, path, namespaces=EMPTY_HASH results = filter(element.to_a, path) when /^[\[!\w:]/u # match on child - matches = [] children = element.to_a results = filter(children, path) else @@ -48,22 +49,20 @@ def QuickPath::match element, path, namespaces=EMPTY_HASH def QuickPath::filter elements, path return elements if path.nil? or path == '' or elements.size == 0 case path - when /^\/\//u # Descendant + when /^\/\//u # Descendant return axe( elements, "descendant-or-self", $' ) - when /^\/?\b(\w[-\w]*)\b::/u # Axe - axe_name = $1 - rest = $' + when /^\/?\b(\w[-\w]*)\b::/u # Axe return axe( elements, $1, $' ) - when /^\/(?=\b([:!\w][-\.\w]*:)?[-!\*\.\w]*\b([^:(]|$)|\*)/u # Child + when /^\/(?=\b([:!\w][-\.\w]*:)?[-!\*\.\w]*\b([^:(]|$)|\*)/u # Child rest = $' results = [] elements.each do |element| results |= filter( element.to_a, rest ) end return results - when /^\/?(\w[-\w]*)\(/u # / Function + when /^\/?(\w[-\w]*)\(/u # / Function return function( elements, $1, $' ) - when Namespace::NAMESPLIT # Element name + when Namespace::NAMESPLIT # Element name name = $2 ns = $1 rest = $' @@ -80,22 +79,22 @@ def QuickPath::filter elements, path matches |= predicate( element.to_a, path[1..-1] ) if element.kind_of? Element end return matches - when /^\[/u # Predicate + when /^\[/u # Predicate return predicate( elements, path ) - when /^\/?\.\.\./u # Ancestor + when /^\/?\.\.\./u # Ancestor return axe( elements, "ancestor", $' ) - when /^\/?\.\./u # Parent + when /^\/?\.\./u # Parent return filter( elements.collect{|e|e.parent}, $' ) - when /^\/?\./u # Self + when /^\/?\./u # Self return filter( elements, $' ) - when /^\*/u # Any + when /^\*/u # Any results = [] elements.each do |element| results |= filter( [element], $' ) if element.kind_of? Element #if element.kind_of? Element - # children = element.to_a - # children.delete_if { |child| !child.kind_of?(Element) } - # results |= filter( children, $' ) + # children = element.to_a + # children.delete_if { |child| !child.kind_of?(Element) } + # results |= filter( children, $' ) #end end return results diff --git a/lib/19/rexml/rexml.rb b/lib/19/rexml/rexml.rb index 23535848cb..f89951171a 100644 --- a/lib/19/rexml/rexml.rb +++ b/lib/19/rexml/rexml.rb @@ -24,7 +24,7 @@ module REXML COPYRIGHT = "Copyright © 2001-2008 Sean Russell " DATE = "2008/019" VERSION = "3.1.7.3" - REVISION = %w$Revision: 26193 $[1] || '' + REVISION = %w$Revision$[1] || '' Copyright = COPYRIGHT Version = VERSION diff --git a/lib/19/rexml/source.rb b/lib/19/rexml/source.rb index 1206150b16..112393cfd4 100644 --- a/lib/19/rexml/source.rb +++ b/lib/19/rexml/source.rb @@ -54,14 +54,12 @@ def initialize(arg, encoding=nil) def encoding=(enc) return unless super @line_break = encode( '>' ) - if enc != UTF_8 + if @encoding != 'UTF-8' @buffer = decode(@buffer) @to_utf = true else @to_utf = false - if @buffer.respond_to? :force_encoding - @buffer.force_encoding Encoding::UTF_8 - end + @buffer.force_encoding ::Encoding::UTF_8 end end diff --git a/lib/19/rexml/text.rb b/lib/19/rexml/text.rb index 05d5341abb..6623c0c03b 100644 --- a/lib/19/rexml/text.rb +++ b/lib/19/rexml/text.rb @@ -105,7 +105,7 @@ def initialize(arg, respect_whitespace=false, parent=nil, raw=nil, @normalized = @unnormalized = nil if arg.kind_of? String - @string = arg.clone + @string = arg.dup @string.squeeze!(" \n\t") unless respect_whitespace elsif arg.kind_of? Text @string = arg.to_s diff --git a/lib/19/rexml/validation/validation.rb b/lib/19/rexml/validation/validation.rb index f0ffa78912..8042e5d062 100644 --- a/lib/19/rexml/validation/validation.rb +++ b/lib/19/rexml/validation/validation.rb @@ -33,7 +33,7 @@ def validate( event ) sattr = [:start_attribute, nil] eattr = [:end_attribute] text = [:text, nil] - k,v = event[2].find { |key,value| + k, = event[2].find { |key,value| sattr[1] = key #puts "Looking for #{sattr.inspect}" m = @current.next( sattr ) diff --git a/lib/19/rexml/xpath.rb b/lib/19/rexml/xpath.rb index 5f7ecefe29..0f99808def 100644 --- a/lib/19/rexml/xpath.rb +++ b/lib/19/rexml/xpath.rb @@ -5,16 +5,19 @@ module REXML # Wrapper class. Use this class to access the XPath functions. class XPath include Functions + # A base Hash object, supposing to be used when initializing a + # default empty namespaces set, but is currently unused. + # TODO: either set the namespaces=EMPTY_HASH, or deprecate this. EMPTY_HASH = {} # Finds and returns the first node that matches the supplied xpath. # element:: - # The context element + # The context element # path:: - # The xpath to search for. If not supplied or nil, returns the first - # node matching '*'. + # The xpath to search for. If not supplied or nil, returns the first + # node matching '*'. # namespaces:: - # If supplied, a Hash which defines a namespace mapping. + # If supplied, a Hash which defines a namespace mapping. # variables:: # If supplied, a Hash which maps $variables in the query # to values. This can be used to avoid XPath injection attacks @@ -42,7 +45,7 @@ def XPath::first element, path=nil, namespaces=nil, variables={} # path:: # The xpath to search for. If not supplied or nil, defaults to '*' # namespaces:: - # If supplied, a Hash which defines a namespace mapping + # If supplied, a Hash which defines a namespace mapping # variables:: # If supplied, a Hash which maps $variables in the query # to values. This can be used to avoid XPath injection attacks diff --git a/lib/19/rexml/xpath_parser.rb b/lib/19/rexml/xpath_parser.rb index 11950ecd93..0fc9da2e08 100644 --- a/lib/19/rexml/xpath_parser.rb +++ b/lib/19/rexml/xpath_parser.rb @@ -5,20 +5,30 @@ require 'rexml/parsers/xpathparser' class Object + # provides a unified +clone+ operation, for REXML::XPathParser + # to use across multiple Object types def dclone clone end end class Symbol + # provides a unified +clone+ operation, for REXML::XPathParser + # to use across multiple Object types def dclone ; self ; end end class Fixnum + # provides a unified +clone+ operation, for REXML::XPathParser + # to use across multiple Object types def dclone ; self ; end end class Float + # provides a unified +clone+ operation, for REXML::XPathParser + # to use across multiple Object types def dclone ; self ; end end class Array + # provides a unified +clone+ operation, for REXML::XPathParser + # to use across multiple Object+ types def dclone klone = self.clone klone.clear @@ -434,7 +444,8 @@ def expr( path_stack, nodeset, context=nil ) when :and left = expr( path_stack.shift, nodeset.dup, context ) #puts "LEFT => #{left.inspect} (#{left.class.name})" - if left == false || left.nil? || !left.inject(false) {|a,b| a | b} + return [] unless left + if left.respond_to?(:inject) and !left.inject(false) {|a,b| a | b} return [] end right = expr( path_stack.shift, nodeset.dup, context ) @@ -686,7 +697,7 @@ def equality_relational_compare( set1, op, set2 ) return rv else res = [] - enum = SyncEnumerator.new( set1, set2 ).each { |i1, i2| + SyncEnumerator.new( set1, set2 ).each { |i1, i2| #puts "i1 = #{i1.inspect} (#{i1.class.name})" #puts "i2 = #{i2.inspect} (#{i2.class.name})" i1 = norm( i1 ) From 91acf0a2fb3d9d430bb577c95b1acf7e30315dd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Nie=C5=82acny?= Date: Mon, 28 May 2012 12:59:56 +0200 Subject: [PATCH 2/3] Add empty search_convpath method --- kernel/common/encoding.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/common/encoding.rb b/kernel/common/encoding.rb index 9baca2ccdc..8aafb885ed 100644 --- a/kernel/common/encoding.rb +++ b/kernel/common/encoding.rb @@ -53,6 +53,9 @@ def initialize(from, to, options=undefined) def convert(str) end + + def self.search_convpath(from, to, options={}) + end end def self.aliases From 65e6ebc7559e042441a3049376f85cf661e43e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Nie=C5=82acny?= Date: Mon, 28 May 2012 14:18:00 +0200 Subject: [PATCH 3/3] Add TODO for search_convpath method --- kernel/common/encoding.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/common/encoding.rb b/kernel/common/encoding.rb index 8aafb885ed..54acc69db8 100644 --- a/kernel/common/encoding.rb +++ b/kernel/common/encoding.rb @@ -54,6 +54,7 @@ def initialize(from, to, options=undefined) def convert(str) end + # TODO: Add implementation for this method def self.search_convpath(from, to, options={}) end end