最近使用开发的过程中出现了一个小问题,顺便记录一下原因和方法--字符串缓存
一、jQuery.buildFragment使用方法
1、参数
jQuery.buildFragment( args, context, scripts );
return { fragment: fragment, cacheable: cacheable };
二、思绪析分
1、处置context参数
根据传入到context参值数的不同,确保context为文档根点节document
2、制限可缓存条件
2.1、字符串小于512字节
2.2、字符串不存在option签标(克隆option签标会失丢选中状态,因此不缓存)
2.3、字符串不存在<object>,<embed>签标(IE 6不能把<object>,<embed>签标嵌入到文档片碎中)
2.4、字符串不存在checked属性(只针对克隆具有checked属性点节时失丢选中状态的浏览器,如:Safria)
2.5、字符串中不存在html5签标(只针对不持支html5签标的浏览器)
3、处置args数组
通过jQuery.clean函数格式化处置数组项字符串,并成生dom点节添加到文档片碎中
4、判断缓存值
如果缓存值为由clean函数处置的文档片碎,则数组项字符串略过clean函数处置
5、返回值
函数返回一个对象,保存文档片碎和可缓存状态
三、码源注释析分
【基于jQuery1.8.3】
1 var rnocache = /<(?:script|object|embed|option|style)/i, 2 rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i, 3 rnoshimcache = new RegExp("<(?:" + nodeNames + ")[\\s/>]", "i"); 4 5 jQuery.fragments = {}; 6 7 8 jQuery.buildFragment = function( args, context, scripts ) { 9 var fragment, cacheable, cachehit, 10 first = args[ 0 ]; 11 12 // Set context from what may come in as undefined or a jQuery collection or a node 13 // Updated to fix #12266 where accessing context[0] could throw an exception in IE9/10 & 14 // also doubles as fix for #8950 where plain objects caused createDocumentFragment exception 15 16 // 根据参数context值的不同,确保context为文档根点节document 17 // jQuery1.8.0版本后以代码于对相之前版本有很大改良,以下是改良地方: 18 // 针对context参值数为undefined, jQuery对象,DOM素元点节情况改良代码 19 // 处理了1.8.0版本中context参数为文档片段(#document-fragment)的bug 20 21 context = context || document; 22 context = !context.nodeType && context[0] || context; 23 context = context.ownerDocument || context; 24 25 26 // Only cache "small" (1/2 KB) HTML strings that are associated with the main document 27 // Cloning options loses the selected state, so don't cache them 28 // IE 6 doesn't like it when you put <object> or <embed> elements in a fragment 29 // Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache 30 // Lastly, IE6,7,8 will not correctly reuse cached fragments that were created from unknown elems #10501 31 32 // html字符串小于512字节 33 // 克隆option签标会失丢选中状态,因此不缓存 34 // IE 6不能把<object>,<embed>签标嵌入到文档片碎中 35 // WebKit浏览器(如:Safria)克隆具有checked属性点节时,也会失丢选中状态,因此不缓存,google最新版本不存在该bug 36 // 最后,IE6,7、8不会正确地重用由html5签标素元创立的缓存片段 37 38 if ( args.length === 1 && typeof first === "string" && first.length < 512 && context === document && 39 first.charAt(0) === "<" && !rnocache.test( first ) && 40 41 // 如果浏览器够能克隆checked属性 和 持支html5,
// 或者html字符串中不存在checked 和 不持支html5签标采取xhtml风格 42 (jQuery.support.checkClone || !rchecked.test( first )) && 43 (jQuery.support.html5Clone || !rnoshimcache.test( first )) ) { 44 45 // Mark cacheable and look for a hit 46 // 如果以上条件都满意,则打上可缓存记标 47 cacheable = true; 48 49 // 将数组项字符串(主要是html字符串)缓存到jQuery.fragment对象的属性表列中,并取获缓存值 50 // 如果clean函数已处置过了第二次同相的字符串容内,缓存值则为clean函数处置的文档片碎,字符串析解可以略过clean处置 51 fragment = jQuery.fragments[ first ]; 52 53 // 在clean函数处置了第一次字符串(与第二次同相)后,cachehit为true 54 cachehit = fragment !== undefined; 55 } 56 57 // 判断缓存值 58 if ( !fragment ) { 59 fragment = context.createDocumentFragment(); 60 61 // 通过clean函数处置args数组,将数组每一项字符串都成生dom点节, 62 // 并且添加到供给的文档片碎(fragment)中,因此返回的对象中的fragment属性 63 // 保存了参数args数组项字符串成生的dom点节 64 jQuery.clean( args, context, fragment, scripts ); 65 66 // Update the cache, but only store false 67 // unless this is a second parsing of the same content 68 69 // 当cachehit为true时,jQuery.fragment[first]为clean函数处置的文档片碎 70 if ( cacheable ) { 71 jQuery.fragments[ first ] = cachehit && fragment; 72 } 73 } 74 75 return { fragment: fragment, cacheable: cacheable }; 76 };
转载请注明转自博客园jQuery.buildFragment码源析分
文章结束给大家分享下程序员的一些笑话语录: 《诺基亚投资手机浏览器UCWEB,资金不详或控股》杯具了,好不容易养大的闺女嫁外国。(心疼是你养的吗?中国创业型公司创业初期哪个从国有银行贷到过钱?)