2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2018-11-02 13:52:33 -04:00
|
|
|
require_relative 'functions'
|
|
|
|
require_relative 'xmltokens'
|
2003-06-09 21:31:01 -04:00
|
|
|
|
|
|
|
module REXML
|
2008-10-01 09:46:53 -04:00
|
|
|
class QuickPath
|
|
|
|
include Functions
|
|
|
|
include XMLTokens
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2011-05-11 18:56:13 -04:00
|
|
|
# A base Hash object to be used when initializing a
|
|
|
|
# default empty namespaces set.
|
2008-10-01 09:46:53 -04:00
|
|
|
EMPTY_HASH = {}
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::first element, path, namespaces=EMPTY_HASH
|
|
|
|
match(element, path, namespaces)[0]
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::each element, path, namespaces=EMPTY_HASH, &block
|
|
|
|
path = "*" unless path
|
|
|
|
match(element, path, namespaces).each( &block )
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::match element, path, namespaces=EMPTY_HASH
|
|
|
|
raise "nil is not a valid xpath" unless path
|
|
|
|
results = nil
|
|
|
|
Functions::namespace_context = namespaces
|
|
|
|
case path
|
|
|
|
when /^\/([^\/]|$)/u
|
|
|
|
# match on root
|
|
|
|
path = path[1..-1]
|
|
|
|
return [element.root.parent] if path == ''
|
|
|
|
results = filter([element.root], path)
|
|
|
|
when /^[-\w]*::/u
|
|
|
|
results = filter([element], path)
|
|
|
|
when /^\*/u
|
|
|
|
results = filter(element.to_a, path)
|
|
|
|
when /^[\[!\w:]/u
|
|
|
|
# match on child
|
|
|
|
children = element.to_a
|
|
|
|
results = filter(children, path)
|
|
|
|
else
|
|
|
|
results = filter([element], path)
|
|
|
|
end
|
|
|
|
return results
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
# Given an array of nodes it filters the array based on the path. The
|
|
|
|
# result is that when this method returns, the array will contain elements
|
|
|
|
# which match the path
|
|
|
|
def QuickPath::filter elements, path
|
|
|
|
return elements if path.nil? or path == '' or elements.size == 0
|
|
|
|
case path
|
2011-05-18 20:07:25 -04:00
|
|
|
when /^\/\//u # Descendant
|
2008-10-01 09:46:53 -04:00
|
|
|
return axe( elements, "descendant-or-self", $' )
|
2011-05-18 20:07:25 -04:00
|
|
|
when /^\/?\b(\w[-\w]*)\b::/u # Axe
|
2008-10-01 09:46:53 -04:00
|
|
|
return axe( elements, $1, $' )
|
2011-05-18 20:07:25 -04:00
|
|
|
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
|
|
|
|
return function( elements, $1, $' )
|
|
|
|
when Namespace::NAMESPLIT # Element name
|
2008-10-01 09:46:53 -04:00
|
|
|
name = $2
|
|
|
|
ns = $1
|
|
|
|
rest = $'
|
|
|
|
elements.delete_if do |element|
|
2009-03-05 22:56:38 -05:00
|
|
|
!(element.kind_of? Element and
|
2008-10-01 09:46:53 -04:00
|
|
|
(element.expanded_name == name or
|
|
|
|
(element.name == name and
|
|
|
|
element.namespace == Functions.namespace_context[ns])))
|
|
|
|
end
|
|
|
|
return filter( elements, rest )
|
|
|
|
when /^\/\[/u
|
|
|
|
matches = []
|
|
|
|
elements.each do |element|
|
|
|
|
matches |= predicate( element.to_a, path[1..-1] ) if element.kind_of? Element
|
|
|
|
end
|
|
|
|
return matches
|
2011-05-18 20:07:25 -04:00
|
|
|
when /^\[/u # Predicate
|
2008-10-01 09:46:53 -04:00
|
|
|
return predicate( elements, path )
|
2011-05-18 20:07:25 -04:00
|
|
|
when /^\/?\.\.\./u # Ancestor
|
2008-10-01 09:46:53 -04:00
|
|
|
return axe( elements, "ancestor", $' )
|
2011-05-18 20:07:25 -04:00
|
|
|
when /^\/?\.\./u # Parent
|
2008-10-01 09:46:53 -04:00
|
|
|
return filter( elements.collect{|e|e.parent}, $' )
|
2011-05-18 20:07:25 -04:00
|
|
|
when /^\/?\./u # Self
|
2008-10-01 09:46:53 -04:00
|
|
|
return filter( elements, $' )
|
2011-05-18 20:07:25 -04:00
|
|
|
when /^\*/u # Any
|
2008-10-01 09:46:53 -04:00
|
|
|
results = []
|
|
|
|
elements.each do |element|
|
|
|
|
results |= filter( [element], $' ) if element.kind_of? Element
|
|
|
|
#if element.kind_of? Element
|
2011-05-18 20:07:25 -04:00
|
|
|
# children = element.to_a
|
|
|
|
# children.delete_if { |child| !child.kind_of?(Element) }
|
|
|
|
# results |= filter( children, $' )
|
2008-10-01 09:46:53 -04:00
|
|
|
#end
|
|
|
|
end
|
|
|
|
return results
|
|
|
|
end
|
|
|
|
return []
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::axe( elements, axe_name, rest )
|
|
|
|
matches = []
|
|
|
|
matches = filter( elements.dup, rest ) if axe_name =~ /-or-self$/u
|
|
|
|
case axe_name
|
|
|
|
when /^descendant/u
|
|
|
|
elements.each do |element|
|
|
|
|
matches |= filter( element.to_a, "descendant-or-self::#{rest}" ) if element.kind_of? Element
|
|
|
|
end
|
|
|
|
when /^ancestor/u
|
|
|
|
elements.each do |element|
|
|
|
|
while element.parent
|
|
|
|
matches << element.parent
|
|
|
|
element = element.parent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
matches = filter( matches, rest )
|
|
|
|
when "self"
|
|
|
|
matches = filter( elements, rest )
|
|
|
|
when "child"
|
|
|
|
elements.each do |element|
|
|
|
|
matches |= filter( element.to_a, rest ) if element.kind_of? Element
|
|
|
|
end
|
|
|
|
when "attribute"
|
|
|
|
elements.each do |element|
|
|
|
|
matches << element.attributes[ rest ] if element.kind_of? Element
|
|
|
|
end
|
|
|
|
when "parent"
|
|
|
|
matches = filter(elements.collect{|element| element.parent}.uniq, rest)
|
|
|
|
when "following-sibling"
|
|
|
|
matches = filter(elements.collect{|element| element.next_sibling}.uniq,
|
2011-05-18 20:07:25 -04:00
|
|
|
rest)
|
2008-10-01 09:46:53 -04:00
|
|
|
when "previous-sibling"
|
2009-03-05 22:56:38 -05:00
|
|
|
matches = filter(elements.collect{|element|
|
2008-10-01 09:46:53 -04:00
|
|
|
element.previous_sibling}.uniq, rest )
|
|
|
|
end
|
|
|
|
return matches.uniq
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2010-04-06 22:59:33 -04:00
|
|
|
OPERAND_ = '((?=(?:(?!and|or).)*[^\s<>=])[^\s<>=]+)'
|
2008-10-01 09:46:53 -04:00
|
|
|
# A predicate filters a node-set with respect to an axis to produce a
|
2009-03-05 22:56:38 -05:00
|
|
|
# new node-set. For each node in the node-set to be filtered, the
|
|
|
|
# PredicateExpr is evaluated with that node as the context node, with
|
|
|
|
# the number of nodes in the node-set as the context size, and with the
|
2008-10-01 09:46:53 -04:00
|
|
|
# proximity position of the node in the node-set with respect to the
|
|
|
|
# axis as the context position; if PredicateExpr evaluates to true for
|
|
|
|
# that node, the node is included in the new node-set; otherwise, it is
|
|
|
|
# not included.
|
|
|
|
#
|
|
|
|
# A PredicateExpr is evaluated by evaluating the Expr and converting
|
|
|
|
# the result to a boolean. If the result is a number, the result will
|
|
|
|
# be converted to true if the number is equal to the context position
|
|
|
|
# and will be converted to false otherwise; if the result is not a
|
|
|
|
# number, then the result will be converted as if by a call to the
|
|
|
|
# boolean function. Thus a location path para[3] is equivalent to
|
|
|
|
# para[position()=3].
|
2009-03-05 22:56:38 -05:00
|
|
|
def QuickPath::predicate( elements, path )
|
2008-10-01 09:46:53 -04:00
|
|
|
ind = 1
|
|
|
|
bcount = 1
|
|
|
|
while bcount > 0
|
|
|
|
bcount += 1 if path[ind] == ?[
|
|
|
|
bcount -= 1 if path[ind] == ?]
|
|
|
|
ind += 1
|
|
|
|
end
|
|
|
|
ind -= 1
|
|
|
|
predicate = path[1..ind-1]
|
|
|
|
rest = path[ind+1..-1]
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
# have to change 'a [=<>] b [=<>] c' into 'a [=<>] b and b [=<>] c'
|
2010-04-06 22:59:33 -04:00
|
|
|
#
|
|
|
|
predicate.gsub!(
|
|
|
|
/#{OPERAND_}\s*([<>=])\s*#{OPERAND_}\s*([<>=])\s*#{OPERAND_}/u,
|
2008-10-01 09:46:53 -04:00
|
|
|
'\1 \2 \3 and \3 \4 \5' )
|
2011-05-18 20:07:25 -04:00
|
|
|
# Let's do some Ruby trickery to avoid some work:
|
|
|
|
predicate.gsub!( /&/u, "&&" )
|
|
|
|
predicate.gsub!( /=/u, "==" )
|
|
|
|
predicate.gsub!( /@(\w[-\w.]*)/u, 'attribute("\1")' )
|
|
|
|
predicate.gsub!( /\bmod\b/u, "%" )
|
|
|
|
predicate.gsub!( /\b(\w[-\w.]*\()/u ) {
|
|
|
|
fname = $1
|
|
|
|
fname.gsub( /-/u, "_" )
|
|
|
|
}
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2011-05-18 20:07:25 -04:00
|
|
|
Functions.pair = [ 0, elements.size ]
|
|
|
|
results = []
|
|
|
|
elements.each do |element|
|
|
|
|
Functions.pair[0] += 1
|
|
|
|
Functions.node = element
|
|
|
|
res = eval( predicate )
|
|
|
|
case res
|
|
|
|
when true
|
|
|
|
results << element
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
when Integer
|
2011-05-18 20:07:25 -04:00
|
|
|
results << element if Functions.pair[0] == res
|
|
|
|
when String
|
|
|
|
results << element
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
2011-05-18 20:07:25 -04:00
|
|
|
end
|
|
|
|
return filter( results, rest )
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::attribute( name )
|
|
|
|
return Functions.node.attributes[name] if Functions.node.kind_of? Element
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::name()
|
|
|
|
return Functions.node.name if Functions.node.kind_of? Element
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::method_missing( id, *args )
|
|
|
|
begin
|
|
|
|
Functions.send( id.id2name, *args )
|
|
|
|
rescue Exception
|
|
|
|
raise "METHOD: #{id.id2name}(#{args.join ', '})\n#{$!.message}"
|
|
|
|
end
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::function( elements, fname, rest )
|
|
|
|
args = parse_args( elements, rest )
|
|
|
|
Functions.pair = [0, elements.size]
|
|
|
|
results = []
|
|
|
|
elements.each do |element|
|
|
|
|
Functions.pair[0] += 1
|
|
|
|
Functions.node = element
|
|
|
|
res = Functions.send( fname, *args )
|
|
|
|
case res
|
|
|
|
when true
|
|
|
|
results << element
|
Use Integer instead of Fixnum and Bignum.
* object.c, numeric.c, enum.c, ext/-test-/bignum/mul.c,
lib/rexml/quickpath.rb, lib/rexml/text.rb, lib/rexml/xpath_parser.rb,
lib/rubygems/specification.rb, lib/uri/generic.rb,
bootstraptest/test_eval.rb, basictest/test.rb,
test/-ext-/bignum/test_big2str.rb, test/-ext-/bignum/test_div.rb,
test/-ext-/bignum/test_mul.rb, test/-ext-/bignum/test_str2big.rb,
test/csv/test_data_converters.rb, test/date/test_date.rb,
test/json/test_json_generate.rb, test/minitest/test_minitest_mock.rb,
test/openssl/test_cipher.rb, test/rexml/test_jaxen.rb,
test/ruby/test_array.rb, test/ruby/test_basicinstructions.rb,
test/ruby/test_bignum.rb, test/ruby/test_case.rb,
test/ruby/test_class.rb, test/ruby/test_complex.rb,
test/ruby/test_enum.rb, test/ruby/test_eval.rb,
test/ruby/test_iseq.rb, test/ruby/test_literal.rb,
test/ruby/test_math.rb, test/ruby/test_module.rb,
test/ruby/test_numeric.rb, test/ruby/test_range.rb,
test/ruby/test_rational.rb, test/ruby/test_refinement.rb,
test/ruby/test_rubyvm.rb, test/ruby/test_struct.rb,
test/ruby/test_variable.rb, test/rubygems/test_gem_specification.rb,
test/thread/test_queue.rb: Use Integer instead of Fixnum and Bignum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55029 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-17 09:15:57 -04:00
|
|
|
when Integer
|
2008-10-01 09:46:53 -04:00
|
|
|
results << element if Functions.pair[0] == res
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return results
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
def QuickPath::parse_args( element, string )
|
|
|
|
# /.*?(?:\)|,)/
|
|
|
|
arguments = []
|
|
|
|
buffer = ""
|
|
|
|
while string and string != ""
|
|
|
|
c = string[0]
|
|
|
|
string.sub!(/^./u, "")
|
|
|
|
case c
|
|
|
|
when ?,
|
|
|
|
# if depth = 1, then we start a new argument
|
|
|
|
arguments << evaluate( buffer )
|
|
|
|
#arguments << evaluate( string[0..count] )
|
|
|
|
when ?(
|
|
|
|
# start a new method call
|
|
|
|
function( element, buffer, string )
|
|
|
|
buffer = ""
|
|
|
|
when ?)
|
|
|
|
# close the method call and return arguments
|
|
|
|
return arguments
|
|
|
|
else
|
|
|
|
buffer << c
|
|
|
|
end
|
|
|
|
end
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
2003-06-09 21:31:01 -04:00
|
|
|
end
|