Skip to content

Commit

Permalink
Merge branch 'azuisleet'
Browse files Browse the repository at this point in the history
  • Loading branch information
polotek committed Aug 8, 2011
2 parents 9987f2f + 4661cf2 commit 423a6e3
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
6 changes: 5 additions & 1 deletion SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ env.Append(
CCFLAGS = cflags
)

# explicitly link against node under cygwin
if env['PLATFORM'] == 'cygwin':
libs += ['node']

if not env.GetOption('clean'):
conf = Configure(env, custom_tests = {'CheckForNodeJS' : CheckForNodeJS})
print conf.CheckForNodeJS()
Expand All @@ -81,7 +85,7 @@ libxmljs = env.Program(
target = 'libxmljs',
source = cc_sources,
CCFLAGS = cflags,
LIBS = libs + ['v8'],
LIBS = libs,
LIBPATH = libpath
)

Expand Down
17 changes: 9 additions & 8 deletions spec/spec_document.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ describe('A new document', function() {
assert.equal(doc1.toString(), doc2.toString());
});

it('can haz cloned node', function() {
it('can add a cloned node', function() {
var gchild_string = '<grandchild from="julie numar">with love</grandchild>';
var doc1_string = [
'<?xml version="1.0" encoding="UTF-8"?>',
Expand All @@ -152,22 +152,23 @@ describe('A new document', function() {

var doc2_string = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<root><child to="wongfoo"></child><sibling>with content!</sibling></root>',
'<root><child to="wongfoo"/><sibling>with content!</sibling></root>',
''
].join("\n");

var doc1 = libxml.parseXmlString(doc1_string);
var doc2 = libxml.parseXmlString(doc2_string);
doc2.child(0).addChild(doc1.child(0).child(0)); // add gchild to doc 2

var gchild = doc1.child(0).child(0); //the element to operate on

doc2.child(0).addChild(gchild); // add gchild clone to doc2, implicit clone

assert.equal(doc1.toString(), doc2.toString());; // both documents should be the same
assert.equal(doc1.toString(), doc2.toString()); // both documents should be the same

var gchild = doc1.child(0).child(0); //the removed element
assert.equal(gchild, doc2.child(0).child(0), true);
assert. notEqual(gchild, doc2.child(0).child(0)); // these nodes should be different (cloned)

gchild.remove();
// assert.equal(gchild_string, doc2.child(0).child(0).toString()); // doc2 should have gchild


assert.equal(doc2_string, doc1.toString()); //doc1 should be the same as doc2 str

assert.equal(doc1_string, doc2.toString()); //doc2 should be the same as doc1 str
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('An element node', function() {
assert. notEqual(doc, newdoc, true);
assert.equal('child1', newdoc.root().childNodes()[0].name());
gc();
assert. notEqual(child1, elem.childNodes()[0]);
assert.equal(child1, elem.childNodes()[0]); // child1 is the the first child of elem
});
});

Expand Down
9 changes: 4 additions & 5 deletions src/xml_element.cc
Original file line number Diff line number Diff line change
Expand Up @@ -473,16 +473,15 @@ XmlElement::add_next_sibling(XmlElement* element) {
XmlElement *
XmlElement::import_element(XmlElement *element) {
xmlNode *new_child;
if(element->xml_obj->type == XML_ELEMENT_NODE || xml_obj->doc == element->xml_obj->doc) {
return element;

if (xml_obj->doc == element->xml_obj->doc) {
return element;
} else {
new_child = xmlDocCopyNode(element->xml_obj, xml_obj->doc, 1);
if(new_child == NULL) {
return NULL;
}

element->remove();


UpdateV8Memory();

v8::Handle<v8::Object> obj =
Expand Down
29 changes: 27 additions & 2 deletions src/xml_node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ XmlNode::Remove(const v8::Arguments& args) {
return scope.Close(args.This());
}

v8::Handle<v8::Value>
XmlNode::Clone(const v8::Arguments& args) {
v8::HandleScope scope;
XmlNode *node = LibXmlObj::Unwrap<XmlNode>(args.This());
assert(node);

bool recurse = true;

if (args.Length() == 1 && args[0]->IsBoolean())
recurse = args[0]->ToBoolean()->BooleanValue();

return scope.Close(node->clone(recurse));
}

XmlNode::XmlNode(xmlNode* node) : xml_obj(node) {
xml_obj->_private = this;
if(xml_obj->doc) {
Expand Down Expand Up @@ -219,6 +233,13 @@ XmlNode::get_next_sibling() {
return v8::Null();
}

v8::Handle<v8::Value>
XmlNode::clone(bool recurse) {
v8::HandleScope scope;

return scope.Close(LibXmlObj::GetMaybeBuild<XmlElement, xmlNode>(xmlCopyNode(xml_obj, recurse)));
}

v8::Handle<v8::Value>
XmlNode::to_string() {
v8::HandleScope scope;
Expand Down Expand Up @@ -247,7 +268,7 @@ XmlNode::to_string() {
return v8::Null();
}
}

void
XmlNode::remove() {
xmlUnlinkNode(xml_obj);
Expand Down Expand Up @@ -337,7 +358,11 @@ XmlNode::Initialize(v8::Handle<v8::Object> target) {
LXJS_SET_PROTO_METHOD(constructor_template,
"remove",
XmlNode::Remove);


LXJS_SET_PROTO_METHOD(constructor_template,
"clone",
XmlNode::Clone);

LXJS_SET_PROTO_METHOD(constructor_template,
"toString",
XmlNode::ToString);
Expand Down
4 changes: 3 additions & 1 deletion src/xml_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class XmlNode : public LibXmlObj {
static v8::Handle<v8::Value> Type(const v8::Arguments& args);
static v8::Handle<v8::Value> ToString(const v8::Arguments& args);
static v8::Handle<v8::Value> Remove(const v8::Arguments& args);

static v8::Handle<v8::Value> Clone(const v8::Arguments& args);

v8::Persistent<v8::Value> doc;

v8::Handle<v8::Value> get_doc();
Expand All @@ -38,6 +39,7 @@ class XmlNode : public LibXmlObj {
v8::Handle<v8::Value> get_parent();
v8::Handle<v8::Value> get_prev_sibling();
v8::Handle<v8::Value> get_next_sibling();
v8::Handle<v8::Value> clone(bool recurse);
v8::Handle<v8::Value> get_type();
v8::Handle<v8::Value> to_string();
void remove();
Expand Down

0 comments on commit 423a6e3

Please sign in to comment.