One, jquery.buildfragment use method
1. Parameter
Jquery.buildfragment (args, context, scripts); 2, return value
return {fragment:fragment, cacheable:cacheable};
second, the analysis of ideas
1, processing the context parameters
Make sure the context is the document root node, based on the difference in the values passed to the context parameter
2, limit the cache conditions
2.1, the string is less than 512 bytes
2.2, the string does not exist option label (clone option label will lose the selected state, so do not cache)
2.3, the string does not exist <object>,<embed> label (IE 6 cannot embed <object>,<embed> tags in document fragmentation)
2.4, the string does not exist the checked attribute (only the browser that loses the selected state when the clone owns the checked attribute node, such as: Safria)
2.5, the string does not exist HTML5 tags (only for browsers that do not support HTML5 tags)
3, processing args array
Format the array item string with the Jquery.clean function and generate a DOM node to add to the document fragment
4, to determine the cache value
If the cached value is a document fragment processed by the clean function, the array item string is skipped over the clean function
5, return value
function to return an object, save document fragmentation and cache state
Third, source code annotation Analysis
"Based on jQuery1.8.3"
Copy Code code as follows:
var rnocache =/< (?: Script|object|embed|option|style)/I,
rchecked =/checked\s* (?: [^=]|=\s*.checked.) /I,
Rnoshimcache = new RegExp ("< (?:" + Nodenames + ") [\\s/>]", "I");
Jquery.fragments = {};
Jquery.buildfragment = function (args, context, scripts) {
var fragment, cacheable, CacheHit,
A = args[0];
Set context from what could come in as undefined or a jQuery collection or a node
Updated to fix #12266 where accessing context[0] could throw a exception in IE9/10 &
Also doubles as fix for #8950 where plain objects caused createdocumentfragment exception
Make sure the context is the document root node according to the different parameters
After the jQuery1.8.0 version, the code has greatly improved relative to the previous version, and the following are the improvements:
Improved code for context parameter values of undefined, jquery objects, DOM element nodes
Resolves a bug in the 1.8.0 version where the context parameter is a document fragment (#document-fragment)
Context = Context | | Document
context =!context.nodetype && Context[0] | | Context
Context = Context.ownerdocument | | Context
Only cache "Small" (1/2 KB) HTML strings which are associated with the main document
Cloning options loses the selected state, so don ' t cache them
IE 6 doesn ' t like it when it's put <object> or <embed> elements in a fragment
Also, WebKit does not clone ' checked ' attributes in CloneNode, so don ' t cache
Lastly, ie6,7,8 won't correctly reuse cached fragments that were created to unknown Elems #10501
HTML string less than 512 bytes
The Clone option label loses the selected state and therefore does not cache
IE 6 Cannot embed <object>,<embed> tags in document fragments
WebKit browser (such as: Safria) clone has the checked attribute node, will also lose the selected state, so do not cache, the latest version of Google does not exist the bug
Finally, ie6,7, 8 does not properly reuse cached fragments created by HTML5 tag elements
if (args.length = = 1 && typeof a = "string" && First.length < && context = Docume NT &&
First.charat (0) = = "<" &&!rnocache.test (a) &&
If the browser can clone the checked property and support HTML5, or the checked and HTML5 tag elements are not present in the HTML string
(JQuery.support.checkClone | |!rchecked.test (a) &&
(JQuery.support.html5Clone | |!rnoshimcache.test (a)) {
Mark cacheable and look for a hit
If all of the above conditions are met, then a cache mark is made
Cacheable = true;
Caches the array item string (primarily an HTML string) into the property list of the Jquery.fragment object and gets the cached value
If the clean function has processed the same string content for the second time, the cached value is a document fragment processed by the clean function, and string parsing can be skipped over clean processing
fragment = jquery.fragments[A;
CacheHit is true after the clean function has processed the first string (same as the second)
CacheHit = fragment!== undefined;
}
Determining Cache values
if (!fragment) {
fragment = Context.createdocumentfragment ();
Process the args array with the clean function, generating the DOM node for each string of the array.
And added to the provided document fragment (fragment), so the fragment property in the returned object
The DOM node generated by the parameter args array item string is saved
Jquery.clean (args, context, fragment, scripts);
Update the cache, but only store false
Unless this is a second parsing of the same content
Jquery.fragment[first] A document fragment processed for the clean function when CacheHit is true
if (cacheable) {
jquery.fragments[i = CacheHit && fragment;
}
}
return {fragment:fragment, cacheable:cacheable};
};