-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.js
340 lines (308 loc) · 10.2 KB
/
test.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* @import {Root} from 'hast'
*/
import assert from 'node:assert/strict'
import test from 'node:test'
import {h, s} from 'hastscript'
import {JSDOM} from 'jsdom'
import {rehypeDom} from 'rehype-dom'
import rehypeDomParse from 'rehype-dom-parse'
import rehypeDomStringify from 'rehype-dom-stringify'
import rehypeStarryNight from 'rehype-starry-night'
import rehypeParse from 'rehype-parse'
import rehypeSlug from 'rehype-slug'
import rehypeStringify from 'rehype-stringify'
import {unified} from 'unified'
import {u} from 'unist-builder'
import {visit} from 'unist-util-visit'
const {window} = new JSDOM('')
// The globals needed by `rehype-dom`.
global.DOMParser = window.DOMParser
global.document = window.document
test('rehype-dom-parse', async function (t) {
const processor = unified().use(rehypeDomParse).use(rehypeStringify).freeze()
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('rehype-dom-parse')).sort(), [
'default'
])
})
await t.test('should parse a complete document', async function () {
assert.equal(
String(
processor()
.use(function () {
return function (tree) {
assert.deepEqual(
tree,
u('root', [
h('html', [
h('head', [h('title', 'Hi')]),
h('body', [h('h2', 'Hello world!')])
])
])
)
}
})
.data('settings', {fragment: false})
.processSync('<title>Hi</title><h2>Hello world!')
),
'<html><head><title>Hi</title></head><body><h2>Hello world!</h2></body></html>'
)
})
await t.test('should parse a fragment', async function () {
assert.equal(
String(
processor()
.use(function () {
return function (tree) {
assert.deepEqual(
tree,
u('root', [h('title', 'Hi'), h('h2', 'Hello world!')])
)
}
})
.processSync('<title>Hi</title><h2>Hello world!')
),
'<title>Hi</title><h2>Hello world!</h2>'
)
})
await t.test('should parse dataset correctly', async function () {
assert.equal(
String(processor().processSync('<p data-test="true">text, <b>hyper')),
'<p data-test="true">text, <b>hyper</b></p>'
)
})
await t.test('should parse comment correctly', async function () {
assert.equal(
String(processor().processSync('<!--comment-->')),
'<!--comment-->'
)
})
})
test('rehype-dom-stringify', async function (t) {
const processor = unified().use(rehypeParse).use(rehypeDomStringify).freeze()
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('rehype-dom-stringify')).sort(), [
'default'
])
})
await t.test('should stringify a complete document', async function () {
assert.equal(
processor()
.data('settings', {fragment: false})
.processSync('<title>Hi</title><h2>Hello world!')
.toString(),
'<html><head><title>Hi</title></head><body><h2>Hello world!</h2></body></html>'
)
})
await t.test('should stringify a doctype', async function () {
assert.equal(
processor()
.data('settings', {fragment: false})
.processSync('<!doctype html>')
.toString(),
'<DOCTYPE html><html><head></head><body></body></html>'
)
})
await t.test('should support empty documents', async function () {
assert.equal(
processor().processSync('<!doctype html>').toString(),
'<DOCTYPE html><html><head></head><body></body></html>'
)
})
await t.test('should stringify a fragment', async function () {
assert.equal(
processor()
.data('settings', {fragment: true})
.processSync('<title>Hi</title><h2>Hello world!')
.toString(),
'<title>Hi</title><h2>Hello world!</h2>'
)
})
await t.test(
'should stringify dataset attributes correctly',
async function () {
assert.equal(
processor()
.data('settings', {fragment: true})
.processSync('<p data-test="true">text, <b>hyper')
.toString(),
'<p data-test="true">text, <b>hyper</b></p>'
)
}
)
await t.test('should support SVG', async function () {
assert.equal(
processor()
.use(rehypeDomStringify, {namespace: 'http://www.w3.org/2000/svg'})
.stringify(u('root', [s('#foo.bar', s('circle'))])),
'<g id="foo" class="bar"><circle></circle></g>'
)
})
await t.test('should support HTML in SVG', async function () {
assert.equal(
processor()
.use(rehypeDomStringify, {namespace: 'http://www.w3.org/2000/svg'})
.stringify(
u('root', [
s('svg', [
s('foreignObject', [
h('div', {xmlns: 'http://www.w3.org/1999/xhtml'}, 'Alpha')
])
])
])
),
'<svg><foreignObject><div xmlns="http://www.w3.org/1999/xhtml">Alpha</div></foreignObject></svg>'
)
})
await t.test('should support HTML in SVG in HTML', async function () {
assert.equal(
processor().stringify(
u('root', [
h('div', [
s('svg', {xmlns: 'http://www.w3.org/2000/svg'}, [
s('foreignObject', [
h('div', {xmlns: 'http://www.w3.org/1999/xhtml'}, 'Alpha')
])
])
])
])
),
'<div><svg xmlns="http://www.w3.org/2000/svg"><foreignObject><div xmlns="http://www.w3.org/1999/xhtml">Alpha</div></foreignObject></svg></div>'
)
})
await t.test('should stringify namespaced elements', async function () {
assert.equal(
processor()
.use(rehypeDomStringify, {namespace: 'https://example.com'})
.stringify(u('root', [h('example', 'Alpha')])),
'<example>Alpha</example>'
)
})
})
test('rehype-dom', async function (t) {
await t.test('should expose the public api', async function () {
assert.deepEqual(Object.keys(await import('rehype-dom')).sort(), [
'rehypeDom'
])
})
await t.test('should parse a complete document', async function () {
assert.equal(
rehypeDom()
.data('settings', {fragment: false})
.processSync('<title>Hi</title><h2>Hello world!')
.toString(),
'<html><head><title>Hi</title></head><body><h2>Hello world!</h2></body></html>'
)
})
await t.test('should parse a fragment', async function () {
assert.equal(
rehypeDom().processSync('<title>Hi</title><h2>Hello world!').toString(),
'<title>Hi</title><h2>Hello world!</h2>'
)
})
await t.test('should parse as a fragment by default', async function () {
assert.equal(
rehypeDom().processSync('<title>Hi</title><h2>Hello world!').toString(),
'<title>Hi</title><h2>Hello world!</h2>'
)
})
await t.test('should not mangle dataset attributes', async function () {
assert.equal(
rehypeDom().processSync('<p data-test="true">text, <b>hyper').toString(),
'<p data-test="true">text, <b>hyper</b></p>'
)
})
await t.test('should support boolean attributes', async function () {
assert.equal(
rehypeDom().processSync('<input type="checkbox" checked />').toString(),
'<input type="checkbox" checked="">'
)
})
await t.test('should not mangle classnames', async function () {
assert.equal(
rehypeDom().processSync('<div class="foo bar">baz</div>').toString(),
'<div class="foo bar">baz</div>'
)
})
await t.test('should support svg', async function () {
assert.equal(
rehypeDom()
.processSync(
`<svg width=230 height=120 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink>
<circle cx=60 cy=60 r=50 fill=red />
<circle cx=170 cy=60 r=50 fill=green />
</svg>`
)
.toString(),
`<svg width="230" height="120" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<circle cx="60" cy="60" r="50" fill="red"></circle>
<circle cx="170" cy="60" r="50" fill="green"></circle>
</svg>`
)
})
await t.test('should process text in `style` correctly', async function () {
assert.equal(
String(
rehypeDom().processSync(`<style>
body > style {
width: 100px;
}
</style>`)
),
`<style>
body > style {
width: 100px;
}
</style>`
)
})
await t.test('should work w/ `rehype-starry-night`', async function () {
assert.equal(
String(
await rehypeDom()
.use(rehypeStarryNight)
.process(
`<h1>Hello World!</h1>
<pre><code class="language-js">var name = "World";
console.warn("Hello, " + name + "!")</pre></code>`
)
),
`<h1>Hello World!</h1>
<pre><code class="language-js"><span class="pl-k">var</span> name <span class="pl-k">=</span> <span class="pl-s"><span class="pl-pds">"</span>World<span class="pl-pds">"</span></span>;
<span class="pl-en">console</span>.<span class="pl-c1">warn</span>(<span class="pl-s"><span class="pl-pds">"</span>Hello, <span class="pl-pds">"</span></span> <span class="pl-k">+</span> name <span class="pl-k">+</span> <span class="pl-s"><span class="pl-pds">"</span>!<span class="pl-pds">"</span></span>)</code></pre>`
)
})
await t.test('should work w/ `rehype-slug`', async function () {
assert.equal(
rehypeDom()
.use(rehypeSlug)
.processSync('<h1>First</h1><h2>Second')
.toString(),
'<h1 id="first">First</h1><h2 id="second">Second</h2>'
)
})
await t.test('should work w/ a generic plugin', async function () {
assert.equal(
rehypeDom()
.use(function () {
/**
* Transform.
*
* @param {Root} tree
* Tree.
* @returns {undefined}
* Nothing.
*/
return function (tree) {
visit(tree, 'text', function (node) {
node.value = node.value.split('').reverse().join('')
})
}
})
.processSync('<p>a man a plan a canal panama</p>')
.toString(),
'<p>amanap lanac a nalp a nam a</p>'
)
})
})