Skip to content

Commit

Permalink
making nodeset#dup work GH #10
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Mar 19, 2009
1 parent 8af16e5 commit d5b5a08
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Expand Up @@ -4,6 +4,7 @@

* Fixing bug where a node is passed in to Node#new
* Namespace should be assigned on DocumentFragment creation. LH #66
* Nokogiri::XML::NodeSet#dup works GH #10

=== 1.2.2 / 2009-03-14

Expand Down
8 changes: 1 addition & 7 deletions ext/nokogiri/xml_node.c
Expand Up @@ -232,13 +232,7 @@ static VALUE children(VALUE self)

child = child->next;
while(NULL != child) {
if(set->nodeNr >= set->nodeMax) {
// This will grow the nodeset for us
xmlXPathNodeSetAdd(set, child);
} else {
set->nodeTab[set->nodeNr] = child;
set->nodeNr++;
}
xmlXPathNodeSetAdd(set, child);
child = child->next;
}

Expand Down
26 changes: 24 additions & 2 deletions ext/nokogiri/xml_node_set.c
@@ -1,5 +1,26 @@
#include <xml_node_set.h>
#include <libxml/xpathInternals.h>

/*
* call-seq:
* dup
*
* Duplicate this node set
*/
static VALUE dup(VALUE self)
{
xmlNodeSetPtr node_set;
Data_Get_Struct(self, xmlNodeSet, node_set);

xmlNodeSetPtr dupl = xmlXPathNodeSetCreate(NULL);
int i;
for(i = 0; i < node_set->nodeNr; i++) {
xmlXPathNodeSetAdd(dupl, node_set->nodeTab[i]);
}

return Nokogiri_wrap_xml_node_set(dupl);
}

/*
* call-seq:
* length
Expand Down Expand Up @@ -69,11 +90,11 @@ static VALUE to_array(VALUE self, VALUE rb_node)
xmlNodeSetPtr set;
Data_Get_Struct(self, xmlNodeSet, set);

VALUE *elts = calloc(set->nodeNr, sizeof(VALUE *));
VALUE *elts = calloc((size_t)set->nodeNr, sizeof(VALUE *));
int i;
for(i = 0; i < set->nodeNr; i++) {
if(set->nodeTab[i]->_private) {
elts[i] = set->nodeTab[i]->_private;
elts[i] = (VALUE)set->nodeTab[i]->_private;
} else {
elts[i] = Nokogiri_wrap_xml_node(set->nodeTab[i]);
}
Expand Down Expand Up @@ -174,4 +195,5 @@ void init_xml_node_set(void)
rb_define_method(klass, "push", push, 1);
rb_define_method(klass, "unlink", unlink_nodeset, 0);
rb_define_method(klass, "to_a", to_array, 0);
rb_define_method(klass, "dup", dup, 0);
}
9 changes: 9 additions & 0 deletions test/xml/test_node_set.rb
Expand Up @@ -8,6 +8,15 @@ def setup
@xml = Nokogiri::XML.parse(File.read(XML_FILE), XML_FILE)
end

def test_dup
assert node_set = @xml.xpath('//employee')
dup = node_set.dup
assert_equal node_set.length, dup.length
node_set.zip(dup).each do |a,b|
assert_equal a, b
end
end

def test_xmlns_is_automatically_registered
doc = Nokogiri::XML(<<-eoxml)
<root xmlns="http://tenderlovemaking.com/">
Expand Down

0 comments on commit d5b5a08

Please sign in to comment.