Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update insert method #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 24 additions & 10 deletions src/prototype/dom/dom.js
Expand Up @@ -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.
Expand Down Expand Up @@ -932,17 +932,31 @@
* before: "<hr>",
* after: "<hr>"
* });
*
* Insert several elements
*
* $('myelement').insert(
* new Element('div'),
* <span>Lorem ipsum</span>
* "dolore..."
* );
*
* //Result: <div><div></div><span>Lorem ipsum</span>dolore...
*
**/
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;
}

/**
Expand Down
23 changes: 22 additions & 1 deletion test/unit/dom_test.js
Expand Up @@ -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');

Expand Down Expand Up @@ -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')},
"<em>test</em>",
{
before: "<hr>",
after: "<hr>"
}
);
this.assertEqual('<hr><div><h1>h1</h1><p>paragraph</p><span>span</span>simple text<div>div</div><em>test</em></div><hr>',
getInnerHTML(main));

},

testElementWrap: function() {
var element = $('wrap'), parent = document.createElement('div');
element.wrap();
Expand Down