首页 > 代码库 > JQuery日记 5.11 Sizzle选择器(五)

JQuery日记 5.11 Sizzle选择器(五)

//设置当前document和document对应的变量和方法
setDocument = Sizzle.setDocument = function( node ) {
	var hasCompare,
		//node为Element时返回node所属document
		//node为Document时返回node
		//node为空时返回window.document
		doc = node ? node.ownerDocument || node : preferredDoc,
		//document所属window
		parent = doc.defaultView;

	// If no document and documentElement is available, return
	if ( doc === document || doc.nodeType !== 9 || !doc.documentElement ) {
		return document;
	}

	// Set our document
	//设置全局的document为当前doc
	document = doc;
	docElem = doc.documentElement;

	// Support tests
	documentIsHTML = !isXML( doc );

	// Support: IE>8
	// If iframe document is assigned to "document" variable and if iframe has been reloaded,
	// IE will throw "permission denied" error when accessing "document" variable, see jQuery #13936
	// IE6-8 do not support the defaultView property so parent will be undefined
	
	/* 
	 * 如果 iframe文档已经指定了document并且如果iframe重载
	 * 当访问document变量时,IE将抛出"permission denied"错误
	 * IE6-8不支持defaultView所以parent变量为undefined
	 */
	
	//所以要在unload的时候重新setDocument()
	//这个判断说明当前代码在frame中 
	if ( parent && parent !== parent.top ) {
		// IE11 does not have attachEvent, so all must suffer
		//frame卸载时重新设置document
		if ( parent.addEventListener ) {
			parent.addEventListener( "unload", function() {
				//未传入参数document=window.document
				setDocument();
			}, false );
		} else if ( parent.attachEvent ) {
			parent.attachEvent( "onunload", function() {
				setDocument();
			});
		}
	}
	
	//一些能力检查,添加到support中
	/* Attributes
	---------------------------------------------------------------------- */

	// Support: IE<8
	// Verify that getAttribute really returns attributes and not properties (excepting IE8 booleans)
	// IE8以下div.getAttribute("className")会得到class
	support.attributes = assert(function( div ) {
		div.className = "i";
		return !div.getAttribute("className");
	});

	/* getElement(s)By*
	---------------------------------------------------------------------- */

	// Check if getElementsByTagName("*") returns only elements
	//检查getElementsByTagName是否只返回Element元素
	support.getElementsByTagName = assert(function( div ) {
		div.appendChild( doc.createComment("") );
		return !div.getElementsByTagName("*").length;
	});

	// Check if getElementsByClassName can be trusted
	//检查getElementsByClassName是否能正确处理多class的情况
	support.getElementsByClassName = rnative.test( doc.getElementsByClassName ) && assert(function( div ) {
		div.innerHTML = "<div class=‘a‘></div><div class=‘a i‘></div>";

		// Support: Safari<4
		// Catch class over-caching
		div.firstChild.className = "i";
		// Support: Opera<10
		// Catch gEBCN failure to find non-leading classes
		return div.getElementsByClassName("i").length === 2;
	});

	// Support: IE<10
	// Check if getElementById returns elements by name
	// The broken getElementById methods don‘t pick up programatically-set names,
	// so use a roundabout getElementsByName test
	
	//检查是否getElementById会根据name返回元素
	support.getById = assert(function( div ) {
		docElem.appendChild( div ).id = expando;
		return !doc.getElementsByName || !doc.getElementsByName( expando ).length;
	});

	// ID find and filter
	if ( support.getById ) {
		Expr.find["ID"] = function( id, context ) {
			if ( typeof context.getElementById !== strundefined && documentIsHTML ) {
				var m = context.getElementById( id );
				// Check parentNode to catch when Blackberry 4.6 returns
				// nodes that are no longer in the document #6963
				return m && m.parentNode ? [m] : [];
			}
		};
		Expr.filter["ID"] = function( id ) {
			//将转义字符转回字符串
			var attrId = id.replace( runescape, funescape );
			return function( elem ) {
				return elem.getAttribute("id") === attrId;
			};
		};
	} else {
		// Support: IE6/7
		// getElementById is not reliable as a find shortcut
		//IE6,7的getElementById因为会根据name返回元素
		//所以是不能用原生getElementById方法获取元素的
		//所以删除
		delete Expr.find["ID"];

		Expr.filter["ID"] =  function( id ) {
			var attrId = id.replace( runescape, funescape );
			return function( elem ) {
				var node = typeof elem.getAttributeNode !== strundefined && elem.getAttributeNode("id");
				return node && node.value =http://www.mamicode.com/== attrId;>