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
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Math rendering', :js, feature_category: :team_planning do
let!(:project) { create(:project, :public) }
it 'renders inline and display math correctly' do
description = <<~MATH
This math is inline $`a^2+b^2=c^2`$.
This is on a separate line
```math
a^2+b^2=c^2
```
This math is aligned
```math
\\begin{align*}
a&=b+c \\\\
d+e&=f
\\end{align*}
```
MATH
issue = create(:issue, project: project, description: description)
visit project_issue_path(project, issue)
expect(page).to have_selector('.katex .mord.mathnormal', text: 'b')
expect(page).to have_selector('.katex-display .mord.mathnormal', text: 'b')
expect(page).to have_selector('.katex-display .mtable .col-align-l .mord.mathnormal', text: 'f')
end
it 'only renders non XSS links' do
description = <<~MATH
This link is valid $`\\href{javascript:alert('xss');}{xss}`$.
This link is valid $`\\href{https://gitlab.com}{Gitlab}`$.
MATH
issue = create(:issue, project: project, description: description)
visit project_issue_path(project, issue)
page.within '.description > .md' do
# unfortunately there is no class selector for KaTeX's "unsupported command"
# formatting so we must match the style attribute
expect(page).to have_selector('.katex-html .mord[style*="color:"][style*="#cc0000"]', text: '\href')
expect(page).to have_selector('.katex-html a', text: 'Gitlab')
end
end
it 'renders lazy load button' do
description = <<~MATH
```math
\Huge \sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt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rt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
```
MATH
issue = create(:issue, project: project, description: description)
visit project_issue_path(project, issue)
page.within '.description > .md' do
expect(page).to have_selector('.js-lazy-render-math-container', text: /math block exceeds 1000 characters/)
end
end
it 'allows many expansions', :js do
description = <<~MATH
```math
#{'\\mod e ' * 100}
```
MATH
issue = create(:issue, project: project, description: description)
visit project_issue_path(project, issue)
wait_for_requests
page.within '.description > .md' do
expect(page).not_to have_selector('.katex-error')
end
end
it 'shows error message when too many expansions', :js do
description = <<~MATH
```math
#{'\\mod e ' * 150}
```
MATH
issue = create(:issue, project: project, description: description)
visit project_issue_path(project, issue)
wait_for_requests
page.within '.description > .md' do
click_button 'Display anyway'
expect(page).to have_selector('.katex-error', text: /Too many expansions/)
end
end
it 'shows error message when other errors are generated', :js do
description = <<~MATH
```math
\\unknown
```
MATH
issue = create(:issue, project: project, description: description)
visit project_issue_path(project, issue)
wait_for_requests
page.within '.description > .md' do
expect(page).to have_selector('.katex-error',
text: /There was an error rendering this math block. KaTeX parse error/)
end
end
it 'escapes HTML in error', :js do
description = <<~MATH
```math
\\unknown <script>
```
MATH
issue = create(:issue, project: project, description: description)
visit project_issue_path(project, issue)
wait_for_requests
page.within '.description > .md' do
expect(page).to have_selector('.katex-error', text: /&lt;script&gt;/)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\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{\sqrt{}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
```
MATH
wiki_page = build(:wiki_page, { container: project, content: description })
wiki_page.create message: 'math test commit' # rubocop:disable Rails/SaveBang
wiki_page = project.wiki.find_page(wiki_page.slug)
visit project_wiki_path(project, wiki_page)
wait_for_requests
page.within '.js-wiki-page-content' do
expect(page).not_to have_selector('.js-lazy-render-math')
end
end
end
|