FIX: Remove full nested quotes on direct reply (#8581)

It used to check how many quotes were inside a post, without taking
considering that some quotes can contain other quotes. This commit
selects only top level quotes.

I had to use XPath because I could not find an equivalent CSS
selector.
This commit is contained in:
Bianca Nenciu 2019-12-20 10:24:34 +02:00 committed by GitHub
parent d449834a28
commit 1bccd8eca9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 2 deletions

View File

@ -83,7 +83,7 @@ class CookedPostProcessor
def remove_full_quote_on_direct_reply def remove_full_quote_on_direct_reply
return if !SiteSetting.remove_full_quote return if !SiteSetting.remove_full_quote
return if @post.post_number == 1 return if @post.post_number == 1
return if @doc.css("aside.quote").size != 1 return if @doc.xpath("aside[contains(@class, 'quote')]").size != 1
previous = Post previous = Post
.where("post_number < ? AND topic_id = ? AND post_type = ? AND NOT hidden", @post.post_number, @post.topic_id, Post.types[:regular]) .where("post_number < ? AND topic_id = ? AND post_type = ? AND NOT hidden", @post.post_number, @post.topic_id, Post.types[:regular])
@ -99,7 +99,7 @@ class CookedPostProcessor
return if previous_text.gsub(/(\s){2,}/, '\1') != quoted_text.gsub(/(\s){2,}/, '\1') return if previous_text.gsub(/(\s){2,}/, '\1') != quoted_text.gsub(/(\s){2,}/, '\1')
quote_regexp = /\A\s*\[quote.+?\[\/quote\]/im quote_regexp = /\A\s*\[quote.+\[\/quote\]/im
quoteless_raw = @post.raw.sub(quote_regexp, "").strip quoteless_raw = @post.raw.sub(quote_regexp, "").strip
return if @post.raw.strip == quoteless_raw return if @post.raw.strip == quoteless_raw

View File

@ -1662,6 +1662,24 @@ describe CookedPostProcessor do
RAW RAW
end end
let(:raw3) do
<<~RAW.strip
[quote="#{post.user.username}, post:#{post.post_number}, topic:#{topic.id}"]
this is the first post
[/quote]
[quote="#{post.user.username}, post:#{post.post_number}, topic:#{topic.id}"]
this is the first post
[/quote]
and this is the third reply
RAW
end
before do before do
SiteSetting.remove_full_quote = true SiteSetting.remove_full_quote = true
end end
@ -1686,6 +1704,13 @@ describe CookedPostProcessor do
end end
end end
it 'does nothing if there are multiple quotes' do
reply = Fabricate(:post, topic: topic, raw: raw3)
CookedPostProcessor.new(reply).remove_full_quote_on_direct_reply
expect(topic.ordered_posts.pluck(:id)).to eq([post.id, reply.id])
expect(reply.raw).to eq(raw3)
end
it 'does not delete quote if not first paragraph' do it 'does not delete quote if not first paragraph' do
reply = Fabricate(:post, topic: topic, raw: raw2) reply = Fabricate(:post, topic: topic, raw: raw2)
CookedPostProcessor.new(reply).remove_full_quote_on_direct_reply CookedPostProcessor.new(reply).remove_full_quote_on_direct_reply
@ -1715,6 +1740,19 @@ describe CookedPostProcessor do
expect(reply.raw).to eq("and this is the third reply") expect(reply.raw).to eq("and this is the third reply")
end end
it "works with nested quotes" do
reply1 = Fabricate(:post, topic: topic, raw: raw)
reply2 = Fabricate(:post, topic: topic, raw: <<~RAW.strip)
[quote="#{reply1.user.username}, post:#{reply1.post_number}, topic:#{topic.id}"]
#{raw}
[/quote]
quoting a post with a quote
RAW
CookedPostProcessor.new(reply2).remove_full_quote_on_direct_reply
expect(reply2.raw).to eq('quoting a post with a quote')
end
end end
end end