MCPcopy Create free account
hub / github.com/bigskysoftware/_hyperscript / _findBestMatch

Function _findBestMatch

src/core/runtime/morph.js:142–165  ·  view source on GitHub ↗
(ctx, node, startPoint, endPoint)

Source from the content-addressed store, hash-verified

140}
141
142function _findBestMatch(ctx, node, startPoint, endPoint) {
143 if (!(node instanceof Element)) return null;
144 var softMatch = null, displaceMatchCount = 0, scanLimit = 10;
145 var newSet = ctx.idMap.get(node), nodeMatchCount = newSet?.size || 0;
146 if (node.id && !newSet) return null;
147 var cursor = startPoint;
148 while (cursor && cursor !== endPoint) {
149 var oldSet = ctx.idMap.get(cursor);
150 if (_isSoftMatch(cursor, node)) {
151 if (oldSet && newSet && [...oldSet].some(id => newSet.has(id))) return cursor;
152 if (!oldSet) {
153 if (scanLimit > 0 && cursor.isEqualNode(node)) return cursor;
154 if (!softMatch) softMatch = cursor;
155 }
156 }
157 displaceMatchCount += oldSet?.size || 0;
158 if (displaceMatchCount > nodeMatchCount) break;
159 if (cursor.contains(document.activeElement)) break;
160 if (--scanLimit < 1 && nodeMatchCount === 0) break;
161 cursor = cursor.nextSibling;
162 }
163 if (softMatch && _matchesUpcomingSibling(ctx, softMatch, node)) return null;
164 return softMatch;
165}
166
167function _matchesUpcomingSibling(ctx, oldElt, startNode) {
168 if (ctx.futureMatches.has(oldElt)) return true;

Callers 1

_morphChildrenFunction · 0.70

Calls 4

_isSoftMatchFunction · 0.70
_matchesUpcomingSiblingFunction · 0.70
getMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected