-
-
Notifications
You must be signed in to change notification settings - Fork 374
/
data-include-spec.js
225 lines (194 loc) · 6.69 KB
/
data-include-spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
"use strict";
import {
flushIframes,
makeBasicConfig,
makeDefaultBody,
makeRSDoc,
makeStandardOps,
} from "../SpecHelper.js";
describe("Core — Data Include", () => {
afterAll(flushIframes);
// this does not test much, someone for whom this is
// important should provide more tests
const url = "/tests/spec/core/includer.html";
/**
* @param {string} text
*/
function generateDataUrl(text) {
return `data:text/plain;charset=utf-8,${encodeURIComponent(text)}`;
}
it("includes an external file and remove the data-include attr", async () => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody(),
};
const doc = await makeRSDoc(ops, url);
const p = doc.querySelector("#includes > div > p");
expect(p).toBeTruthy();
expect(p.textContent).toBe("INCLUDED");
const div = doc.querySelector("#includes > div");
expect(div.dataset.include).toBe(undefined);
expect(div.dataset.includeFormat).toBe(undefined);
expect(div.dataset.dontRemove).toBe("pass");
});
it("replaces sections when data-include-replace is present", async () => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody(),
};
const doc = await makeRSDoc(ops, url);
const missing = doc.getElementById("this-should-be-missing");
expect(missing).toBe(null);
const included = doc.getElementById("replacement-test");
expect(included).toBeTruthy();
const heading = doc.querySelector("#replacement-test > h3");
expect(heading).toBeTruthy();
expect(heading.textContent).toBe("Replacement");
});
it("gracefully handles empty data-includes", async () => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody(),
};
const doc = await makeRSDoc(ops, url);
const container = doc.getElementById("empty-include");
expect(container).toBeTruthy();
expect(container.textContent).toBe("");
});
it("includes text when data-include-format is 'text'", async () => {
const ops = {
config: makeBasicConfig(),
body: makeDefaultBody(),
};
const doc = await makeRSDoc(ops, url);
const container = doc.getElementById("no-replace");
expect(container).toBeTruthy();
expect(container.textContent).toBe("<p>pass</p>");
});
it("includes a URL and processes it as markdown", async () => {
// Data URI encoding of: "## PASS", which markdown converts to a H2 element.
const ops = {
config: makeBasicConfig(),
body: `${makeDefaultBody()}<section
id="includes"
data-include="${generateDataUrl("## PASS")}">
</section>`,
};
ops.config.format = "markdown";
const doc = await makeRSDoc(ops);
const h2 = doc.querySelector("#includes > h2");
expect(h2).toBeTruthy();
expect(h2.textContent).toBe("1. PASS");
expect(doc.querySelectorAll("*[data-include]")).toHaveSize(0);
});
it("processes single line markdown text", async () => {
const include = `On construction, see [here](https://respec.org/)`;
const ops = {
config: {
...makeBasicConfig(),
format: "markdown",
},
body: `
<section id="abstract"></section>
<section data-include="${generateDataUrl(
include
)}" data-include-format="markdown"></section>
`,
};
ops.abstract = null;
const doc = await makeRSDoc(ops);
const anchor = doc.querySelector("section a[href*=respec]");
expect(anchor).toBeTruthy();
expect(anchor.href).toContain("https://respec.org/");
// Shouldn't break other sections
expect(doc.getElementById("abstract")).toBeTruthy();
});
describe("multiline markdown text", () => {
const include = generateDataUrl(`# R
1. Rose
Blue rose is blue
Red rose is red
Rose of the wasteland is violet
`);
it("processes multiline markdown text", async () => {
const ops = {
config: {
...makeBasicConfig(),
format: "markdown",
},
body: `
<section id="abstract"></section>
<section id="test" data-include="${include}" data-include-format="markdown"></section>
`,
};
ops.abstract = null;
const doc = await makeRSDoc(ops);
const li = doc.querySelector("section#test li");
expect(li).toBeTruthy();
expect(li.textContent).toBe("Rose");
const [p1, p2, p3] = [...doc.querySelectorAll("#test p")];
expect(p1.textContent).toContain("Blue");
expect(p2.textContent).toContain("Red");
expect(p3.textContent).toContain("wasteland");
// Shouldn't break other sections
expect(doc.getElementById("abstract")).toBeTruthy();
});
it("processes multiline markdown replacement text", async () => {
const ops = {
config: {
...makeBasicConfig(),
format: "markdown",
},
body: `
<section id="abstract"></section>
<section id="test">
<div data-include="${include}" data-include-format="markdown" data-include-replace></div>
</section>
`,
};
ops.abstract = null;
const doc = await makeRSDoc(ops);
const li = doc.querySelector("section#test li");
expect(li).toBeTruthy();
expect(li.textContent).toBe("Rose");
const [p1, p2, p3] = [...doc.querySelectorAll("#test p")];
expect(p1.textContent).toContain("Blue");
expect(p2.textContent).toContain("Red");
expect(p3.textContent).toContain("wasteland");
expect(li.parentElement.parentElement.localName).toBe("section");
// Shouldn't break other sections
expect(doc.getElementById("abstract")).toBeTruthy();
});
it("processes markdown with unescaped html code blocks", async () => {
const includeBody = `
## Test
A paragraph.
\`\`\`html
<!DOCTYPE html>
<html lang="en">
<body>
<div class="note">note</div>
</body>
</html>
\`\`\`
`;
const body = `<section
id="includes"
data-include-format="markdown"
data-include="${generateDataUrl(includeBody)}"
></section>`;
const ops = makeStandardOps(null, body);
const doc = await makeRSDoc(ops);
const h2 = doc.querySelector("#includes > h2");
expect(h2).toBeTruthy();
expect(h2.textContent).toContain("Test");
const p = doc.querySelector("#includes > p");
expect(p).toBeTruthy();
expect(p.textContent).toBe("A paragraph.");
const pre = doc.querySelector("#includes > pre");
expect(pre.querySelector("code").classList).toContain("html");
expect(pre.textContent).toContain("<!DOCTYPE html>");
expect(doc.querySelector("#includes .note")).toBeFalsy();
});
});
});