From 23d69941ec2d295ed2a677e3d4b6a4909b3907f0 Mon Sep 17 00:00:00 2001 From: Joenio Costa Date: Tue, 24 May 2011 11:08:42 -0300 Subject: [PATCH] Escape URLs to avoid error when try to find image sources from article --- app/models/article.rb | 2 +- test/unit/article_test.rb | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/models/article.rb b/app/models/article.rb index 56e39cc..8106e9b 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -512,7 +512,7 @@ class Article < ActiveRecord::Base def body_images_paths require 'uri' Hpricot(self.body.to_s).search('img[@src]').collect do |i| - (self.profile && self.profile.environment) ? URI.join(self.profile.environment.top_url, i.attributes['src']).to_s : i.attributes['src'] + (self.profile && self.profile.environment) ? URI.join(self.profile.environment.top_url, URI.escape(i.attributes['src'])).to_s : i.attributes['src'] end end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index bb31deb..a4cbf20 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -1555,4 +1555,11 @@ class ArticleTest < Test::Unit::TestCase assert_equal [], a.body_images_paths end + should 'survive to a invalid src attribute while looking for images in body' do + article = Article.new(:body => "An article with invalid src in img tag ", :profile => @profile) + assert_nothing_raised URI::InvalidURIError do + assert_equal ['http://localhost/path%20with%20spaces.png'], article.body_images_paths + end + end + end -- libgit2 0.21.2