Skip to content

Commit d91e124

Browse files
committed
rexml: Fix XPath string() implementation
* lib/rexml/functions.rb( REXML::Functions.string): * Support context node. * Fix implementation for document node to remove out of root nodes. * Support processing instruction node. * Improve implementation for integer to omit decimals. * test/rexml/test_jaxen.rb: Enable processing instruction test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63221 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent c44f7d7 commit d91e124

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

lib/rexml/functions.rb

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,36 @@ def Functions::get_namespace( node_set = nil )
136136
# An object of a type other than the four basic types is converted to a
137137
# string in a way that is dependent on that type.
138138
def Functions::string( object=nil )
139-
#object = @context unless object
140-
if object.instance_of? Array
141-
string( object[0] )
142-
elsif defined? object.node_type
143-
if object.node_type == :attribute
139+
object = @@context[:node] if object.nil?
140+
if object.respond_to?(:node_type)
141+
case object.node_type
142+
when :attribute
144143
object.value
145-
elsif object.node_type == :element || object.node_type == :document
144+
when :element
146145
string_value(object)
146+
when :document
147+
string_value(object.root)
148+
when :processing_instruction
149+
object.content
147150
else
148151
object.to_s
149152
end
150-
elsif object.nil?
151-
return ""
152153
else
153-
object.to_s
154+
case object
155+
when Array
156+
string(object[0])
157+
when Numeric
158+
integer = object.to_i
159+
if object == integer
160+
"%d" % integer
161+
else
162+
object.to_s
163+
end
164+
when nil
165+
""
166+
else
167+
object.to_s
168+
end
154169
end
155170
end
156171

test/rexml/test_jaxen.rb

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def _test_much_ado ; process_test_case("much_ado") ; end
2828
def _test_namespaces ; process_test_case("namespaces") ; end
2929
def _test_nitf ; process_test_case("nitf") ; end
3030
def _test_numbers ; process_test_case("numbers") ; end
31-
def _test_pi ; process_test_case("pi") ; end
31+
def test_pi ; process_test_case("pi") ; end
3232
def _test_pi2 ; process_test_case("pi2") ; end
3333
def _test_simple ; process_test_case("simple") ; end
3434
def _test_testNamespaces ; process_test_case("testNamespaces") ; end
@@ -79,29 +79,12 @@ def process_context(doc, context)
7979
def process_value_of(context, variables, namespaces, value_of)
8080
expected = value_of.text
8181
xpath = value_of.attributes["select"]
82-
matched = XPath.first(context, xpath, namespaces, variables)
82+
matched = XPath.match(context, xpath, namespaces, variables)
8383

8484
message = user_message(context, xpath, matched)
85-
86-
if expected.nil?
87-
assert_nil(matched, message)
88-
else
89-
case matched
90-
when Element
91-
assert_equal(expected, matched.text, message)
92-
when Attribute, Text, Comment, TrueClass, FalseClass
93-
assert_equal(expected, matched.to_s, message)
94-
when Instruction
95-
assert_equal(expected, matched.content, message)
96-
when Integer, Float
97-
assert_equal(expected.to_f, matched, message)
98-
when String
99-
assert_equal(expected, matched, message)
100-
else
101-
flunk("#{message}\n" +
102-
"Unexpected match value: <#{matched.inspect}>")
103-
end
104-
end
85+
assert_equal(expected || "",
86+
REXML::Functions.string(matched),
87+
message)
10588
end
10689

10790
# processes a tests/document/context/test node ( where @exception is false or doesn't exist )

0 commit comments

Comments
 (0)