Skip to content

Commit

Permalink
Merge pull request #89 from node-xmpp/banana-gorilla
Browse files Browse the repository at this point in the history
make clone a standalone method
  • Loading branch information
sonnyp committed Mar 31, 2016
2 parents 1072a17 + 85830df commit 6ccfc52
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
8 changes: 4 additions & 4 deletions benchmarks/ltx.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ 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'},
createElement('body', null, 'Where there is love there is life.')
)
})

suite.add('serialize', function () {
el.toString()
})

suite.add('clone', function () {
el.clone()
})
Expand Down
16 changes: 4 additions & 12 deletions lib/Element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions lib/clone.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6ccfc52

Please sign in to comment.