diff --git a/src/prototype/dom/dom.js b/src/prototype/dom/dom.js index acc42dbeb..7a9e7fdb4 100644 --- a/src/prototype/dom/dom.js +++ b/src/prototype/dom/dom.js @@ -889,8 +889,8 @@ } /** - * Element.insert(@element, content) -> Element - * - content (String | Element | Object): The content to insert. + * Element.insert(@element, *content) -> Element + * - content (String | Element | Object): Collection the content to insert. * * Inserts content `above`, `below`, at the `top`, and/or at the `bottom` of * the given element, depending on the option(s) given. @@ -932,17 +932,31 @@ * before: "
", * after: "
" * }); + * + * Insert several elements + * + * $('myelement').insert( + * new Element('div'), + * Lorem ipsum + * "dolore..." + * ); + * + * //Result:
Lorem ipsumdolore... + * **/ - function insert(element, insertions) { - element = $(element); + function insert() { + var args = $A(arguments); - if (isContent(insertions)) - insertions = { bottom: insertions }; - - for (var position in insertions) - insertContentAt(element, insertions[position], position); + element = $(args[0]); - return element; + args.slice(1).each(function(insertions) { + if (isContent(insertions)) + insertions = { bottom: insertions }; + + for (var position in insertions) + insertContentAt(element, insertions[position], position); + }); + return element; } /** diff --git a/test/unit/dom_test.js b/test/unit/dom_test.js index b9ef36b6f..9838ad981 100644 --- a/test/unit/dom_test.js +++ b/test/unit/dom_test.js @@ -85,7 +85,7 @@ new Test.Unit.Runner({ this.info("browser uses native getElementsByClassName; skipping tests"); return; } - + var div = $('class_names'), list = $('class_names_ul'); @@ -257,6 +257,27 @@ new Test.Unit.Runner({ this.assert(getInnerHTML('element-insertions-main').endsWith('some backward-compatibility testing bottom')); }, + testInsertionWithSeveralElements: function() { + var main = new Element('div'); + var container = new Element('div'); + main.insert(container); + + container.insert(new Element('p').update('paragraph'), + new Element('span').update('span'), + "simple text", + new Element('div').update('div'), + {top: new Element('h1').update('h1')}, + "test", + { + before: "
", + after: "
" + } + ); + this.assertEqual('

h1

paragraph

spansimple text
div
test

', + getInnerHTML(main)); + + }, + testElementWrap: function() { var element = $('wrap'), parent = document.createElement('div'); element.wrap();