2019-04-29 19:27:42 -05:00
# frozen_string_literal: true
2023-01-09 05:18:21 -06:00
require " discourse_diff "
2013-12-16 11:11:46 -06:00
2022-07-27 21:27:38 -05:00
RSpec . describe DiscourseDiff do
2013-12-16 11:11:46 -06:00
describe " inline_html " do
2014-06-15 22:10:19 -05:00
it " does not lead to XSS " do
2014-06-15 19:24:54 -05:00
a = " <test>start</test> "
b = " <test>end</test> "
2023-01-09 05:18:21 -06:00
prev = " <div> #{ CGI . escapeHTML ( a ) } </div> "
cur = " <div> #{ CGI . escapeHTML ( b ) } </div> "
2014-06-15 19:24:54 -05:00
2017-07-27 20:20:09 -05:00
diff = DiscourseDiff . new ( prev , cur )
2023-01-09 05:18:21 -06:00
expect ( diff . inline_html ) . not_to match ( %r{ </?test> } )
expect ( diff . side_by_side_html ) . not_to match ( %r{ </?test> } )
2014-06-15 19:24:54 -05:00
end
2013-12-16 11:11:46 -06:00
it " returns an empty div when no content is diffed " do
2015-01-09 10:34:37 -06:00
expect ( DiscourseDiff . new ( " " , " " ) . inline_html ) . to eq ( " <div class= \" inline-diff \" ></div> " )
2013-12-16 11:11:46 -06:00
end
it " returns the diffed content when there is no difference " do
before = after = " <p>this is a paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . inline_html ) . to eq (
" <div class= \" inline-diff \" ><p>this is a paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds <ins> tags around added text " do
before = " <p>this is a paragraph</p> "
after = " <p>this is a great paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . inline_html ) . to eq (
" <div class= \" inline-diff \" ><p>this is a <ins>great </ins>paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds <del> tags around removed text " do
before = " <p>this is a great paragraph</p> "
after = " <p>this is a paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . inline_html ) . to eq (
" <div class= \" inline-diff \" ><p>this is a <del>great </del>paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds .diff-ins class when a paragraph is added " do
before = " <p>this is the first paragraph</p> "
after = " <p>this is the first paragraph</p><p>this is the second paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . inline_html ) . to eq (
" <div class= \" inline-diff \" ><p>this is the first paragraph</p><p class= \" diff-ins \" >this is the second paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds .diff-del class when a paragraph is removed " do
before = " <p>this is the first paragraph</p><p>this is the second paragraph</p> "
after = " <p>this is the second paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . inline_html ) . to eq (
" <div class= \" inline-diff \" ><p class= \" diff-del \" >this is the first paragraph</p><p>this is the second paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
2016-05-09 01:44:21 -05:00
it " does not break diff on character references " do
before = " <p>'</p> "
after = " <p></p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . inline_html ) . to eq (
" <div class= \" inline-diff \" ><p><del>& # 39;</del></p></div> " ,
)
2016-05-09 01:44:21 -05:00
end
2013-12-16 11:11:46 -06:00
end
describe " side_by_side_html " do
it " returns two empty divs when no content is diffed " do
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( " " , " " ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ></div><div class= \" revision-content \" ></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " returns the diffed content on both sides when there is no difference " do
before = after = " <p>this is a paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ><p>this is a paragraph</p></div><div class= \" revision-content \" ><p>this is a paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds <ins> tags around added text on the right div " do
before = " <p>this is a paragraph</p> "
after = " <p>this is a great paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ><p>this is a paragraph</p></div><div class= \" revision-content \" ><p>this is a <ins>great </ins>paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
2019-10-16 10:59:42 -05:00
it " adds <ins> and <del> tags on consecutive paragraphs " do
2019-10-11 02:50:37 -05:00
before = " <p>this is one paragraph</p><p>here is yet another</p> "
after = " <p>this is one great paragraph</p><p>here is another</p> "
got = DiscourseDiff . new ( before , after ) . side_by_side_html
2023-01-09 05:18:21 -06:00
expect ( got ) . to eq (
" <div class= \" revision-content \" ><p>this is one paragraph</p><p>here is <del>yet </del>another</p></div><div class= \" revision-content \" ><p>this is one <ins>great </ins>paragraph</p><p>here is another</p></div> " ,
)
2019-10-11 02:50:37 -05:00
end
2013-12-16 11:11:46 -06:00
it " adds <del> tags around removed text on the left div " do
before = " <p>this is a great paragraph</p> "
after = " <p>this is a paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ><p>this is a <del>great </del>paragraph</p></div><div class= \" revision-content \" ><p>this is a paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds .diff-ins class when a paragraph is added " do
before = " <p>this is the first paragraph</p> "
after = " <p>this is the first paragraph</p><p>this is the second paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ><p>this is the first paragraph</p></div><div class= \" revision-content \" ><p>this is the first paragraph</p><p class= \" diff-ins \" >this is the second paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds .diff-del class when a paragraph is removed " do
before = " <p>this is the first paragraph</p><p>this is the second paragraph</p> "
after = " <p>this is the second paragraph</p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ><p class= \" diff-del \" >this is the first paragraph</p><p>this is the second paragraph</p></div><div class= \" revision-content \" ><p>this is the second paragraph</p></div> " ,
)
2013-12-16 11:11:46 -06:00
end
2016-05-09 01:44:21 -05:00
it " does not break diff on character references " do
before = " <p>'</p> "
after = " <p></p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ><p><del>& # 39;</del></p></div><div class= \" revision-content \" ><p></p></div> " ,
)
2016-05-09 01:44:21 -05:00
end
2021-08-10 08:14:37 -05:00
it " escapes attribute values " do
before = " <p data-attr='Some \" quoted \" string'></p> "
after = " <p data-attr='Some \" quoted \" string'></p> "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_html ) . to eq (
" <div class= \" revision-content \" ><p data-attr= \" Some "quoted" string \" ></p></div><div class= \" revision-content \" ><p data-attr= \" Some "quoted" string \" ></p></div> " ,
)
2021-08-10 08:14:37 -05:00
end
2013-12-16 11:11:46 -06:00
end
describe " side_by_side_markdown " do
it " returns an empty table when no content is diffed " do
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( " " , " " ) . side_by_side_markdown ) . to eq (
" <table class= \" markdown \" ></table> " ,
)
2013-12-16 11:11:46 -06:00
end
it " properly escape html tags " do
before = " "
after = " <img src= \" //domain.com/image.png> \" "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_markdown ) . to eq (
" <table class= \" markdown \" ><tr><td></td><td class= \" diff-ins \" ><img src="//domain.com/image.png>"</td></tr></table> " ,
)
2013-12-16 11:11:46 -06:00
end
it " returns the diffed content on both columns when there is no difference " do
before = after = " this is a paragraph "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_markdown ) . to eq (
" <table class= \" markdown \" ><tr><td>this is a paragraph</td><td>this is a paragraph</td></tr></table> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds <ins> tags around added text on the second column " do
before = " this is a paragraph "
after = " this is a great paragraph "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_markdown ) . to eq (
" <table class= \" markdown \" ><tr><td class= \" diff-del \" >this is a paragraph</td><td class= \" diff-ins \" >this is a <ins>great </ins>paragraph</td></tr></table> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds <del> tags around removed text on the first column " do
before = " this is a great paragraph "
after = " this is a paragraph "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_markdown ) . to eq (
" <table class= \" markdown \" ><tr><td class= \" diff-del \" >this is a <del>great </del>paragraph</td><td class= \" diff-ins \" >this is a paragraph</td></tr></table> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds .diff-ins class when a paragraph is added " do
before = " this is the first paragraph "
after = " this is the first paragraph \n this is the second paragraph "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_markdown ) . to eq (
" <table class= \" markdown \" ><tr><td class= \" diff-del \" >this is the first paragraph</td><td class= \" diff-ins \" >this is the first paragraph<ins> \n this is the second paragraph</ins></td></tr></table> " ,
)
2013-12-16 11:11:46 -06:00
end
it " adds .diff-del class when a paragraph is removed " do
before = " this is the first paragraph \n this is the second paragraph "
after = " this is the second paragraph "
2023-01-09 05:18:21 -06:00
expect ( DiscourseDiff . new ( before , after ) . side_by_side_markdown ) . to eq (
" <table class= \" markdown \" ><tr><td class= \" diff-del \" >this is the first paragraph \n </td><td></td></tr><tr><td>this is the second paragraph</td><td>this is the second paragraph</td></tr></table> " ,
)
2013-12-16 11:11:46 -06:00
end
end
end