From 0c5e56ba4949079f878edbc4f136a19df16167d2 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Mon, 3 Sep 2012 11:57:36 -0700 Subject: [PATCH 1/2] manipulation: DRY out `append` and `prepend` --- lib/api/manipulation.js | 51 ++++++++++++----------------------------- 1 file changed, 15 insertions(+), 36 deletions(-) diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index f8e3c0836b..abb38fa663 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -16,46 +16,25 @@ var makeCheerioArray = function(elems) { }, []); }; -var append = exports.append = function() { - var elems = slice.call(arguments), - dom = makeCheerioArray(elems); - - this.each(function(i, el) { - if (_.isFunction(elems[0])) { - // No yet supported - return this; - } else { - if (!el.children) el.children = []; - el.children = el.children.concat(dom); - updateDOM(el.children, el); - } - }); +var _insert = function(concatenator) { + return function() { + var elems = slice.call(arguments), + dom = makeCheerioArray(elems); - return this; + return this.each(function(i, el) { + if (_.isFunction(elems[0])) return el; // not yet supported + updateDOM(concatenator(dom, el.children || (el.children = [])), el); + }); + }; }; +var append = exports.append = _insert(function(dom, children) { + return children.concat(dom); +}); -/* - TODO: Refactor, only one line difference between, - this function and append -*/ -var prepend = exports.prepend = function() { - var elems = slice.call(arguments), - dom = makeCheerioArray(elems); - - this.each(function(i, el) { - if (_.isFunction(elems[0])) { - // No yet supported - return this; - } else { - if (!el.children) el.children = []; - el.children = dom.concat(el.children); - updateDOM(el.children, el); - } - }); - - return this; -}; +var prepend = exports.prepend = _insert(function(dom, children) { + return dom.concat(children); +}); var after = exports.after = function() { var elems = slice.call(arguments), From 346e01d0db4ab1806be273a163ebb3e959bf4af0 Mon Sep 17 00:00:00 2001 From: Matt Mueller Date: Wed, 3 Oct 2012 23:18:21 +0900 Subject: [PATCH 2/2] Update Readme.md --- Readme.md | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/Readme.md b/Readme.md index 568038f834..dae9a97693 100644 --- a/Readme.md +++ b/Readme.md @@ -443,25 +443,6 @@ $('.pear').html() ### Miscellaneous DOM element methods that don't fit anywhere else -#### .get( [index] ) -Retrieve the DOM elements matched by the cheerio object. If no index is specified, it will get an array of all matched elements. - -```js -$('li').get(0) -//=> { raw: 'li class="apple"', ... } - -$('li').get() -//=> [ {...}, {...}, {...} ] -``` - -#### .size() -Return the number of elements in the cheerio object. Same as `length`. - -```js -$('li').size() -//=> 3 -``` - #### .toArray() Retrieve all the DOM elements contained in the jQuery set, as an array.