Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

update insert method #76

Open
wants to merge 1 commit into from

2 participants

mike killemov
mike

Hi.

I updated insert method for Element. Now possible to pass more than one element to insert.

killemov

I implemented this functionality also. My version also allows (nested) arrays.
killemov@16ecf74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 3, 2012
  1. mike

    update insert method

    fntzr authored
This page is out of date. Refresh to see the latest.
Showing with 46 additions and 11 deletions.
  1. +24 −10 src/prototype/dom/dom.js
  2. +22 −1 test/unit/dom_test.js
34 src/prototype/dom/dom.js
View
@@ -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: "<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;
}
/**
23 test/unit/dom_test.js
View
@@ -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')},
+ "<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();
Something went wrong with that request. Please try again.