···22 <img src="https://github.com/phlex-ruby/morphlex/assets/246692/128ebe6a-bdf3-4b88-8a40-f29df64b3ac8" alt="Morphlex" width="481">
33</p>
4455-Morphlex is a 2KB DOM morphing library that transforms one DOM tree to match another while preserving element state and making minimal changes.
55+Morphlex is a ~2KB (gzipped) DOM morphing library that transforms one DOM tree to match another while preserving element state and making minimal changes.
6677## Installation
88···2929// Morph only the inner content
3030morphInner(currentNode, referenceNode)
3131```
3232-3333-The `currentNode` is transformed to match the `referenceNode`. The `referenceNode` remains unchanged.
3434-3535-## How it works
3636-3737-Morphlex uses a smart matching algorithm that produces minimal DOM operations for common patterns like inserts, deletes, and reordering.
3838-3939-### Matching strategy
4040-4141-When morphing child nodes, Morphlex tries multiple matching strategies in order of specificity:
4242-4343-1. **Exact match** — Nodes that are completely identical (`isEqualNode`)
4444-2. **ID match** — Elements with the same `id` attribute
4545-3. **ID set match** — Elements containing the same nested IDs (see below)
4646-4. **Tag match** — Elements with the same tag name, or nodes with the same type
4747-4848-This cascading approach means most updates find optimal matches quickly, while still handling edge cases gracefully.
4949-5050-### ID sets
5151-5252-ID sets are inspired by [Idiomorph](https://github.com/bigskysoftware/idiomorph). Each element is tagged with the set of IDs it contains, including deeply nested ones. This helps match elements even when they've moved or been restructured.
5353-5454-For example, if you have a card with `id="card-123"` nested inside a container, the container's ID set includes `"card-123"`. When morphing, Morphlex can match the container based on that nested ID, even if the container itself has no ID.
5555-5656-### Minimal operations
5757-5858-After matching, Morphlex processes nodes in order and makes the minimum number of DOM operations:
5959-6060-- Matched elements are moved into position (if needed) and recursively morphed
6161-- New nodes are inserted at the correct position
6262-- Unmatched nodes are removed
6363-6464-This means operations like sorting a list or inserting items in the middle produce exactly the moves you'd expect, with no unnecessary removals or recreations.
6565-6666-## Contributing
6767-6868-Found a bug or have a feature request? Open an issue. Want to contribute? Open a pull request.