aboutsummaryrefslogtreecommitdiff
path: root/__tests__/index.js
blob: 6cfa4716f5deab51d31c48d13e42f8117848a85d (plain)
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
'use strict';

import {join} from 'path';
import {readFileSync as file} from 'fs';
import test from 'ava';
import raw from 'rehype-raw';
import reParse from 'remark-parse';
import stringify from 'rehype-stringify';
import remark2rehype from 'remark-rehype';
import unified from 'unified';
import parse5 from 'parse5';
import plugin from '..';

const renderDefault = text => unified()
  .use(reParse)
  .use(plugin)
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const render = text => unified()
  .use(reParse)
  .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'})
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const renderFootnotes = text => unified()
  .use(reParse, {footnotes: true})
  .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'})
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const renderRaw = text => unified()
  .use(reParse)
  .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'})
  .use(remark2rehype, {allowDangerousHTML: true})
  .use(raw)
  .use(stringify)
  .processSync(text);

const generateExtendParser = extendsOptions => text => unified()
  .use(reParse)
  .use(plugin, extendsOptions)
  .use(remark2rehype)
  .use(stringify)
  .processSync(text);

const parse = x => parse5.parse(x);

const mainTestString = 'Inline *test*{style="em:4"} paragraph. Use **multiple**{ style="color:pink"} inline ~~block~~ tag. Line `tagCode`{ style="color:yellow"}.';

/* Basic tests */

test('basic-default', t => {
  const {contents} = renderDefault(mainTestString);
  t.deepEqual(parse(contents), parse('<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>'));
});

test('basic', t => {
  const {contents} = render(mainTestString);
  t.deepEqual(parse(contents), parse(`
<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>`));
});

test('basic-raw', t => {
  const {contents} = renderRaw(mainTestString);
  t.deepEqual(parse(contents), parse(`
<p>Inline <em style="em:4">test</em> paragraph. Use <strong style="color:pink">multiple</strong> inline <del>block</del> tag. Line <code style="color:yellow">tagCode</code>.</p>`));
});

/* Support tests
 *
 * They test the support of one element each.
 */

test('em', t => {
  const {contents} = render('textexamplenointerest **Important**{style=4em} still no interest');
  t.deepEqual(parse(contents), parse('<p>textexamplenointerest <strong style="4em">Important</strong> still no interest</p>'));
});

test('fenced code', t => {
  const fencedCodeString = `~~~lang info=string
This is an awesome code

~~~
`;
  const {contents} = render(fencedCodeString);
  t.deepEqual(parse(contents), parse(`<pre><code class="language-lang" info="string">This is an awesome code

</code></pre>`));
});

test('fenced code brackets', t => {
  const fencedCodeString = `~~~lang {info=string}
This is an awesome code

~~~
`;
  const {contents} = render(fencedCodeString);
  t.deepEqual(parse(contents), parse(`<pre><code class="language-lang" info="string">This is an awesome code

</code></pre>`));
});

test('fenced code brackets and spaces', t => {
  const fencedCodeString = `~~~lang   {info=string}
This is an awesome code

~~~
`;
  const {contents} = render(fencedCodeString);
  t.deepEqual(parse(contents), parse(`<pre><code class="language-lang" info="string">This is an awesome code

</code></pre>`));
});

test('image', t => {
  const imageMd = '![Test image](url.com){ alt="This is alt"  longdesc="qsdf"}';
  const {contents} = render(imageMd);
  t.deepEqual(parse(contents), parse('<p><img src="url.com" alt="This is alt" longdesc="qsdf"/></p>'));
});

test('link', t => {
  const linkMd = 'This is a link :[Test link](ache.one){ ping="http://ache.one/big.brother"}';
  const {contents} = render(linkMd);
  t.deepEqual(parse(contents), parse('<p>This is a link :<a href="ache.one" ping="http://ache.one/big.brother">Test link</a></p>'));
});

test('atx header', t => {
  const imageMd = `
Title of the article
====================
{data-id="title"}

`;
  const {contents} = renderDefault(imageMd);
  t.deepEqual(parse(contents), parse('<h1 data-id="title">Title of the article</h1>'));
});

test('emphasis and strong', t => {
  const emphasis = 'Hey ! *That looks cool*{style="color: blue;"} ! No, that\'s **not**{.not} !';
  const {contents} = renderDefault(emphasis);
  t.deepEqual(parse(contents), parse('<p>Hey ! <em style="color: blue;">That looks cool</em> ! No, that\'s <strong class="not">not</strong> !'));
});

test('linkReference', t => {
  const linkRef = `[Google][google]{hreflang="en"}

[google]: https://google.com
`;
  const {contents} = renderDefault(linkRef);
  t.deepEqual(parse(contents), parse('<p><a href="https://google.com" hreflang="en">Google</a></p>'));
});

test('footnote', t => {
  const footnotes = `Since XP is good we should always use XP[^xp]{ data-id=xp }

[^xp]: Apply XP principe to XP.
`;
  const {contents} = renderFootnotes(footnotes);
  t.deepEqual(parse(contents), parse(`<p>Since XP is good we should always use XP<sup id="fnref-xp"><a href="#fn-xp" class="footnote-ref" data-id="xp">xp</a></sup></p>
<div class="footnotes">
<hr>
<ol>
<li id="fn-xp">Apply XP principe to XP.<a href="#fnref-xp" class="footnote-backref">↩</a></li>
</ol>
</div>`));
});

/* Readme tests
 *
 * Should be act acording to the README.md
 */

test('readme-default', t => {
  const fileExample = file(join(__dirname, 'readMeTest.txt'));
  const {contents} = renderDefault(fileExample);
  t.deepEqual(parse(contents), parse(`
<p><img src="img" alt="alt" height="50"></p>
<p><a href="https://rms.sexy" rel="external">Hot babe with computer</a></p>
<h3 style="color:red;">This is a title</h3>
<p>Npm stand for <em style="color:yellow;">node</em> packet manager.</p>
<p>This is a <strong>Unicorn</strong> !</p>
<p>Your problem is <del style="color: grey;">at line 18</del>. My mistake, it's at line 14.</p>
<p>You can use the <code>fprintf</code> function to format the output to a file.</p>`));
});

test('readme', t => {
  const fileExample = file(join(__dirname, 'readMeTest.txt'));
  const {contents} = render(fileExample);
  t.deepEqual(parse(contents), parse(`
<p><img src="img" alt="alt" height="50"></p>
<p><a href="https://rms.sexy" rel="external">Hot babe with computer</a></p>
<h3 style="color:red;">This is a title</h3>
<p>Npm stand for <em style="color:yellow;">node</em> packet manager.</p>
<p>This is a <strong awesome="">Unicorn</strong> !</p>
<p>Your problem is <del style="color: grey;">at line 18</del>. My mistake, it's at line 14.</p>
<p>You can use the <code language="c">fprintf</code> function to format the output to a file.</p>`));
});

/* Extended tests
 *
 * They test the support of the feature that extended the pool of attribute
 * that can be parsed.
 */

test('extended', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" }
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is an awesome image : <img src="aws://image.jpg" alt="Awesome image"></p>`));
});

test('extended Dangerous', t => {
  const renderExtended = generateExtendParser({extend: {image: ['quality', 'onload']}});
  const dangerousString = `*Wait* !
This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" onload="launchAwesomeFunction();" }
`;
  const {contents} = renderExtended(dangerousString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is an awesome image : <img src="aws://image.jpg" alt="Awesome image" quality="80" onload="launchAwesomeFunction();"></p>`));
});

test('extended-global', t => {
  const renderExtended = generateExtendParser({extend: {'*': ['ex-attr']}});
  const globalString = ' *Wait* ! You are **beautiful**{ ex-attr="true" } !';
  const {contents} = renderExtended(globalString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! You are <strong ex-attr="true">beautiful</strong> !</p>'));
});

test('extended-invalid-scope', t => {
  const renderExtended = generateExtendParser({scope: 'invalid', extend: {strong: ['ex-attr']}});
  const invalidString = '*Wait* ! You are **beautiful**{ ex-attr="true" onload="qdss" pss="NOK" } !';
  const {contents} = renderExtended(invalidString);
  t.deepEqual(parse(contents), parse('<p><em>Wait</em> ! You are <strong ex-attr="true">beautiful</strong> !</p>'));
});

test('invalid-scope', t => {
  const renderExtended = generateExtendParser({extend: 'exAttr'});
  const invalidString = ' *Wait* ! I **love**{ exAttr="true" onload="qdss" pss="NOK" } you !';
  const {contents} = renderExtended(invalidString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! I <strong>love</strong> you !</p>'));
});

test('invalid-extend', t => {
  const renderExtended = generateExtendParser({extend: 'exAttr'});
  const invalidString = ' *Wait* ! I **love**{ exAttr="true" onload="qdss" attr="NOK" style="color: red;"} you!';
  const {contents} = renderExtended(invalidString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! I <strong style="color: red;">love</strong> you!</p>'));
});

/* Special attributes tests
  *
  * aria attributes: Focused on accessibility. They have the form aria-*
  * Global custom attributes: User ended attributes. They have the form data-*
  *
  */

test('global-aria', t => {
  const invalidString = ' *Wait* ! I **love**{ style="color: pink;" aria-love="true" } you!';
  const {contents} = renderDefault(invalidString);
  t.deepEqual(parse(contents), parse('<p> <em>Wait</em> ! I <strong style="color: pink;" aria-love="true">love</strong> you!</p>'));
});

test('global custom attribute', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data-id=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test" data-id="2"></p>`));

  t.notDeepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});

test('global custom attributes 2', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data-id-node=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test" data-id-node="2"></p>`));

  t.notDeepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});

test('global custom attributes 3', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data--id=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});

test('global custom attributes 4', t => {
  const renderExtended = generateExtendParser({extends: {image: ['quality']}});
  const extentedString = `*Wait* !
This is a test image : ![test](img.jpg){data-i=2}
`;
  const {contents} = renderExtended(extentedString);
  t.deepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test" data-i=2></p>`));

  t.notDeepEqual(parse(contents), parse(`<p><em>Wait</em> !
This is a test image : <img src="img.jpg" alt="test"></p>`));
});