Skip to content

Commit

Permalink
Merge pull request madrobby#154 from errorhandler/master.
Browse files Browse the repository at this point in the history
Added .replaceWith
  • Loading branch information
madrobby committed May 2, 2011
2 parents f6f938e + 7b0e91e commit eb4f84a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/zepto.js
Expand Up @@ -162,6 +162,14 @@ var Zepto = (function() {
}
})
},
replaceWith: function(newContent) {
return this.each(function() {
var element = $(this),
prev = element.prev();
element.remove();
prev.after(newContent);
});
},
hide: function(){
return this.css("display", "none")
},
Expand Down
20 changes: 20 additions & 0 deletions test/zepto.html
Expand Up @@ -35,6 +35,11 @@ <h1>Zepto DOM unit tests</h1>

<div id="attr_1" data-id="someId1" data-name="someName1"></div>
<div id="attr_2" data-id="someId2" data-name="someName2"></div>
<div class="replacewith">
<div class="inner first">Hello</div>
<div class="inner second">And</div>
<div class="inner third">Goodbye</div>
</div>

<div id="data_attr" data-blah="whatever"></div>

Expand Down Expand Up @@ -138,6 +143,8 @@ <h1>Zepto DOM unit tests</h1>
<input type="text" id="BooleanInput" required />

<form id="some_form"></form>

<div class="replace_test_div">test</div>

<script>

Expand Down Expand Up @@ -507,6 +514,19 @@ <h1>Zepto DOM unit tests</h1>
t.assertEqual("1",$("span.c",el).text());
},

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

t.assertUndefined($('div.second').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'));
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]);
},

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

0 comments on commit eb4f84a

Please sign in to comment.