<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -7,6 +7,7 @@
 * Bugfixes
 
   * XML fragments with namespaces do not raise an exception (regression in 1.4.0)
+  * Node#matches? works in nodes contained by a DocumentFragment. GH #158
 
 === 1.4.0 / 2009/10/30
 </diff>
      <filename>CHANGELOG.rdoc</filename>
    </modified>
    <modified>
      <diff>@@ -279,7 +279,7 @@ module Nokogiri
       ###
       # Returns true if this Node matches +selector+
       def matches? selector
-        document.search(selector).include?(self)
+        ancestors.last.search(selector).include?(self)
       end
 
       ####
@@ -435,8 +435,10 @@ module Nokogiri
 
         return NodeSet.new(document, parents) unless selector
 
+        root = parents.last
+
         NodeSet.new(document, parents.find_all { |parent|
-          parent.matches?(selector)
+          root.search(selector).include?(parent)
         })
       end
 </diff>
      <filename>lib/nokogiri/xml/node.rb</filename>
    </modified>
    <modified>
      <diff>@@ -9,6 +9,19 @@ module Nokogiri
         @html = Nokogiri::HTML.parse(File.read(HTML_FILE), HTML_FILE)
       end
 
+      def test_ancestors_search
+        html = %q{
+          &lt;div&gt;
+            &lt;ul&gt;
+              &lt;li&gt;foo&lt;/li&gt;
+            &lt;/ul&gt;
+          &lt;/div&gt;
+        }
+        fragment = Nokogiri::HTML.fragment html
+        li = fragment.at('li')
+        assert li.matches?('li')
+      end
+
       def test_fun_encoding
         string = %Q(&lt;body&gt;&#12371;&#12435;&#12395;&#12385;&#12399;&lt;/body&gt;)
         html = Nokogiri::HTML::DocumentFragment.parse(</diff>
      <filename>test/html/test_document_fragment.rb</filename>
    </modified>
    <modified>
      <diff>@@ -78,6 +78,14 @@ module Nokogiri
         assert_equal 'div', list.first.name
       end
 
+      def test_matches_inside_fragment
+        fragment = DocumentFragment.new @html
+        fragment &lt;&lt; XML::Node.new('a', @html)
+
+        a = fragment.children.last
+        assert a.matches?('a'), 'a should match'
+      end
+
       def test_css_matches?
         assert node = @html.at('a.bar')
         assert node.matches?('a.bar')</diff>
      <filename>test/html/test_node.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>108b5d24231bda324157b215f1284a3e9593aeeb</id>
    </parent>
  </parents>
  <author>
    <name>Aaron Patterson</name>
    <email>aaron.patterson@gmail.com</email>
  </author>
  <url>http://github.com/entangledstate/nokogiri/commit/d41db1a1d949ab7827b54e2789b5ee1addf2c45b</url>
  <id>d41db1a1d949ab7827b54e2789b5ee1addf2c45b</id>
  <committed-date>2009-11-05T14:34:14-08:00</committed-date>
  <authored-date>2009-11-05T14:34:14-08:00</authored-date>
  <message>Node#matches? works in nodes contained by a DocumentFragment. closes #158</message>
  <tree>787229612c0dd9c7f812f1c4e43c0c1155f9ee7a</tree>
  <committer>
    <name>Aaron Patterson</name>
    <email>aaron.patterson@gmail.com</email>
  </committer>
</commit>
