summaryrefslogtreecommitdiff
path: root/spec/lib/banzai/filter/inline_diff_filter_spec.rb
blob: c09065fb746f49e6e4a433247d9d06c08ec6e16c (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
# frozen_string_literal: true

require 'spec_helper'

describe Banzai::Filter::InlineDiffFilter do
  include FilterSpecHelper

  it 'adds inline diff span tags for deletions when using square brackets' do
    doc = "START [-something deleted-] END"
    expect(filter(doc).to_html).to eq('START <span class="idiff left right deletion">something deleted</span> END')
  end

  it 'adds inline diff span tags for deletions when using curley braces' do
    doc = "START {-something deleted-} END"
    expect(filter(doc).to_html).to eq('START <span class="idiff left right deletion">something deleted</span> END')
  end

  it 'does not add inline diff span tags when a closing tag is not provided' do
    doc = "START [- END"
    expect(filter(doc).to_html).to eq(doc)
  end

  it 'adds inline span tags for additions when using square brackets' do
    doc = "START [+something added+] END"
    expect(filter(doc).to_html).to eq('START <span class="idiff left right addition">something added</span> END')
  end

  it 'adds inline span tags for additions  when using curley braces' do
    doc = "START {+something added+} END"
    expect(filter(doc).to_html).to eq('START <span class="idiff left right addition">something added</span> END')
  end

  it 'does not add inline diff span tags when a closing addition tag is not provided' do
    doc = "START {+ END"
    expect(filter(doc).to_html).to eq(doc)
  end

  it 'does not add inline diff span tags when the tags do not match' do
    examples = [
      "{+ additions +]",
      "[+ additions +}",
      "{- delletions -]",
      "[- delletions -}"
    ]

    examples.each do |doc|
      expect(filter(doc).to_html).to eq(doc)
    end
  end

  it 'prevents user-land html being injected' do
    doc = "START {+&lt;script&gt;alert('I steal cookies')&lt;/script&gt;+} END"
    expect(filter(doc).to_html).to eq("START <span class=\"idiff left right addition\">&lt;script&gt;alert('I steal cookies')&lt;/script&gt;</span> END")
  end

  it 'preserves content inside pre tags' do
    doc = "<pre>START {+something added+} END</pre>"
    expect(filter(doc).to_html).to eq(doc)
  end

  it 'preserves content inside code tags' do
    doc = "<code>START {+something added+} END</code>"
    expect(filter(doc).to_html).to eq(doc)
  end

  it 'preserves content inside tt tags' do
    doc = "<tt>START {+something added+} END</tt>"
    expect(filter(doc).to_html).to eq(doc)
  end
end