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. 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),