From 31e6f9507bc47ca96ac596af41f1d1c348c53d51 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Thu, 2 Sep 2010 12:27:22 -0300 Subject: [PATCH] Rewrite Article#first_paragraph using an actual HTML parser --- app/models/article.rb | 5 +++-- test/unit/article_test.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/models/article.rb b/app/models/article.rb index dd05ac8..5e0c7e3 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -1,3 +1,5 @@ +require 'hpricot' + class Article < ActiveRecord::Base # xss_terminate plugin can't sanitize array fields @@ -340,8 +342,7 @@ class Article < ActiveRecord::Base end def first_paragraph - to_html =~ /

(.*)<\/p>/ - $1 || '' + Hpricot(to_html).search('p').first.to_html end def creator diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 45d3783..2bda498 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -89,8 +89,8 @@ class ArticleTest < Test::Unit::TestCase should 'provide first paragraph of HTML version' do profile = create_user('testinguser').person a = fast_create(Article, :name => 'my article', :profile_id => profile.id) - a.expects(:body).returns('

the first paragraph of the article

The second paragraph') - assert_equal 'the first paragraph of the article', a.first_paragraph + a.expects(:body).returns('

the first paragraph of the article

The second paragraph

') + assert_equal '

the first paragraph of the article

', a.first_paragraph end should 'inform the icon to be used' do -- libgit2 0.21.2