Skip to content

Commit

Permalink
Merge pull request from GHSA-55j9-c3mp-6fcq
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed Jul 6, 2023
1 parent e75ad1d commit 32ebeed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
6 changes: 5 additions & 1 deletion app/helpers/formatting_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def account_bio_format(account)
end

def account_field_value_format(field, with_rel_me: true)
html_aware_format(field.value, field.account.local?, with_rel_me: with_rel_me, with_domains: true, multiline: false)
if field.verified? && !field.account.local?
TextFormatter.shortened_link(field.value_for_verification)
else
html_aware_format(field.value, field.account.local?, with_rel_me: with_rel_me, with_domains: true, multiline: false)
end
end
end
34 changes: 21 additions & 13 deletions app/lib/text_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ def to_s
html.html_safe # rubocop:disable Rails/OutputSafety
end

class << self
include ERB::Util

def shortened_link(url, rel_me: false)
url = Addressable::URI.parse(url).to_s
rel = rel_me ? (DEFAULT_REL + %w(me)) : DEFAULT_REL

prefix = url.match(URL_PREFIX_REGEX).to_s
display_url = url[prefix.length, 30]
suffix = url[prefix.length + 30..-1]

Check failure on line 60 in app/lib/text_formatter.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/SlicingWithRange: Prefer ary[n..] over ary[n..-1].
cutoff = url[prefix.length..-1].length > 30

Check failure on line 61 in app/lib/text_formatter.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/SlicingWithRange: Prefer ary[n..] over ary[n..-1].

<<~HTML.squish
<a href="#{h(url)}" target="_blank" rel="#{rel.join(' ')}"><span class="invisible">#{h(prefix)}</span><span class="#{cutoff ? 'ellipsis' : ''}">#{h(display_url)}</span><span class="invisible">#{h(suffix)}</span></a>
HTML
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
h(url)
end
end

private

def rewrite
Expand All @@ -70,19 +90,7 @@ def rewrite
end

def link_to_url(entity)
url = Addressable::URI.parse(entity[:url]).to_s
rel = with_rel_me? ? (DEFAULT_REL + %w(me)) : DEFAULT_REL

prefix = url.match(URL_PREFIX_REGEX).to_s
display_url = url[prefix.length, 30]
suffix = url[prefix.length + 30..-1]
cutoff = url[prefix.length..-1].length > 30

<<~HTML.squish
<a href="#{h(url)}" target="_blank" rel="#{rel.join(' ')}"><span class="invisible">#{h(prefix)}</span><span class="#{cutoff ? 'ellipsis' : ''}">#{h(display_url)}</span><span class="invisible">#{h(suffix)}</span></a>
HTML
rescue Addressable::URI::InvalidURIError, IDN::Idna::IdnaError
h(entity[:url])
TextFormatter.shortened_link(entity[:url], rel_me: with_rel_me?)
end

def link_to_hashtag(entity)
Expand Down

0 comments on commit 32ebeed

Please sign in to comment.