From 43520f2d4f4c412572bddc762050ae210c0f32ae Mon Sep 17 00:00:00 2001 From: "Alexey.Kupriyanenko" Date: Mon, 5 May 2014 19:35:26 +0300 Subject: [PATCH] handle specific casese with appending --- src/manipulation.js | 20 ++++++++++++++------ test/manipulation.js | 12 ++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index 950d481..ce7f0e1 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -19,13 +19,25 @@ fn.append = function(appended) { length = this.length, setter; + // create jBone object and then append if (isString(appended) && rquickExpr.exec(appended)) { appended = jBone(appended); - } else if (!isObject(appended)) { + } + // create text node for inserting + else if (!isObject(appended)) { appended = document.createTextNode(appended); } - if (appended instanceof jBone) { + // just append NodeElement + if (appended instanceof Node) { + setter = function(el) { + el.appendChild(appended); + }; + } + // wrap object by jBone, and then append + else { + appended = appended instanceof jBone ? appended : jBone(appended); + setter = function(el, i) { appended.forEach(function(node) { if (i) { @@ -35,10 +47,6 @@ fn.append = function(appended) { } }); }; - } else if (appended instanceof Node) { - setter = function(el) { - el.appendChild(appended); - }; } for (; i < length; i++) { diff --git a/test/manipulation.js b/test/manipulation.js index 0a9bd7f..2caed4a 100644 --- a/test/manipulation.js +++ b/test/manipulation.js @@ -82,6 +82,18 @@ describe('jBone Manipulation', function() { expect(a.find('span')).to.have.length(2); }); + it('append(NodeList)', function() { + var a = jBone('
'), + f = document.createDocumentFragment(); + + f.appendChild(document.createElement('span')); + f.appendChild(document.createElement('span')); + + a.append(f.childNodes); + + expect(a.find('span')).to.have.length(2); + }); + it('append(text)', function() { var a = jBone('
');