Skip to content

Commit 568b03f

Browse files
committed
serialize document option; fixes #7
1 parent 1b70b8c commit 568b03f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/lib/serialize.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1-
// @vitest-environment jsdom
1+
import {JSDOM} from "jsdom";
22
import {assert, test} from "vitest";
3-
import type {CellSpec} from "./notebook.js";
3+
import type {CellSpec, Notebook} from "./notebook.js";
44
import {toNotebook} from "./notebook.js";
5-
import {deserialize, serialize} from "./serialize.js";
5+
import {deserialize as _deserialize, serialize as _serialize} from "./serialize.js";
6+
7+
const {window} = new JSDOM();
8+
9+
function serialize(notebook: Notebook): string {
10+
return _serialize(notebook, {document: window.document});
11+
}
12+
13+
function deserialize(data: string): Notebook {
14+
return _deserialize(data, {parser: new window.DOMParser()});
15+
}
616

717
test("serializes unpinned cells", () => {
818
const notebook1 = toNotebook({
@@ -90,7 +100,7 @@ test("serialization escapes </script>, in various forms", () => {
90100
{id: 6, mode: "js", pinned: true, value: `'<\\/script>'`},
91101
{id: 7, mode: "js", pinned: true, value: `'<\\/script '`},
92102
{id: 8, mode: "js", pinned: true, value: `'<\\\\/SCRIPT '`},
93-
{id: 9, mode: "js", pinned: true, value: `'<\\\\/sCrIpT '`},
103+
{id: 9, mode: "js", pinned: true, value: `'<\\\\/sCrIpT '`}
94104
]
95105
});
96106
const html = serialize(notebook1);

src/lib/serialize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type {CellSpec, Cell, Notebook, NotebookTheme} from "./notebook.js";
22
import {toNotebook} from "./notebook.js";
33
import {isEmpty} from "./text.js";
44

5-
export function serialize(notebook: Notebook): string {
5+
export function serialize(notebook: Notebook, {document = window.document} = {}): string {
66
const _notebook = document.createElement("notebook");
77
_notebook.setAttribute("theme", notebook.theme);
88
if (notebook.readOnly) _notebook.setAttribute("readonly", "");

0 commit comments

Comments
 (0)