X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fmanipulation.js;h=d8dbca96c7b7c5e9d9d4d017ca2dc47537bae8db;hb=c0802e5d21548ce5c41e51ff500308d5a5c9b249;hp=0327f93b2379adc26c73c53d4e9d7dfee214e2da;hpb=7c04a64016c1f295dfdf4e731f693f8400a987bc;p=jquery.git
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index 0327f93..d8dbca9 100644
--- a/test/unit/manipulation.js
+++ b/test/unit/manipulation.js
@@ -1,19 +1,24 @@
+module("manipulation");
+
+var bareObj = function(value) { return value; };
+var functionReturningObj = function(value) { return (function() { return value; }); };
+
test("text()", function() {
expect(1);
var expected = "This link has class=\"blog\": Simon Willison's Weblog";
equals( jQuery('#sap').text(), expected, 'Check for merged text of more then one element.' );
});
-test("wrap(String|Element)", function() {
+var testWrap = function(val) {
expect(10);
var defaultText = 'Try them out:'
- var result = jQuery('#first').wrap('
').text();
+ 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();
var defaultText = 'Try them out:'
- var result = jQuery('#first').wrap(document.getElementById('empty')).parent();
+ 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' );
@@ -21,27 +26,36 @@ test("wrap(String|Element)", function() {
jQuery('#check1').click(function() {
var checkbox = this;
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
- jQuery(checkbox).wrap( '' );
+ jQuery(checkbox).wrap(val( '' ));
ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
}).click();
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
- j.wrap("");
+ j.wrap(val( "" ));
equals( jQuery("#nonnodes > i").length, 3, "Check node,textnode,comment wraps ok" );
equals( jQuery("#nonnodes > i").text(), j.text() + j[1].nodeValue, "Check node,textnode,comment wraps doesn't hurt text" );
// Try wrapping a disconnected node
- j = jQuery("").wrap("");
+ 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" );
+}
+
+test("wrap(String|Element)", function() {
+ testWrap(bareObj);
});
-test("wrapAll(String|Element)", function() {
+test("wrap(Function)", function() {
+ testWrap(functionReturningObj);
+})
+
+var testWrapAll = function(val) {
expect(8);
var prev = jQuery("#firstp")[0].previousSibling;
var p = jQuery("#firstp,#first")[0].parentNode;
- var result = jQuery('#firstp,#first').wrapAll('');
+
+ var result = jQuery('#firstp,#first').wrapAll(val( '' ));
equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
ok( jQuery('#firstp').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
@@ -51,13 +65,22 @@ test("wrapAll(String|Element)", function() {
reset();
var prev = jQuery("#firstp")[0].previousSibling;
var p = jQuery("#first")[0].parentNode;
- var result = jQuery('#firstp,#first').wrapAll(document.getElementById('empty'));
+ var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
- equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
+ equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
+}
+
+test("wrapAll(String|Element)", function() {
+ testWrapAll(bareObj);
});
-test("wrapInner(String|Element)", function() {
+// TODO: Figure out why each(wrapAll) is not equivalent to wrapAll
+// test("wrapAll(Function)", function() {
+// testWrapAll(functionReturningObj);
+// })
+
+var testWrapInner = function(val) {
expect(6);
var num = jQuery("#first").children().length;
var result = jQuery('#first').wrapInner('');
@@ -70,52 +93,61 @@ test("wrapInner(String|Element)", function() {
var result = jQuery('#first').wrapInner(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" );
+ equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
+}
+
+test("wrapInner(String|Element)", function() {
+ testWrapInner(bareObj);
});
-test("append(String|Element|Array<Element>|jQuery)", function() {
+// TODO: wrapInner uses wrapAll -- get wrapAll working with Function
+// test("wrapInner(Function)", function() {
+// testWrapInner(functionReturningObj)
+// })
+
+var testAppend = function(valueObj) {
expect(21);
var defaultText = 'Try them out:'
- var result = jQuery('#first').append('buga');
+ var result = jQuery('#first').append(valueObj('buga'));
equals( result.text(), defaultText + 'buga', 'Check if text appending works' );
- equals( jQuery('#select3').append('').find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
+ equals( jQuery('#select3').append(valueObj('')).find('option:last-child').attr('value'), 'appendTest', 'Appending html options to select element');
reset();
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
- jQuery('#sap').append(document.getElementById('first'));
+ jQuery('#sap').append(valueObj(document.getElementById('first')));
equals( expected, jQuery('#sap').text(), "Check for appending of element" );
reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
- jQuery('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
+ jQuery('#sap').append(valueObj([document.getElementById('first'), document.getElementById('yahoo')]));
equals( expected, jQuery('#sap').text(), "Check for appending of array of elements" );
reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
- jQuery('#sap').append(jQuery("#first, #yahoo"));
+ jQuery('#sap').append(valueObj(jQuery("#first, #yahoo")));
equals( expected, jQuery('#sap').text(), "Check for appending of jQuery object" );
reset();
- jQuery("#sap").append( 5 );
+ jQuery("#sap").append(valueObj( 5 ));
ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
reset();
- jQuery("#sap").append( " text with spaces " );
+ jQuery("#sap").append(valueObj( " text with spaces " ));
ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
reset();
- ok( jQuery("#sap").append([]), "Check for appending an empty array." );
- ok( jQuery("#sap").append(""), "Check for appending an empty string." );
- ok( jQuery("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
+ 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();
- jQuery("#sap").append(document.getElementById('form'));
+ jQuery("#sap").append(valueObj( document.getElementById('form') ));
equals( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
reset();
var pass = true;
try {
- jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append("test
");
+ jQuery( jQuery("#iframe")[0].contentWindow.document.body ).append(valueObj( "test
" ));
} catch(e) {
pass = false;
}
@@ -123,27 +155,27 @@ test("append(String|Element|Array<Element>|jQuery)", function() {
ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
reset();
- jQuery('').appendTo('#form').append('');
+ jQuery('').appendTo('#form').append(valueObj( '' ));
t( 'Append legend', '#legend', ['legend'] );
reset();
- jQuery('#select1').append('');
+ jQuery('#select1').append(valueObj( '' ));
equals( jQuery('#select1 option:last').text(), "Test", "Appending <OPTION> (all caps)" );
- jQuery('#table').append('');
+ jQuery('#table').append(valueObj( '' ));
ok( jQuery('#table colgroup').length, "Append colgroup" );
- jQuery('#table colgroup').append('');
+ jQuery('#table colgroup').append(valueObj( '' ));
ok( jQuery('#table colgroup col').length, "Append col" );
reset();
- jQuery('#table').append('');
+ jQuery('#table').append(valueObj( '' ));
ok( jQuery('#table caption').length, "Append caption" );
reset();
jQuery('form:last')
- .append('')
- .append('');
+ .append(valueObj( '' ))
+ .append(valueObj( '' ));
t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
@@ -155,8 +187,16 @@ test("append(String|Element|Array<Element>|jQuery)", function() {
d.contents().appendTo("#nonnodes");
d.remove();
ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
+}
+
+test("append(String|Element|Array<Element>|jQuery)", function() {
+ testAppend(bareObj);
});
+test("append(Function)", function() {
+ testAppend(functionReturningObj);
+})
+
test("appendTo(String|Element|Array<Element>|jQuery)", function() {
expect(12);
var defaultText = 'Try them out:'
@@ -208,29 +248,37 @@ test("appendTo(String|Element|Array<Element>|jQuery)", function() {
reset();
});
-test("prepend(String|Element|Array<Element>|jQuery)", function() {
+var testPrepend = function(val) {
expect(5);
var defaultText = 'Try them out:'
- var result = jQuery('#first').prepend('buga');
+ var result = jQuery('#first').prepend(val( 'buga' ));
equals( result.text(), 'buga' + defaultText, 'Check if text prepending works' );
- equals( jQuery('#select3').prepend('').find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
+ equals( jQuery('#select3').prepend(val( '' )).find('option:first-child').attr('value'), 'prependTest', 'Prepending html options to select element');
reset();
var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
- jQuery('#sap').prepend(document.getElementById('first'));
+ jQuery('#sap').prepend(val( document.getElementById('first') ));
equals( expected, jQuery('#sap').text(), "Check for prepending of element" );
reset();
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
- jQuery('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
+ jQuery('#sap').prepend(val( [document.getElementById('first'), document.getElementById('yahoo')] ));
equals( expected, jQuery('#sap').text(), "Check for prepending of array of elements" );
reset();
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
- jQuery('#sap').prepend(jQuery("#first, #yahoo"));
+ jQuery('#sap').prepend(val( jQuery("#first, #yahoo") ));
equals( expected, jQuery('#sap').text(), "Check for prepending of jQuery object" );
+}
+
+test("prepend(String|Element|Array<Element>|jQuery)", function() {
+ testPrepend(bareObj);
});
+test("prepend(Function)", function() {
+ testPrepend(functionReturningObj);
+})
+
test("prependTo(String|Element|Array<Element>|jQuery)", function() {
expect(6);
var defaultText = 'Try them out:'
@@ -260,28 +308,36 @@ test("prependTo(String|Element|Array<Element>|jQuery)", function() {
t( "Prepend Select", "#prependSelect2, #prependSelect1", ["prependSelect2", "prependSelect1"] );
});
-test("before(String|Element|Array<Element>|jQuery)", function() {
+var testBefore = function(val) {
expect(4);
var expected = 'This is a normal link: bugaYahoo';
- jQuery('#yahoo').before('buga');
+ jQuery('#yahoo').before(val( 'buga' ));
equals( expected, jQuery('#en').text(), 'Insert String before' );
reset();
expected = "This is a normal link: Try them out:Yahoo";
- jQuery('#yahoo').before(document.getElementById('first'));
+ jQuery('#yahoo').before(val( document.getElementById('first') ));
equals( expected, jQuery('#en').text(), "Insert element before" );
reset();
expected = "This is a normal link: Try them out:diveintomarkYahoo";
- jQuery('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
+ jQuery('#yahoo').before(val( [document.getElementById('first'), document.getElementById('mark')] ));
equals( expected, jQuery('#en').text(), "Insert array of elements before" );
reset();
expected = "This is a normal link: diveintomarkTry them out:Yahoo";
- jQuery('#yahoo').before(jQuery("#first, #mark"));
+ jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
equals( expected, jQuery('#en').text(), "Insert jQuery before" );
+}
+
+test("before(String|Element|Array<Element>|jQuery)", function() {
+ testBefore(bareObj);
});
+test("before(Function)", function() {
+ testBefore(functionReturningObj);
+})
+
test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
expect(4);
var expected = 'This is a normal link: bugaYahoo';
@@ -304,28 +360,36 @@ test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
equals( expected, jQuery('#en').text(), "Insert jQuery before" );
});
-test("after(String|Element|Array<Element>|jQuery)", function() {
+var testAfter = function(val) {
expect(4);
var expected = 'This is a normal link: Yahoobuga';
- jQuery('#yahoo').after('buga');
+ jQuery('#yahoo').after(val( 'buga' ));
equals( expected, jQuery('#en').text(), 'Insert String after' );
reset();
expected = "This is a normal link: YahooTry them out:";
- jQuery('#yahoo').after(document.getElementById('first'));
+ jQuery('#yahoo').after(val( document.getElementById('first') ));
equals( expected, jQuery('#en').text(), "Insert element after" );
reset();
expected = "This is a normal link: YahooTry them out:diveintomark";
- jQuery('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
+ jQuery('#yahoo').after(val( [document.getElementById('first'), document.getElementById('mark')] ));
equals( expected, jQuery('#en').text(), "Insert array of elements after" );
reset();
expected = "This is a normal link: YahoodiveintomarkTry them out:";
- jQuery('#yahoo').after(jQuery("#first, #mark"));
- equals( expected, jQuery('#en').text(), "Insert jQuery after" );
+ jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
+ equals( expected, jQuery('#en').text(), "Insert jQuery after" );
+};
+
+test("after(String|Element|Array<Element>|jQuery)", function() {
+ testAfter(bareObj);
});
+test("after(Function)", function() {
+ testAfter(functionReturningObj);
+})
+
test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
expect(4);
var expected = 'This is a normal link: Yahoobuga';
@@ -348,36 +412,44 @@ test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
equals( expected, jQuery('#en').text(), "Insert jQuery after" );
});
-test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
+var testReplaceWith = function(val) {
expect(10);
- jQuery('#yahoo').replaceWith('buga');
+ jQuery('#yahoo').replaceWith(val( 'buga' ));
ok( jQuery("#replace")[0], 'Replace element with string' );
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
reset();
- jQuery('#yahoo').replaceWith(document.getElementById('first'));
+ jQuery('#yahoo').replaceWith(val( document.getElementById('first') ));
ok( jQuery("#first")[0], 'Replace element with element' );
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after element' );
reset();
- jQuery('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
+ jQuery('#yahoo').replaceWith(val( [document.getElementById('first'), document.getElementById('mark')] ));
ok( jQuery("#first")[0], 'Replace element with array of elements' );
ok( jQuery("#mark")[0], 'Replace element with array of elements' );
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
reset();
- jQuery('#yahoo').replaceWith(jQuery("#first, #mark"));
+ jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
ok( jQuery("#first")[0], 'Replace element with set of elements' );
ok( jQuery("#mark")[0], 'Replace element with set of elements' );
- ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
+ ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
+}
+
+test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
+ testReplaceWith(bareObj);
});
+test("replaceWith(Function)", function() {
+ testReplaceWith(functionReturningObj);
+})
+
test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
expect(10);
jQuery('buga').replaceAll("#yahoo");
ok( jQuery("#replace")[0], 'Replace element with string' );
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
-
+
reset();
jQuery(document.getElementById('first')).replaceAll("#yahoo");
ok( jQuery("#first")[0], 'Replace element with element' );
@@ -462,7 +534,12 @@ test("clone() on XML nodes", function() {
}
test("val()", function() {
- expect(8);
+ expect(9);
+
+ document.getElementById('text1').value = "bla";
+ equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
+
+ reset();
equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
// ticket #1714 this caused a JS error in IE
@@ -481,77 +558,95 @@ test("val()", function() {
});
-test("val(String/Number)", function() {
- expect(6);
- document.getElementById('text1').value = "bla";
- equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
-
- jQuery("#text1").val('test');
+var testVal = function(valueObj) {
+ expect(5);
+
+ jQuery("#text1").val(valueObj( 'test' ));
equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
- jQuery("#text1").val(67);
+ jQuery("#text1").val(valueObj( 67 ));
equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
- jQuery("#select1").val("3");
+ jQuery("#select1").val(valueObj( "3" ));
equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
- jQuery("#select1").val(2);
+ jQuery("#select1").val(valueObj( 2 ));
equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
- j.val("asdf");
+ j.val(valueObj( "asdf" ));
equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
j.removeAttr("value");
+}
+
+test("val(String/Number)", function() {
+ testVal(bareObj);
});
-test("html(String)", function() {
+test("val(Function)", function() {
+ testVal(functionReturningObj);
+})
+
+var testHtml = function(valueObj) {
expect(17);
+ window.debug = true;
+
jQuery.scriptorder = 0;
var div = jQuery("#main > div");
- div.html("test");
+ div.html(valueObj("test"));
var pass = true;
for ( var i = 0; i < div.size(); i++ ) {
if ( div.get(i).childNodes.length != 1 ) pass = false;
}
ok( pass, "Set HTML" );
+
+ window.debug = false;
reset();
// using contents will get comments regular, text, and comment nodes
var j = jQuery("#nonnodes").contents();
- j.html("bold");
-
+ j.html(valueObj("bold"));
+
// this is needed, or the expando added by jQuery unique will yield a different html
j.find('b').removeData();
equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "bold", "Check node,textnode,comment with html()" );
-
- jQuery("#main").html("");
- jQuery("#main select").html("");
+
+ jQuery("#main").html(valueObj(""));
+ jQuery("#main select").html(valueObj(""));
equals( jQuery("#main select").val(), "O2", "Selected option correct" );
-
+
var $div = jQuery('');
- equals( $div.html( 5 ).html(), '5', 'Setting a number as html' );
- equals( $div.html( 0 ).html(), '0', 'Setting a zero as html' );
-
+ equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
+ equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
+
reset();
-
- jQuery("#main").html('');
-
+
+ jQuery("#main").html(valueObj(''));
+
stop();
-
- jQuery("#main").html('');
-
- jQuery("#main").html('foo ');
-
+
+ jQuery("#main").html(valueObj(''));
+
+ jQuery("#main").html(valueObj('foo '));
+
// it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
- jQuery("#main").html("