* Return the lowest common ancestor of A and B, or null if they are in * different trees.
(instA, instB)
| 8103 | |
| 8104 | |
| 8105 | function getLowestCommonAncestor(instA, instB) { |
| 8106 | var depthA = 0; |
| 8107 | |
| 8108 | for (var tempA = instA; tempA; tempA = getParent(tempA)) { |
| 8109 | depthA++; |
| 8110 | } |
| 8111 | |
| 8112 | var depthB = 0; |
| 8113 | |
| 8114 | for (var tempB = instB; tempB; tempB = getParent(tempB)) { |
| 8115 | depthB++; |
| 8116 | } // If A is deeper, crawl up. |
| 8117 | |
| 8118 | |
| 8119 | while (depthA - depthB > 0) { |
| 8120 | instA = getParent(instA); |
| 8121 | depthA--; |
| 8122 | } // If B is deeper, crawl up. |
| 8123 | |
| 8124 | |
| 8125 | while (depthB - depthA > 0) { |
| 8126 | instB = getParent(instB); |
| 8127 | depthB--; |
| 8128 | } // Walk in lockstep until we find a match. |
| 8129 | |
| 8130 | |
| 8131 | var depth = depthA; |
| 8132 | |
| 8133 | while (depth--) { |
| 8134 | if (instA === instB || instA === instB.alternate) { |
| 8135 | return instA; |
| 8136 | } |
| 8137 | |
| 8138 | instA = getParent(instA); |
| 8139 | instB = getParent(instB); |
| 8140 | } |
| 8141 | |
| 8142 | return null; |
| 8143 | } |
| 8144 | /** |
| 8145 | * Simulates the traversal of a two-phase, capture/bubble event dispatch. |
| 8146 | */ |
no test coverage detected