Skip to content

Commit

Permalink
xpath methods can return things other than nodesets, you know, cause …
Browse files Browse the repository at this point in the history
…its fast. closes #208
  • Loading branch information
tenderlove committed Jan 20, 2010
1 parent 52994fd commit 45c6d1d
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 73 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* XML::NodeSet#slice gracefully handles offset+length larger than the set length. GH #200
* XML::Node#content= safely unlinks previous content. GH #203
* XML::Node#namespace= takes nil as a parameter
* XML::Node#xpath returns things other than NodeSet objects. GH #208

=== 1.4.1 / 2009/12/10

Expand Down
2 changes: 0 additions & 2 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ ext/nokogiri/xml_syntax_error.c
ext/nokogiri/xml_syntax_error.h
ext/nokogiri/xml_text.c
ext/nokogiri/xml_text.h
ext/nokogiri/xml_xpath.c
ext/nokogiri/xml_xpath.h
ext/nokogiri/xml_xpath_context.c
ext/nokogiri/xml_xpath_context.h
ext/nokogiri/xslt_stylesheet.c
Expand Down
1 change: 0 additions & 1 deletion ext/nokogiri/nokogiri.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ void Init_nokogiri()
init_xml_comment();
init_xml_node_set();
init_xml_xpath_context();
init_xml_xpath();
init_xml_sax_parser_context();
init_xml_sax_parser();
init_xml_sax_push_parser();
Expand Down
1 change: 0 additions & 1 deletion ext/nokogiri/nokogiri.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ int is_2_6_16(void) ;
#include <xml_document_fragment.h>
#include <xml_comment.h>
#include <xml_node_set.h>
#include <xml_xpath.h>
#include <xml_dtd.h>
#include <xml_attribute_decl.h>
#include <xml_element_decl.h>
Expand Down
51 changes: 0 additions & 51 deletions ext/nokogiri/xml_xpath.c

This file was deleted.

11 changes: 0 additions & 11 deletions ext/nokogiri/xml_xpath.h

This file was deleted.

24 changes: 21 additions & 3 deletions ext/nokogiri/xml_xpath_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,31 @@ static VALUE evaluate(int argc, VALUE *argv, VALUE self)
rb_exc_raise(Nokogiri_wrap_xml_syntax_error(klass, error));
}

VALUE xpath_object = Nokogiri_wrap_xml_xpath(xpath);
VALUE thing = Qnil;

assert(ctx->doc);
assert(DOC_RUBY_OBJECT_TEST(ctx->doc));

rb_iv_set(xpath_object, "@document", DOC_RUBY_OBJECT(ctx->doc));
return xpath_object;
switch(xpath->type) {
case XPATH_STRING:
thing = NOKOGIRI_STR_NEW2(xpath->stringval);
break;
case XPATH_NODESET:
thing = Nokogiri_wrap_xml_node_set(xpath->nodesetval,
DOC_RUBY_OBJECT(ctx->doc));
break;
case XPATH_NUMBER:
thing = rb_float_new(xpath->floatval);
break;
case XPATH_BOOLEAN:
thing = xpath->boolval == 1 ? Qtrue : Qfalse;
break;
default:
thing = Nokogiri_wrap_xml_node_set(xmlXPathNodeSetCreate(NULL),
DOC_RUBY_OBJECT(ctx->doc));
}

return thing;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def xpath *paths
sets = paths.map { |path|
ctx = XPathContext.new(self)
ctx.register_namespaces(ns)
ctx.evaluate(path, handler).node_set
ctx.evaluate(path, handler)
}
return sets.first if sets.length == 1

Expand Down
6 changes: 3 additions & 3 deletions test/xml/test_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -467,21 +467,21 @@ def test_find_with_namespace

ctx = Nokogiri::XML::XPathContext.new(doc)
ctx.register_ns 'tenderlove', 'http://tenderlovemaking.com/'
set = ctx.evaluate('//tenderlove:foo').node_set
set = ctx.evaluate('//tenderlove:foo')
assert_equal 1, set.length
assert_equal 'foo', set.first.name

# It looks like only the URI is important:
ctx = Nokogiri::XML::XPathContext.new(doc)
ctx.register_ns 'america', 'http://tenderlovemaking.com/'
set = ctx.evaluate('//america:foo').node_set
set = ctx.evaluate('//america:foo')
assert_equal 1, set.length
assert_equal 'foo', set.first.name

# Its so important that a missing slash will cause it to return nothing
ctx = Nokogiri::XML::XPathContext.new(doc)
ctx.register_ns 'america', 'http://tenderlovemaking.com'
set = ctx.evaluate('//america:foo').node_set
set = ctx.evaluate('//america:foo')
assert_equal 0, set.length
end

Expand Down
12 changes: 12 additions & 0 deletions test/xml/test_xpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ def saves_node_set node_set
}.new
end

def test_boolean
assert_equal false, @xml.xpath('1 = 2')
end

def test_number
assert_equal 2, @xml.xpath('1 + 1')
end

def test_string
assert_equal 'foo', @xml.xpath('concat("fo", "o")')
end

def test_css_search_uses_custom_selectors_with_arguments
set = @xml.css('employee > address:my_filter("domestic", "Yes")', @handler)
assert set.length > 0
Expand Down

0 comments on commit 45c6d1d

Please sign in to comment.