Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions lib/rexml/xpath_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1154,38 +1154,28 @@ def equality_relational_compare(set1, op, set2)
compare(node_string1, op, node_string2)
end
elsif set1.kind_of? Array or set2.kind_of? Array
# If one is nodeset and other is number, compare number to each item
# in nodeset s.t. number op number(string(item))
# If one is nodeset and other is string, compare string to each item
# in nodeset s.t. string op string(item)
# If one is nodeset and other is boolean, compare boolean to each item
# in nodeset s.t. boolean op boolean(item)
if set1.kind_of? Array
a = set1
b = set2
else
a = set2
b = set1
end

case b
when true, false
a.any? do |node|
compare(@functions.boolean(node), op, b)
end
when Numeric
a.any? do |node|
compare(@functions.number(node), op, b)
end
when /\A\d+(\.\d+)?\z/
b = @functions.number(b)
a.any? do |node|
compare(@functions.number(node), op, b)
if [set1, set2].any? {|value| value == true or value == false}
# If one object to be compared is a node-set and the other is
# a boolean, then the comparison will be true if and only if
# the result of performing the comparison on the boolean and
# on the result of converting the node-set to a boolean using
# the boolean function is true.
compare(@functions.boolean(set1), op, @functions.boolean(set2))
elsif set1.kind_of? Array
# If one object to be compared is a node-set and the other is
# a number (string), then the comparison will be true if and
# only if there is a node in the node-set such that the result
# of performing the comparison on the number to be compared
# and on the result of converting the string-value of that
# node to a number using the number function (on the
# string-value of the node and the other string) is true.
set1.any? do |node|
compare(node_set_item_value(node, set2), op, set2)
end
else
b = @functions.string(b)
a.any? do |node|
compare(@functions.string(node), op, b)
# Keep the operand order: "1 < @n" is not "@n < 1".
set2.any? do |node|
compare(set1, op, node_set_item_value(node, set1))
end
end
else
Expand All @@ -1200,6 +1190,14 @@ def equality_relational_compare(set1, op, set2)
end
end

def node_set_item_value(node, other)
if other.kind_of? Numeric
@functions.number(node)
else
@functions.string(node)
end
end

def value_type(value)
case value
when true, false
Expand Down
83 changes: 83 additions & 0 deletions test/xpath/test_compare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ def test_string_false
assert_equal([false],
match(xml, "/root/child='nonexistent'"))
end

def test_boolean_empty
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
</root>
XML
assert_equal([[true], [false]],
[
match(xml, "/root/child=false()"),
match(xml, "false()!=/root/child"),
])
end

def test_string_number_like
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>100</child>
</root>
XML
assert_equal([[false], [true]],
[
match(xml, "/root/child='100.0'"),
match(xml, "'100'=/root/child"),
])
end
end

class TestBoolean < self
Expand Down Expand Up @@ -192,6 +219,62 @@ def test_string_false
assert_equal([false],
match(xml, "/root/child>'200'"))
end

def test_boolean_left
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child/>
</root>
XML
assert_equal([[false], [true]],
[
match(xml, "false()>/root/child"),
match(xml, "true()>/root/nonexistent"),
])
end

def test_number_left
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>200</child>
<child>300</child>
</root>
XML
assert_equal([[false], [true]],
[
match(xml, "150>/root/child"),
match(xml, "250>/root/child"),
])
end

def test_string_left
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<child>200</child>
<child>300</child>
</root>
XML
assert_equal([[false], [true]],
[
match(xml, "'150'>/root/child"),
match(xml, "'250'>/root/child"),
])
end

def test_predicate_number_left
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item price="5"/>
<item price="50"/>
</root>
XML
assert_equal(["5"],
match(xml, "/root/item[10>@price]").collect {|item| item.attributes["price"]})
end
end

class TestBoolean < self
Expand Down