JSFiddle - React, Tailwind, and code Playground
HTML
<script src="http://code.jquery.com/qunit/git/qunit.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css">
<h1 id="qunit-header">Unit Tests</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"></div>
JavaScript
// http://stackoverflow.com/a/9468280/1185698
(function ($) {
$.fn.replaceTagName = function (replaceWith) {
var tags = [],
i = this.length;
while (i--) {
var newElement = document.createElement(replaceWith),
thisi = this[i],
thisia = thisi.attributes;
for (var a = thisia.length - 1; a >= 0; a--) {
var attrib = thisia[a];
newElement.setAttribute(attrib.name, attrib.value);
};
newElement.innerHTML = thisi.innerHTML;
$(thisi).after(newElement).remove();
tags[i - 1] = newElement;
}
return $(tags);
};
})(window.jQuery);
module('jQuery.replaceTagName', {
teardown: function () {
$("#qunit-fixture").children().remove();
}
});
test('Should properly return jQuery object', function() {
$("#qunit-fixture").append("<div></div>");
var result = $('#qunit-fixture div').replaceTagName('span');
result.hasClass('test'); // raises exception if wrong
ok(true);
});
test('Should replace tag names', function () {
$("#qunit-fixture").append("<div></div>");
$('#qunit-fixture div').replaceTagName('span');
notEqual($('#qunit-fixture span').length, 0);
equal($('#qunit-fixture div').length, 0);
});
test('Should perserve innerHTML text', function () {
$("#qunit-fixture").append("<div>innerHTML</div>");
$('#qunit-fixture div').replaceTagName('span');
equal($('#qunit-fixture span').html(), 'innerHTML');
});
test('Should perserve innerHTML elements', function () {
$("#qunit-fixture").append("<div><p>test</p></div>");
$('#qunit-fixture div').replaceTagName('span');
equal($('#qunit-fixture span').html(), '<p>test</p>');
});
test('Should perserve attributes of innerHTML elements', function () {
$("#qunit-fixture").append('<div class="testing-div"><p class="attr" id="test-paragraph">test</p></div>');
$('#qunit-fixture...