diff --git a/Readme.md b/Readme.md index 29dba03c5f..13271d06de 100644 --- a/Readme.md +++ b/Readme.md @@ -586,6 +586,20 @@ $.html() // ``` +#### .appendTo( target ) +Insert every element in the set of matched elements to the end of the target. + +```js +$('
  • Plum
  • ').appendTo('#fruits') +$.html() +//=> +``` + #### .prepend( content, [content, ...] ) Inserts content as the *first* child of each of the selected elements. @@ -600,6 +614,20 @@ $.html() // ``` +#### .prependTo( target ) +Insert every element in the set of matched elements to the beginning of the target. + +```js +$('
  • Plum
  • ').prependTo('#fruits') +$.html() +//=> +``` + #### .after( content, [content, ...] ) Insert content next to each element in the set of matched elements. @@ -614,7 +642,7 @@ $.html() // ``` -#### .insertAfter( content ) +#### .insertAfter( target ) Insert every element in the set of matched elements after the target. ```js @@ -642,7 +670,7 @@ $.html() // ``` -#### .insertBefore( content ) +#### .insertBefore( target ) Insert every element in the set of matched elements before the target. ```js diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index 8947e87e5d..670837d3dc 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -103,23 +103,23 @@ var uniqueSplice = function(array, spliceIdx, spliceCount, newElems, parent) { }; exports.appendTo = function(target) { - if (typeof target === 'string') { - target = this.constructor.call(this.constructor, target, null, this._originalRoot); - } + if (!target.cheerio) { + target = this.constructor.call(this.constructor, target, null, this._originalRoot); + } - target.append(this); + target.append(this); - return this; + return this; }; exports.prependTo = function(target) { - if (typeof target === 'string') { - target = this.constructor.call(this.constructor, target, null, this._originalRoot); - } + if (!target.cheerio) { + target = this.constructor.call(this.constructor, target, null, this._originalRoot); + } - target.prepend(this); + target.prepend(this); - return this; + return this; }; exports.append = _insert(function(dom, children, parent) { diff --git a/test/api/manipulation.js b/test/api/manipulation.js index b38c43f5ee..0d4ba2a68d 100644 --- a/test/api/manipulation.js +++ b/test/api/manipulation.js @@ -498,20 +498,62 @@ describe('$(...)', function() { describe('.appendTo', function() { - it('(Node) : should add element as last child', function() { + it('(html) : should add element as last child', function() { + var $plum = $('
  • Plum
  • ').appendTo(fruits); + expect($plum.parent().children().eq(3).hasClass('plum')).to.be.ok(); + }); + + it('($(...)) : should add element as last child', function() { $('
  • Plum
  • ').appendTo($fruits); expect($fruits.children().eq(3).hasClass('plum')).to.be.ok(); }); + it('(Node) : should add element as last child', function() { + $('
  • Plum
  • ').appendTo($fruits[0]); + expect($fruits.children().eq(3).hasClass('plum')).to.be.ok(); + }); + + it('(selector) : should add element as last child', function() { + $('
  • Plum
  • ').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 = $(''); + $('
  • Plum
  • ').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 = $('
  • Plum
  • ').prependTo(fruits); + expect($plum.parent().children().eq(0).hasClass('plum')).to.be.ok(); + }); + + it('($(...)) : should add element as first child', function() { $('
  • Plum
  • ').prependTo($fruits); expect($fruits.children().eq(0).hasClass('plum')).to.be.ok(); }); + it('(Node) : should add node as first child', function() { + $('
  • Plum
  • ').prependTo($fruits[0]); + expect($fruits.children().eq(0).hasClass('plum')).to.be.ok(); + }); + + it('(selector) : should add element as first child', function() { + $('
  • Plum
  • ').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 = $(''); + $('
  • Plum
  • ').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() { @@ -808,7 +850,7 @@ describe('$(...)', function() { }); it('(Node) : should add element as previous sibling', function() { - var plum = $('
  • Plum
  • '); + var plum = $('
  • Plum
  • ')[0]; $('.apple').before(plum); expect($('.apple').prev().hasClass('plum')).to.be.ok(); });