Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make clone a standalone method #89

Merged
merged 1 commit into from
Mar 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}