2016-09-09 20:04:34 +02:00
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
|
|
RSpec.describe Formatter do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:local_account) { Fabricate(:account, domain: nil, username: 'alice') }
|
2018-04-25 14:12:28 +02:00
|
|
|
|
let(:remote_account) { Fabricate(:account, domain: 'remote.test', username: 'bob', url: 'https://remote.test/') }
|
2017-05-05 19:48:22 +02:00
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
shared_examples 'encode and link URLs' do
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a stand-alone medium URL' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4' }
|
2016-09-09 20:04:34 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include 'href="https://hackernoon.com/the-power-to-build-communities-a-response-to-mark-zuckerberg-3f2cac9148a4"'
|
2017-05-12 17:46:44 +02:00
|
|
|
|
end
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
2016-09-09 20:04:34 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a stand-alone google URL' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'http://google.com' }
|
2016-09-09 20:04:34 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="http://google.com"'
|
2017-05-12 17:46:44 +02:00
|
|
|
|
end
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a stand-alone IDN URL' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'https://nic.みんな/' }
|
2017-02-22 19:35:11 +01:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="https://nic.みんな/"'
|
2017-05-12 17:46:44 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
it 'has display URL' do
|
|
|
|
|
is_expected.to include '<span class="">nic.みんな/</span>'
|
2017-05-12 17:46:44 +02:00
|
|
|
|
end
|
2017-05-05 19:48:22 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with a trailing period' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona. ' }
|
2017-05-24 14:32:53 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL but not the period' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include 'href="http://www.mcmansionhell.com/post/156408871451/50-states-of-mcmansion-hell-scottsdale-arizona"'
|
2017-05-24 14:32:53 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL enclosed with parentheses' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { '(http://google.com/)' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL but not the parentheses' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include 'href="http://google.com/"'
|
2017-05-12 17:46:44 +02:00
|
|
|
|
end
|
2017-05-05 19:48:22 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with a trailing exclamation point' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'http://www.google.com!' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL but not the exclamation point' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="http://www.google.com"'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with a trailing single quote' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { "http://www.google.com'" }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL but not the single quote' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="http://www.google.com"'
|
2017-04-25 15:03:51 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with a trailing angle bracket' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'http://www.google.com>' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL but not the angle bracket' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="http://www.google.com"'
|
2017-04-25 15:03:51 +02:00
|
|
|
|
end
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
2017-04-25 15:03:51 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with a query string' do
|
2019-02-02 19:01:18 +01:00
|
|
|
|
context 'with escaped unicode character' do
|
|
|
|
|
let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink' }
|
2017-06-04 14:58:57 +02:00
|
|
|
|
|
2019-02-02 19:01:18 +01:00
|
|
|
|
it 'matches the full URL' do
|
|
|
|
|
is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=autolink"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with unicode character' do
|
|
|
|
|
let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓&q=autolink' }
|
|
|
|
|
|
|
|
|
|
it 'matches the full URL' do
|
|
|
|
|
is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓&q=autolink"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with unicode character at the end' do
|
|
|
|
|
let(:text) { 'https://www.ruby-toolbox.com/search?utf8=✓' }
|
|
|
|
|
|
|
|
|
|
it 'matches the full URL' do
|
|
|
|
|
is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=✓"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'with escaped and not escaped unicode characters' do
|
|
|
|
|
let(:text) { 'https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&utf81=✓&q=autolink' }
|
|
|
|
|
|
|
|
|
|
it 'preserves escaped unicode characters' do
|
|
|
|
|
is_expected.to include 'href="https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&utf81=✓&q=autolink"'
|
|
|
|
|
end
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with parentheses in it' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'https://en.wikipedia.org/wiki/Diaspora_(software)' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include 'href="https://en.wikipedia.org/wiki/Diaspora_(software)"'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
end
|
|
|
|
|
|
2019-02-09 20:13:11 +01:00
|
|
|
|
context 'given a URL in quotation marks' do
|
|
|
|
|
let(:text) { '"https://example.com/"' }
|
|
|
|
|
|
|
|
|
|
it 'does not match the quotation marks' do
|
|
|
|
|
is_expected.to include 'href="https://example.com/"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'given a URL in angle brackets' do
|
|
|
|
|
let(:text) { '<https://example.com/>' }
|
|
|
|
|
|
|
|
|
|
it 'does not match the angle brackets' do
|
|
|
|
|
is_expected.to include 'href="https://example.com/"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with Japanese path string' do
|
2017-09-14 18:03:20 +02:00
|
|
|
|
let(:text) { 'https://ja.wikipedia.org/wiki/日本' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="https://ja.wikipedia.org/wiki/日本"'
|
2017-09-14 18:03:20 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with Korean path string' do
|
2017-09-14 18:03:20 +02:00
|
|
|
|
let(:text) { 'https://ko.wikipedia.org/wiki/대한민국' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="https://ko.wikipedia.org/wiki/대한민국"'
|
2017-09-14 18:03:20 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-02-09 20:13:11 +01:00
|
|
|
|
context 'given a URL with a full-width space' do
|
|
|
|
|
let(:text) { 'https://example.com/ abc123' }
|
|
|
|
|
|
|
|
|
|
it 'does not match the full-width space' do
|
|
|
|
|
is_expected.to include 'href="https://example.com/"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'given a URL in Japanese quotation marks' do
|
|
|
|
|
let(:text) { '「[https://example.org/」' }
|
|
|
|
|
|
|
|
|
|
it 'does not match the quotation marks' do
|
|
|
|
|
is_expected.to include 'href="https://example.org/"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with Simplified Chinese path string' do
|
2017-09-14 18:03:20 +02:00
|
|
|
|
let(:text) { 'https://baike.baidu.com/item/中华人民共和国' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="https://baike.baidu.com/item/中华人民共和国"'
|
2017-09-14 18:03:20 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL with Traditional Chinese path string' do
|
2017-09-14 18:03:20 +02:00
|
|
|
|
let(:text) { 'https://zh.wikipedia.org/wiki/臺灣' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'matches the full URL' do
|
2018-01-03 20:51:33 +01:00
|
|
|
|
is_expected.to include 'href="https://zh.wikipedia.org/wiki/臺灣"'
|
2017-09-14 18:03:20 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL containing unsafe code (XSS attack, visible part)' do
|
2017-09-16 21:33:52 +02:00
|
|
|
|
let(:text) { %q{http://example.com/b<del>b</del>} }
|
|
|
|
|
|
2019-02-09 20:13:11 +01:00
|
|
|
|
it 'does not include the HTML in the URL' do
|
|
|
|
|
is_expected.to include '"http://example.com/b"'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'escapes the HTML' do
|
2017-09-16 21:33:52 +02:00
|
|
|
|
is_expected.to include '<del>b</del>'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a URL containing unsafe code (XSS attack, invisible part)' do
|
2017-09-16 21:33:52 +02:00
|
|
|
|
let(:text) { %q{http://example.com/blahblahblahblah/a<script>alert("Hello")</script>} }
|
|
|
|
|
|
2019-02-09 20:13:11 +01:00
|
|
|
|
it 'does not include the HTML in the URL' do
|
|
|
|
|
is_expected.to include '"http://example.com/blahblahblahblah/a"'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'escapes the HTML' do
|
2017-09-16 21:33:52 +02:00
|
|
|
|
is_expected.to include '<script>alert("Hello")</script>'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given text containing HTML code (script tag)' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { '<script>alert("Hello")</script>' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'escapes the HTML' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include '<p><script>alert("Hello")</script></p>'
|
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
end
|
2017-04-19 14:52:18 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given text containing HTML (XSS attack)' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { %q{<img src="javascript:alert('XSS');">} }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'escapes the HTML' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include '<p><img src="javascript:alert('XSS');"></p>'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given an invalid URL' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'http://www\.google\.com' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'outputs the raw URL' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to eq '<p>http://www\.google\.com</p>'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given text containing a hashtag' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { '#hashtag' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'creates a hashtag link' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include '/tags/hashtag" class="mention hashtag" rel="tag">#<span>hashtag</span></a>'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
end
|
2019-02-09 03:39:38 +01:00
|
|
|
|
|
|
|
|
|
context 'given text containing a hashtag with Unicode chars' do
|
|
|
|
|
let(:text) { '#hashtagタグ' }
|
|
|
|
|
|
|
|
|
|
it 'creates a hashtag link' do
|
|
|
|
|
is_expected.to include '/tags/hashtag%E3%82%BF%E3%82%B0" class="mention hashtag" rel="tag">#<span>hashtagタグ</span></a>'
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-01-11 02:15:25 +01:00
|
|
|
|
|
|
|
|
|
context 'given a stand-alone xmpp: URI' do
|
|
|
|
|
let(:text) { 'xmpp:user@instance.com' }
|
|
|
|
|
|
|
|
|
|
it 'matches the full URI' do
|
|
|
|
|
is_expected.to include 'href="xmpp:user@instance.com"'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'given a an xmpp: URI with a query-string' do
|
|
|
|
|
let(:text) { 'please join xmpp:muc@instance.com?join right now' }
|
|
|
|
|
|
|
|
|
|
it 'matches the full URI' do
|
|
|
|
|
is_expected.to include 'href="xmpp:muc@instance.com?join"'
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
|
2018-08-31 15:16:59 +02:00
|
|
|
|
describe '#format_spoiler' do
|
|
|
|
|
subject { Formatter.instance.format_spoiler(status) }
|
|
|
|
|
|
|
|
|
|
context 'given a post containing plain text' do
|
2018-10-04 12:36:53 +02:00
|
|
|
|
let(:status) { Fabricate(:status, text: 'text', spoiler_text: 'Secret!', uri: nil) }
|
2018-08-31 15:16:59 +02:00
|
|
|
|
|
|
|
|
|
it 'Returns the spoiler text' do
|
|
|
|
|
is_expected.to eq 'Secret!'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
|
|
|
|
let!(:emoji) { Fabricate(:custom_emoji) }
|
2018-10-04 12:36:53 +02:00
|
|
|
|
let(:status) { Fabricate(:status, text: 'text', spoiler_text: ':coolcat: Secret!', uri: nil) }
|
2018-08-31 15:16:59 +02:00
|
|
|
|
let(:text) { ':coolcat: Beep boop' }
|
|
|
|
|
|
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/<img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2018-08-31 15:16:59 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
describe '#format' do
|
|
|
|
|
subject { Formatter.instance.format(status) }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with local status' do
|
|
|
|
|
context 'given a reblogged post' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:reblog) { Fabricate(:status, account: local_account, text: 'Hello world', uri: nil) }
|
|
|
|
|
let(:status) { Fabricate(:status, reblog: reblog) }
|
|
|
|
|
|
|
|
|
|
it 'returns original status with credit to its author' do
|
|
|
|
|
is_expected.to include 'RT <span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span> Hello world'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing plain text' do
|
2018-10-04 12:36:53 +02:00
|
|
|
|
let(:status) { Fabricate(:status, text: 'text', uri: nil) }
|
2017-06-04 14:58:57 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'paragraphizes the text' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to eq '<p>text</p>'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing line feeds' do
|
2018-10-04 12:36:53 +02:00
|
|
|
|
let(:status) { Fabricate(:status, text: "line\nfeed", uri: nil) }
|
2017-06-04 14:58:57 +02:00
|
|
|
|
|
|
|
|
|
it 'removes line feeds' do
|
|
|
|
|
is_expected.not_to include "\n"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing linkable mentions' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:status) { Fabricate(:status, mentions: [ Fabricate(:mention, account: local_account) ], text: '@alice') }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'creates a mention link' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include '<a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span>'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing unlinkable mentions' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:status) { Fabricate(:status, text: '@alice', uri: nil) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'does not create a mention link' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include '@alice'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context do
|
|
|
|
|
subject do
|
|
|
|
|
status = Fabricate(:status, text: text, uri: nil)
|
|
|
|
|
Formatter.instance.format(status)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
include_examples 'encode and link URLs'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-09-19 02:42:40 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with custom_emojify option' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let!(:emoji) { Fabricate(:custom_emoji) }
|
|
|
|
|
let(:status) { Fabricate(:status, account: local_account, text: text) }
|
|
|
|
|
|
|
|
|
|
subject { Formatter.instance.format(status, custom_emojify: true) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { ':coolcat: Beep boop' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2017-09-19 02:42:40 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { 'Beep :coolcat: boop' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2017-09-19 02:42:40 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with concatenated emoji shortcodes' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { ':coolcat::coolcat:' }
|
|
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
|
is_expected.to match(/:coolcat::coolcat:/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { 'Beep boop :coolcat:' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2017-09-19 02:42:40 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with remote status' do
|
2017-09-06 19:01:28 +02:00
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: 'Beep boop') }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'reformats the post' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to eq 'Beep boop'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-09-19 02:42:40 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with custom_emojify option' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
|
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: text) }
|
|
|
|
|
|
|
|
|
|
subject { Formatter.instance.format(status, custom_emojify: true) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { '<p>:coolcat: Beep boop<br />' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2017-09-19 02:42:40 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { '<p>Beep :coolcat: boop</p>' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2017-09-19 02:42:40 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with concatenated emoji' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { '<p>:coolcat::coolcat:</p>' }
|
|
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
|
is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2017-09-19 02:42:40 +02:00
|
|
|
|
let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2017-09-19 02:42:40 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#reformat' do
|
|
|
|
|
subject { Formatter.instance.reformat(text) }
|
2017-04-19 14:52:18 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing plain text' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { 'Beep boop' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'keeps the plain text' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to include 'Beep boop'
|
2017-05-12 17:46:44 +02:00
|
|
|
|
end
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing script tags' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { '<script>alert("Hello")</script>' }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'strips the scripts' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to_not include '<script>alert("Hello")</script>'
|
2017-04-19 14:52:18 +02:00
|
|
|
|
end
|
2017-02-22 19:35:11 +01:00
|
|
|
|
end
|
2017-06-17 20:26:05 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing malicious classes' do
|
2018-01-03 03:54:08 +01:00
|
|
|
|
let(:text) { '<span class="mention status__content__spoiler-link">Show more</span>' }
|
2017-06-17 20:26:05 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'strips the malicious classes' do
|
2017-06-17 20:26:05 +02:00
|
|
|
|
is_expected.to_not include 'status__content__spoiler-link'
|
|
|
|
|
end
|
|
|
|
|
end
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
2017-05-09 18:17:41 +02:00
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
describe '#plaintext' do
|
|
|
|
|
subject { Formatter.instance.plaintext(status) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with local status' do
|
2018-10-04 12:36:53 +02:00
|
|
|
|
let(:status) { Fabricate(:status, text: '<p>a text by a nerd who uses an HTML tag in text</p>', uri: nil) }
|
2017-05-12 17:46:44 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'returns the raw text' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to eq '<p>a text by a nerd who uses an HTML tag in text</p>'
|
2017-05-09 18:17:41 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2017-05-24 15:36:10 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with remote status' do
|
2018-10-04 12:36:53 +02:00
|
|
|
|
let(:status) { Fabricate(:status, account: remote_account, text: '<script>alert("Hello")</script>') }
|
2017-05-24 15:36:10 +02:00
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
it 'returns tag-stripped text' do
|
|
|
|
|
is_expected.to eq ''
|
2017-05-24 15:36:10 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2016-09-09 20:04:34 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
describe '#simplified_format' do
|
|
|
|
|
subject { Formatter.instance.simplified_format(account) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with local status' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:account) { Fabricate(:account, domain: nil, note: text) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing linkable mentions for local accounts' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { '@alice' }
|
|
|
|
|
|
|
|
|
|
before { local_account }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'creates a mention link' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to eq '<p><span class="h-card"><a href="https://cb6e6126.ngrok.io/@alice" class="u-url mention">@<span>alice</span></a></span></p>'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing linkable mentions for remote accounts' do
|
2018-04-25 14:12:28 +02:00
|
|
|
|
let(:text) { '@bob@remote.test' }
|
2017-06-04 14:58:57 +02:00
|
|
|
|
|
|
|
|
|
before { remote_account }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'creates a mention link' do
|
2018-04-25 14:12:28 +02:00
|
|
|
|
is_expected.to eq '<p><span class="h-card"><a href="https://remote.test/" class="u-url mention">@<span>bob</span></a></span></p>'
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post containing unlinkable mentions' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { '@alice' }
|
2016-09-09 20:04:34 +02:00
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'does not create a mention link' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
is_expected.to eq '<p>@alice</p>'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with custom_emojify option' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let!(:emoji) { Fabricate(:custom_emoji) }
|
|
|
|
|
|
|
|
|
|
before { account.note = text }
|
|
|
|
|
subject { Formatter.instance.simplified_format(account, custom_emojify: true) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { ':coolcat: Beep boop' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2018-04-01 23:55:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { 'Beep :coolcat: boop' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2018-04-01 23:55:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with concatenated emoji shortcodes' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { ':coolcat::coolcat:' }
|
|
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
|
is_expected.to match(/:coolcat::coolcat:/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { 'Beep boop :coolcat:' }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
it 'converts the shortcode to an image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/boop <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2018-04-01 23:55:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
include_examples 'encode and link URLs'
|
2016-09-09 20:04:34 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with remote status' do
|
2017-06-04 14:58:57 +02:00
|
|
|
|
let(:text) { '<script>alert("Hello")</script>' }
|
|
|
|
|
let(:account) { Fabricate(:account, domain: 'remote', note: text) }
|
|
|
|
|
|
|
|
|
|
it 'reformats' do
|
|
|
|
|
is_expected.to_not include '<script>alert("Hello")</script>'
|
|
|
|
|
end
|
2018-04-01 23:55:42 +02:00
|
|
|
|
|
|
|
|
|
context 'with custom_emojify option' do
|
|
|
|
|
let!(:emoji) { Fabricate(:custom_emoji, domain: remote_account.domain) }
|
|
|
|
|
|
|
|
|
|
before { remote_account.note = text }
|
|
|
|
|
|
|
|
|
|
subject { Formatter.instance.simplified_format(remote_account, custom_emojify: true) }
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the start' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { '<p>:coolcat: Beep boop<br />' }
|
|
|
|
|
|
|
|
|
|
it 'converts shortcode to image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/<p><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2018-04-01 23:55:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode in the middle' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { '<p>Beep :coolcat: boop</p>' }
|
|
|
|
|
|
|
|
|
|
it 'converts shortcode to image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/Beep <img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2018-04-01 23:55:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with concatenated emoji shortcodes' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { '<p>:coolcat::coolcat:</p>' }
|
|
|
|
|
|
|
|
|
|
it 'does not touch the shortcodes' do
|
|
|
|
|
is_expected.to match(/<p>:coolcat::coolcat:<\/p>/)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-29 01:20:56 +02:00
|
|
|
|
context 'given a post with an emoji shortcode at the end' do
|
2018-04-01 23:55:42 +02:00
|
|
|
|
let(:text) { '<p>Beep boop<br />:coolcat:</p>' }
|
|
|
|
|
|
|
|
|
|
it 'converts shortcode to image tag' do
|
2019-07-21 18:10:40 +02:00
|
|
|
|
is_expected.to match(/<br><img draggable="false" class="emojione custom-emoji" alt=":coolcat:"/)
|
2018-04-01 23:55:42 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-09-09 20:04:34 +02:00
|
|
|
|
end
|
2017-06-04 14:58:57 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
describe '#sanitize' do
|
|
|
|
|
let(:html) { '<script>alert("Hello")</script>' }
|
|
|
|
|
|
|
|
|
|
subject { Formatter.instance.sanitize(html, Sanitize::Config::MASTODON_STRICT) }
|
2016-09-09 20:04:34 +02:00
|
|
|
|
|
2017-06-04 14:58:57 +02:00
|
|
|
|
it 'sanitizes' do
|
2018-10-29 14:05:25 +01:00
|
|
|
|
is_expected.to eq ''
|
2016-09-09 20:04:34 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|