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

math_filter_spec.rb « filter « banzai « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 128f8532d3991cf04008c311505c66ac9e9a175d (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Banzai::Filter::MathFilter do
  include FilterSpecHelper

  it 'leaves regular inline code unchanged' do
    input = "<code>2+2</code>"
    doc = filter(input)

    expect(doc.to_s).to eq input
  end

  it 'removes surrounding dollar signs and adds class code, math and js-render-math' do
    doc = filter("$<code>2+2</code>$")

    expect(doc.to_s).to eq '<code class="code math js-render-math" data-math-style="inline">2+2</code>'
  end

  it 'only removes surrounding dollar signs' do
    doc = filter("test $<code>2+2</code>$ test")
    before = doc.xpath('descendant-or-self::text()[1]').first
    after = doc.xpath('descendant-or-self::text()[3]').first

    expect(before.to_s).to eq 'test '
    expect(after.to_s).to eq ' test'
  end

  it 'only removes surrounding single dollar sign' do
    doc = filter("test $$<code>2+2</code>$$ test")
    before = doc.xpath('descendant-or-self::text()[1]').first
    after = doc.xpath('descendant-or-self::text()[3]').first

    expect(before.to_s).to eq 'test $'
    expect(after.to_s).to eq '$ test'
  end

  it 'adds data-math-style inline attribute to inline math' do
    doc = filter('$<code>2+2</code>$')
    code = doc.xpath('descendant-or-self::code').first

    expect(code['data-math-style']).to eq 'inline'
  end

  it 'adds class code and math to inline math' do
    doc = filter('$<code>2+2</code>$')
    code = doc.xpath('descendant-or-self::code').first

    expect(code[:class]).to include("code")
    expect(code[:class]).to include("math")
  end

  it 'adds js-render-math class to inline math' do
    doc = filter('$<code>2+2</code>$')
    code = doc.xpath('descendant-or-self::code').first

    expect(code[:class]).to include("js-render-math")
  end

  # Cases with faulty syntax. Should be a no-op

  it 'ignores cases with missing dolar sign at the end' do
    input = "test $<code>2+2</code> test"
    doc = filter(input)

    expect(doc.to_s).to eq input
  end

  it 'ignores cases with missing dolar sign at the beginning' do
    input = "test <code>2+2</code>$ test"
    doc = filter(input)

    expect(doc.to_s).to eq input
  end

  it 'ignores dollar signs if it is not adjacent' do
    input = '<p>We check strictly $<code>2+2</code> and  <code>2+2</code>$ </p>'
    doc = filter(input)

    expect(doc.to_s).to eq input
  end

  it 'ignores dollar signs if they are inside another element' do
    input = '<p>We check strictly <em>$</em><code>2+2</code><em>$</em></p>'
    doc = filter(input)

    expect(doc.to_s).to eq input
  end

  # Display math

  it 'adds data-math-style display attribute to display math' do
    doc = filter('<pre class="code highlight js-syntax-highlight language-math" v-pre="true"><code>2+2</code></pre>')
    pre = doc.xpath('descendant-or-self::pre').first

    expect(pre['data-math-style']).to eq 'display'
  end

  it 'adds js-render-math class to display math' do
    doc = filter('<pre class="code highlight js-syntax-highlight language-math" v-pre="true"><code>2+2</code></pre>')
    pre = doc.xpath('descendant-or-self::pre').first

    expect(pre[:class]).to include("js-render-math")
  end

  it 'ignores code blocks that are not math' do
    input = '<pre class="code highlight js-syntax-highlight language-plaintext" v-pre="true"><code>2+2</code></pre>'
    doc = filter(input)

    expect(doc.to_s).to eq input
  end

  it 'requires the pre to contain both code and math' do
    input = '<pre class="highlight js-syntax-highlight language-plaintext language-math" v-pre="true"><code>2+2</code></pre>'
    doc = filter(input)

    expect(doc.to_s).to eq input
  end

  it 'dollar signs around to display math' do
    doc = filter('$<pre class="code highlight js-syntax-highlight language-math" v-pre="true"><code>2+2</code></pre>$')
    before = doc.xpath('descendant-or-self::text()[1]').first
    after = doc.xpath('descendant-or-self::text()[3]').first

    expect(before.to_s).to eq '$'
    expect(after.to_s).to eq '$'
  end

  it 'limits how many elements can be marked as math' do
    stub_const('Banzai::Filter::MathFilter::RENDER_NODES_LIMIT', 2)

    doc = filter('$<code>2+2</code>$ + $<code>3+3</code>$ + $<code>4+4</code>$')

    expect(doc.search('.js-render-math').count).to eq(2)
  end
end