diff --git a/CHANGELOG b/CHANGELOG index b8afa3d..f10bdda 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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. diff --git a/src/dom.js b/src/dom.js index 1b70bfd..b10b917 100644 --- a/src/dom.js +++ b/src/dom.js @@ -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; }, diff --git a/test/unit/dom.html b/test/unit/dom.html index b178956..e501a16 100644 --- a/test/unit/dom.html +++ b/test/unit/dom.html @@ -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('