[Pkg-javascript-commits] [ltx] 443/469: make clone a standalone method

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03:36 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 85830df88172241ac0476a1233609d2b6a87c0d6
Author: Sonny Piers <sonny at fastmail.net>
Date:   Thu Mar 31 16:23:27 2016 +0200

    make clone a standalone method
---
 benchmarks/ltx.js |  8 ++++----
 lib/Element.js    | 16 ++++------------
 lib/clone.js      | 10 ++++++++++
 3 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/benchmarks/ltx.js b/benchmarks/ltx.js
index b0c436e..885057a 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 at bar', from: 'bar at 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 f5bfbfa..73f6b19 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 0000000..26bbf9a
--- /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
+}

-- 
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