当前位置: 代码迷 >> Web前端 >> jQuery源码学习(3)- 缓冲区(buildFragment)
  详细解决方案

jQuery源码学习(3)- 缓冲区(buildFragment)

热度:218   发布时间:2012-07-24 17:47:58.0
jQuery源码学习(三)-- 缓冲区(buildFragment)
var buildFragment = function(args, nodes, scripts){
        var fragment, cacheable, cacheresults, doc, first = args[0];
        /**
         * 继续使用原来的doc
         */
        if (nodes && nodes[0]) {
            doc = nodes[0].ownerDocument || nodes[0];
        }
        
        // Ensure that an attr object doesn't incorrectly stand in as a document
        // object
        // Chrome and Firefox seem to allow this to occur and will throw
        // exception
        // Fixes #8950
		/**
		 * documentFragment 是一个无父窗口的document对象,相当于缓冲区
		 */
        if (!doc.createDocumentFragment) {
            doc = document;
        }
        
        // 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
        if (args.length === 1 && typeof first === "string" &&  first.length < 512 && doc === document &&  first.charAt(0) === "<" && !rnocache.test(first) && (jQuery.support.checkClone || !rchecked.test(first)) && (jQuery.support.html5Clone || !rnoshimcache.test(first))) {
            cacheable = true;//支持缓存
            cacheresults = jQuery.fragments[first];//将first缓存起来
            if (cacheresults && cacheresults !== 1) {
                fragment = cacheresults;
            }
        }
        /**
         * 缓存不存在
         */
        if (!fragment) {
            fragment = doc.createDocumentFragment();//创建文档缓存
            jQuery.clean(args, doc, fragment, scripts);
        }
        /**
         * 如果支持缓存
         */
        if (cacheable) {
            jQuery.fragments[first] = cacheresults ? fragment : 1;
        }
        /**
         * 返回缓存信息
         */
        return {
            fragment: fragment,
            cacheable: cacheable
        };
    };
?

待写

  相关解决方案