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
Improve document handling
joel.drapper.me
4 months ago
3b136336
45252fd9
+43
-3
2 changed files
expand all
collapse all
unified
split
src
morphlex.ts
test
new
document.ts
+4
-3
src/morphlex.ts
···
33
33
*
34
34
* @param from The source document to morph from.
35
35
* @param to The target document or string to morph to.
36
36
+
* @param options Optional configuration for the morphing behavior.
36
37
* @example
37
38
* ```ts
38
38
-
* morphDocument(document, newDocument)
39
39
+
* morphDocument(document, "<html>...</html>", { preserveModified: true })
39
40
* ```
40
41
*/
41
41
-
export function morphDocument(from: Document, to: Document | string): void {
42
42
+
export function morphDocument(from: Document, to: Document | string, options?: Options): void {
42
43
if (typeof to === "string") to = parseDocument(to)
43
43
-
morph(from.documentElement, to.documentElement)
44
44
+
morph(from.documentElement, to.documentElement, options)
44
45
}
45
46
46
47
/**
+39
test/new/document.ts
···
1
1
+
import { test, expect } from "vitest"
2
2
+
import { morphDocument } from "../../src/morphlex"
3
3
+
4
4
+
test("morphing an entire document", () => {
5
5
+
const parser = new DOMParser()
6
6
+
const originalDocument = parser.parseFromString(
7
7
+
`
8
8
+
<html>
9
9
+
<head>
10
10
+
<title>Original Title</title>
11
11
+
<meta name="description" content="original">
12
12
+
</head>
13
13
+
<body>
14
14
+
<div id="content">Original Content</div>
15
15
+
</body>
16
16
+
</html>
17
17
+
`,
18
18
+
"text/html",
19
19
+
)
20
20
+
21
21
+
morphDocument(
22
22
+
originalDocument,
23
23
+
`
24
24
+
<html>
25
25
+
<head>
26
26
+
<title>New Title</title>
27
27
+
<meta name="description" content="new">
28
28
+
</head>
29
29
+
<body>
30
30
+
<div id="content">New Content</div>
31
31
+
</body>
32
32
+
</html>
33
33
+
`,
34
34
+
)
35
35
+
36
36
+
expect(document.title).toBe("New Title")
37
37
+
expect(document.querySelector('meta[name="description"]')?.getAttribute("content")).toBe("new")
38
38
+
expect(document.querySelector("#content")?.textContent).toBe("New Content")
39
39
+
})