tangled
alpha
login
or
join now
yippee.fun
/
morphlex
0
fork
atom
Precise DOM morphing
morphing
typescript
dom
0
fork
atom
overview
issues
pulls
pipelines
Clean up files
joel.drapper.me
4 months ago
d28f3f18
84a46318
-98
3 changed files
expand all
collapse all
unified
split
terser-config.json
test_debug.js
test_trace.js
-5
terser-config.json
···
1
1
-
{
2
2
-
"mangle": true,
3
3
-
"module": true,
4
4
-
"compress": true
5
5
-
}
-54
test_debug.js
···
1
1
-
import { morph } from "./src/morphlex.js"
2
2
-
3
3
-
function dom(html) {
4
4
-
const tmp = document.createElement("div")
5
5
-
tmp.innerHTML = html.trim()
6
6
-
return tmp.firstChild
7
7
-
}
8
8
-
9
9
-
const from = dom(`
10
10
-
<ul>
11
11
-
<li>Item 1</li>
12
12
-
<li>Item 2</li>
13
13
-
<li>Item 3</li>
14
14
-
</ul>
15
15
-
`)
16
16
-
17
17
-
const to = dom(`
18
18
-
<ul>
19
19
-
<li>Item 2</li>
20
20
-
<li>Item 3</li>
21
21
-
</ul>
22
22
-
`)
23
23
-
24
24
-
console.log("From before:", from.outerHTML)
25
25
-
console.log("To:", to.outerHTML)
26
26
-
27
27
-
const observer = new MutationObserver((records) => {
28
28
-
console.log("Mutations:", records.length)
29
29
-
records.forEach((r, i) => {
30
30
-
console.log(` ${i}: type=${r.type}`)
31
31
-
if (r.type === "childList") {
32
32
-
console.log(` added: ${r.addedNodes.length}, removed: ${r.removedNodes.length}`)
33
33
-
r.removedNodes.forEach(n => console.log(` removed: ${n.nodeName} ${n.textContent?.trim()}`))
34
34
-
}
35
35
-
})
36
36
-
})
37
37
-
38
38
-
observer.observe(from, { childList: true, subtree: true })
39
39
-
40
40
-
morph(from, to)
41
41
-
42
42
-
const pending = observer.takeRecords()
43
43
-
console.log("Pending mutations:", pending.length)
44
44
-
pending.forEach((r, i) => {
45
45
-
console.log(` ${i}: type=${r.type}`)
46
46
-
if (r.type === "childList") {
47
47
-
console.log(` added: ${r.addedNodes.length}, removed: ${r.removedNodes.length}`)
48
48
-
r.removedNodes.forEach(n => console.log(` removed: ${n.nodeName} ${n.textContent?.trim()}`))
49
49
-
}
50
50
-
})
51
51
-
52
52
-
observer.disconnect()
53
53
-
54
54
-
console.log("From after:", from.outerHTML)
-39
test_trace.js
···
1
1
-
import { morph } from "./src/morphlex.js"
2
2
-
import { JSDOM } from "jsdom"
3
3
-
4
4
-
const dom = new JSDOM()
5
5
-
global.document = dom.window.document
6
6
-
global.Element = dom.window.Element
7
7
-
global.HTMLInputElement = dom.window.HTMLInputElement
8
8
-
global.DOMParser = dom.window.DOMParser
9
9
-
global.Node = dom.window.Node
10
10
-
11
11
-
function domEl(html) {
12
12
-
const tmp = document.createElement("div")
13
13
-
tmp.innerHTML = html.trim()
14
14
-
return tmp.firstChild
15
15
-
}
16
16
-
17
17
-
const a = domEl(`<div>
18
18
-
<input type="text" value="wrong">
19
19
-
<input type="checkbox" value="right">
20
20
-
</div>`)
21
21
-
22
22
-
const b = domEl(`<div>
23
23
-
<input type="checkbox" value="new">
24
24
-
</div>`)
25
25
-
26
26
-
const textInput = a.children[0]
27
27
-
const checkboxInput = a.children[1]
28
28
-
29
29
-
console.log("Before morph:")
30
30
-
console.log("textInput:", textInput.outerHTML)
31
31
-
console.log("checkboxInput:", checkboxInput.outerHTML)
32
32
-
33
33
-
morph(a, b)
34
34
-
35
35
-
console.log("\nAfter morph:")
36
36
-
console.log("a.children.length:", a.children.length)
37
37
-
console.log("a.children[0]:", a.children[0].outerHTML)
38
38
-
console.log("a.children[0] === textInput:", a.children[0] === textInput)
39
39
-
console.log("a.children[0] === checkboxInput:", a.children[0] === checkboxInput)