Skip to content

Commit

Permalink
Merge pull request #4498 from fecgov/feature/4491-sitewide-search-hotfix
Browse files Browse the repository at this point in the history
[Adding to hotfix branch] Fix filter when results have `{` or `}`
  • Loading branch information
rfultz authored Mar 19, 2021
2 parents 2b37fb2 + 7653f15 commit 743f970
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
13 changes: 10 additions & 3 deletions fec/home/templatetags/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,22 @@ def web_app_url(path):

@register.filter()
def highlight_matches(text):
"""Replaces the highlight markers with span tags for digitalgov search results"""
highlighted_text = text.replace('\ue000', '<span class="t-highlight">').replace('\ue001', '</span>')
"""
Replaces the highlight markers with span tags for digitalgov search results.
Because format_html uses str.format, remove { and } because they are special characters.
"""
cleaned_text = text.replace("{", "").replace("}", "")
highlighted_text = cleaned_text.replace(
"\ue000", '<span class="t-highlight">'
).replace("\ue001", "</span>")

return format_html(highlighted_text)


@register.filter(name='splitlines')
def splitlines(value):
"""
Returns the value turned into a list.
Returns the value turned into a list.
"""
return value.splitlines()

Expand Down
23 changes: 23 additions & 0 deletions fec/home/tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.test import TestCase

from home.templatetags.filters import highlight_matches


class TestFilters(TestCase):

def test_highlight_matches(self):
text = '\ue000Highlighted\ue001 results'
highlighted_text = highlight_matches(text)
self.assertEqual(
highlighted_text,
'<span class="t-highlight">Highlighted</span> results'
)

def test_highlight_matches_with_brackets(self):
"""highlight_matches should remove { and } from results"""
text = '\ue000Highlighted\ue001 {results}'
highlighted_text = highlight_matches(text)
self.assertEqual(
highlighted_text,
'<span class="t-highlight">Highlighted</span> results'
)

0 comments on commit 743f970

Please sign in to comment.