Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

I am add CDATA to libxmljs #124

Merged
merged 1 commit into from

4 participants

@dudochkin-victor

Similarly, we can add support for text boxes and comments.

@defunctzombie

Why are you strdup(ing) here? I do not think this is necessary.

@defunctzombie
Collaborator

This doesn't need to be on the readme, if we accept this pull request we should add the example/docs to the wiki.

@defunctzombie
Collaborator

Please add tests for this.

@polotek
Owner

@dudochkin-victor we will likely do another release soon. If you'd like this to go in, please add tests. Thanks.

@ncb000gt ncb000gt referenced this pull request from a commit
@ncb000gt ncb000gt Small check to make sure cdata is added and represented in the string…
… form for GH-124.

Signed-off-by: Nick Campbell <nicholas.j.campbell@gmail.com>
09d5598
@ncb000gt ncb000gt merged commit 3c9c8b9 into polotek:master
@ncb000gt ncb000gt referenced this pull request
Closed

Can not use cdata #135

@znerol znerol referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 26, 2012
  1. @dudochkin-victor

    CDATA Patch

    dudochkin-victor authored
This page is out of date. Refresh to see the latest.
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();
Something went wrong with that request. Please try again.