Skip to content

Commit

Permalink
Fixed replaceWith for case where target had no previousSibling
Browse files Browse the repository at this point in the history
  • Loading branch information
aglemann committed May 20, 2011
1 parent e75d7cf commit c6e0d57
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
5 changes: 1 addition & 4 deletions src/zepto.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,7 @@ var Zepto = (function() {
},
replaceWith: function(newContent) {
return this.each(function() {
var element = $(this),
prev = element.prev();
element.remove();
prev.after(newContent);
this.parentNode.replaceChild($(newContent)[0], this);
});
},
wrap: function(newContent) {
Expand Down
9 changes: 4 additions & 5 deletions test/zepto.html
Original file line number Diff line number Diff line change
Expand Up @@ -526,13 +526,12 @@ <h1>Zepto DOM unit tests</h1>
},

testReplaceWith: function(t) {
$('div.second').replaceWith('<h2 id="replace_test">New heading</h2>');

t.assertUndefined($('div.second').get(0));
$('div.first').replaceWith('<h2 id="replace_test">New heading</h2>');
t.assertUndefined($('div.first').get(0));
t.assert(document.getElementById("replace_test").nodeType);
t.assertEqual($('.replacewith h2#replace_test').get(0), document.getElementById("replace_test"));

$('#replace_test').replaceWith($('#replace_test_div'));
$('#replace_test').replaceWith($('.replace_test_div'));
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]);
Expand All @@ -549,7 +548,7 @@ <h1>Zepto DOM unit tests</h1>
t.assert(document.getElementsByClassName("wrap_test")[0].nodeType);
t.assertEqual($('#wrap_test_div span').length, $('.wrap_test').length);
},

testFind: function(t){
var found = $('p#find1').find('span.findme');
t.assertLength(4, found);
Expand Down

0 comments on commit c6e0d57

Please sign in to comment.