From f0dc4d2cbb20d70d15d29bcf91bee718c92eda35 Mon Sep 17 00:00:00 2001 From: Mike Pennisi Date: Wed, 12 Mar 2014 20:50:46 -0400 Subject: [PATCH] Deprecate `$.fn.toArray` Because `$.fn.get` is more powerful than `toArray` and because `toArray` is not implemented by jQuery, the `toArray` method is no longer necessary. Deprecate it so it may be more safely removed in some future version of this library. --- Readme.md | 8 -------- lib/api/manipulation.js | 4 ++-- lib/api/traversing.js | 10 +++++----- lib/cheerio.js | 2 ++ test/api.manipulation.js | 12 ++++++------ test/api.traversing.js | 4 ++-- 6 files changed, 17 insertions(+), 23 deletions(-) diff --git a/Readme.md b/Readme.md index 36f96b5712..b7dcad2d74 100644 --- a/Readme.md +++ b/Readme.md @@ -624,14 +624,6 @@ $.xml() ### Miscellaneous DOM element methods that don't fit anywhere else -#### .toArray() -Retrieve all the DOM elements contained in the jQuery set, as an array. - -```js -$('li').toArray() -//=> [ {...}, {...}, {...} ] -``` - #### .clone() #### Clone the cheerio object. diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index bbab180fa8..7f201c6a74 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -13,7 +13,7 @@ var makeDomArray = function(elem) { if (elem == null) { return []; } else if (elem.cheerio) { - return elem.toArray(); + return elem.get(); } else if (_.isArray(elem)) { return _.flatten(elem.map(makeDomArray)); } else if (_.isString(elem)) { @@ -195,7 +195,7 @@ var html = exports.html = function(str) { return $.html(this[0].children); } - str = str.cheerio ? str.toArray() : evaluate(str); + str = str.cheerio ? str.get() : evaluate(str); domEach(this, function(i, el) { el.children = str; diff --git a/lib/api/traversing.js b/lib/api/traversing.js index 0fc0298185..2f22b60aa2 100644 --- a/lib/api/traversing.js +++ b/lib/api/traversing.js @@ -38,7 +38,7 @@ var parents = exports.parents = function(selector) { // When multiple DOM elements are in the original set, the resulting set will // be in *reverse* order of the original elements as well, with duplicates // removed. - this.toArray().reverse().forEach(function(elem) { + this.get().reverse().forEach(function(elem) { traverseParents(this, elem.parent, selector, Infinity) .forEach(function(node) { if (parentNodes.indexOf(node) === -1) { @@ -109,9 +109,9 @@ var nextUntil = exports.nextUntil = function(selector, filterSelector) { var elems = [], untilNode, untilNodes; if (typeof selector === 'string') { - untilNode = select(selector, this.nextAll().toArray())[0]; + untilNode = select(selector, this.nextAll().get())[0]; } else if (selector && selector.cheerio) { - untilNodes = selector.toArray(); + untilNodes = selector.get(); } else if (selector) { untilNode = selector; } @@ -171,9 +171,9 @@ var prevUntil = exports.prevUntil = function(selector, filterSelector) { var elems = [], untilNode, untilNodes; if (typeof selector === 'string') { - untilNode = select(selector, this.prevAll().toArray())[0]; + untilNode = select(selector, this.prevAll().get())[0]; } else if (selector && selector.cheerio) { - untilNodes = selector.toArray(); + untilNodes = selector.get(); } else if (selector) { untilNode = selector; } diff --git a/lib/cheerio.js b/lib/cheerio.js index bc1f7f94ee..d421f05a00 100644 --- a/lib/cheerio.js +++ b/lib/cheerio.js @@ -139,6 +139,8 @@ Cheerio.prototype._make = function(dom) { /** * Turn a cheerio object into an array + * + * @deprecated */ Cheerio.prototype.toArray = function() { diff --git a/test/api.manipulation.js b/test/api.manipulation.js index 7affc37c98..0d19110dbd 100644 --- a/test/api.manipulation.js +++ b/test/api.manipulation.js @@ -57,7 +57,7 @@ describe('$(...)', function() { it('(Array) : should append all elements in the array', function() { var $fruits = $(fruits); var more = $('
  • Plum
  • Grape
  • ') - .toArray(); + .get(); $fruits.append(more); expect($fruits.children(3).hasClass('plum')).to.be.ok(); expect($fruits.children(4).hasClass('grape')).to.be.ok(); @@ -190,7 +190,7 @@ describe('$(...)', function() { it('(Array) : should add all elements in the array as inital children', function() { var $fruits = $(fruits); var more = $('
  • Plum
  • Grape
  • ') - .toArray(); + .get(); $fruits.prepend(more); expect($fruits.children(0).hasClass('plum')).to.be.ok(); expect($fruits.children(1).hasClass('grape')).to.be.ok(); @@ -296,7 +296,7 @@ describe('$(...)', function() { it('(Array) : should add all elements in the array as next sibling', function() { var $fruits = $(fruits); var more = $('
  • Plum
  • Grape
  • ') - .toArray(); + .get(); $('.apple', $fruits).after(more); expect($fruits.children(1).hasClass('plum')).to.be.ok(); expect($fruits.children(2).hasClass('grape')).to.be.ok(); @@ -438,7 +438,7 @@ describe('$(...)', function() { it('(Array) : should add all elements in the array as previous sibling', function() { var $fruits = $(fruits); var more = $('
  • Plum
  • Grape
  • ') - .toArray(); + .get(); $('.apple', $fruits).before(more); expect($fruits.children(0).hasClass('plum')).to.be.ok(); expect($fruits.children(1).hasClass('grape')).to.be.ok(); @@ -541,7 +541,7 @@ describe('$(...)', function() { it('(Array) : should replace one
  • tag with the elements in the array', function() { var $fruits = $(fruits); var more = $('
  • Plum
  • Grape
  • ') - .toArray(); + .get(); $('.pear', $fruits).replaceWith(more); expect($fruits.children(2).hasClass('plum')).to.be.ok(); @@ -592,7 +592,7 @@ describe('$(...)', function() { it('(fn) : should invoke the callback with the correct argument and context', function() { var $fruits = $(fruits); - var origChildren = $fruits.children().toArray(); + var origChildren = $fruits.children().get(); var args = []; var thisValues = []; diff --git a/test/api.traversing.js b/test/api.traversing.js index 9cbb0ca723..d9b26111d3 100644 --- a/test/api.traversing.js +++ b/test/api.traversing.js @@ -579,7 +579,7 @@ describe('$(...)', function() { return [1, [3, 4]]; }); - expect($mapped.toArray()).to.eql([ + expect($mapped.get()).to.eql([ 1, [3, 4], 1, [3, 4], 1, [3, 4] @@ -593,7 +593,7 @@ describe('$(...)', function() { return [null, undefined]; }); - expect($mapped.toArray()).to.eql([ + expect($mapped.get()).to.eql([ null, undefined, null, undefined, null, undefined,