Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#56694] include token index class, specs

Tobias Dillmann782aad2dcc38
diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb
index 5258c498e0c..a8b4a262704 100644
--- a/app/helpers/search_helper.rb
+++ b/app/helpers/search_helper.rb
@@ -76,7 +76,9 @@ module SearchHelper
doc.traverse do |node|
next unless node.text?
- highlighted_text = node.content.gsub(/(#{Regexp.escape(token)})/i, '<span class="search-highlight">\1</span>')
+ t = (tokens.index(node.content.downcase) || 0) % 4
+ highlighted_text = node.content.gsub(/(#{Regexp.escape(token)})/i,
+ "<span class='search-highlight token-#{t}'>\\1</span>")
node.replace(Nokogiri::HTML::DocumentFragment.parse(highlighted_text))
end
end
diff --git a/spec/helpers/search_helper_spec.rb b/spec/helpers/search_helper_spec.rb
index bdefdedd6b7..5613f9779dd 100644
--- a/spec/helpers/search_helper_spec.rb
+++ b/spec/helpers/search_helper_spec.rb
@@ -130,9 +130,9 @@ RSpec.describe "search/index" do
context "with the token in the description" do
let(:tokens) { %w(description) }
- it "shows the text in the description" do
+ it "shows the formatted text in the description" do
expect(helper.highlight_tokens_in_event(event, tokens))
- .to eql 'The <span class="search-highlight token-0">description</span> of the event'
+ .to eql '<p class="op-uc-p">The <span class="search-highlight token-0">description</span> of the event</p>'
end
end
@@ -140,9 +140,28 @@ RSpec.describe "search/index" do
let(:tokens) { %w(description) }
let(:journal_notes) { "" }
- it "shows the text in the description" do
+ it "shows the formatted text in the description" do
expect(helper.highlight_tokens_in_event(event, tokens))
- .to eql 'The <span class="search-highlight token-0">description</span> of the event'
+ .to eql '<p class="op-uc-p">The <span class="search-highlight token-0">description</span> of the event</p>'
+ end
+
+ context "with elaborate markdown formatting in the description" do
+ let(:event_description) do
+ <<~MARKDOWN
+ This is a paragraph with **bold** and *italic* text #{'hello ' * 100}.
+ MARKDOWN
+ end
+ let(:tokens) { %w(bold) }
+
+ it "formats it to abbreviated HTML" do
+ expectation = <<~HTML.squish
+ <p class="op-uc-p">This is a paragraph with
+ <strong><span class="search-highlight token-0">bold</span></strong>
+ and <em>italic</em> text #{'hello ' * 15} hello...</p>
+ HTML
+
+ expect(helper.highlight_tokens_in_event(event, tokens)).to eql(expectation)
+ end
end
end
@@ -179,9 +198,9 @@ RSpec.describe "search/index" do
context "with the token in neither" do
let(:tokens) { %w(bogus) }
- it "shows the description (without highlight)" do
+ it "shows the formatted description (without highlight)" do
expect(helper.highlight_tokens_in_event(event, tokens))
- .to eql "The description of the event"
+ .to eql '<p class="op-uc-p">The description of the event</p>'
end
end
end