Commit 9c28a320 authored by Vladimir Zhuravlev's avatar Vladimir Zhuravlev Committed by Dave Methvin

Fix #11566, allow appending to DocumentFragment, closes gh-814.

parent 0b352f6c
...@@ -127,7 +127,7 @@ jQuery.fn.extend({ ...@@ -127,7 +127,7 @@ jQuery.fn.extend({
append: function() { append: function() {
return this.domManip(arguments, true, function( elem ) { return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 ) { if ( this.nodeType === 1 || this.nodeType === 11 ) {
this.appendChild( elem ); this.appendChild( elem );
} }
}); });
...@@ -135,7 +135,7 @@ jQuery.fn.extend({ ...@@ -135,7 +135,7 @@ jQuery.fn.extend({
prepend: function() { prepend: function() {
return this.domManip(arguments, true, function( elem ) { return this.domManip(arguments, true, function( elem ) {
if ( this.nodeType === 1 ) { if ( this.nodeType === 1 || this.nodeType === 11 ) {
this.insertBefore( elem, this.firstChild ); this.insertBefore( elem, this.firstChild );
} }
}); });
......
...@@ -280,40 +280,89 @@ test("unwrap()", function() { ...@@ -280,40 +280,89 @@ test("unwrap()", function() {
jQuery("body > span.unwrap").remove(); jQuery("body > span.unwrap").remove();
}); });
var testAppend = function(valueObj) { var getWrappedElement = function() {
expect(46); return jQuery("#sap");
var defaultText = "Try them out:" };
var result = jQuery("#first").append(valueObj("<b>buga</b>"));
equal( result.text(), defaultText + "buga", "Check if text appending works" );
equal( jQuery("#select3").append(valueObj("<option value='appendTest'>Append Test</option>")).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element");
QUnit.reset(); var getWrappedDocumentFragment = function() {
var f = document.createDocumentFragment();
// copy contents of #sap into new fragment
var clone = jQuery("#sap")[0].cloneNode(true);
var childs = clone.childNodes;
while (clone.childNodes.length) {
f.appendChild(clone.childNodes[0]);
}
clone = null;
return jQuery(f);
};
var testAppendForObject = function(valueObj, isFragment) {
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
jQuery("#sap").append(valueObj(document.getElementById("first"))); var objType = " " + (isFragment ? "(DocumentFragment)" : "(Element)");
equal( jQuery("#sap").text(), expected, "Check for appending of element" ); var getObj = isFragment ? getWrappedDocumentFragment : getWrappedElement;
var obj = getObj();
obj.append(valueObj(document.getElementById("first")));
equal( obj.text(), expected, "Check for appending of element" + objType);
QUnit.reset(); QUnit.reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
jQuery("#sap").append(valueObj([document.getElementById("first"), document.getElementById("yahoo")])); obj = getObj();
equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" ); obj.append(valueObj([document.getElementById("first"), document.getElementById("yahoo")]));
equal( obj.text(), expected, "Check for appending of array of elements" + objType );
QUnit.reset(); QUnit.reset();
expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:"; expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
jQuery("#sap").append(valueObj(jQuery("#yahoo, #first"))); obj = getObj();
equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" ); obj.append(valueObj(jQuery("#yahoo, #first")));
equal( obj.text(), expected, "Check for appending of jQuery object" + objType );
QUnit.reset(); QUnit.reset();
jQuery("#sap").append(valueObj( 5 )); obj = getObj();
ok( jQuery("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" ); obj.append(valueObj( 5 ));
ok( obj.text().match( /5$/ ), "Check for appending a number" + objType );
QUnit.reset();
obj = getObj();
obj.append(valueObj( " text with spaces " ));
ok( obj.text().match(/ text with spaces $/), "Check for appending text with spaces" + objType );
QUnit.reset();
obj = getObj();
ok( obj.append(valueObj( [] )), "Check for appending an empty array." + objType );
ok( obj.append(valueObj( "" )), "Check for appending an empty string." + objType );
ok( obj.append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." + objType );
QUnit.reset();
obj = getObj();
obj.append(valueObj( document.getElementById("form") ));
equal( obj.children("form").size(), 1, "Check for appending a form" + objType ); // Bug #910
QUnit.reset(); QUnit.reset();
jQuery("#sap").append(valueObj( " text with spaces " )); var obj = getObj();
ok( jQuery("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" ); var prev = obj.children().length;
obj.append(
"<span></span>",
"<span></span>",
"<span></span>"
);
equal( obj.children().length, prev + 3, "Make sure that multiple arguments works." + objType );
QUnit.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." ); var testAppend = function(valueObj) {
expect(56);
testAppendForObject(valueObj, false);
testAppendForObject(valueObj, true);
var defaultText = "Try them out:"
var result = jQuery("#first").append(valueObj("<b>buga</b>"));
equal( result.text(), defaultText + "buga", "Check if text appending works" );
equal( jQuery("#select3").append(valueObj("<option value='appendTest'>Append Test</option>")).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element");
QUnit.reset(); QUnit.reset();
jQuery("form").append(valueObj("<input name='radiotest' type='radio' checked='checked' />")); jQuery("form").append(valueObj("<input name='radiotest' type='radio' checked='checked' />"));
...@@ -339,10 +388,6 @@ var testAppend = function(valueObj) { ...@@ -339,10 +388,6 @@ var testAppend = function(valueObj) {
ok( jQuery(this).is(":checked"), "Append with name attribute after checked attribute"); ok( jQuery(this).is(":checked"), "Append with name attribute after checked attribute");
}).remove(); }).remove();
QUnit.reset();
jQuery("#sap").append(valueObj( document.getElementById("form") ));
equal( jQuery("#sap>form").size(), 1, "Check for appending a form" ); // Bug #910
QUnit.reset(); QUnit.reset();
var pass = true; var pass = true;
try { try {
...@@ -404,17 +449,6 @@ var testAppend = function(valueObj) { ...@@ -404,17 +449,6 @@ var testAppend = function(valueObj) {
equal( $radio[0].checked, true, "Reappending radios uphold which radio is checked" ); equal( $radio[0].checked, true, "Reappending radios uphold which radio is checked" );
equal( $radioNot[0].checked, false, "Reappending radios uphold not being checked" ); equal( $radioNot[0].checked, false, "Reappending radios uphold not being checked" );
QUnit.reset(); QUnit.reset();
var prev = jQuery("#sap").children().length;
jQuery("#sap").append(
"<span></span>",
"<span></span>",
"<span></span>"
);
equal( jQuery("#sap").children().length, prev + 3, "Make sure that multiple arguments works." );
QUnit.reset();
} }
test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() { test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment