Skip to content

Commit

Permalink
adding a method to set the root element of the document
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Sep 15, 2008
1 parent 9c65d59 commit d18f1ac
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
19 changes: 19 additions & 0 deletions ext/nokogiri/xml_document.c
Expand Up @@ -24,6 +24,24 @@ static VALUE serialize(VALUE self)
return rb_str;
}

/*
* call-seq:
* root=
*
* Set the root element on this document
*/
static VALUE set_root(VALUE self, VALUE root)
{
xmlDocPtr doc;
xmlNodePtr new_root;

Data_Get_Struct(self, xmlDoc, doc);
Data_Get_Struct(root, xmlNode, new_root);

xmlDocSetRootElement(doc, new_root);
return root;
}

/*
* call-seq:
* root
Expand Down Expand Up @@ -107,6 +125,7 @@ void init_xml_document()
rb_define_singleton_method(klass, "load_external_subsets=", load_external_subsets_set, 1);

rb_define_method(klass, "root", root, 0);
rb_define_method(klass, "root=", set_root, 1);
rb_define_method(klass, "serialize", serialize, 0);
}

Expand Down
9 changes: 7 additions & 2 deletions ext/nokogiri/xml_node.c
Expand Up @@ -2,6 +2,9 @@

VALUE Nokogiri_wrap_xml_node(xmlNodePtr root)
{
if(root->_private)
return (VALUE)root->_private;

VALUE klass = rb_eval_string("Nokogiri::XML::Node");
VALUE node = Data_Wrap_Struct(klass, NULL, NULL, root);
root->_private = (void *)node;
Expand Down Expand Up @@ -197,8 +200,7 @@ static VALUE document(VALUE self)

static VALUE new(int argc, VALUE *argv, VALUE klass)
{
VALUE name;
VALUE ns;
VALUE name, ns;
xmlNsPtr xml_ns = NULL;

rb_scan_args(argc, argv, "11", &name, &ns);
Expand All @@ -209,6 +211,9 @@ static VALUE new(int argc, VALUE *argv, VALUE klass)
xmlNodePtr node = xmlNewNode(xml_ns, (xmlChar *)StringValuePtr(name));
VALUE rb_node = Data_Wrap_Struct(klass, NULL, NULL, node);
node->_private = (void *)rb_node;

if(rb_block_given_p()) rb_yield(rb_node);

return rb_node;
}

Expand Down
1 change: 0 additions & 1 deletion lib/nokogiri.rb
Expand Up @@ -5,7 +5,6 @@
require 'nokogiri/native'

module Nokogiri

class << self
def parse(string, url = nil, encoding = nil, options = 32)
doc =
Expand Down
6 changes: 6 additions & 0 deletions lib/nokogiri/hpricot.rb
@@ -1,5 +1,6 @@

require 'nokogiri'
require 'nokogiri/xml/builder'

module Nokogiri
module XML
Expand All @@ -13,6 +14,11 @@ def search(rule)
end
end

def Nokogiri(*args, &block)
builder = Nokogiri::XML::Builder.new
builder.instance_eval(&block)
end


module Nokogiri
module Hpricot
Expand Down
14 changes: 14 additions & 0 deletions lib/nokogiri/xml/builder.rb
@@ -0,0 +1,14 @@
module Nokogiri
module XML
class Builder
def initialize
@doc = Nokogiri::XML::Document.new
end

def method_missing(method, *args, &block)
node = Nokogiri::XML::Node.new(method.to_s)
node.content = args.first
end
end
end
end
16 changes: 16 additions & 0 deletions test/xml/test_document.rb
Expand Up @@ -47,6 +47,22 @@ def test_new
assert doc.xml?
assert_nil doc.root
end

def test_set_root
doc = nil
assert_nothing_raised {
doc = Nokogiri::XML::Document.new
}
assert doc
assert doc.xml?
assert_nil doc.root
node = Nokogiri::XML::Node.new("b") { |n|
n.content = 'hello world'
}
assert_equal('hello world', node.content)
doc.root = node
assert_equal(node, doc.root)
end
end
end
end

0 comments on commit d18f1ac

Please sign in to comment.