From dfba2b6e0ab7af0240812f96d116faf918582584 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 5 Jun 2013 15:28:10 -0400 Subject: [PATCH] FIX: Strip links from google indexed bios when the users are new. --- app/models/user.rb | 11 +++++------ app/views/users/show.html.erb | 2 +- lib/pretty_text.rb | 9 +++++++++ spec/components/pretty_text_spec.rb | 13 +++++++++++++ spec/models/user_spec.rb | 4 +++- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index f7d716717cc..da28f739fca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -433,13 +433,12 @@ class User < ActiveRecord::Base def bio_excerpt excerpt = PrettyText.excerpt(bio_cooked, 350) 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 - fragment = Nokogiri::HTML.fragment(excerpt) - fragment.css('a').each do |a| - a.replace(a.text) - end - fragment.to_html + def bio_processed + return bio_cooked if bio_cooked.blank? || has_trust_level?(:basic) + PrettyText.strip_links(bio_cooked) end def delete_all_posts!(guardian) diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index fdfee589790..58d0ea4c506 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,6 +1,6 @@

<%= @user.username %>

-

<%= raw @user.bio_cooked %>

+

<%= raw @user.bio_processed %>

<%= t 'powered_by_html' %>

diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb index f2050a96892..f3f39ec4ead 100644 --- a/lib/pretty_text.rb +++ b/lib/pretty_text.rb @@ -239,6 +239,15 @@ module PrettyText ExcerptParser.get_excerpt(html, max_length, options) 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 def self.ctx_load(*files) diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb index 8c74454e278..01db243292f 100644 --- a/spec/components/pretty_text_spec.rb +++ b/spec/components/pretty_text_spec.rb @@ -183,6 +183,19 @@ test 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 batman")).to eq("I'm the batman") + end + + it "strips links but leaves the text content" do + expect(PrettyText.strip_links("I'm the linked batman")).to eq("I'm the linked batman") + end + end describe "apply cdn" do it "should detect bare links to images and apply a CDN" do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3b726566926..261525c466c 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -836,7 +836,7 @@ describe User do end - describe "bio_excerpt" do + describe "bio link stripping" do it "returns an empty string with no bio" do 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 expect(user.bio_excerpt).to eq("im sissy and i love http://ponycorns.com") + expect(user.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") 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") + expect(user.bio_processed).to eq("

im sissy and i love http://ponycorns.com

") end end