[Pkg-javascript-commits] [ltx] 180/469: Element.clone: fixes node-xmpp issue #36
Jonas Smedegaard
dr at jones.dk
Wed Aug 31 13:01:43 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository ltx.
commit fd3582a0d0c9c8557aa0d61a90fc90b909b6e162
Author: Astro <astro at spaceboyz.net>
Date: Fri Oct 28 17:10:16 2011 +0200
Element.clone: fixes node-xmpp issue #36
---
lib/element.js | 19 ++++++++++++++++++-
test/test_element.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 66 insertions(+), 1 deletion(-)
diff --git a/lib/element.js b/lib/element.js
index cd9e8f3..37c2058 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -131,7 +131,7 @@ Element.prototype.cnode = function(child) {
return child;
};
-/** create text node and return element */
+/** add text node and return element */
Element.prototype.t = function(text) {
this.children.push(text);
return this;
@@ -164,6 +164,23 @@ Element.prototype.remove = function(el, xmlns) {
return this;
};
+/**
+ * To use in case you want the same XML data for separate uses.
+ * Please refrain from this practise unless you know what you are
+ * doing. Building XML with ltx is easy!
+ */
+Element.prototype.clone = function() {
+ var clone = new Element(this.name, {});
+ for(var k in this.attrs) {
+ if (this.attrs.hasOwnProperty(k))
+ clone.attrs[k] = this.attrs[k];
+ }
+ this.children.forEach(function(child) {
+ clone.cnode(child.clone ? child.clone() : child);
+ });
+ return clone;
+};
+
/*** Serialization ***/
Element.prototype.toString = function() {
diff --git a/test/test_element.js b/test/test_element.js
index 17de103..7f9a059 100644
--- a/test/test_element.js
+++ b/test/test_element.js
@@ -55,5 +55,53 @@ vows.describe('ltx').addBatch({
assert.equal(el.getChildren('c').length, 0);
assert.equal(el.getChildren('c2').length, 1);
}
+ },
+
+ 'clone': {
+ 'clones': function() {
+ var orig = new ltx.Element('msg', { type: 'get' }).
+ c('content').t('foo').root();
+ var clone = orig.clone();
+ assert.equal(clone.name, orig.name);
+ assert.equal(clone.attrs.type, orig.attrs.type);
+ assert.equal(clone.attrs.to, orig.attrs.to);
+ assert.equal(clone.children.length, orig.children.length);
+ assert.equal(clone.getChildText('content'), orig.getChildText('content'));
+
+ assert.equal(orig.getChild('content').up(), orig);
+ assert.equal(clone.getChild('content').up(), clone);
+ },
+ 'mod attr': function() {
+ var orig = new ltx.Element('msg', { type: 'get' });
+ var clone = orig.clone();
+ clone.attrs.type += '-result';
+
+ assert.equal(orig.attrs.type, 'get');
+ assert.equal(clone.attrs.type, 'get-result');
+ },
+ 'rm attr': function() {
+ var orig = new ltx.Element('msg', { from: 'me' });
+ var clone = orig.clone();
+ delete clone.attrs.from;
+ clone.attrs.to = 'you';
+
+ assert.equal(orig.attrs.from, 'me');
+ assert.equal(orig.attrs.to, undefined);
+ assert.equal(clone.attrs.from, undefined);
+ assert.equal(clone.attrs.to, 'you');
+ },
+ 'mod child': function() {
+ var orig = new ltx.Element('msg', { type: 'get' }).
+ c('content').t('foo').root();
+ var clone = orig.clone();
+ clone.getChild('content').
+ t('bar').
+ name = 'description';
+
+ assert.equal(orig.children[0].name, 'content');
+ assert.equal(orig.getChildText('content'), 'foo');
+ assert.equal(clone.children[0].name, 'description');
+ assert.equal(clone.getChildText('description'), 'foobar');
+ }
}
}).run();
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/ltx.git
More information about the Pkg-javascript-commits
mailing list