Skip to content

A platform for community discussion. Free, open, simple.

FIX: Consider live links in `<code>` as links when counting

Robin Wardc74d10cf343c
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('<img alt="smiley" title=":smiley:" src="/assets/emoji/smiley.png" class="avatar"> <img alt="wink" title=":wink:" src="/assets/emoji/wink.png" class="avatar">', newuser) }
let(:post_with_favicon) { post_with_body('<img src="/assets/favicons/wikipedia.png" class="favicon">', newuser) }
let(:post_image_within_quote) { post_with_body('[quote]<img src="coolimage.png">[/quote]', newuser) }
+ let(:post_image_within_code) { post_with_body('<code><img src="coolimage.png"></code>', newuser) }
+ let(:post_image_within_pre) { post_with_body('<pre><img src="coolimage.png"></pre>', newuser) }
let(:post_with_thumbnail) { post_with_body('<img src="/assets/emoji/smiley.png" class="thumbnail">', newuser) }
let(:post_with_two_classy_images) { post_with_body("<img src='http://discourse.org/logo.png' class='classy'> <img src='http://bbc.co.uk/sherlock.jpg' class='classy'>", 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("<code>http://www.google.com</code>", newuser) }
let(:post_two_links) { post_with_body("<a href='http://discourse.org'>discourse</a> <a href='http://twitter.com'>twitter</a>", 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]