prototype: Make Element#wrap accept a second argument for setting attributes on the wrapper. Allow wrapping elements which are not part of the document. Closes #9071.

This commit is contained in:
Sam Stephenson 2007-07-24 19:08:55 +00:00
parent 44ef74813b
commit c0735dba77
3 changed files with 23 additions and 8 deletions

View File

@ -1,5 +1,7 @@
*SVN*
* Make Element#wrap accept a second argument for setting attributes on the wrapper. Allow wrapping elements which are not part of the document. Closes #9071. [Tobie Langel]
* Improvements for Element#replace, Element#update and Element#insert. Closes #7429, #9060. [Tobie Langel]
- Element#replace/update/insert uses the argument's toElement or toHTML method if present (toElement has precedence if both are present).
- Element#replace and Element#update now also accept DOM elements.

View File

@ -124,12 +124,14 @@ Element.Methods = {
return element;
},
wrap: function(element, wrapper) {
wrap: function(element, wrapper, attributes) {
element = $(element);
wrapper = wrapper || 'div';
if (typeof wrapper == 'string') wrapper = new Element(wrapper);
else Element.extend(wrapper);
element.parentNode.replaceChild(wrapper, element);
if (wrapper && wrapper.nodeType)
$(wrapper).writeAttribute(attributes || {});
else if (typeof wrapper == 'string') wrapper = new Element(wrapper, attributes);
else wrapper = new Element('div', wrapper);
if (element.parentNode)
element.parentNode.replaceChild(wrapper, element);
wrapper.appendChild(element);
return element;
},

View File

@ -548,13 +548,24 @@
testElementWrap: function() {with(this) {
var element = $('wrap'), parent = document.createElement('div');
element.wrap('div');
element.wrap();
assert(getInnerHTML('wrap-container').startsWith('<div><p'));
element.wrap(parent);
element.wrap('div');
assert(getInnerHTML('wrap-container').startsWith('<div><div><p'));
assert(typeof parent.setStyle == 'function');
element.wrap();
element.wrap(parent);
assert(getInnerHTML('wrap-container').startsWith('<div><div><div><p'));
element.wrap('div', {className: 'wrapper'});
assert(element.up().hasClassName('wrapper'));
element.wrap({className: 'other-wrapper'});
assert(element.up().hasClassName('other-wrapper'));
element.wrap(new Element('div'), {className: 'yet-other-wrapper'});
assert(element.up().hasClassName('yet-other-wrapper'));
var orphan = new Element('p'), div = new Element('div');
orphan.wrap(div);
assertEqual(orphan.parentNode, div);
}},
testElementVisible: function(){with(this) {