Commit 0b8b6900ac0a021271b4da6b036ddbbc6e125ea6
1 parent
d0561132
Exists in
master
and in
29 other branches
Set relative path to images and link on TextArticle
Showing
2 changed files
with
66 additions
and
0 deletions
Show diff stats
app/models/text_article.rb
| @@ -20,4 +20,23 @@ class TextArticle < Article | @@ -20,4 +20,23 @@ class TextArticle < Article | ||
| 20 | def can_display_versions? | 20 | def can_display_versions? |
| 21 | true | 21 | true |
| 22 | end | 22 | end |
| 23 | + | ||
| 24 | + before_save :set_relative_path | ||
| 25 | + | ||
| 26 | + def set_relative_path | ||
| 27 | + parsed = Hpricot(self.body.to_s) | ||
| 28 | + parsed.search('img[@src]').map { |i| change_element_path(i, 'src') } | ||
| 29 | + parsed.search('a[@href]').map { |i| change_element_path(i, 'href') } | ||
| 30 | + self.body = parsed.to_s | ||
| 31 | + end | ||
| 32 | + | ||
| 33 | + def change_element_path(el, attribute) | ||
| 34 | + fullpath = /(https?):\/\/(#{environment.default_hostname})(:\d+)?(\/.*)/.match(el[attribute]) | ||
| 35 | + if fullpath | ||
| 36 | + domain = fullpath[2] | ||
| 37 | + path = fullpath[4] | ||
| 38 | + el[attribute] = path if domain == environment.default_hostname | ||
| 39 | + end | ||
| 40 | + end | ||
| 41 | + | ||
| 23 | end | 42 | end |
test/unit/text_article_test.rb
| @@ -37,4 +37,51 @@ class TextArticleTest < ActiveSupport::TestCase | @@ -37,4 +37,51 @@ class TextArticleTest < ActiveSupport::TestCase | ||
| 37 | assert_equal Blog.icon_name, TextArticle.icon_name(article) | 37 | assert_equal Blog.icon_name, TextArticle.icon_name(article) |
| 38 | end | 38 | end |
| 39 | 39 | ||
| 40 | + should 'change image path to relative' do | ||
| 41 | + person = create_user('testuser').person | ||
| 42 | + article = TextArticle.new(:profile => person, :name => 'test') | ||
| 43 | + env = Environment.default | ||
| 44 | + article.body = "<img src=\"http://#{env.default_hostname}/test.png\" />" | ||
| 45 | + article.save! | ||
| 46 | + assert_equal "<img src=\"/test.png\" />", article.body | ||
| 47 | + end | ||
| 48 | + | ||
| 49 | + should 'change link to relative path' do | ||
| 50 | + person = create_user('testuser').person | ||
| 51 | + article = TextArticle.new(:profile => person, :name => 'test') | ||
| 52 | + env = Environment.default | ||
| 53 | + article.body = "<a href=\"http://#{env.default_hostname}/test\">test</a>" | ||
| 54 | + article.save! | ||
| 55 | + assert_equal "<a href=\"/test\">test</a>", article.body | ||
| 56 | + end | ||
| 57 | + | ||
| 58 | + should 'change image path to relative for domain with https' do | ||
| 59 | + person = create_user('testuser').person | ||
| 60 | + article = TextArticle.new(:profile => person, :name => 'test') | ||
| 61 | + env = Environment.default | ||
| 62 | + article.body = "<img src=\"https://#{env.default_hostname}/test.png\" />" | ||
| 63 | + article.save! | ||
| 64 | + assert_equal "<img src=\"/test.png\" />", article.body | ||
| 65 | + end | ||
| 66 | + | ||
| 67 | + should 'change image path to relative for domain with port' do | ||
| 68 | + person = create_user('testuser').person | ||
| 69 | + article = TextArticle.new(:profile => person, :name => 'test') | ||
| 70 | + env = Environment.default | ||
| 71 | + article.body = "<img src=\"http://#{env.default_hostname}:3000/test.png\" />" | ||
| 72 | + article.save! | ||
| 73 | + assert_equal "<img src=\"/test.png\" />", article.body | ||
| 74 | + end | ||
| 75 | + | ||
| 76 | + should 'change image path to relative for domain with www' do | ||
| 77 | + person = create_user('testuser').person | ||
| 78 | + article = TextArticle.new(:profile => person, :name => 'test') | ||
| 79 | + env = Environment.default | ||
| 80 | + env.force_www = true | ||
| 81 | + env.save! | ||
| 82 | + article.body = "<img src=\"http://#{env.default_hostname}:3000/test.png\" />" | ||
| 83 | + article.save! | ||
| 84 | + assert_equal "<img src=\"/test.png\" />", article.body | ||
| 85 | + end | ||
| 86 | + | ||
| 40 | end | 87 | end |