From: John Resig Date: Mon, 7 Sep 2009 18:58:01 +0000 (+0000) Subject: Split out the fragment-building code from domManip. Switched core.js to using that... X-Git-Url: http://git.asbjorn.it/?a=commitdiff_plain;h=9d8d74569c8114f9c9b0f42bca71536e154c5944;p=jquery.git Split out the fragment-building code from domManip. Switched core.js to using that instead. Also moved the standalone tag detection to $(...) for performance. --- diff --git a/src/core.js b/src/core.js index 5ccf158..6553258 100644 --- a/src/core.js +++ b/src/core.js @@ -31,6 +31,9 @@ var jQuery = function( selector, context ) { // Used for trimming whitespace rtrim = /^\s+|\s+$/g, + // Match a standalone tag + rsingleTag = /^<(\w+)\s*\/?>$/, + // Keep a UserAgent string for use with jQuery.browser userAgent = navigator.userAgent.toLowerCase(), @@ -41,7 +44,7 @@ var jQuery = function( selector, context ) { jQuery.fn = jQuery.prototype = { init: function( selector, context ) { - var match, elem, ret; + var match, elem, ret, doc; // Handle $(""), $(null), or $(undefined) if ( !selector ) { @@ -70,7 +73,19 @@ jQuery.fn = jQuery.prototype = { // HANDLE: $(html) -> $(array) if ( match[1] ) { - selector = jQuery.clean( [ match[1] ], context ); + doc = (context ? context.ownerDocument || context : document); + + // If a single string is passed in and it's a single tag + // just do a createElement and skip the rest + ret = rsingleTag.exec( selector ); + + if ( ret ) { + selector = [ doc.createElement( ret[1] ) ]; + + } else { + ret = buildFragment( [ match[1] ], [ doc ] ); + selector = (ret.cacheable ? ret.fragment.cloneNode(true) : ret.fragment).childNodes; + } // HANDLE: $("#id") } else { diff --git a/src/manipulation.js b/src/manipulation.js index dbf86a7..30ba5ab 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -1,6 +1,5 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g, rleadingWhitespace = /^\s+/, - rsingleTag = /^<(\w+)\s*\/?>$/, rxhtmlTag = /(<(\w+)[^>]*?)\/>/g, rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i, rinsideTable = /^<(thead|tbody|tfoot|colg|cap)/, @@ -166,8 +165,7 @@ jQuery.fn.extend({ }, domManip: function( args, table, callback ) { - var fragment, scripts, cacheable, cached, cacheresults, first, - value = args[0]; + var results, first, value = args[0], scripts = []; if ( jQuery.isFunction(value) ) { return this.each(function() { @@ -177,23 +175,14 @@ jQuery.fn.extend({ } if ( this[0] ) { - if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf(" 1 || i > 0 ? - fragment.cloneNode(true) : - fragment + results.cacheable || this.length > 1 || i > 0 ? + results.fragment.cloneNode(true) : + results.fragment ); } } @@ -213,10 +202,6 @@ jQuery.fn.extend({ if ( scripts ) { jQuery.each( scripts, evalScript ); } - - if ( cacheable ) { - jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1; - } } return this; @@ -230,6 +215,33 @@ jQuery.fn.extend({ } }); +function buildFragment(args, nodes, scripts){ + var fragment, cacheable, cached, cacheresults, doc; + + if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf(""); + var $div = jQuery("
")[0]; var $span = jQuery("", $div); equals($span.length, 1, "Verify a span created with a div context works, #1763"); });