X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=sidebyside;f=test%2Funit%2Fmanipulation.js;h=37234d86d3e2443496239573080d92d1ac1e3668;hb=c1d719b580ea78c33961113030d7fa25bcc98e6f;hp=3eef1221cdf2df0d907c944c123ea675045d51e7;hpb=3e9ef6f5c08e63a90ef2dfd3bdc833994e7a0ac8;p=jquery.git
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index 3eef122..37234d8 100644
--- a/test/unit/manipulation.js
+++ b/test/unit/manipulation.js
@@ -1,4 +1,7 @@
-module("manipulation");
+module("manipulation", { teardown: moduleTeardown });
+
+// Ensure that an extended Array prototype doesn't break jQuery
+Array.prototype.arrayProtoFn = function(arg) { throw("arrayProtoFn should not be called"); };
var bareObj = function(value) { return value; };
var functionReturningObj = function(value) { return (function() { return value; }); };
@@ -22,7 +25,9 @@ var testText = function(valueObj) {
j.text(valueObj("hi!"));
equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
- equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
+
+ // Blackberry 4.6 doesn't maintain comments in the DOM
+ equals( jQuery("#nonnodes")[0].childNodes.length < 3 ? 8 : j[2].nodeType, 8, "Check node,textnode,comment with text()" );
}
test("text(String)", function() {
@@ -35,33 +40,33 @@ test("text(Function)", function() {
test("text(Function) with incoming value", function() {
expect(2);
-
+
var old = "This link has class=\"blog\": Simon Willison's Weblog";
-
+
jQuery('#sap').text(function(i, val) {
equals( val, old, "Make sure the incoming value is correct." );
return "foobar";
});
-
+
equals( jQuery("#sap").text(), "foobar", 'Check for merged text of more then one element.' );
-
- reset();
+
+ QUnit.reset();
});
var testWrap = function(val) {
- expect(18);
+ expect(19);
var defaultText = 'Try them out:'
var result = jQuery('#first').wrap(val( '
' )).text();
equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
- reset();
+ QUnit.reset();
var defaultText = 'Try them out:'
var result = jQuery('#first').wrap(val( document.getElementById('empty') )).parent();
ok( result.is('ol'), 'Check for element wrapping' );
equals( result.text(), defaultText, 'Check for element wrapping' );
- reset();
+ QUnit.reset();
jQuery('#check1').click(function() {
var checkbox = this;
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
@@ -72,14 +77,26 @@ var testWrap = function(val) {
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
j.wrap(val( "" ));
- equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
+
+ // Blackberry 4.6 doesn't maintain comments in the DOM
+ equals( jQuery("#nonnodes > i").length, jQuery("#nonnodes")[0].childNodes.length, "Check node,textnode,comment wraps ok" );
equals( jQuery("#nonnodes > i").text(), j.text(), "Check node,textnode,comment wraps doesn't hurt text" );
// Try wrapping a disconnected node
+ var cacheLength = 0;
+ for (var i in jQuery.cache) {
+ cacheLength++;
+ }
+
j = jQuery("").wrap(val( "" ));
equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );
+ for (i in jQuery.cache) {
+ cacheLength--;
+ }
+ equals(cacheLength, 0, "No memory leak in jQuery.cache (bug #7165)");
+
// Wrap an element containing a text node
j = jQuery("").wrap("
test
");
equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
@@ -98,12 +115,19 @@ var testWrap = function(val) {
// Wrap an element with a jQuery set and event
result = jQuery("").click(function(){
ok(true, "Event triggered.");
+
+ // Remove handlers on detached elements
+ result.unbind();
+ jQuery(this).unbind();
});
j = jQuery("").wrap(result);
equals( j[0].parentNode.nodeName.toLowerCase(), "div", "Wrapping works." );
j.parent().trigger("click");
+
+ // clean up attached elements
+ QUnit.reset();
}
test("wrap(String|Element)", function() {
@@ -126,7 +150,7 @@ var testWrapAll = function(val) {
equals( jQuery("#first").parent().parent()[0].previousSibling, prev, "Correct Previous Sibling" );
equals( jQuery("#first").parent().parent()[0].parentNode, p, "Correct Parent" );
- reset();
+ QUnit.reset();
var prev = jQuery("#firstp")[0].previousSibling;
var p = jQuery("#first")[0].parentNode;
var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
@@ -139,35 +163,41 @@ test("wrapAll(String|Element)", function() {
testWrapAll(bareObj);
});
-// TODO: Figure out why each(wrapAll) is not equivalent to wrapAll
-// test("wrapAll(Function)", function() {
-// testWrapAll(functionReturningObj);
-// })
-
var testWrapInner = function(val) {
- expect(6);
+ expect(11);
var num = jQuery("#first").children().length;
- var result = jQuery('#first').wrapInner('
');
+ var result = jQuery('#first').wrapInner(val('
'));
+ equals( jQuery("#first").children().length, 1, "Only one child" );
+ ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
+ equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
+
+ QUnit.reset();
+ var num = jQuery("#first").html("foo
test
test2
").children().length;
+ var result = jQuery('#first').wrapInner(val('
'));
equals( jQuery("#first").children().length, 1, "Only one child" );
ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
- reset();
+ QUnit.reset();
var num = jQuery("#first").children().length;
- var result = jQuery('#first').wrapInner(document.getElementById('empty'));
+ var result = jQuery('#first').wrapInner(val(document.getElementById('empty')));
equals( jQuery("#first").children().length, 1, "Only one child" );
ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
+
+ var div = jQuery("");
+ div.wrapInner(val(""));
+ equals(div.children().length, 1, "The contents were wrapped.");
+ equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
}
test("wrapInner(String|Element)", function() {
testWrapInner(bareObj);
});
-// TODO: wrapInner uses wrapAll -- get wrapAll working with Function
-// test("wrapInner(Function)", function() {
-// testWrapInner(functionReturningObj)
-// })
+test("wrapInner(Function)", function() {
+ testWrapInner(functionReturningObj)
+});
test("unwrap()", function() {
expect(9);
@@ -197,59 +227,79 @@ test("unwrap()", function() {
});
var testAppend = function(valueObj) {
- expect(22);
+ expect(37);
var defaultText = 'Try them out:'
var result = jQuery('#first').append(valueObj('buga'));
equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
equals( jQuery('#select3').append(valueObj('')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
- reset();
+ QUnit.reset();
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
jQuery('#sap').append(valueObj(document.getElementById('first')));
- equals( expected, jQuery('#sap').text(), "Check for appending of element" );
+ equals( jQuery('#sap').text(), expected, "Check for appending of element" );
- reset();
+ QUnit.reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
- equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
+ equals( jQuery('#sap').text(), expected, "Check for appending of array of elements" );
- reset();
+ QUnit.reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
- jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
- equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
+ jQuery('#sap').append(valueObj(jQuery("#yahoo, #first")));
+ equals( jQuery('#sap').text(), expected, "Check for appending of jQuery object" );
- reset();
+ QUnit.reset();
jQuery("#sap").append(valueObj( 5 ));
ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
- reset();
+ QUnit.reset();
jQuery("#sap").append(valueObj( " text with spaces " ));
ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
- reset();
+ QUnit.reset();
ok( jQuery("#sap").append(valueObj( [] )), "Check for appending an empty array." );
ok( jQuery("#sap").append(valueObj( "" )), "Check for appending an empty string." );
ok( jQuery("#sap").append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." );
- reset();
+ QUnit.reset();
+ jQuery("form").append(valueObj(''));
+ jQuery("form input[name=radiotest]").each(function(){
+ ok( jQuery(this).is(':checked'), "Append checked radio");
+ }).remove();
+
+ QUnit.reset();
+ jQuery("form").append(valueObj(''));
+ jQuery("form input[name=radiotest]").each(function(){
+ ok( jQuery(this).is(':checked'), "Append alternately formated checked radio");
+ }).remove();
+
+ QUnit.reset();
+ jQuery("form").append(valueObj(''));
+ jQuery("form input[name=radiotest]").each(function(){
+ ok( jQuery(this).is(':checked'), "Append HTML5-formated checked radio");
+ }).remove();
+
+ QUnit.reset();
jQuery("#sap").append(valueObj( document.getElementById('form') ));
equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
- reset();
+ QUnit.reset();
var pass = true;
try {
- jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "
test
" ));
- } catch(e) {
+ var body = jQuery("#iframe")[0].contentWindow.document.body;
+
pass = false;
- }
+ jQuery( body ).append(valueObj( "
test
" ));
+ pass = true;
+ } catch(e) {}
ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
- reset();
+ QUnit.reset();
jQuery('').appendTo('#form').append(valueObj( '' ));
t( 'Append legend', '#legend', ['legend'] );
- reset();
+ QUnit.reset();
jQuery('#select1').append(valueObj( '' ));
equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
@@ -259,11 +309,11 @@ var testAppend = function(valueObj) {
jQuery('#table colgroup').append(valueObj( '