Permalink
Browse files

Add Element#wrapContent

  • Loading branch information...
1 parent 8b8b651 commit add9d3504056e49596593c4d199980dd744b22d1 kangax committed May 23, 2008
Showing with 41 additions and 7 deletions.
  1. +34 −0 element.methods.js
  2. +4 −4 function.extensions.js
  3. +3 −3 test/function.extensions.html
View
@@ -151,4 +151,38 @@ Element.Methods.enableClassName = function(element, className, condition) {
return Element[condition ? 'addClassName' : 'removeClassName'](element, className);
};
+/**
+ * Element.wrapContent(@element, wrapper, attributes) -> @element
+ *
+ * Wraps element's content with wrapper (i.e. a DOMElement or an html string)
+ *
+ *
+ * <pre id="foo">
+ * some content <span>some tag</span>
+ * </pre>
+ * ...
+ * $('foo').wrapContent('<code class="javascript">');
+ * ...
+ * <pre>
@nakajima
nakajima May 23, 2008

Does this method remove the element’s id attribute?

+ * <code class="javascript">
+ * some content <span>some tag</span>
+ * </code>
+ * </pre>
+ *
+ **/
+Element.Methods.wrapContent = function(element, wrapper, attributes) {
+ if (!(element = $(element))) return;
+ if (Object.isElement(wrapper))
+ $(wrapper).writeAttribute(attributes || { })
+ else if (Object.isString(wrapper))
+ wrapper = new Element(wrapper, attributes);
+ else wrapper = new Element('div', wrapper);
+
+ while (element.firstChild)
+ wrapper.appendChild(element.firstChild)
+
+ element.appendChild(wrapper);
+ return element;
+};
+
Element.addMethods();
@@ -1,15 +1,15 @@
/**
- * Function#negate() -> Function
+ * Function#not() -> Function
*
* Returns negated function
*
* Find all hidden elements:
*
* $$('*').findAll(function(element) { return !element.visible() }); // old way
- * $$('*').findAll(Element.visible.negate()); // using negate
+ * $$('*').findAll(Element.visible.not()); // using not
*
**/
-Function.prototype.negate = function() {
+Function.prototype.not = function() {
var f = this;
return function() {
return !f.apply(f, arguments);
@@ -44,7 +44,7 @@ Function.prototype.runOnce = function() {
*
**/
Function.prototype._new = function() {
- var __method = this, args = arguments;
+ var __method = this, args = $A(arguments);
function C() { return __method.apply(this, args); };
C.prototype = __method.prototype;
return new C;
@@ -21,16 +21,16 @@
<script type="text/javascript">
new Test.Unit.Runner({
- testFunctionPrototypeNegate: function() {
+ testFunctionPrototypeNot: function() {
var truthy = function() { return true; };
var falsy = function() { return false; };
var something = function() { return {foo: 'bar'} };
this.assert(truthy());
- this.assert(!truthy.negate()());
+ this.assert(!truthy.not()());
this.assert(!falsy());
- this.assert(falsy.negate()());
+ this.assert(falsy.not()());
},
testFunctionPrototypeRunOnce: function() {

0 comments on commit add9d35

Please sign in to comment.