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
        };
    };
 

待写

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值