idiomorph icon indicating copy to clipboard operation
idiomorph copied to clipboard

Add back old BestMatch routine

Open MichaelWest22 opened this issue 11 months ago • 0 comments

https://codepen.io/MichaelWest22/pen/yyBwPww https://codepen.io/MichaelWest22/pen/XJrGzxv

In the old idiomorph there is extra bestMatch code that handles outerHTML swaps. It handles the situation where you are replacing a single node with possibly multiple nodes via the outerHTML morph mode and it find the most id'ed node and swaps that with the single node. This works because we know there is just one node in the old content so finding the best match for a single node is a simple routine. When trying to integrate this with the new Algorithm I found that it was possible to make this feature more generic. By finding the last node in the old child nodes that has id content at each tree level we can apply the original bestMatch checking just to this last node with id's. It may be possible to do this same kind of logic every time but it would get far to complex and slow so instead we can apply it just to the last node with id's. We know this node is the last one we care about so can then check all remaining new nodes for the very best match easily. To do this we just scan for the last id with node at the start of the morphChildren loop and apply an extra check at the start of findBestMatch which will cause it to insert new child nodes that are not the best match till the best match shows up.

Added some tests to show off where it applies and also the above two codepen's show off how it works but this feature only covers off what is kind of an edge case and will often not be needed outside of these edge cases.

MichaelWest22 avatar Jan 31 '25 08:01 MichaelWest22