mojolicious / dom.js

:crystal_ball: A fast and very small HTML/XML DOM parser with CSS selectors
https://www.npmjs.com/package/@mojojs/dom
MIT License
20 stars 3 forks source link

Optimize DOM object reuse #1

Closed kraih closed 2 years ago

kraih commented 2 years ago
import DOM from '@mojojs/dom';

const html = new DOM('<html><body><p>Test 1</p><p>Test 2</p></body></html>');
const div = new DOM('<div>Hello World!</div>', {fragment: true});
html.at('p').replace(div);

It would be nice if the case above was optimized, so that replace didn't have to stringify and re-parse the div snippet. Since our node tree is made up of classes, a simple deep clone on it won't work, but we could add clone methods to all the node classes, so that const clone = div.currentNode.clone() would simply give us a proper deep cloned version with classes in tact. Then all the modification methods of the DOM object could rely on that.

kraih commented 2 years ago

Added with https://github.com/mojolicious/dom.js/commit/4d34d31d64a1c3b926aad0bea1ab393213c00242.