Optimize for the case where a fragment-bound element is being injected into one other...
[jquery.git] / src / core.js
index 3ff95e0..81b4831 100644 (file)
@@ -100,6 +100,8 @@ jQuery.fn = jQuery.prototype = {
                                                selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes;
                                        }
 
+                                       return jQuery.merge( this, selector );
+                                       
                                // HANDLE: $("#id")
                                } else {
                                        elem = document.getElementById( match[2] );
@@ -470,6 +472,27 @@ jQuery.extend({
        error: function( msg ) {
                throw msg;
        },
+       
+       parseJSON: function( data ) {
+               if ( typeof data !== "string" || !data ) {
+                       return null;
+               }
+               
+               // Make sure the incoming data is actual JSON
+               // Logic borrowed from http://json.org/json2.js
+               if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
+                       .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
+                       .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {
+
+                       // Try to use the native JSON parser first
+                       return window.JSON && window.JSON.parse ?
+                               window.JSON.parse( data ) :
+                               (new Function("return " + data))();
+
+               } else {
+                       jQuery.error( "Invalid JSON: " + data );
+               }
+       },
 
        noop: function() {},