summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/shared/gr-formatted-text/gr-formatted-text_test.ts
blob: 3a23a38d2e607f4c857477f31b49df2c92f937ce (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
 * @license
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import '../../../test/common-test-setup-karma';
import './gr-formatted-text';
import {
  GrFormattedText,
  Block,
  ListBlock,
  Paragraph,
  QuoteBlock,
  PreBlock,
  CodeBlock,
  InlineItem,
  ListItem,
  TextSpan,
  LinkSpan,
} from './gr-formatted-text';

const basicFixture = fixtureFromElement('gr-formatted-text');

suite('gr-formatted-text tests', () => {
  let element: GrFormattedText;

  function assertSpan(actual: InlineItem, expected: InlineItem) {
    assert.equal(actual.type, expected.type);
    assert.equal(actual.text, expected.text);
    switch (actual.type) {
      case 'link':
        assert.equal(actual.url, (expected as LinkSpan).url);
        break;
    }
  }

  function assertTextBlock(block: Block, spans: InlineItem[]) {
    assert.equal(block.type, 'paragraph');
    const paragraph = block as Paragraph;
    assert.equal(paragraph.spans.length, spans.length);
    for (let i = 0; i < paragraph.spans.length; ++i) {
      assertSpan(paragraph.spans[i], spans[i]);
    }
  }

  function assertPreBlock(block: Block, text: string) {
    assert.equal(block.type, 'pre');
    const preBlock = block as PreBlock;
    assert.equal(preBlock.text, text);
  }

  function assertCodeBlock(block: Block, text: string) {
    assert.equal(block.type, 'code');
    const preBlock = block as CodeBlock;
    assert.equal(preBlock.text, text);
  }

  function assertSimpleTextBlock(block: Block, text: string) {
    assertTextBlock(block, [{type: 'text', text}]);
  }

  function assertListBlock(block: Block, items: ListItem[]) {
    assert.equal(block.type, 'list');
    const listBlock = block as ListBlock;
    assert.deepEqual(listBlock.items, items);
  }

  function assertQuoteBlock(block: Block): QuoteBlock {
    assert.equal(block.type, 'quote');
    return block as QuoteBlock;
  }

  setup(() => {
    element = basicFixture.instantiate();
  });

  test('parse empty', () => {
    assert.lengthOf(element._computeBlocks(''), 0);
  });

  for (const text of [
    'Para1',
    'Para 1\nStill para 1',
    'Para 1\n\nPara 2\n\nPara 3',
  ]) {
    test('parse simple', () => {
      const comment = {type: 'text', text} as TextSpan;
      const result = element._computeBlocks(text);
      assert.lengthOf(result, 1);
      assertTextBlock(result[0], [comment]);
    });
  }

  test('parse link', () => {
    const comment = '[text](url)';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertTextBlock(result[0], [{type: 'link', text: 'text', url: 'url'}]);
  });

  test('link with javascript protocol does not set href', () => {
    const comment = '[text](javascript:alert`1`)';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertTextBlock(result[0], [{type: 'link', text: 'text', url: ''}]);
  });

  test('link with whitespace and javascript protocol does not set href', () => {
    const comment = '[text](   javascript:alert`1`)';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertTextBlock(result[0], [{type: 'link', text: 'text', url: ''}]);
  });

  test('parse inline code', () => {
    const comment = 'text `code`';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertTextBlock(result[0], [
      {type: 'text', text: 'text '},
      {type: 'code', text: 'code'},
    ]);
  });

  test('parse quote', () => {
    const comment = '> Quote text';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    const quoteBlock = assertQuoteBlock(result[0]);
    assert.lengthOf(quoteBlock.blocks, 1);
    assertSimpleTextBlock(quoteBlock.blocks[0], 'Quote text');
  });

  test('parse quote lead space', () => {
    const comment = ' > Quote text';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    const quoteBlock = assertQuoteBlock(result[0]);
    assert.lengthOf(quoteBlock.blocks, 1);
    assertSimpleTextBlock(quoteBlock.blocks[0], 'Quote text');
  });

  test('parse multiline quote', () => {
    const comment = '> Quote line 1\n> Quote line 2\n > Quote line 3\n';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    const quoteBlock = assertQuoteBlock(result[0]);
    assert.lengthOf(quoteBlock.blocks, 1);
    assertSimpleTextBlock(
      quoteBlock.blocks[0],
      'Quote line 1\nQuote line 2\nQuote line 3'
    );
  });

  test('parse pre', () => {
    const comment = '    Four space indent.';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertPreBlock(result[0], comment);
  });

  test('parse one space pre', () => {
    const comment = ' One space indent.\n Another line.';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertPreBlock(result[0], comment);
  });

  test('parse tab pre', () => {
    const comment = '\tOne tab indent.\n\tAnother line.\n  Yet another!';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertPreBlock(result[0], comment);
  });

  test('parse star list', () => {
    const comment = '* Item 1\n* Item 2\n* Item 3';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertListBlock(result[0], [
      {spans: [{type: 'text', text: 'Item 1'}]},
      {spans: [{type: 'text', text: 'Item 2'}]},
      {spans: [{type: 'text', text: 'Item 3'}]},
    ]);
  });

  test('parse dash list', () => {
    const comment = '- Item 1\n- Item 2\n- Item 3';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertListBlock(result[0], [
      {spans: [{type: 'text', text: 'Item 1'}]},
      {spans: [{type: 'text', text: 'Item 2'}]},
      {spans: [{type: 'text', text: 'Item 3'}]},
    ]);
  });

  test('parse mixed list', () => {
    const comment = '- Item 1\n* Item 2\n- Item 3\n* Item 4';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertListBlock(result[0], [
      {spans: [{type: 'text', text: 'Item 1'}]},
      {spans: [{type: 'text', text: 'Item 2'}]},
      {spans: [{type: 'text', text: 'Item 3'}]},
      {spans: [{type: 'text', text: 'Item 4'}]},
    ]);
  });

  test('parse mixed block types', () => {
    const comment =
      'Paragraph\nacross\na\nfew\nlines.' +
      '\n\n' +
      '> Quote\n> across\n> not many lines.' +
      '\n\n' +
      'Another paragraph' +
      '\n\n' +
      '* Series\n* of\n* list\n* items' +
      '\n\n' +
      'Yet another paragraph' +
      '\n\n' +
      '\tPreformatted text.' +
      '\n\n' +
      'Parting words.';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 7);
    assertSimpleTextBlock(result[0], 'Paragraph\nacross\na\nfew\nlines.\n');

    const quoteBlock = assertQuoteBlock(result[1]);
    assert.lengthOf(quoteBlock.blocks, 1);
    assertSimpleTextBlock(
      quoteBlock.blocks[0],
      'Quote\nacross\nnot many lines.'
    );

    assertSimpleTextBlock(result[2], 'Another paragraph\n');
    assertListBlock(result[3], [
      {spans: [{type: 'text', text: 'Series'}]},
      {spans: [{type: 'text', text: 'of'}]},
      {spans: [{type: 'text', text: 'list'}]},
      {spans: [{type: 'text', text: 'items'}]},
    ]);
    assertSimpleTextBlock(result[4], 'Yet another paragraph\n');
    assertPreBlock(result[5], '\tPreformatted text.');
    assertSimpleTextBlock(result[6], 'Parting words.');
  });

  test('bullet list 1', () => {
    const comment = 'A\n\n* line 1';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'A\n');
    assertListBlock(result[1], [{spans: [{type: 'text', text: 'line 1'}]}]);
  });

  test('bullet list 2', () => {
    const comment = 'A\n\n* line 1\n* 2nd line';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'A\n');
    assertListBlock(result[1], [
      {spans: [{type: 'text', text: 'line 1'}]},
      {spans: [{type: 'text', text: '2nd line'}]},
    ]);
  });

  test('bullet list 3', () => {
    const comment = 'A\n* line 1\n* 2nd line\n\nB';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 3);
    assertSimpleTextBlock(result[0], 'A');
    assertListBlock(result[1], [
      {spans: [{type: 'text', text: 'line 1'}]},
      {spans: [{type: 'text', text: '2nd line'}]},
    ]);
    assertSimpleTextBlock(result[2], 'B');
  });

  test('bullet list 4', () => {
    const comment = '* line 1\n* 2nd line\n\nB';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertListBlock(result[0], [
      {spans: [{type: 'text', text: 'line 1'}]},
      {spans: [{type: 'text', text: '2nd line'}]},
    ]);
    assertSimpleTextBlock(result[1], 'B');
  });

  test('bullet list 5', () => {
    const comment =
      'To see this bug, you have to:\n' +
      '* Be on IMAP or EAS (not on POP)\n' +
      '* Be very unlucky\n';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'To see this bug, you have to:');
    assertListBlock(result[1], [
      {spans: [{type: 'text', text: 'Be on IMAP or EAS (not on POP)'}]},
      {spans: [{type: 'text', text: 'Be very unlucky'}]},
    ]);
  });

  test('bullet list 6', () => {
    const comment =
      'To see this bug,\n' +
      'you have to:\n' +
      '* Be on IMAP or EAS (not on POP)\n' +
      '* Be very unlucky\n';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'To see this bug,\nyou have to:');
    assertListBlock(result[1], [
      {spans: [{type: 'text', text: 'Be on IMAP or EAS (not on POP)'}]},
      {spans: [{type: 'text', text: 'Be very unlucky'}]},
    ]);
  });

  test('dash list 1', () => {
    const comment = 'A\n- line 1\n- 2nd line';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'A');
    assertListBlock(result[1], [
      {spans: [{type: 'text', text: 'line 1'}]},
      {spans: [{type: 'text', text: '2nd line'}]},
    ]);
  });

  test('dash list 2', () => {
    const comment = 'A\n- line 1\n- 2nd line\n\nB';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 3);
    assertSimpleTextBlock(result[0], 'A');
    assertListBlock(result[1], [
      {spans: [{type: 'text', text: 'line 1'}]},
      {spans: [{type: 'text', text: '2nd line'}]},
    ]);
    assertSimpleTextBlock(result[2], 'B');
  });

  test('dash list 3', () => {
    const comment = '- line 1\n- 2nd line\n\nB';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertListBlock(result[0], [
      {spans: [{type: 'text', text: 'line 1'}]},
      {spans: [{type: 'text', text: '2nd line'}]},
    ]);
    assertSimpleTextBlock(result[1], 'B');
  });

  test('list with links', () => {
    const comment = '- [text](http://url)\n- 2nd line';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertListBlock(result[0], [
      {
        spans: [{type: 'link', text: 'text', url: 'http://url'}],
      },
      {spans: [{type: 'text', text: '2nd line'}]},
    ]);
  });

  test('nested list will NOT be recognized', () => {
    // will be rendered as two separate lists
    const comment = '- line 1\n  - line with indentation\n- line 2';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 3);
    assertListBlock(result[0], [{spans: [{type: 'text', text: 'line 1'}]}]);
    assertPreBlock(result[1], '  - line with indentation');
    assertListBlock(result[2], [{spans: [{type: 'text', text: 'line 2'}]}]);
  });

  test('pre format 1', () => {
    const comment = 'A\n  This is pre\n  formatted';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'A');
    assertPreBlock(result[1], '  This is pre\n  formatted');
  });

  test('pre format 2', () => {
    const comment = 'A\n  This is pre\n  formatted\n\nbut this is not';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 3);
    assertSimpleTextBlock(result[0], 'A');
    assertPreBlock(result[1], '  This is pre\n  formatted');
    assertSimpleTextBlock(result[2], 'but this is not');
  });

  test('pre format 3', () => {
    const comment = 'A\n  Q\n    <R>\n  S\n\nB';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 3);
    assertSimpleTextBlock(result[0], 'A');
    assertPreBlock(result[1], '  Q\n    <R>\n  S');
    assertSimpleTextBlock(result[2], 'B');
  });

  test('pre format 4', () => {
    const comment = '  Q\n    <R>\n  S\n\nB';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertPreBlock(result[0], '  Q\n    <R>\n  S');
    assertSimpleTextBlock(result[1], 'B');
  });

  test('pre format 5', () => {
    const comment = '  Q\n    <R>\n  S\n \nB';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertPreBlock(result[0], '  Q\n    <R>\n  S');
    assertSimpleTextBlock(result[1], ' \nB');
  });

  test('quote 1', () => {
    const comment = "> I'm happy with quotes!!";
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    const quoteBlock = assertQuoteBlock(result[0]);
    assert.lengthOf(quoteBlock.blocks, 1);
    assertSimpleTextBlock(quoteBlock.blocks[0], "I'm happy with quotes!!");
  });

  test('quote 2', () => {
    const comment = "> I'm happy\n > with quotes!\n\nSee above.";
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    const quoteBlock = assertQuoteBlock(result[0]);
    assert.lengthOf(quoteBlock.blocks, 1);
    assertSimpleTextBlock(quoteBlock.blocks[0], "I'm happy\nwith quotes!");
    assertSimpleTextBlock(result[1], 'See above.');
  });

  test('quote 3', () => {
    const comment = 'See this said:\n > a quoted\n > string block\n\nOK?';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 3);
    assertSimpleTextBlock(result[0], 'See this said:');
    const quoteBlock = assertQuoteBlock(result[1]);
    assert.lengthOf(quoteBlock.blocks, 1);
    assertSimpleTextBlock(quoteBlock.blocks[0], 'a quoted\nstring block');
    assertSimpleTextBlock(result[2], 'OK?');
  });

  test('nested quotes', () => {
    const comment = ' > > prior\n > \n > next\n';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    const outerQuoteBlock = assertQuoteBlock(result[0]);
    assert.lengthOf(outerQuoteBlock.blocks, 2);
    const nestedQuoteBlock = assertQuoteBlock(outerQuoteBlock.blocks[0]);
    assert.lengthOf(nestedQuoteBlock.blocks, 1);
    assertSimpleTextBlock(nestedQuoteBlock.blocks[0], 'prior');
    assertSimpleTextBlock(outerQuoteBlock.blocks[1], 'next');
  });

  test('code 1', () => {
    const comment = '```\n// test code\n```';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertCodeBlock(result[0], '// test code');
  });

  test('code 2', () => {
    const comment = 'test code\n```// test code```';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'test code');
    assertCodeBlock(result[1], '// test code');
  });

  test('not a code block', () => {
    const comment = 'test code\n```// test code';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 1);
    assertSimpleTextBlock(result[0], 'test code\n```// test code');
  });

  test('not a code block 2', () => {
    const comment = 'test code\n```\n// test code';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'test code');
    assertSimpleTextBlock(result[1], '```\n// test code');
  });

  test('not a code block 3', () => {
    const comment = 'test code\n```';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 2);
    assertSimpleTextBlock(result[0], 'test code');
    assertSimpleTextBlock(result[1], '```');
  });

  test('mix all 1', () => {
    const comment =
      ' bullets:\n- bullet 1\n- bullet 2\n\ncode example:\n' +
      '```// test code```\n\n> reference is here';
    const result = element._computeBlocks(comment);
    assert.lengthOf(result, 5);
    assert.equal(result[0].type, 'pre');
    assert.equal(result[1].type, 'list');
    assert.equal(result[2].type, 'paragraph');
    assert.equal(result[3].type, 'code');
    assert.equal(result[4].type, 'quote');
  });
});