Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tenderlove/nokogiri
Browse files Browse the repository at this point in the history
* 'master' of github.com:tenderlove/nokogiri:
  Fix searching css on empty fragments.
  Test case for searching empty fragments.
  cleaning up previous fix, because aaron has high standards.
  • Loading branch information
tenderlove committed Nov 5, 2009
2 parents d41db1a + 84aa2ea commit 17f247b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
6 changes: 5 additions & 1 deletion lib/nokogiri/xml/document_fragment.rb
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions lib/nokogiri/xml/fragment_handler.rb
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions test/html/test_document_fragment.rb
Expand Up @@ -39,6 +39,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
Expand Down

0 comments on commit 17f247b

Please sign in to comment.