Skip to content

Commit

Permalink
Fix .replaceWith to allow more than 1 node as replacement -- with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekhaled committed May 24, 2011
1 parent 2b6487b commit 65a1c1d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/zepto.js
Expand Up @@ -174,7 +174,9 @@ var Zepto = (function() {
},
replaceWith: function(newContent) {
return this.each(function() {
this.parentNode.replaceChild($(newContent)[0], this);
var par=this.parentNode,next=this.nextSibling;
$(this).remove();
next ? $(next).before(newContent) : $(par).append(newContent);
});
},
wrap: function(newContent) {
Expand Down
6 changes: 6 additions & 0 deletions test/zepto.html
Expand Up @@ -535,6 +535,12 @@ <h1>Zepto DOM unit tests</h1>
t.assertUndefined($('#replace_test').get(0));
t.assert(document.getElementsByClassName("replace_test_div")[0].nodeType);
t.assertEqual($('.replacewith h2#replace_test').get(0), document.getElementsByClassName("replace_test")[0]);

//Multiple elements
$('.replacewith .replace_test_div').replaceWith('<div class="inner first">hi</div><div class="inner fourth">hello</div>');
t.assertLength(4,$('.replacewith div'));
t.assertEqual("inner first", $('.replacewith div')[0].className);
t.assertEqual("inner fourth", $('.replacewith div')[1].className);
},

testWrap: function(t) {
Expand Down

0 comments on commit 65a1c1d

Please sign in to comment.