Welcome to mirror list, hosted at ThFree Co, Russian Federation.

markdown_spec.js « cells « notebook « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7dc6f90d2022379b6bf492ba87277418ff66c478 (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
import { mount } from '@vue/test-utils';
import katex from 'katex';
import Vue, { nextTick } from 'vue';
import markdownTableJson from 'test_fixtures/blob/notebook/markdown-table.json';
import basicJson from 'test_fixtures/blob/notebook/basic.json';
import mathJson from 'test_fixtures/blob/notebook/math.json';
import MarkdownComponent from '~/notebook/cells/markdown.vue';

const Component = Vue.extend(MarkdownComponent);

window.katex = katex;

function buildCellComponent(cell, relativePath = '') {
  return mount(Component, {
    propsData: {
      cell,
    },
    provide: {
      relativeRawPath: relativePath,
    },
  }).vm;
}

function buildMarkdownComponent(markdownContent, relativePath = '') {
  return buildCellComponent(
    {
      cell_type: 'markdown',
      metadata: {},
      source: markdownContent,
    },
    relativePath,
  );
}

describe('Markdown component', () => {
  let vm;
  let cell;
  let json;

  beforeEach(async () => {
    json = basicJson;

    // eslint-disable-next-line prefer-destructuring
    cell = json.cells[1];

    vm = buildCellComponent(cell);

    await nextTick();
  });

  it('does not render prompt', () => {
    expect(vm.$el.querySelector('.prompt span')).toBeNull();
  });

  it('does not render the markdown text', () => {
    expect(vm.$el.querySelector('.markdown').innerHTML.trim()).not.toEqual(cell.source.join(''));
  });

  it('renders the markdown HTML', () => {
    expect(vm.$el.querySelector('.markdown h1')).not.toBeNull();
  });

  it('sanitizes Markdown output', async () => {
    Object.assign(cell, {
      source: [
        '[XSS](data:text/html;base64,PHNjcmlwdD5hbGVydChkb2N1bWVudC5kb21haW4pPC9zY3JpcHQ+Cg==)\n',
      ],
    });

    await nextTick();
    expect(vm.$el.querySelector('a').getAttribute('href')).toBeNull();
  });

  it('sanitizes HTML', async () => {
    const findLink = () => vm.$el.querySelector('.xss-link');
    Object.assign(cell, {
      source: ['<a href="test.js" data-remote=true data-type="script" class="xss-link">XSS</a>\n'],
    });

    await nextTick();
    expect(findLink().getAttribute('data-remote')).toBe(null);
    expect(findLink().getAttribute('data-type')).toBe(null);
  });

  describe('When parsing images', () => {
    it.each([
      [
        'for relative images in root folder, it does',
        '![](local_image.png)\n',
        'src="/raw/local_image',
      ],
      [
        'for relative images in child folders, it does',
        '![](data/local_image.png)\n',
        'src="/raw/data',
      ],
      ["for embedded images, it doesn't", '![](data:image/jpeg;base64)\n', 'src="data:'],
      ["for images urls, it doesn't", '![](http://image.png)\n', 'src="http:'],
    ])('%s', async ([testMd, mustContain]) => {
      vm = buildMarkdownComponent([testMd], '/raw/');

      await nextTick();

      expect(vm.$el.innerHTML).toContain(mustContain);
    });
  });

  describe('tables', () => {
    beforeEach(() => {
      json = markdownTableJson;
    });

    it('renders images and text', async () => {
      vm = buildCellComponent(json.cells[0]);

      await nextTick();
      const images = vm.$el.querySelectorAll('img');
      expect(images.length).toBe(5);

      const columns = vm.$el.querySelectorAll('td');
      expect(columns.length).toBe(6);

      expect(columns[0].textContent).toEqual('Hello ');
      expect(columns[1].textContent).toEqual('Test ');
      expect(columns[2].textContent).toEqual('World ');
      expect(columns[3].textContent).toEqual('Fake ');
      expect(columns[4].textContent).toEqual('External image: ');
      expect(columns[5].textContent).toEqual('Empty');

      expect(columns[0].innerHTML).toContain('<img src="data:image/jpeg;base64');
      expect(columns[1].innerHTML).toContain('<img src="data:image/png;base64');
      expect(columns[2].innerHTML).toContain('<img src="data:image/jpeg;base64');
      expect(columns[3].innerHTML).toContain('<img>');
      expect(columns[4].innerHTML).toContain('<img src="https://www.google.com/');
    });
  });

  describe('katex', () => {
    beforeEach(() => {
      json = mathJson;
    });

    it('renders multi-line katex', async () => {
      vm = buildCellComponent(json.cells[0]);

      await nextTick();
      expect(vm.$el.querySelector('.katex')).not.toBeNull();
    });

    it('renders inline katex', async () => {
      vm = buildCellComponent(json.cells[1]);

      await nextTick();
      expect(vm.$el.querySelector('p:first-child .katex')).not.toBeNull();
    });

    it('renders multiple inline katex', async () => {
      vm = buildCellComponent(json.cells[1]);

      await nextTick();
      expect(vm.$el.querySelectorAll('p:nth-child(2) .katex')).toHaveLength(4);
    });

    it('output cell in case of katex error', async () => {
      vm = buildMarkdownComponent(['Some invalid $a & b$ inline formula $b & c$\n', '\n']);

      await nextTick();
      // expect one paragraph with no katex formula in it
      expect(vm.$el.querySelectorAll('p')).toHaveLength(1);
      expect(vm.$el.querySelectorAll('p .katex')).toHaveLength(0);
    });

    it('output cell and render remaining formula in case of katex error', async () => {
      vm = buildMarkdownComponent([
        'An invalid $a & b$ inline formula and a vaild one $b = c$\n',
        '\n',
      ]);

      await nextTick();
      // expect one paragraph with no katex formula in it
      expect(vm.$el.querySelectorAll('p')).toHaveLength(1);
      expect(vm.$el.querySelectorAll('p .katex')).toHaveLength(1);
    });

    it('renders math formula in list object', async () => {
      vm = buildMarkdownComponent(["- list with inline $a=2$ inline formula $a' + b = c$\n", '\n']);

      await nextTick();
      // expect one list with a katex formula in it
      expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
      expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
    });

    it("renders math formula with tick ' in it", async () => {
      vm = buildMarkdownComponent(["- list with inline $a=2$ inline formula $a' + b = c$\n", '\n']);

      await nextTick();
      // expect one list with a katex formula in it
      expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
      expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
    });

    it('renders math formula with less-than-operator < in it', async () => {
      vm = buildMarkdownComponent(['- list with inline $a=2$ inline formula $a + b < c$\n', '\n']);

      await nextTick();
      // expect one list with a katex formula in it
      expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
      expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
    });

    it('renders math formula with greater-than-operator > in it', async () => {
      vm = buildMarkdownComponent(['- list with inline $a=2$ inline formula $a + b > c$\n', '\n']);

      await nextTick();
      // expect one list with a katex formula in it
      expect(vm.$el.querySelectorAll('li')).toHaveLength(1);
      expect(vm.$el.querySelectorAll('li .katex')).toHaveLength(2);
    });
  });
});