From 22704867a07b2353bc6b0bc53295bd0dfc89c166 Mon Sep 17 00:00:00 2001 From: Ben Zimmer Date: Sun, 12 May 2013 14:30:34 +0200 Subject: [PATCH 1/2] strips HTML tags off of story title for headline use --- app/models/story.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/story.rb b/app/models/story.rb index 7e41645b7..2c51dc3f7 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -8,7 +8,7 @@ class Story < ActiveRecord::Base UNTITLED = "[untitled]" def headline - self.title.nil? ? UNTITLED : self.title[0, 50] + self.title.nil? ? UNTITLED : Loofah::Helpers.strip_tags(self.title)[0, 50] end def lead From 9761e52c745c970a95e931157e82cceff6bcd513 Mon Sep 17 00:00:00 2001 From: Ben Zimmer Date: Sun, 12 May 2013 14:33:39 +0200 Subject: [PATCH 2/2] specs for HTML tag stripping of headline (Depends on PR #84) --- spec/models/story_spec.rb | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/spec/models/story_spec.rb b/spec/models/story_spec.rb index 3667a4dbd..24d09c6d5 100644 --- a/spec/models/story_spec.rb +++ b/spec/models/story_spec.rb @@ -13,13 +13,31 @@ end describe "#headline" do - it "truncates to 50 chars" do - story.headline.size.should eq(50) + context "plain string title" do + + it "truncates to 50 chars" do + story.headline.size.should eq(50) + end + + it "uses a fallback string if story has no title" do + story.title = nil + story.headline.should eq(Story::UNTITLED) + end end - it "uses a fallback string if story has no title" do - story.title = nil - story.headline.should eq(Story::UNTITLED) + context "title with HTML tags" do + before { story.title = "#{Faker::Lorem.sentence(50)}" } + + it "strips HTML tags from story title" do + story.headline.should_not start_with('') + story.headline.should_not end_with('') + end + + it "truncates to 50 chars after stripping tags" do + story.headline.should_not start_with('') + story.headline.size.should eq(50) + end + end end