Skip to content

Commit

Permalink
Added appendTo and prependTo with tests #641
Browse files Browse the repository at this point in the history
  • Loading branch information
digihaven authored and Delgan committed Jan 3, 2016
1 parent b27bed6 commit ce8829d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/api/manipulation.js
Expand Up @@ -102,6 +102,26 @@ var uniqueSplice = function(array, spliceIdx, spliceCount, newElems, parent) {
return array.splice.apply(array, spliceArgs);
};

exports.appendTo = function(target) {
if (typeof target === 'string') {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}

target.append(this);

return this;
};

exports.prependTo = function(target) {
if (typeof target === 'string') {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}

target.prepend(this);

return this;
};

exports.append = _insert(function(dom, children, parent) {
uniqueSplice(children, children.length, 0, dom, parent);
});
Expand Down
18 changes: 18 additions & 0 deletions test/api/manipulation.js
Expand Up @@ -496,6 +496,24 @@ describe('$(...)', function() {
});
});

describe('.appendTo', function() {

it('(Node) : should add element as last child', function() {
$('<li class="plum">Plum</li>').appendTo($fruits);
expect($fruits.children().eq(3).hasClass('plum')).to.be.ok();
});

});

describe('.prependTo', function() {

it('(html) : should add element as first child', function() {
$('<li class="plum">Plum</li>').prependTo($fruits);
expect($fruits.children().eq(0).hasClass('plum')).to.be.ok();
});

});

describe('.after', function() {

it('() : should do nothing', function() {
Expand Down

0 comments on commit ce8829d

Please sign in to comment.