Skip to content
Browse files

CDATA Patch

  • Loading branch information...
1 parent 47d4c88 commit 3c9c8b9f9325f746a1db9aba562bd0ba02477a37 @dudochkin-victor dudochkin-victor committed Mar 26, 2012
Showing with 59 additions and 0 deletions.
  1. +19 −0 README.md
  2. +8 −0 lib/element.js
  3. +30 −0 src/xml_element.cc
  4. +2 −0 src/xml_element.h
View
19 README.md
@@ -24,6 +24,25 @@ LibXML bindings for [node.js](http://nodejs.org/)
console.log(child.attr('foo').value()); // prints "bar"
+CDATA Example by Dudochkin Victor <blacksmith@gogoo.ru>:
+
+ var util = require('util');
+ var libxml = require('libxmljs');
+
+ var doc = libxml.Document();
+ var elem = doc.node('name1');
+ var newChild = libxml.Element(doc, 'new-child');
+ elem.addChild(newChild);
+
+
+ var child1 = elem.node('child1');
+ var child2 = elem.node('child2', 'second');
+ var newChild = libxml.Element(doc, 'new-child');
+ var name2 = elem.node('name2');
+ name2.addChild(newChild);
+ child2.cdata('<h1>cdata test</h1>').cdata('<p>It\'s worked</p>').cdata('<hr/>All done');
+ util.puts('Document With CDATA: ' + doc.toString());
+
## Basics
* Docs - [http://github.com/polotek/libxmljs/wiki](http://github.com/polotek/libxmljs/wiki)
View
8 lib/element.js
@@ -55,6 +55,14 @@ Element.prototype.node = function(name, content) {
return elem;
};
+/// helper method to attach a cdata to this element
+/// @param name the element name
+/// @param {String} [content] element content
+Element.prototype.cdata = function(content) {
+ this.addCData(content);
+ return this;
+};
+
Element.prototype.get = function() {
return this.find.apply(this, arguments)[0];
};
View
30 src/xml_element.cc
@@ -119,6 +119,27 @@ XmlElement::AddChild(const v8::Arguments& args) {
}
v8::Handle<v8::Value>
+XmlElement::AddCData(const v8::Arguments& args) {
+ v8::HandleScope scope;
+ XmlElement* element = ObjectWrap::Unwrap<XmlElement>(args.Holder());
+ assert(element);
+
+ v8::String::Utf8Value content_(args[0]->ToString());
+ char* content = NULL;
+ content = strdup(*content_);
+
+ xmlNode* elem = xmlNewCDataBlock(element->xml_obj->doc,
+ (const xmlChar*)content,
+ xmlStrlen((const xmlChar*)content));
+
+ if(content)
+ free(content);
+
+ element->add_cdata(elem);
+ return scope.Close(args.Holder());
+}
+
+v8::Handle<v8::Value>
XmlElement::Find(const v8::Arguments& args) {
v8::HandleScope scope;
XmlElement* element = ObjectWrap::Unwrap<XmlElement>(args.Holder());
@@ -308,6 +329,12 @@ XmlElement::add_child(XmlElement* child) {
xmlAddChild(xml_obj, child->xml_obj);
}
+void
+XmlElement::add_cdata(xmlNode* cdata) {
+ xmlAddChild(xml_obj, cdata);
+}
+
+
v8::Handle<v8::Value>
XmlElement::get_child(int32_t idx) {
v8::HandleScope scope;
@@ -478,6 +505,9 @@ XmlElement::Initialize(v8::Handle<v8::Object> target)
XmlElement::AddChild);
NODE_SET_PROTOTYPE_METHOD(constructor_template,
+ "addCData",
+ XmlElement::AddCData);
+ NODE_SET_PROTOTYPE_METHOD(constructor_template,
"_attr",
XmlElement::Attr);
View
2 src/xml_element.h
@@ -31,6 +31,7 @@ class XmlElement : public XmlNode {
static v8::Handle<v8::Value> Child(const v8::Arguments& args);
static v8::Handle<v8::Value> ChildNodes(const v8::Arguments& args);
static v8::Handle<v8::Value> AddChild(const v8::Arguments& args);
+ static v8::Handle<v8::Value> AddCData(const v8::Arguments& args);
static v8::Handle<v8::Value> NextElement(const v8::Arguments& args);
static v8::Handle<v8::Value> PrevElement(const v8::Arguments& args);
static v8::Handle<v8::Value> AddPrevSibling(const v8::Arguments& args);
@@ -46,6 +47,7 @@ class XmlElement : public XmlNode {
v8::Handle<v8::Value> get_attrs();
void set_attr(const char* name, const char* value);
void add_child(XmlElement* child);
+ void add_cdata(xmlNode* cdata);
void set_content(const char* content);
v8::Handle<v8::Value> get_content();
v8::Handle<v8::Value> get_next_element();

0 comments on commit 3c9c8b9

Please sign in to comment.
Something went wrong with that request. Please try again.