FIX: Strip links from google indexed bios when the users are new.

This commit is contained in:
Robin Ward 2013-06-05 15:28:10 -04:00
parent 729e4080a6
commit dfba2b6e0a
5 changed files with 31 additions and 8 deletions

View File

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

View File

@ -1,6 +1,6 @@
<h2><%= @user.username %></h2> <h2><%= @user.username %></h2>
<p><%= raw @user.bio_cooked %></p> <p><%= raw @user.bio_processed %></p>
<p><%= t 'powered_by_html' %></p> <p><%= t 'powered_by_html' %></p>

View File

@ -239,6 +239,15 @@ module PrettyText
ExcerptParser.get_excerpt(html, max_length, options) ExcerptParser.get_excerpt(html, max_length, options)
end end
def self.strip_links(string)
return string if string.blank?
# If the user is not basic, strip links from their bio
fragment = Nokogiri::HTML.fragment(string)
fragment.css('a').each {|a| a.replace(a.text) }
fragment.to_html
end
protected protected
def self.ctx_load(*files) def self.ctx_load(*files)

View File

@ -183,6 +183,19 @@ test
end end
describe "strip links" do
it "returns blank for blank input" do
expect(PrettyText.strip_links("")).to be_blank
end
it "does nothing to a string without links" do
expect(PrettyText.strip_links("I'm the <b>batman</b>")).to eq("I'm the <b>batman</b>")
end
it "strips links but leaves the text content" do
expect(PrettyText.strip_links("I'm the linked <a href='http://en.wikipedia.org/wiki/Batman'>batman</a>")).to eq("I'm the linked batman")
end
end
describe "apply cdn" do describe "apply cdn" do
it "should detect bare links to images and apply a CDN" do it "should detect bare links to images and apply a CDN" do

View File

@ -836,7 +836,7 @@ describe User do
end end
describe "bio_excerpt" do describe "bio link stripping" do
it "returns an empty string with no bio" do it "returns an empty string with no bio" do
expect(Fabricate.build(:user).bio_excerpt).to be_blank expect(Fabricate.build(:user).bio_excerpt).to be_blank
@ -852,11 +852,13 @@ describe User do
it "includes the link if the user is not new" do 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>") expect(user.bio_excerpt).to eq("im sissy and i love <a href='http://ponycorns.com' rel='nofollow'>http://ponycorns.com</a>")
expect(user.bio_processed).to eq("<p>im sissy and i love <a href=\"http://ponycorns.com\" rel=\"nofollow\">http://ponycorns.com</a></p>")
end end
it "removes the link if the user is new" do it "removes the link if the user is new" do
user.trust_level = TrustLevel.levels[:newuser] user.trust_level = TrustLevel.levels[:newuser]
expect(user.bio_excerpt).to eq("im sissy and i love http://ponycorns.com") expect(user.bio_excerpt).to eq("im sissy and i love http://ponycorns.com")
expect(user.bio_processed).to eq("<p>im sissy and i love http://ponycorns.com</p>")
end end
end end