| 1670 | //这是一个递归深搜的过程。 |
| 1671 | //addCombinator方法就是为了生成有位置词素的匹配器。 |
| 1672 | function addCombinator(matcher, combinator, base) { |
| 1673 | var dir = combinator.dir, |
| 1674 | checkNonElements = base && dir === "parentNode", |
| 1675 | doneName = done++; //第几个关系选择器 |
| 1676 | |
| 1677 | return combinator.first ? |
| 1678 | // Check against closest ancestor/preceding element |
| 1679 | // 检查最靠近的祖先元素 |
| 1680 | // 如果是紧密关系的位置词素 |
| 1681 | function(elem, context, xml) { |
| 1682 | while ((elem = elem[dir])) { |
| 1683 | if (elem.nodeType === 1 || checkNonElements) { |
| 1684 | //找到第一个亲密的节点,立马就用终极匹配器判断这个节点是否符合前面的规则 |
| 1685 | return matcher(elem, context, xml); |
| 1686 | } |
| 1687 | } |
| 1688 | } : |
| 1689 | |
| 1690 | // Check against all ancestor/preceding elements |
| 1691 | //检查最靠近的祖先元素或兄弟元素(概据>、~、+还有空格检查) |
| 1692 | //如果是不紧密关系的位置词素 |
| 1693 | function(elem, context, xml) { |
| 1694 | var data, cache, outerCache, |
| 1695 | dirkey = dirruns + " " + doneName; |
| 1696 | |
| 1697 | // We can't set arbitrary data on XML nodes, so they don't benefit from dir caching |
| 1698 | // 我们不可以在xml节点上设置任意数据,所以它们不会从dir缓存中受益 |
| 1699 | if (xml) { |
| 1700 | while ((elem = elem[dir])) { |
| 1701 | if (elem.nodeType === 1 || checkNonElements) { |
| 1702 | if (matcher(elem, context, xml)) { |
| 1703 | return true; |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | } else { |
| 1708 | while ((elem = elem[dir])) { |
| 1709 | //如果是不紧密的位置关系 |
| 1710 | //那么一直匹配到true为止 |
| 1711 | //例如祖宗关系的话,就一直找父亲节点直到有一个祖先节点符合规则为止 |
| 1712 | if (elem.nodeType === 1 || checkNonElements) { |
| 1713 | outerCache = elem[expando] || (elem[expando] = {}); |
| 1714 | //如果有缓存且符合下列条件则不用再次调用matcher函数 |
| 1715 | if ((cache = outerCache[dir]) && cache[0] === dirkey) { |
| 1716 | if ((data = cache[1]) === true || data === cachedruns) { |
| 1717 | return data === true; |
| 1718 | } |
| 1719 | } else { |
| 1720 | cache = outerCache[dir] = [dirkey]; |
| 1721 | cache[1] = matcher(elem, context, xml) || cachedruns; //cachedruns//正在匹配第几个元素 |
| 1722 | if (cache[1] === true) { |
| 1723 | return true; |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | }; |