Skip to content

Commit

Permalink
Add Element#wrapContent
Browse files Browse the repository at this point in the history
  • Loading branch information
kangax committed May 23, 2008
1 parent 8b8b651 commit add9d35
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
34 changes: 34 additions & 0 deletions element.methods.js
Expand Up @@ -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>

This comment has been minimized.

Copy link
@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();
8 changes: 4 additions & 4 deletions function.extensions.js
@@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions test/function.extensions.html
Expand Up @@ -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() {
Expand Down

0 comments on commit add9d35

Please sign in to comment.