Strip out links when displaying a new user's bio

This commit is contained in:
Robin Ward 2013-06-04 12:05:36 -04:00
parent 2259e97d42
commit 4392a17b54
6 changed files with 49 additions and 13 deletions

View File

@ -36,9 +36,9 @@
</div>
<div class="container">
<div class='user-info clearfix'>
<div class='about-me'>
{{{bio_excerpt}}}
</div>
<div class='about-me'>
{{{bio_excerpt}}}
</div>
</div>
</div>
<div class="container">

View File

@ -431,7 +431,15 @@ class User < ActiveRecord::Base
end
def bio_excerpt
PrettyText.excerpt(bio_cooked, 350)
excerpt = PrettyText.excerpt(bio_cooked, 350)
return excerpt if excerpt.blank? || has_trust_level?(:basic)
# If the user is not basic, strip links from their bio
fragment = Nokogiri::HTML.fragment(excerpt)
fragment.css('a').each do |a|
a.replace(a.text)
end
fragment.to_html
end
def delete_all_posts!(guardian)

View File

@ -28,15 +28,16 @@ class UserSerializer < BasicUserSerializer
end
def bio_excerpt
e = object.bio_excerpt
unless e && e.length > 0
e = if scope.user && scope.user.id == object.id
I18n.t('user_profile.no_info_me', username_lower: object.username_lower)
else
I18n.t('user_profile.no_info_other', name: object.name)
end
# If they have a bio return it
excerpt = object.bio_excerpt
return excerpt if excerpt.present?
# Without a bio, determine what message to show
if scope.user && scope.user.id == object.id
I18n.t('user_profile.no_info_me', username_lower: object.username_lower)
else
I18n.t('user_profile.no_info_other', name: object.name)
end
e
end
private_attributes :email,

View File

@ -15,6 +15,7 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
catch(:done) do
parser.parse(html) unless html.nil?
end
me.excerpt.strip!
me.excerpt
end

View File

@ -135,7 +135,7 @@ test
end
it "should insert a space between to Ps" do
PrettyText.excerpt("<p>a</p><p>b</p>",5).should == "a b "
PrettyText.excerpt("<p>a</p><p>b</p>",5).should == "a b"
end
it "should strip quotes" do

View File

@ -836,6 +836,32 @@ describe User do
end
describe "bio_excerpt" do
it "returns an empty string with no bio" do
expect(Fabricate.build(:user).bio_excerpt).to be_blank
end
context "with a user that has a link in their bio" do
let(:user) { Fabricate.build(:user, bio_raw: "im sissy and i love http://ponycorns.com") }
before do
# Let's cook that bio up good
user.send(:cook)
end
it "includes the link if the user is not new" do
expect(user.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com' rel='nofollow'>http://ponycorns.com</a>")
end
it "removes the link if the user is new" do
user.trust_level = TrustLevel.levels[:newuser]
expect(user.bio_excerpt).to eq("im sissy and i love http://ponycorns.com")
end
end
end
describe 'update_time_read!' do
let(:user) { Fabricate(:user) }