JQuery. buildFragment usage and Analysis of ideas

Source: Internet
Author: User

I. jQuery. buildFragment usage
1. Parameters
JQuery. buildFragment (args, context, scripts); 2. Return Value
Return {fragment: fragment, cacheable: cacheable };
Ii. Train of Thought Analysis
1. process the context Parameter
Make sure that context is the document root node document
2. Restrict cache Conditions
2.1. the string is less than 512 bytes.
2.2. the option label does not exist in the string (the clone option label will lose the selected status, so it is not cached)
2.3. The <object>, <embed> tag does not exist in the string (IE 6 cannot embed the <object>, <embed> tag into the File Fragment)
2.4. The string does not have the checked attribute (only for browsers with the checked attribute node being cloned that lose the selected status, such as Safria)
2.5. The html5 tag does not exist in the string (only for browsers that do not support html5 tags)
3. process The args Array
Format the array string using the jQuery. clean function, and generate a dom node to add it to the File Fragment.
4. Determine the cache Value
If the cached value is a File Fragment processed by the clean function, the array item string is skipped by the clean function.
5. return values
The function returns an object to save the file fragments and the cache status.
Iii. source code annotation Analysis
[Based on jQuery1.8.3] Copy codeThe Code is 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,
First = args [0];
// Set context from what may come in as undefined or a jQuery collection or a node
// Updated to fix #12266 where accessing context [0] cocould throw an exception in IE9/10 &
// Also doubles as fix for #8950 where plain objects caused createDocumentFragment exception
// Make sure that context is the document root node according to the context value of the Parameter
// The code after jQuery1.8.0 is greatly improved compared with the previous version. The following are the improvements:
// Code improvement for the context parameter values of undefined and jQuery objects and DOM element nodes
// Fixed the bug where the context parameter in version 1.8.0 is a document segment (# document-fragment ).
Context = context | document;
Context =! Context. nodeType & context [0] | context;
Context = context. ownerDocument | context;
// Only cache "small" (1/2 KB) HTML strings that are associated with the main document
// Cloning options loses the selected state, so don't cache them
// IE 6 doesn't like it when you put <object> or <embed> elements in a fragment
// Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache
// Lastly, IE6, 7,8 will not correctly reuse cached fragments that were created from unknown elems #10501
// Html string smaller than 512 bytes
// The clone option label will lose the selected status, so it is not cached
// The <object> and <embed> labels cannot be embedded into file fragments in IE 6.
// When the WebKit browser (such as Safria) clones a node with the checked attribute, the selected status is also lost, so the node is not cached. This bug does not exist in the latest google version.
// Finally, IE6, 7, and 8 won't properly reuse the cache fragments created by html5 tag Elements
If (args. length = 1 & typeof first = "string" & first. length <512 & context = document &&
First. charAt (0) ===" <"&&! Rnocache. test (first )&&
// If the browser can clone the checked attribute and support html5, or the html string does not contain checked or html5 tag Elements
(JQuery. support. checkClone |! Rchecked. test (first ))&&
(JQuery.support.html 5 Clone |! Rnoshimcache. test (first ))){
// Mark cacheable and look for a hit
// If all the above conditions are met, the cache can be marked
Cacheable = true;
// Cache the array string (mainly html string) to the attribute list of the jQuery. fragment object and obtain the cache value.
// If the clean function has processed the same string content for the second time, the cache value is the File Fragment processed by the clean function. String Parsing can be omitted from the clean function.
Fragment = jQuery. fragments [first];
// After the clean function processes the first string (the same as the second string), cachehit is true.
Cachehit = fragment! = Undefined;
}
// Determine the cache Value
If (! Fragment ){
Fragment = context. createDocumentFragment ();
// Use the clean function to process the args array and generate a dom node for each string of the array,
// And add it to the provided fragment. Therefore, the fragment attribute in the returned object
// Saves the dom node generated by the args array string parameter
JQuery. clean (args, context, fragment, scripts );
// Update the cache, but only store false
// Unless this is a second parsing of the same content
// When cachehit is true, jQuery. fragment [first] is the File fragment processed by the clean function.
If (cacheable ){
JQuery. fragments [first] = cachehit & fragment;
}
}
Return {fragment: fragment, cacheable: cacheable };
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.