| 1543 | } |
| 1544 | |
| 1545 | function addCombinator( matcher, combinator, base ) { |
| 1546 | var dir = combinator.dir, |
| 1547 | checkNonElements = base && dir === "parentNode", |
| 1548 | doneName = done++; |
| 1549 | |
| 1550 | return combinator.first ? |
| 1551 | // Check against closest ancestor/preceding element |
| 1552 | // 检查最近的祖先/前元素 first |
| 1553 | function( elem, context, xml ) { |
| 1554 | while ( (elem = elem[ dir ]) ) { |
| 1555 | if ( elem.nodeType === 1 || checkNonElements ) { |
| 1556 | return matcher( elem, context, xml ); |
| 1557 | } |
| 1558 | } |
| 1559 | } : |
| 1560 | |
| 1561 | // Check against all ancestor/preceding elements |
| 1562 | //前检查所有祖先/元素 |
| 1563 | function( elem, context, xml ) { |
| 1564 | var oldCache, outerCache, |
| 1565 | newCache = [ dirruns, doneName ]; |
| 1566 | |
| 1567 | // We can't set arbitrary data on XML nodes, so they don't benefit from dir caching |
| 1568 | if ( xml ) { |
| 1569 | while ( (elem = elem[ dir ]) ) { |
| 1570 | if ( elem.nodeType === 1 || checkNonElements ) { |
| 1571 | if ( matcher( elem, context, xml ) ) { |
| 1572 | return true; |
| 1573 | } |
| 1574 | } |
| 1575 | } |
| 1576 | } else { |
| 1577 | //递归当前元素的父元素 |
| 1578 | //这个层级选择器是 "空"或者"~" |
| 1579 | while ( (elem = elem[ dir ]) ) { |
| 1580 | if ( elem.nodeType === 1 || checkNonElements ) { |
| 1581 | outerCache = elem[ expando ] || (elem[ expando ] = {}); |
| 1582 | if ( (oldCache = outerCache[ dir ]) && |
| 1583 | oldCache[ 0 ] === dirruns && oldCache[ 1 ] === doneName ) { |
| 1584 | |
| 1585 | // Assign to newCache so results back-propagate to previous elements |
| 1586 | return (newCache[ 2 ] = oldCache[ 2 ]); |
| 1587 | } else { |
| 1588 | // Reuse newcache so results back-propagate to previous elements |
| 1589 | outerCache[ dir ] = newCache; |
| 1590 | |
| 1591 | // A match means we're done; a fail means we have to keep checking |
| 1592 | if ( (newCache[ 2 ] = matcher( elem, context, xml )) ) { |
| 1593 | return true; |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | }; |
| 1600 | } |
| 1601 | |
| 1602 | function elementMatcher( matchers ) { |