Skip to content

Commit 9459832

Browse files
committed
Limit to mention and lightbox anchor tags
1 parent ec73f54 commit 9459832

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

app/services/discourse_translator/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def self.language_supported?(detected_lang)
7575
def self.strip_tags_for_detection(detection_text)
7676
html_doc = Nokogiri::HTML::DocumentFragment.parse(detection_text)
7777
html_doc.css("img").remove
78-
html_doc.css("a").remove
78+
html_doc.css("a.mention").remove
79+
html_doc.css("a.lightbox").remove
7980
html_doc.to_html
8081
end
8182

spec/services/base_spec.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,27 @@ class EmptyTranslator < DiscourseTranslator::Base
4141
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
4242
end
4343

44-
it "strips anchor tags" do
45-
post.cooked = "<a href='http://cat.com/image.png' />"
44+
it "strips @ mention anchor tags" do
45+
post.cooked = "<a class='mention' href='/u/cat' >cat</a>"
4646
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
4747
end
4848

49+
it "strips lightbox anchor tags" do
50+
post.cooked = "<a class='lightbox' href='http://cloudfront.net/image.png' />"
51+
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
52+
end
53+
54+
it "leaves other anchor tags alone" do
55+
cooked = <<~HTML
56+
<p>
57+
<a href="http://cat.com/image.png"></a>
58+
<a class="derp" href="http://cat.com/image.png"></a>
59+
</p>
60+
HTML
61+
post.cooked = cooked
62+
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq(cooked)
63+
end
64+
4965
it "truncates to DETECTION_CHAR_LIMIT of 1000" do
5066
post.cooked = "a" * 1001
5167
expect(DiscourseTranslator::Base.text_for_detection(post).length).to eq(1000)

0 commit comments

Comments
 (0)