Skip to content

Commit

Permalink
Merge pull request #797 from Delgan/641-appendTo_prependTo
Browse files Browse the repository at this point in the history
Fix PR #726 adding `appendTo()` and `prependTo()`
  • Loading branch information
fb55 committed Feb 1, 2016
2 parents 8c9b2e0 + b09db31 commit 1b9c5c9
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
32 changes: 30 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,20 @@ $.html()
// </ul>
```

#### .appendTo( target )
Insert every element in the set of matched elements to the end of the target.

```js
$('<li class="plum">Plum</li>').appendTo('#fruits')
$.html()
//=> <ul id="fruits">
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// <li class="plum">Plum</li>
// </ul>
```

#### .prepend( content, [content, ...] )
Inserts content as the *first* child of each of the selected elements.

Expand All @@ -600,6 +614,20 @@ $.html()
// </ul>
```

#### .prependTo( target )
Insert every element in the set of matched elements to the beginning of the target.

```js
$('<li class="plum">Plum</li>').prependTo('#fruits')
$.html()
//=> <ul id="fruits">
// <li class="plum">Plum</li>
// <li class="apple">Apple</li>
// <li class="orange">Orange</li>
// <li class="pear">Pear</li>
// </ul>
```

#### .after( content, [content, ...] )
Insert content next to each element in the set of matched elements.

Expand All @@ -614,7 +642,7 @@ $.html()
// </ul>
```

#### .insertAfter( content )
#### .insertAfter( target )
Insert every element in the set of matched elements after the target.

```js
Expand Down Expand Up @@ -642,7 +670,7 @@ $.html()
// </ul>
```

#### .insertBefore( content )
#### .insertBefore( target )
Insert every element in the set of matched elements before the target.

```js
Expand Down
20 changes: 20 additions & 0 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
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 (!target.cheerio) {
target = this.constructor.call(this.constructor, target, null, this._originalRoot);
}

target.append(this);

return this;
};

exports.prependTo = function(target) {
if (!target.cheerio) {
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
62 changes: 61 additions & 1 deletion test/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,66 @@ describe('$(...)', function() {
});
});

describe('.appendTo', function() {

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

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

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

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

it('(Array) : should add element as last child of all elements in the array', function() {
var $multiple = $('<ul><li>Apple</li></ul><ul><li>Orange</li></ul>');
$('<li class="plum">Plum</li>').appendTo($multiple.get());
expect($multiple.first().children().eq(1).hasClass('plum')).to.be.ok();
expect($multiple.last().children().eq(1).hasClass('plum')).to.be.ok();
});
});

describe('.prependTo', function() {

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

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

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

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

it('(Array) : should add element as first child of all elements in the array', function() {
var $multiple = $('<ul><li>Apple</li></ul><ul><li>Orange</li></ul>');
$('<li class="plum">Plum</li>').prependTo($multiple.get());
expect($multiple.first().children().eq(0).hasClass('plum')).to.be.ok();
expect($multiple.last().children().eq(0).hasClass('plum')).to.be.ok();
});
});

describe('.after', function() {

it('() : should do nothing', function() {
Expand Down Expand Up @@ -790,7 +850,7 @@ describe('$(...)', function() {
});

it('(Node) : should add element as previous sibling', function() {
var plum = $('<li class="plum">Plum</li>');
var plum = $('<li class="plum">Plum</li>')[0];
$('.apple').before(plum);
expect($('.apple').prev().hasClass('plum')).to.be.ok();
});
Expand Down

0 comments on commit 1b9c5c9

Please sign in to comment.