diff --git a/app/models/post.rb b/app/models/post.rb index 496224570..b97aa40af 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -115,10 +115,21 @@ class Post < ActiveRecord::Base self.cooked = nil end + def self.white_listed_image_classes + @white_listed_image_classes ||= ['avatar'] + end + def image_count return 0 unless self.raw.present? - cooked_document.search("img.emoji").remove - cooked_document.search("img").count + + cooked_document.search("img").reject{ |t| + dom_class = t["class"] + if dom_class + (Post.white_listed_image_classes & dom_class.split(" ")).count > 0 + else + false + end + }.count end def link_count diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 88c943ec0..146a51599 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -86,7 +86,8 @@ describe Post do let(:post_no_images) { Fabricate.build(:post, post_args) } let(:post_one_image) { Fabricate.build(:post, post_args.merge(raw: "![sherlock](http://bbc.co.uk/sherlock.jpg)")) } let(:post_two_images) { Fabricate.build(:post, post_args.merge(raw: " ")) } - let(:post_with_emoticons) { Fabricate.build(:post, post_args.merge(raw: 'smiley wink')) } + let(:post_with_avatars) { Fabricate.build(:post, post_args.merge(raw: 'smiley wink')) } + let(:post_with_two_classy_images) { Fabricate.build(:post, post_args.merge(raw: " ")) } it "returns 0 images for an empty post" do Fabricate.build(:post).image_count.should == 0 @@ -100,10 +101,14 @@ describe Post do post_two_images.image_count.should == 2 end - it "doesn't count emoticons as images" do - post_with_emoticons.image_count.should == 0 + it "doesn't count avatars as images" do + post_with_avatars.image_count.should == 0 end + it "doesn't count whitelisted images" do + Post.stubs(:white_listed_image_classes).returns(["classy"]) + post_with_two_classy_images.image_count.should == 0 + end context "validation" do it "allows a new user to make a post with one image" do diff --git a/vendor/gems/discourse_emoji/lib/discourse_emoji/engine.rb b/vendor/gems/discourse_emoji/lib/discourse_emoji/engine.rb index 3944406a3..5caafd703 100644 --- a/vendor/gems/discourse_emoji/lib/discourse_emoji/engine.rb +++ b/vendor/gems/discourse_emoji/lib/discourse_emoji/engine.rb @@ -9,8 +9,9 @@ module DiscourseEmoji app.config.after_initialize do DiscoursePluginRegistry.setup(DiscourseEmoji::Plugin) + Post.white_listed_image_classes << "emoji" end end end -end \ No newline at end of file +end