diff --git a/benchmarks/ltx.js b/benchmarks/ltx.js index b0c436e5..885057a6 100644 --- a/benchmarks/ltx.js +++ b/benchmarks/ltx.js @@ -56,10 +56,6 @@ suite.add('parse', function () { parse(XML) }) -suite.add('serialize', function () { - el.toString() -}) - suite.add('createElement (jsx)', function () { createElement( 'message', {to: 'foo@bar', from: 'bar@foo', type: 'chat', id: 'foobar'}, @@ -67,6 +63,10 @@ suite.add('createElement (jsx)', function () { ) }) +suite.add('serialize', function () { + el.toString() +}) + suite.add('clone', function () { el.clone() }) diff --git a/lib/Element.js b/lib/Element.js index f5bfbfaf..73f6b196 100644 --- a/lib/Element.js +++ b/lib/Element.js @@ -10,8 +10,10 @@ var nameEqual = equality.name var attrsEqual = equality.attrs var childrenEqual = equality.children +var clone = require('./clone') + /** - * This cheap replica of DOM/Builder puts me to shame :-) + * Element * * Attributes are in the element.attrs object. Children is a list of * either other Elements or Strings for text content. @@ -283,18 +285,8 @@ 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 this.constructor(this.name, this.attrs) - for (var i = 0; i < this.children.length; i++) { - var child = this.children[i] - clone.cnode(child.clone ? child.clone() : child) - } - return clone + return clone(this) } Element.prototype.text = function (val) { diff --git a/lib/clone.js b/lib/clone.js new file mode 100644 index 00000000..26bbf9a6 --- /dev/null +++ b/lib/clone.js @@ -0,0 +1,10 @@ +'use strict' + +module.exports = function clone (el) { + var clone = new el.constructor(el.name, el.attrs) + for (var i = 0; i < el.children.length; i++) { + var child = el.children[i] + clone.cnode(child.clone ? child.clone() : child) + } + return clone +}