From c74d10cf343c4641a2a0474c340f4add0f3f4886 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 6 Mar 2018 11:46:12 -0500 Subject: [PATCH] FIX: Consider live links in `` as links when counting --- app/models/post_analyzer.rb | 2 +- spec/models/post_spec.rb | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rb index 57e67c5efae..ab8ec1903d2 100644 --- a/app/models/post_analyzer.rb +++ b/app/models/post_analyzer.rb @@ -130,7 +130,7 @@ class PostAnalyzer def cooked_stripped @cooked_stripped ||= begin doc = Nokogiri::HTML.fragment(cook(@raw, topic_id: @topic_id)) - doc.css("pre, code, aside.quote > .title, aside.quote .mention, .onebox, .elided").remove + doc.css("pre .mention, aside.quote > .title, aside.quote .mention, .onebox, .elided").remove doc end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index b5571fb370e..f706ea5003d 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -170,6 +170,8 @@ describe Post do let(:post_with_avatars) { post_with_body('smiley wink', newuser) } let(:post_with_favicon) { post_with_body('', newuser) } let(:post_image_within_quote) { post_with_body('[quote][/quote]', newuser) } + let(:post_image_within_code) { post_with_body('', newuser) } + let(:post_image_within_pre) { post_with_body('
', newuser) } let(:post_with_thumbnail) { post_with_body('', newuser) } let(:post_with_two_classy_images) { post_with_body(" ", newuser) } @@ -199,12 +201,24 @@ describe Post do expect(post_one_image).not_to be_valid end - it "doesn't allow more than `min_trust_to_post_images`" do + it "doesn't allow more than `min_trust_to_post_images` in a quote" do SiteSetting.min_trust_to_post_images = 4 post_one_image.user.trust_level = 3 expect(post_image_within_quote).not_to be_valid end + it "doesn't allow more than `min_trust_to_post_images` in code" do + SiteSetting.min_trust_to_post_images = 4 + post_one_image.user.trust_level = 3 + expect(post_image_within_code).not_to be_valid + end + + it "doesn't allow more than `min_trust_to_post_images` in pre" do + SiteSetting.min_trust_to_post_images = 4 + post_one_image.user.trust_level = 3 + expect(post_image_within_pre).not_to be_valid + end + it "doesn't allow more than `min_trust_to_post_images`" do SiteSetting.min_trust_to_post_images = 4 post_one_image.user.trust_level = 4 @@ -379,6 +393,7 @@ describe Post do let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) } let(:post_one_link) { post_with_body("[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", newuser) } let(:post_onebox) { post_with_body("http://www.google.com", newuser) } + let(:post_code_link) { post_with_body("http://www.google.com", newuser) } let(:post_two_links) { post_with_body("discourse twitter", newuser) } let(:post_with_mentions) { post_with_body("hello @#{newuser.username} how are you doing?", newuser) } @@ -427,6 +442,12 @@ describe Post do expect(post_onebox).not_to be_valid end + it "considers links within code" do + SiteSetting.min_trust_to_post_links = 3 + post_onebox.user.trust_level = TrustLevel[2] + expect(post_code_link).not_to be_valid + end + it "doesn't allow allow links if `min_trust_to_post_links` is not met" do SiteSetting.min_trust_to_post_links = 2 post_two_links.user.trust_level = TrustLevel[1]