logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe
commit: 3ea5b948a4cee9ea5a1e229f567974c323947ef5
parent: 164bad171f8ebfc5fb240f9c8369df34a506a0df
Author: Yamagishi Kazutoshi <ykzts@desire.sh>
Date:   Tue, 25 Apr 2017 22:03:51 +0900

Decodes URL containing IDN (#2436)


Diffstat:

Mapp/lib/formatter.rb10+++-------
Mspec/lib/formatter_spec.rb21++++++++++++++++-----
2 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb @@ -61,20 +61,15 @@ class Formatter result = '' last_index = entities.reduce(0) do |index, entity| + normalized_url = Addressable::URI.parse(entity[:url]).normalize indices = entity[:indices] result += encode(chars[index...indices.first].join) - result += Twitter::Autolink.send(:link_to_text, entity, link_html(entity[:url]), entity[:url], html_attrs) + result += Twitter::Autolink.send(:link_to_text, entity, link_html(entity[:url]), normalized_url, html_attrs) indices.last end result += encode(chars[last_index..-1].join) end - def link_urls(html) - Twitter::Autolink.auto_link_urls(html, url_target: '_blank', - link_attribute_block: lambda { |_, a| a[:rel] << ' noopener' }, - link_text_block: lambda { |_, text| link_html(text) }) - end - def link_mentions(html, mentions) html.gsub(Account::MENTION_RE) do |match| acct = Account::MENTION_RE.match(match)[1] @@ -102,6 +97,7 @@ class Formatter end def link_html(url) + url = Addressable::URI.parse(url).display_uri.to_s prefix = url.match(/\Ahttps?:\/\/(www\.)?/).to_s text = url[prefix.length, 30] suffix = url[prefix.length + 30..-1] diff --git a/spec/lib/formatter_spec.rb b/spec/lib/formatter_spec.rb @@ -18,7 +18,7 @@ RSpec.describe Formatter do end it 'contains a link' do - expect(subject).to match('<a href="http://google.com" rel="nofollow noopener" target="_blank"><span class="invisible">http://</span><span class="">google.com</span><span class="invisible"></span></a>') + expect(subject).to match('<a href="http://google.com/" rel="nofollow noopener" target="_blank"><span class="invisible">http://</span><span class="">google.com/</span><span class="invisible"></span></a>') end context 'matches a stand-alone medium URL' do @@ -31,7 +31,18 @@ RSpec.describe Formatter do context 'matches a stand-alone google URL' do let(:local_text) { 'http://google.com' } it 'has valid url' do - expect(subject).to include('href="http://google.com"') + expect(subject).to include('href="http://google.com/"') + end + end + + context 'matches a stand-alone IDN URL' do + let(:local_text) { 'https://nic.みんな/' } + it 'has valid url' do + expect(subject).to include('href="https://nic.xn--q9jyb4c/"') + end + + it 'has display url' do + expect(subject).to include('<span class="">nic.みんな/</span>') end end @@ -51,21 +62,21 @@ RSpec.describe Formatter do context 'matches a URL without exclamation point' do let(:local_text) { 'http://www.google.com!' } it 'has valid url' do - expect(subject).to include('href="http://www.google.com"') + expect(subject).to include('href="http://www.google.com/"') end end context 'matches a URL without single quote' do let(:local_text) { "http://www.google.com'" } it 'has valid url' do - expect(subject).to include('href="http://www.google.com"') + expect(subject).to include('href="http://www.google.com/"') end end context 'matches a URL without angle brackets' do let(:local_text) { 'http://www.google.com>' } it 'has valid url' do - expect(subject).to include('href="http://www.google.com"') + expect(subject).to include('href="http://www.google.com/"') end end