Skip to content

Commit

Permalink
CDATA Patch
Browse files Browse the repository at this point in the history
  • Loading branch information
dudochkin-victor committed Mar 26, 2012
1 parent 47d4c88 commit 3c9c8b9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>:

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)
Expand Down
8 changes: 8 additions & 0 deletions lib/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
Expand Down
30 changes: 30 additions & 0 deletions src/xml_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,27 @@ XmlElement::AddChild(const v8::Arguments& args) {
return scope.Close(args.Holder());
}

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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -477,6 +504,9 @@ XmlElement::Initialize(v8::Handle<v8::Object> target)
"addChild",
XmlElement::AddChild);

NODE_SET_PROTOTYPE_METHOD(constructor_template,
"addCData",
XmlElement::AddCData);
NODE_SET_PROTOTYPE_METHOD(constructor_template,
"_attr",
XmlElement::Attr);
Expand Down
2 changes: 2 additions & 0 deletions src/xml_element.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 3c9c8b9

Please sign in to comment.