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 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