Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

* Add ability to insert new PI-nodes into the xmltree (Axel Struebing).

  • Loading branch information...
commit 2374a0971c6f67cde0279323db8b469e6f233a2c 1 parent c1f6406
Charlie Savage authored
4 HISTORY
View
@@ -1,5 +1,9 @@
= Release History
+== 2.2.3 / 2012-??-?? Charlie Savage
+
+* Add ability to insert new PI-nodes into the xmltree (Axel Struebing).
+
== 2.2.2 / 2011-08-29 Charlie Savage
* ++API CHANGE+++
1  MANIFEST
View
@@ -138,6 +138,7 @@ test/tc_node_cdata.rb
test/tc_node_comment.rb
test/tc_node_copy.rb
test/tc_node_edit.rb
+test/tc_node_pi.rb
test/tc_node_text.rb
test/tc_node_write.rb
test/tc_node_xlink.rb
38 ext/libxml/ruby_xml_node.c
View
@@ -190,6 +190,43 @@ static VALUE rxml_node_new_comment(int argc, VALUE *argv, VALUE klass)
/*
* call-seq:
+ * XML::Node.new_pi(name, content = nil) -> XML::Node
+ *
+ * Create a new pi node, optionally setting
+ * the node's content.
+ *
+ */
+static VALUE rxml_node_new_pi(int argc, VALUE *argv, VALUE klass)
+{
+ VALUE name = Qnil;
+ VALUE content = Qnil;
+ xmlNodePtr xnode;
+
+ rb_scan_args(argc, argv, "11", &name, &content);
+
+ if (NIL_P(name))
+ {
+ rb_raise(rb_eRuntimeError, "You must provide me with a name for a PI.");
+ }
+ name = rb_obj_as_string(name);
+ if (NIL_P(content))
+ {
+ xnode = xmlNewPI((xmlChar*) StringValuePtr(name), NULL);
+ }
+ else
+ {
+ content = rb_obj_as_string(content);
+ xnode = xmlNewPI((xmlChar*) StringValuePtr(name), (xmlChar*) StringValueCStr(content));
+ }
+
+ if (xnode == NULL)
+ rxml_raise(&xmlLastError);
+
+ return rxml_node_wrap(xnode);
+}
+
+/*
+ * call-seq:
* XML::Node.new_text(content) -> XML::Node
*
* Create a new text node.
@@ -1342,6 +1379,7 @@ void rxml_init_node(void)
rb_define_singleton_method(cXMLNode, "new_cdata", rxml_node_new_cdata, -1);
rb_define_singleton_method(cXMLNode, "new_comment", rxml_node_new_comment, -1);
+ rb_define_singleton_method(cXMLNode, "new_pi", rxml_node_new_pi, -1);
rb_define_singleton_method(cXMLNode, "new_text", rxml_node_new_text, 1);
/* Initialization */
40 test/tc_node_pi.rb
View
@@ -0,0 +1,40 @@
+# encoding: UTF-8
+
+require './test_helper'
+
+require 'test/unit'
+
+class NodeCommentTest < Test::Unit::TestCase
+ def setup
+ xp = XML::Parser.string('<root></root>')
+ @doc = xp.parse
+ assert_instance_of(XML::Document, @doc)
+ @root = @doc.root
+ end
+
+ def test_libxml_node_add_pi_01
+ @root << XML::Node.new_pi('mypi')
+ assert_equal '<root><?mypi?></root>',
+ @root.to_s.gsub(/\n\s*/,'')
+ end
+
+ def test_libxml_node_add_pi_02
+ @root << XML::Node.new_pi('mypi')
+ assert_equal 'pi',
+ @root.child.node_type_name
+ end
+
+ def test_libxml_node_add_pi_03
+ @root << el = XML::Node.new_pi('mypi')
+ el << "_this_is_added"
+ assert_equal '<root><?mypi _this_is_added?></root>',
+ @root.to_s.gsub(/\n\s*/,'')
+ end
+
+ def test_libxml_node_add_pi_04
+ @root << XML::Node.new_pi('mypi','mycontent')
+ assert_equal '<root><?mypi mycontent?></root>',
+ @root.to_s.gsub(/\n\s*/,'')
+ end
+
+end
1  test/test_suite.rb
View
@@ -22,6 +22,7 @@
require './tc_node_comment'
require './tc_node_copy'
require './tc_node_edit'
+require './tc_node_pi'
require './tc_node_text'
require './tc_node_write'
require './tc_node_xlink'
Please sign in to comment.
Something went wrong with that request. Please try again.