[Pkg-javascript-commits] [ltx] 361/469: kinda clone attrs obj argument

Jonas Smedegaard dr at jones.dk
Wed Aug 31 13:03:26 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 e67e7eff22a6d0edcf80a5154ecaf756f1210366
Author: Sonny Piers <sonny at fastmail.net>
Date:   Wed Dec 3 16:52:42 2014 +0100

    kinda clone attrs obj argument
---
 lib/element.js       | 17 +++++++++++------
 ltx-browser.js       | 17 +++++++++++------
 test/element-test.js | 15 +++++++++++++--
 3 files changed, 35 insertions(+), 14 deletions(-)

diff --git a/lib/element.js b/lib/element.js
index 0c77dc1..befaccd 100644
--- a/lib/element.js
+++ b/lib/element.js
@@ -9,8 +9,8 @@
 function Element(name, attrs) {
     this.name = name
     this.parent = null
-    this.attrs = attrs || {}
     this.children = []
+    this.setAttrs(attrs);
 }
 
 /*** Accessors ***/
@@ -81,6 +81,15 @@ Element.prototype.getXmlns = function() {
     return namespaces
 }
 
+Element.prototype.setAttrs = function(attrs) {
+    this.attrs = {}
+    if (attrs) {
+        for (var i in attrs) {
+            if (attrs.hasOwnProperty(i))
+                this.attrs[i] = attrs[i]
+        }
+    }
+};
 
 /**
  * xmlns can be null, returns the matching attribute.
@@ -262,11 +271,7 @@ Element.prototype.remove = function(el, xmlns) {
  * doing. Building XML with ltx is easy!
  */
 Element.prototype.clone = function() {
-    var clone = this._getElement(this.name, {})
-    for (var k in this.attrs) {
-        if (this.attrs.hasOwnProperty(k))
-            clone.attrs[k] = this.attrs[k]
-    }
+    var clone = this._getElement(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)
diff --git a/ltx-browser.js b/ltx-browser.js
index f6e7d69..2b954c8 100644
--- a/ltx-browser.js
+++ b/ltx-browser.js
@@ -122,8 +122,8 @@ module.exports = DOMElement
 function Element(name, attrs) {
     this.name = name
     this.parent = null
-    this.attrs = attrs || {}
     this.children = []
+    this.setAttrs(attrs);
 }
 
 /*** Accessors ***/
@@ -194,6 +194,15 @@ Element.prototype.getXmlns = function() {
     return namespaces
 }
 
+Element.prototype.setAttrs = function(attrs) {
+    this.attrs = {}
+    if (attrs) {
+        for (var i in attrs) {
+            if (attrs.hasOwnProperty(i))
+                this.attrs[i] = attrs[i]
+        }
+    }
+};
 
 /**
  * xmlns can be null, returns the matching attribute.
@@ -375,11 +384,7 @@ Element.prototype.remove = function(el, xmlns) {
  * doing. Building XML with ltx is easy!
  */
 Element.prototype.clone = function() {
-    var clone = this._getElement(this.name, {})
-    for (var k in this.attrs) {
-        if (this.attrs.hasOwnProperty(k))
-            clone.attrs[k] = this.attrs[k]
-    }
+    var clone = this._getElement(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)
diff --git a/test/element-test.js b/test/element-test.js
index 7647d4b..cfc74f9 100644
--- a/test/element-test.js
+++ b/test/element-test.js
@@ -5,6 +5,17 @@ var vows = require('vows')
   , ltx = require('./../lib/index')
 
 vows.describe('ltx').addBatch({
+    'new element': {
+        'doesn\'t reference original attrs object': function() {
+            var o = {'foo': 'bar'}
+            var e = new ltx.Element('e', o)
+            assert.notEqual(e.attrs, o)
+            e.attrs.bar = 'foo'
+            assert.equal(o.bar, undefined)
+            o.foobar = 'barfoo'
+            assert.equal(e.attrs.foobar, undefined)
+        }
+    },
     'serialization': {
         'serialize an element': function() {
             var e = new ltx.Element('e')
@@ -154,7 +165,7 @@ vows.describe('ltx').addBatch({
             assert.equal(orig.getChildText('content'), 'foo')
             assert.equal(clone.children[0].name, 'description')
             assert.equal(clone.getChildText('description'), 'foobar')
-        }
+        },
     },
     'children': {
         'getChildren': function() {
@@ -165,7 +176,7 @@ vows.describe('ltx').addBatch({
             .c('c').t('cbar').up()
             .t('bar')
             .root()
-            
+
             var children = el.children
             assert.equal(children.length, 4)
             assert.equal(children[0].name, 'b')

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