X-Git-Url: http://git.asbjorn.it/?a=blobdiff_plain;ds=inline;f=test%2Funit%2Fmanipulation.js;h=00984168ca385de088031b221f029fc9128687fa;hb=b996026e383eae34dd52824dda5bbe029e2eda51;hp=0327f93b2379adc26c73c53d4e9d7dfee214e2da;hpb=7c04a64016c1f295dfdf4e731f693f8400a987bc;p=jquery.git
diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js
index 0327f93..0098416 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() {
- expect(10);
+var testWrap = function(val) {
+ expect(15);
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,47 @@ 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" );
+
+ // 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" );
+ equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
+
+ // Try to wrap an element with multiple elements (should fail)
+ j = jQuery("
").children().wrap("");
+ equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
+ equals( j.length, 1, "There should only be one element (no cloning)." );
+ equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
+}
+
+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 +76,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" );
+}
+
+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('
');
@@ -71,51 +105,91 @@ test("wrapInner(String|Element)", function() {
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" );
+}
+
+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 testUnwrap = function() {
+ expect(9);
+
+ jQuery("body").append('
ab
cd
ef
');
+
+ var abcd = jQuery('#unwrap1 > span, #unwrap2 > span').get(),
+ abcdef = jQuery('#unwrap span').get();
+
+ equals( jQuery('#unwrap1 span, #unwrap2 span:first').unwrap().length, 3, 'make #unwrap1 and #unwrap2 go away' );
+ same( jQuery('#unwrap > span').get(), abcd, 'all four spans should still exist' );
+
+ same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap3 > span').get(), 'make all b in #unwrap3 go away' );
+
+ same( jQuery('#unwrap3 span').unwrap().get(), jQuery('#unwrap > span.unwrap3').get(), 'make #unwrap3 go away' );
+
+ same( jQuery('#unwrap').children().get(), abcdef, '#unwrap only contains 6 child spans' );
+
+ same( jQuery('#unwrap > span').unwrap().get(), jQuery('body > span.unwrap').get(), 'make the 6 spans become children of body' );
+
+ same( jQuery('body > span.unwrap').unwrap().get(), jQuery('body > span.unwrap').get(), 'can\'t unwrap children of body' );
+ same( jQuery('body > span.unwrap').unwrap().get(), abcdef, 'can\'t unwrap children of body' );
+
+ same( jQuery('body > span.unwrap').get(), abcdef, 'body contains 6 .unwrap child spans' );
+
+ jQuery('body > span.unwrap').remove();
+}
+
+test("unwrap()", function() {
+ testUnwrap();
+});
+
+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("