[前][次][番号順一覧][スレッド一覧]

ruby-changes:50881

From: kou <ko1@a...>
Date: Wed, 4 Apr 2018 15:54:02 +0900 (JST)
Subject: [ruby-changes:50881] kou:r63088 (trunk): rexml: Fix a XPath bug of /child::node()

kou	2018-04-04 15:53:57 +0900 (Wed, 04 Apr 2018)

  New Revision: 63088

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63088

  Log:
    rexml: Fix a XPath bug of /child::node()
        
    [Bug #14600]
        
    * lib/rexml/xpath_parser.rb: Fix a bug that "/child::node()" returns
      XML declaration and text nodes out of root element.
    
    * test/rexml/test_jaxen.rb: Enable more tests.

  Modified files:
    trunk/lib/rexml/xpath_parser.rb
    trunk/test/rexml/test_jaxen.rb
Index: lib/rexml/xpath_parser.rb
===================================================================
--- lib/rexml/xpath_parser.rb	(revision 63087)
+++ lib/rexml/xpath_parser.rb	(revision 63088)
@@ -212,7 +212,19 @@ module REXML https://github.com/ruby/ruby/blob/trunk/lib/rexml/xpath_parser.rb#L212
           nodeset.each do |node|
             nt = node.node_type
             # trace(:child, nt, node)
-            new_nodeset += node.children if nt == :element or nt == :document
+            case nt
+            when :element
+              new_nodeset.concat(node.children)
+            when :document
+              node.children.each do |child|
+                case child
+                when XMLDecl, Text
+                  # ignore
+                else
+                  new_nodeset << child
+                end
+              end
+            end
           end
           nodeset = new_nodeset
           node_types = ELEMENTS
Index: test/rexml/test_jaxen.rb
===================================================================
--- test/rexml/test_jaxen.rb	(revision 63087)
+++ test/rexml/test_jaxen.rb	(revision 63088)
@@ -21,8 +21,9 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_jaxen.rb#L21
     def test_id ; process_test_case("id") ; end
     def test_jaxen24 ; process_test_case("jaxen24") ; end
     def test_lang ; process_test_case("lang") ; end
+    # document() function for XSLT isn't supported
     def _test_message ; process_test_case("message") ; end
-    def _test_moreover ; process_test_case("moreover") ; end
+    def test_moreover ; process_test_case("moreover") ; end
     def _test_much_ado ; process_test_case("much_ado") ; end
     def _test_namespaces ; process_test_case("namespaces") ; end
     def _test_nitf ; process_test_case("nitf") ; end
@@ -80,13 +81,7 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_jaxen.rb#L81
       xpath = value_of.attributes["select"]
       matched = XPath.first(context, xpath, namespaces, variables)
 
-      message = ""
-      context.each_with_index do |node, i|
-        message << "Node#{i}:\n"
-        message << node.to_s
-      end
-      message << "XPath: <#{xpath}>\n"
-      message << "Matched <#{matched.class}>"
+      message = user_message(context, xpath, matched)
 
       if expected.nil?
         assert_nil(matched, message)
@@ -117,11 +112,12 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_jaxen.rb#L112
       expected = test.attributes["count"]
       if expected
         assert_equal(Integer(expected, 10),
-                     matched.size)
+                     matched.size,
+                     user_message(context, xpath, matched))
       end
 
       XPath.each(test, "valueOf") do |value_of|
-        process_value_of(mathched, variables, namespaces, value_of)
+        process_value_of(matched, variables, namespaces, value_of)
       end
     end
 
@@ -132,5 +128,16 @@ module REXMLTests https://github.com/ruby/ruby/blob/trunk/test/rexml/test_jaxen.rb#L128
         XPath.match(context, select, namespaces, variables)
       end
     end
+
+    def user_message(context, xpath, matched)
+      message = ""
+      context.each_with_index do |node, i|
+        message << "Node#{i}:\n"
+        message << "#{node}\n"
+      end
+      message << "XPath: <#{xpath}>\n"
+      message << "Matched <#{matched}>"
+      message
+    end
   end
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]