Skip to content

Commit

Permalink
support function argument for wrap, wrapInner
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Sep 9, 2012
1 parent df1fd33 commit e27e59a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,16 @@ var Zepto = (function() {
return this.before(newContent).remove()
},
wrap: function(structure){
if (this[0]) var dom = $(structure).get(0),
clone = dom.parentNode || this.length > 1
return this.each(function(){
$(this).wrapAll(clone ? dom.cloneNode(true) : dom)
var func = isFunction(structure)
if (this[0] && !func)
var dom = $(structure).get(0),
clone = dom.parentNode || this.length > 1

return this.each(function(index){
$(this).wrapAll(
func ? structure.call(this, index) :
clone ? dom.cloneNode(true) : dom
)
})
},
wrapAll: function(structure){
Expand All @@ -417,9 +423,11 @@ var Zepto = (function() {
return this
},
wrapInner: function(structure){
return this.each(function(){
var self = $(this), contents = self.contents()
contents.length ? contents.wrapAll(structure) : self.append(structure)
var func = isFunction(structure)
return this.each(function(index){
var self = $(this), contents = self.contents(),
dom = func ? structure.call(this, index) : structure
contents.length ? contents.wrapAll(dom) : self.append(dom)
})
},
unwrap: function(){
Expand Down
22 changes: 22 additions & 0 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,17 @@ <h1>Zepto DOM unit tests</h1>
t.assert(el.find('a').parent().is('span'))
},

testWrapFunction: function(t) {
var el = $('<div><b>A</b><b>B</b></div>')
el.find('b').wrap(function(index){
return '<a class=link' + index + $(this).text() + ' />'
})
t.assertEqual(
'<a class="link0A"><b>A</b></a><a class="link1B"><b>B</b></a>',
el.html()
)
},

testWrapAll: function(t) {
var el = $('#wrapall_test')
el.find('span').wrapAll('<p><a/></p>')
Expand Down Expand Up @@ -1094,6 +1105,17 @@ <h1>Zepto DOM unit tests</h1>
t.assertLength(0, $el.find('div').contents())
},

testWrapInnerFunction: function(t) {
var el = $('<div><b>A</b><b>B</b></div>')
el.find('b').wrapInner(function(index){
return '<a class=link' + index + $(this).text() + ' />'
})
t.assertEqual(
'<b><a class="link0A">A</a></b><b><a class="link1B">B</a></b>',
el.html()
)
},

testUnwrap: function(t){
var context=$("#unwrap_test")

Expand Down

0 comments on commit e27e59a

Please sign in to comment.