From 8086d1f3fb9bc6cdcee3c4810bd4b876d930dc3a Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Wed, 4 Nov 2009 18:49:48 -0500 Subject: [PATCH 1/3] cleaning up previous fix, because aaron has high standards. (higher than mine, anyway.) --- lib/nokogiri/xml/fragment_handler.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/nokogiri/xml/fragment_handler.rb b/lib/nokogiri/xml/fragment_handler.rb index 68e046c14d..06f63e645b 100644 --- a/lib/nokogiri/xml/fragment_handler.rb +++ b/lib/nokogiri/xml/fragment_handler.rb @@ -28,13 +28,15 @@ def start_element name, attrs = [] @doc_started = true if @original_html =~ regex return unless @doc_started - if @document.root and match = name.match(QNAME_REGEX) - prefix, name = match[1], match[2] - ns = @document.root.namespace_definitions.detect { |x| - x.prefix == prefix - } - else - ns = nil + ns = nil + if @document.root + match = name.match(QNAME_REGEX) + if match + prefix, name = match[1], match[2] + ns = @document.root.namespace_definitions.detect { |x| + x.prefix == prefix + } + end end node = Element.new(name, @document) From 2ffddfce0dc2e5ac41fb75299922422c603b48b2 Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Fri, 6 Nov 2009 05:03:06 +0800 Subject: [PATCH 2/3] Test case for searching empty fragments. --- test/html/test_document_fragment.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/html/test_document_fragment.rb b/test/html/test_document_fragment.rb index abcf5a36b1..26e3aaa4ce 100644 --- a/test/html/test_document_fragment.rb +++ b/test/html/test_document_fragment.rb @@ -26,6 +26,16 @@ def test_fragment_should_have_document assert_equal @html, fragment.document end + def test_empty_fragment_should_be_searchable_by_css + fragment = Nokogiri::HTML.fragment("") + assert_equal 0, fragment.css("a").size + end + + def test_empty_fragment_should_be_searchable + fragment = Nokogiri::HTML.fragment("") + assert_equal 0, fragment.search("//a").size + end + def test_name fragment = Nokogiri::HTML::DocumentFragment.new(@html) assert_equal '#document-fragment', fragment.name From 84aa2ea3a75108f8bc1351953fcec7324ba5ac8b Mon Sep 17 00:00:00 2001 From: Chris Eppstein Date: Fri, 6 Nov 2009 05:29:44 +0800 Subject: [PATCH 3/3] Fix searching css on empty fragments. --- lib/nokogiri/xml/document_fragment.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/nokogiri/xml/document_fragment.rb b/lib/nokogiri/xml/document_fragment.rb index 7cf03773c9..af4450c082 100644 --- a/lib/nokogiri/xml/document_fragment.rb +++ b/lib/nokogiri/xml/document_fragment.rb @@ -51,7 +51,11 @@ def to_xml *args ### # Search this fragment. See Nokogiri::XML::Node#css def css *args - children.css(*args) + if children.any? + children.css(*args) + else + NodeSet.new(document) + end end alias :serialize :to_s