Skip to content

Commit

Permalink
adding initialize to all nodes. closes #56
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 14514e7dbef152ded45dc050ba3a671102a052a0
Author: Aaron Patterson <[email protected]>
Date:   Sat May 16 19:18:40 2009 -0700

    fixing up attribute initialize

commit 2969620eb170cb59e7268ac442d87458baca9585
Author: Aaron Patterson <[email protected]>
Date:   Sat May 16 19:16:45 2009 -0700

    fixing xml comment initialize

commit ed4affcfc5df76c08f387ec599dbf0b1ddd35385
Author: Aaron Patterson <[email protected]>
Date:   Sat May 16 19:13:12 2009 -0700

    fixing entity reference initialize

commit 87706dd8afd95da85dca865f11cd9ccb06b7b761
Author: Aaron Patterson <[email protected]>
Date:   Sat May 16 19:11:07 2009 -0700

    adding initialize for processing instruction

commit e02d099eb71abae7ac99576200036ece841cb046
Author: Aaron Patterson <[email protected]>
Date:   Sat May 16 19:05:42 2009 -0700

    getting document fragment working

commit eed074ec01a300169703e67326fc06de4c15ef8a
Author: Aaron Patterson <[email protected]>
Date:   Sat May 16 19:02:03 2009 -0700

    fixing text node initialize

commit 84cfbed5eaff63b378ccdac267f3c2cd677fa502
Author: Aaron Patterson <[email protected]>
Date:   Sat May 16 19:00:28 2009 -0700

    starting work on initialize method
  • Loading branch information
tenderlove committed May 17, 2009
1 parent ad5effa commit 9f904ba
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 22 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ lib/nokogiri/xml/node.rb
lib/nokogiri/xml/node/save_options.rb
lib/nokogiri/xml/node_set.rb
lib/nokogiri/xml/notation.rb
lib/nokogiri/xml/processing_instruction.rb
lib/nokogiri/xml/reader.rb
lib/nokogiri/xml/relax_ng.rb
lib/nokogiri/xml/sax.rb
Expand Down
15 changes: 11 additions & 4 deletions ext/nokogiri/xml_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,20 @@ static VALUE set_value(VALUE self, VALUE content)

/*
* call-seq:
* new(document, content)
* new(document, name)
*
* Create a new Attr element on the +document+ with +name+
*/
static VALUE new(VALUE klass, VALUE doc, VALUE name)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr xml_doc;
Data_Get_Struct(doc, xmlDoc, xml_doc);
VALUE document;
VALUE name;
VALUE rest;

rb_scan_args(argc, argv, "2*", &document, &name, &rest);

Data_Get_Struct(document, xmlDoc, xml_doc);

xmlAttrPtr node = xmlNewDocProp(
xml_doc,
Expand All @@ -60,6 +66,7 @@ static VALUE new(VALUE klass, VALUE doc, VALUE name)
NOKOGIRI_ROOT_NODE((xmlNodePtr)node);

VALUE rb_node = Nokogiri_wrap_xml_node(klass, (xmlNodePtr)node);
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

if(rb_block_given_p()) rb_yield(rb_node);

Expand All @@ -80,6 +87,6 @@ void init_xml_attr()

cNokogiriXmlAttr = klass;

rb_define_singleton_method(klass, "new", new, 2);
rb_define_singleton_method(klass, "new", new, -1);
rb_define_method(klass, "value=", set_value, 1);
}
11 changes: 9 additions & 2 deletions ext/nokogiri/xml_cdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
*
* Create a new CData element on the +document+ with +content+
*/
static VALUE new(VALUE klass, VALUE doc, VALUE content)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr xml_doc;
VALUE doc;
VALUE content;
VALUE rest;

rb_scan_args(argc, argv, "2*", &doc, &content, &rest);

Data_Get_Struct(doc, xmlDoc, xml_doc);

xmlNodePtr node = xmlNewCDataBlock(
Expand All @@ -20,6 +26,7 @@ static VALUE new(VALUE klass, VALUE doc, VALUE content)
NOKOGIRI_ROOT_NODE(node);

VALUE rb_node = Nokogiri_wrap_xml_node(klass, node);
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

if(rb_block_given_p()) rb_yield(rb_node);

Expand All @@ -42,5 +49,5 @@ void init_xml_cdata()

cNokogiriXmlCData = klass;

rb_define_singleton_method(klass, "new", new, 2);
rb_define_singleton_method(klass, "new", new, -1);
}
13 changes: 10 additions & 3 deletions ext/nokogiri/xml_comment.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@
*
* Create a new Comment element on the +document+ with +content+
*/
static VALUE new(VALUE klass, VALUE doc, VALUE content)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr xml_doc;
Data_Get_Struct(doc, xmlDoc, xml_doc);
VALUE document;
VALUE content;
VALUE rest;

rb_scan_args(argc, argv, "2*", &document, &content, &rest);

Data_Get_Struct(document, xmlDoc, xml_doc);

xmlNodePtr node = xmlNewDocComment(
xml_doc,
(const xmlChar *)StringValuePtr(content)
);

VALUE rb_node = Nokogiri_wrap_xml_node(klass, node);
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

NOKOGIRI_ROOT_NODE(node);

Expand All @@ -40,5 +47,5 @@ void init_xml_comment()

cNokogiriXmlComment = klass;

rb_define_singleton_method(klass, "new", new, 2);
rb_define_singleton_method(klass, "new", new, -1);
}
12 changes: 9 additions & 3 deletions ext/nokogiri/xml_document_fragment.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
*
* Create a new DocumentFragment element on the +document+
*/
static VALUE new(VALUE klass, VALUE doc)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr xml_doc;
Data_Get_Struct(doc, xmlDoc, xml_doc);
VALUE document;
VALUE rest;

rb_scan_args(argc, argv, "1*", &document, &rest);

Data_Get_Struct(document, xmlDoc, xml_doc);

xmlNodePtr node = xmlNewDocFragment(xml_doc->doc);
if(node->doc->children)
Expand All @@ -18,6 +23,7 @@ static VALUE new(VALUE klass, VALUE doc)
NOKOGIRI_ROOT_NODE(node);

VALUE rb_node = Nokogiri_wrap_xml_node(klass, node);
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

if(rb_block_given_p()) rb_yield(rb_node);

Expand All @@ -38,5 +44,5 @@ void init_xml_document_fragment()

cNokogiriXmlDocumentFragment = klass;

rb_define_singleton_method(klass, "new", new, 1);
rb_define_singleton_method(klass, "new", new, -1);
}
13 changes: 10 additions & 3 deletions ext/nokogiri/xml_entity_reference.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
*
* Create a new EntityReference element on the +document+ with +name+
*/
static VALUE new(VALUE klass, VALUE doc, VALUE name)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr xml_doc;
Data_Get_Struct(doc, xmlDoc, xml_doc);
VALUE document;
VALUE name;
VALUE rest;

rb_scan_args(argc, argv, "2*", &document, &name, &rest);

Data_Get_Struct(document, xmlDoc, xml_doc);

xmlNodePtr node = xmlNewReference(
xml_doc,
Expand All @@ -19,6 +25,7 @@ static VALUE new(VALUE klass, VALUE doc, VALUE name)
NOKOGIRI_ROOT_NODE(node);

VALUE rb_node = Nokogiri_wrap_xml_node(klass, node);
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

if(rb_block_given_p()) rb_yield(rb_node);

Expand All @@ -39,5 +46,5 @@ void init_xml_entity_reference()

cNokogiriXmlEntityReference = klass;

rb_define_singleton_method(klass, "new", new, 2);
rb_define_singleton_method(klass, "new", new, -1);
}
10 changes: 8 additions & 2 deletions ext/nokogiri/xml_node.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,14 @@ static VALUE add_namespace(VALUE self, VALUE prefix, VALUE href)
*
* Create a new node with +name+ sharing GC lifecycle with +document+
*/
static VALUE new(VALUE klass, VALUE name, VALUE document)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr doc;
VALUE name;
VALUE document;
VALUE rest;

rb_scan_args(argc, argv, "2*", &name, &document, &rest);

Data_Get_Struct(document, xmlDoc, doc);

Expand All @@ -697,6 +702,7 @@ static VALUE new(VALUE klass, VALUE name, VALUE document)
NOKOGIRI_ROOT_NODE(node);

VALUE rb_node = Nokogiri_wrap_xml_node(klass, node);
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

if(rb_block_given_p()) rb_yield(rb_node);

Expand Down Expand Up @@ -885,7 +891,7 @@ void init_xml_node()
cNokogiriXmlEntityDeclaration =
rb_define_class_under(xml, "EntityDeclaration", klass);

rb_define_singleton_method(klass, "new", new, 2);
rb_define_singleton_method(klass, "new", new, -1);

rb_define_method(klass, "add_namespace", add_namespace, 2);
rb_define_method(klass, "node_name", get_name, 0);
Expand Down
14 changes: 11 additions & 3 deletions ext/nokogiri/xml_processing_instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@
* Create a new ProcessingInstruction element on the +document+ with +name+
* and +content+
*/
static VALUE new(VALUE klass, VALUE doc, VALUE name, VALUE content)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr xml_doc;
Data_Get_Struct(doc, xmlDoc, xml_doc);
VALUE document;
VALUE name;
VALUE content;
VALUE rest;

rb_scan_args(argc, argv, "3*", &document, &name, &content, &rest);

Data_Get_Struct(document, xmlDoc, xml_doc);

xmlNodePtr node = xmlNewDocPI(
xml_doc,
Expand All @@ -21,6 +28,7 @@ static VALUE new(VALUE klass, VALUE doc, VALUE name, VALUE content)
NOKOGIRI_ROOT_NODE(node);

VALUE rb_node = Nokogiri_wrap_xml_node(klass, node);
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

if(rb_block_given_p()) rb_yield(rb_node);

Expand All @@ -42,5 +50,5 @@ void init_xml_processing_instruction()

cNokogiriXmlProcessingInstruction = klass;

rb_define_singleton_method(klass, "new", new, 3);
rb_define_singleton_method(klass, "new", new, -1);
}
11 changes: 9 additions & 2 deletions ext/nokogiri/xml_text.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@
*
* Create a new Text element on the +document+ with +content+
*/
static VALUE new(VALUE klass, VALUE string, VALUE document)
static VALUE new(int argc, VALUE *argv, VALUE klass)
{
xmlDocPtr doc;
VALUE string;
VALUE document;
VALUE rest;

rb_scan_args(argc, argv, "2*", &string, &document, &rest);

Data_Get_Struct(document, xmlDoc, doc);

xmlNodePtr node = xmlNewText((xmlChar *)StringValuePtr(string));
node->doc = doc;

VALUE rb_node = Nokogiri_wrap_xml_node(klass, node) ;
rb_funcall2(rb_node, rb_intern("initialize"), argc, argv);

if(rb_block_given_p()) rb_yield(rb_node);

Expand All @@ -36,5 +43,5 @@ void init_xml_text()

cNokogiriXmlText = klass;

rb_define_singleton_method(klass, "new", new, 2);
rb_define_singleton_method(klass, "new", new, -1);
}
1 change: 1 addition & 0 deletions lib/nokogiri/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'nokogiri/xml/cdata'
require 'nokogiri/xml/document'
require 'nokogiri/xml/document_fragment'
require 'nokogiri/xml/processing_instruction'
require 'nokogiri/xml/node_set'
require 'nokogiri/xml/syntax_error'
require 'nokogiri/xml/xpath'
Expand Down
3 changes: 3 additions & 0 deletions lib/nokogiri/xml/document_fragment.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module Nokogiri
module XML
class DocumentFragment < Nokogiri::XML::Node
def initialize document
end

###
# return the name for DocumentFragment
def name
Expand Down
4 changes: 4 additions & 0 deletions lib/nokogiri/xml/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class Node
# The Document associated with this Node.
attr_accessor :document

def initialize name, document
# ... Ya. This is empty on purpose.
end

###
# Decorate this node with the decorators set up in this node's Document
def decorate!
Expand Down
8 changes: 8 additions & 0 deletions lib/nokogiri/xml/processing_instruction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Nokogiri
module XML
class ProcessingInstruction < Node
def initialize document, name, content
end
end
end
end
27 changes: 27 additions & 0 deletions test/xml/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,33 @@ def test_subclass_#{klass.name.gsub('::', '_')}
end
}
end

{
Nokogiri::XML::CDATA => 'doc, "foo"',
Nokogiri::XML::Attr => 'doc, "foo"',
Nokogiri::XML::Comment => 'doc, "foo"',
Nokogiri::XML::EntityReference => 'doc, "foo"',
Nokogiri::XML::ProcessingInstruction => 'doc, "foo", "bar"',
Nokogiri::XML::DocumentFragment => 'doc',
Nokogiri::XML::Node => '"foo", doc',
Nokogiri::XML::Text => '"foo", doc',
}.each do |klass, constructor|
class_eval <<-eocode, __FILE__, __LINE__ + 1
def test_subclass_initialize_#{klass.name.gsub('::', '_')}
doc = Nokogiri::XML::Document.new
klass = Class.new(#{klass.name}) do
attr_accessor :initialized_with
def initialize *args
@initialized_with = args
end
end
node = klass.new(#{constructor}, 1)
assert_equal [#{constructor}, 1], node.initialized_with
end
eocode
end

def test_subclass_dup
subclass = Class.new(Nokogiri::XML::Node)
node = subclass.new('foo', @xml).dup
Expand Down

0 comments on commit 9f904ba

Please sign in to comment.