/
convertor.spec.js
257 lines (223 loc) 路 9.77 KB
/
convertor.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
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
/**
* @fileoverview test convertor
* @author NHN Ent. FE Development Lab <dl_javascript@nhnent.com>
*/
import Convertor from '../../src/js/convertor';
import EventManager from '../../src/js/eventManager';
describe('Convertor', () => {
let convertor, em;
beforeEach(() => {
em = new EventManager();
convertor = new Convertor(em);
convertor.initHtmlSanitizer();
});
describe('markdown to html', () => {
it('converting markdown to html', () => {
expect(convertor.toHTML('# HELLO WORLD')).toEqual('<h1>HELLO WORLD</h1>\n');
expect(convertor.toHTMLWithCodeHightlight('# HELLO WORLD')).toEqual('<h1>HELLO WORLD</h1>\n');
});
it('sanitize script tags', () => {
expect(convertor.toHTML('<script>alert("test");</script>')).toEqual('');
expect(convertor.toHTMLWithCodeHightlight('<script>alert("test");</script>')).toEqual('');
});
it('escape vertical bar', () => {
expect(convertor.toHTML('| 1 | 2 |\n| -- | -- |\n| 4\\|5 | 6 |\n').match(/\/td/g).length).toEqual(2);
expect(convertor.toHTMLWithCodeHightlight('| 1 | 2 |\n| -- | -- |\n| 3 | 4\\|4 |\n').match(/\/td/g).length)
.toEqual(2);
});
it('Avoid hidden last cell in table', () => {
expect(convertor.toHTML('| a | | |\n| ----------- | --- | --- |\n| | b | |\n| | | |\ntext').match(/\/td/g).length).toEqual(6);
});
it('Avoid hidden last cell in table', () => {
expect(convertor.toHTML('first\n\n<br>first\n\n```\nsecond\n\n\nsecond\n```\n\n')).toBe('<p>first</p>\n<p><br data-tomark-pass="">first</p>\n<pre><code>second\n\n\nsecond\n</code></pre>\n');
});
it('do not add line breaks in table before and after image syntax', () => {
expect(convertor._markdownToHtmlWithCodeHighlight('\n| ![nhnent](http://www.nhnent.com/) | | |\n| ----------- | --- | --- |\n| | b | |\n| | | |\ntext').match(/\/td/g).length).toEqual(6);
});
it('do not add line breaks in list before and after image syntax', () => {
expect(convertor._markdownToHtmlWithCodeHighlight('\n* asd![nhnent](http://www.nhnent.com/)\n- asd![nhnent](http://www.nhnent.com/)\n1. asd![nhnent](http://www.nhnent.com/)\n* [ ] asd![nhnent](http://www.nhnent.com/)\n').match(/\/p/g)).toBe(null);
});
it('should store number of backticks in code to data-backticks attribute', () => {
expect(convertor.toHTML('`code span`').trim()).toEqual('<p><code data-backticks="1">code span</code></p>');
expect(convertor.toHTML('```code span```').trim()).toEqual('<p><code data-backticks="3">code span</code></p>');
expect(convertor.toHTMLWithCodeHightlight('`code span`').trim()).toEqual('<p><code data-backticks="1">code span</code></p>');
expect(convertor.toHTMLWithCodeHightlight('```code span```').trim()).toEqual('<p><code data-backticks="3">code span</code></p>');
});
it('should store number of backticks in codeblock to data-backtics attribute', () => {
expect(convertor.toHTML('```\ncode block\n```').replace(/\n/g, '').trim()).toEqual('<pre><code>code block</code></pre>');
expect(convertor.toHTML('````\ncode block\n````').replace(/\n/g, '').trim()).toEqual('<pre><code data-backticks="4">code block</code></pre>');
});
it('should convert blockquote even if there is a line above it (ref #989)', () => {
expect(convertor.toHTML('text above\n> quote').replace(/\n/g, '')).toEqual('<p>text above</p><blockquote><p>quote</p></blockquote>');
});
});
describe('html to markdown', () => {
it('converting markdown to html', () => {
expect(convertor.toMarkdown('<h1 id="hello-world">HELLO WORLD</h1>')).toEqual('# HELLO WORLD');
});
it('should reserve br on multi line breaks', () => {
expect(convertor.toMarkdown('HELLO WORLD<br><br><br>!')).toEqual('HELLO WORLD\n\n<br>\n!');
});
it('should not reserve br on normal line breaks', () => {
expect(convertor.toMarkdown('HELLO WORLD<br><br>!')).toEqual('HELLO WORLD\n\n!');
});
it('should not reserve br in codeblock', () => {
expect(convertor.toMarkdown('<pre><code>HELLO WORLD\n\n\n\n\n!</code></pre>')).toEqual('```\nHELLO WORLD\n\n\n\n\n!\n```');
});
it('should reserve br to inline in table', () => {
const html = '<table>' +
'<thead><th>1</th><th>2</th><th>3</th></thead>' +
'<tbody><td>HELLO WORLD<br><br><br><br><br>!</td><td>4</td><td>5</td></tbody>' +
'</table>';
const markdown = '| 1 | 2 | 3 |\n| --- | --- | --- |\n| HELLO WORLD<br><br><br><br><br>! | 4 | 5 |';
expect(convertor.toMarkdown(html)).toEqual(markdown);
});
it('should escape html in html text', () => {
// valid tags
expect(convertor.toMarkdown('im <span> text')).toEqual('im \\<span> text');
expect(convertor.toMarkdown('im <span attr="value"> text')).toEqual('im \\<span attr="value"> text');
expect(convertor.toMarkdown('im <!-- comment --> text')).toEqual('im \\<!-- comment --> text');
// common mark auto link
expect(convertor.toMarkdown('im <http://google.com> text')).toEqual('im \\<http://google.com> text');
// invalid tags
expect(convertor.toMarkdown('im <\\span> text')).toEqual('im <\\span> text');
expect(convertor.toMarkdown('im </span attr="value"> text')).toEqual('im </span attr="value"> text');
});
it('should print number of backticks for code according to data-backticks attribute', () => {
expect(convertor.toMarkdown('<code data-backticks="1">code span</code>').trim()).toEqual('`code span`');
expect(convertor.toMarkdown('<code data-backticks="3">code span</code>').trim()).toEqual('```code span```');
});
it('should print number of backticks for code block according to data-backticks attribute', () => {
expect(convertor.toMarkdown('<pre><code>code block</code></pre>').trim()).toEqual('```\ncode block\n```');
expect(convertor.toMarkdown('<pre><code data-backticks="4">code block</code></pre>').trim()).toEqual('````\ncode block\n````');
});
it('should treat $ special characters', () => {
expect(convertor.toMarkdown('<span>,;:$&+=</span>').trim()).toEqual('<span>,;:$&+=</span>');
});
it('should convert BRs to newline', () => {
expect(convertor.toMarkdown('text<br><br>text')).toBe('text\n\ntext');
expect(convertor.toMarkdown('<b>text</b><br><br>text')).toBe('**text**\n\ntext');
expect(convertor.toMarkdown('<i>text</i><br><br>text')).toBe('_text_\n\ntext');
expect(convertor.toMarkdown('<s>text</s><br><br>text')).toBe('~~text~~\n\ntext');
expect(convertor.toMarkdown('<code>text</code><br><br>text')).toBe('`text`\n\ntext');
expect(convertor.toMarkdown('<a href="some_url">text</a><br><br>text')).toBe('[text](some_url)\n\ntext');
expect(convertor.toMarkdown('<span style="color:#ff0000">text</span><br><br>text'))
.toBe('<span style="color:#ff0000">text</span>\n\ntext');
});
});
describe('event', () => {
it('convertorAfterMarkdownToHtmlConverted event fired after html convert', () => {
let param;
em.listen('convertorAfterMarkdownToHtmlConverted', data => {
param = data;
});
convertor.toHTML('# HELLO WORLD');
expect(param).toEqual('<h1>HELLO WORLD</h1>\n');
});
it('convertorAfterHtmlToMarkdownConverted event fired after markdown convert', () => {
let param;
em.listen('convertorAfterHtmlToMarkdownConverted', data => {
param = data;
});
convertor.toMarkdown('<h1 id="hello-world">HELLO WORLD</h1>');
expect(param).toEqual('# HELLO WORLD');
});
});
describe('should not convert to', () => {
it('code in list', () => {
const markdown = [
'* codeblock',
'',
'1. codeblock',
'',
'paragraph',
'',
' code',
' block'
].join('\n');
const expectedMarkdown = [
'* codeblock',
'',
'1. codeblock',
'',
'paragraph',
'',
'```',
'code',
'block',
'```'
].join('\n');
const expectedHTML = [
'<ul>',
'<li>codeblock</li>',
'</ul>',
'<ol>',
'<li>codeblock</li>',
'</ol>',
'<p>paragraph</p>',
'<pre><code>code',
'block</code></pre>',
''
].join('\n');
const result = convertor.toHTML(markdown);
expect(result).toEqual(expectedHTML);
expect(convertor.toMarkdown(result)).toEqual(expectedMarkdown);
});
it('blockquote in list', () => {
const markdown = [
'* > blockquote',
'',
'1. > blockquote',
'',
'paragraph',
'',
'> blockquote'
].join('\n');
const html = [
'<ul>',
'<li>> blockquote</li>',
'</ul>',
'<ol>',
'<li>> blockquote</li>',
'</ol>',
'<p>paragraph</p>',
'<blockquote>',
'<p>blockquote</p>',
'</blockquote>',
''
].join('\n');
const resultMarkdown = [
'* \\> blockquote',
'',
'1. \\> blockquote',
'',
'paragraph',
'',
'> blockquote'
].join('\n');
const result = convertor.toHTML(markdown);
expect(result).toEqual(html);
expect(convertor.toMarkdown(convertor.toHTML(markdown))).toEqual(resultMarkdown);
});
it('< & > in codeblock', () => {
const markdown = [
'```',
'<span>',
'```'
].join('\n');
const html = [
'<pre><code><span>',
'</code></pre>',
''
].join('\n');
const resultMarkdown = [
'```',
'<span>',
'',
'```'
].join('\n');
expect(convertor.toHTML(markdown)).toEqual(html);
expect(convertor.toMarkdown(html)).toEqual(resultMarkdown);
});
});
});