Commit 31e6f9507bc47ca96ac596af41f1d1c348c53d51
1 parent
b449ecbd
Exists in
master
and in
29 other branches
Rewrite Article#first_paragraph using an actual HTML parser
(ActionItem1668)
Showing
2 changed files
with
5 additions
and
4 deletions
Show diff stats
app/models/article.rb
1 | +require 'hpricot' | ||
2 | + | ||
1 | class Article < ActiveRecord::Base | 3 | class Article < ActiveRecord::Base |
2 | 4 | ||
3 | # xss_terminate plugin can't sanitize array fields | 5 | # xss_terminate plugin can't sanitize array fields |
@@ -340,8 +342,7 @@ class Article < ActiveRecord::Base | @@ -340,8 +342,7 @@ class Article < ActiveRecord::Base | ||
340 | end | 342 | end |
341 | 343 | ||
342 | def first_paragraph | 344 | def first_paragraph |
343 | - to_html =~ /<p>(.*)<\/p>/ | ||
344 | - $1 || '' | 345 | + Hpricot(to_html).search('p').first.to_html |
345 | end | 346 | end |
346 | 347 | ||
347 | def creator | 348 | def creator |
test/unit/article_test.rb
@@ -89,8 +89,8 @@ class ArticleTest < Test::Unit::TestCase | @@ -89,8 +89,8 @@ class ArticleTest < Test::Unit::TestCase | ||
89 | should 'provide first paragraph of HTML version' do | 89 | should 'provide first paragraph of HTML version' do |
90 | profile = create_user('testinguser').person | 90 | profile = create_user('testinguser').person |
91 | a = fast_create(Article, :name => 'my article', :profile_id => profile.id) | 91 | a = fast_create(Article, :name => 'my article', :profile_id => profile.id) |
92 | - a.expects(:body).returns('<p>the first paragraph of the article</p> The second paragraph') | ||
93 | - assert_equal 'the first paragraph of the article', a.first_paragraph | 92 | + a.expects(:body).returns('<p>the first paragraph of the article</p><p>The second paragraph</p>') |
93 | + assert_equal '<p>the first paragraph of the article</p>', a.first_paragraph | ||
94 | end | 94 | end |
95 | 95 | ||
96 | should 'inform the icon to be used' do | 96 | should 'inform the icon to be used' do |