Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/services/discourse_translator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ def self.language_supported?(detected_lang)

private

def self.strip_img_for_detection(detection_text)
def self.strip_tags_for_detection(detection_text)
html_doc = Nokogiri::HTML::DocumentFragment.parse(detection_text)
html_doc.css("img").remove
html_doc.css("a.mention,a.lightbox").remove
html_doc.to_html
end

def self.text_for_detection(topic_or_post)
strip_img_for_detection(get_text(topic_or_post).truncate(DETECTION_CHAR_LIMIT, omission: nil))
strip_tags_for_detection(
get_text(topic_or_post).truncate(DETECTION_CHAR_LIMIT, omission: nil),
)
end

def self.text_for_translation(topic_or_post)
Expand Down
21 changes: 21 additions & 0 deletions spec/services/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ class EmptyTranslator < DiscourseTranslator::Base
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
end

it "strips @ mention anchor tags" do
post.cooked = "<a class='mention' href='/u/cat' >cat</a>"
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
end

it "strips lightbox anchor tags" do
post.cooked = "<a class='lightbox' href='http://cloudfront.net/image.png' />"
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq("")
end

it "leaves other anchor tags alone" do
cooked = <<~HTML
<p>
<a href="http://cat.com/image.png"></a>
<a class="derp" href="http://cat.com/image.png"></a>
</p>
HTML
post.cooked = cooked
expect(DiscourseTranslator::Base.text_for_detection(post)).to eq(cooked)
end

it "truncates to DETECTION_CHAR_LIMIT of 1000" do
post.cooked = "a" * 1001
expect(DiscourseTranslator::Base.text_for_detection(post).length).to eq(1000)
Expand Down
Loading