Skip to content

Commit b3c5e35

Browse files
authored
Merge pull request #5994 from dojutsu-user/fix-highlighting-prob
Fix: no highlighting of matched keywords in search results
2 parents 6935ddf + ebdcf99 commit b3c5e35

File tree

7 files changed

+26
-20
lines changed

7 files changed

+26
-20
lines changed

readthedocs/core/static-src/core/js/doc-embed/search.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function attach_elastic_search_query(data) {
5454

5555
var item = $('<a>', {'href': link});
5656
item.html(title);
57-
item.find('em').addClass('highlighted');
57+
item.find('span').addClass('highlighted');
5858
list_item.append(item);
5959

6060
// If the document is from subproject, add extra information
@@ -99,9 +99,9 @@ function attach_elastic_search_query(data) {
9999
'<%= domain_subtitle %>' +
100100
'</a>' +
101101
'</div>' +
102-
'<span>' +
102+
'<div>' +
103103
'<%= domain_content %>' +
104-
'</span>';
104+
'</div>';
105105

106106
// if the result is page section
107107
if(inner_hits[j].type === "sections") {
@@ -179,7 +179,7 @@ function attach_elastic_search_query(data) {
179179
);
180180
}
181181

182-
contents.find('em').addClass('highlighted');
182+
contents.find('span').addClass('highlighted');
183183
list_item.append(contents);
184184

185185
// Create some spacing between the results.

readthedocs/core/static/core/js/readthedocs-doc-embed.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readthedocs/projects/static/projects/js/tools.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readthedocs/search/faceted_search.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class PageSearchBase(RTDFacetedSearch):
108108
'domains.name^2',
109109
'domains.display_name',
110110
]
111+
_common_highlight_options = {
112+
'encoder': 'html',
113+
'number_of_fragments': 1,
114+
'pre_tags': ['<span>'],
115+
'post_tags': ['</span>'],
116+
}
111117
fields = _outer_fields
112118

113119
# need to search for both 'and' and 'or' operations
@@ -126,7 +132,7 @@ def count(self):
126132

127133
def query(self, search, query):
128134
"""Manipulates query to support nested query."""
129-
search = search.highlight_options(encoder='html', number_of_fragments=1)
135+
search = search.highlight_options(**self._common_highlight_options)
130136

131137
# match query for the title (of the page) field.
132138
match_title_query = Match(title=query)
@@ -137,14 +143,13 @@ def query(self, search, query):
137143
path='sections',
138144
fields=self._section_fields,
139145
inner_hits={
140-
'highlight': {
141-
'encoder': 'html',
142-
'number_of_fragments': 1,
143-
'fields': {
146+
'highlight': dict(
147+
self._common_highlight_options,
148+
fields={
144149
'sections.title': {},
145150
'sections.content': {},
146151
}
147-
}
152+
)
148153
}
149154
)
150155

@@ -154,15 +159,14 @@ def query(self, search, query):
154159
path='domains',
155160
fields=self._domain_fields,
156161
inner_hits={
157-
'highlight': {
158-
'encoder': 'html',
159-
'number_of_fragments': 1,
160-
'fields': {
162+
'highlight': dict(
163+
self._common_highlight_options,
164+
fields={
161165
'domains.type_display': {},
162166
'domains.name': {},
163167
'domains.display_name': {},
164168
}
165-
}
169+
)
166170
}
167171
)
168172

readthedocs/search/tests/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def test_search_works_with_sections_and_domains_query(
102102

103103
# checking highlighting of results
104104
highlighted_words = re.findall( # this gets all words inside <em> tag
105-
'<em>(.*?)</em>',
105+
'<span>(.*?)</span>',
106106
highlight[0]
107107
)
108108
assert len(highlighted_words) > 0

readthedocs/search/tests/test_views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def _get_highlight(self, result, data_type):
120120

121121
def _get_highlighted_words(self, string):
122122
highlighted_words = re.findall(
123-
'<em>(.*?)</em>',
123+
'<span>(.*?)</span>',
124124
string
125125
)
126126
return highlighted_words
@@ -238,6 +238,7 @@ def test_file_search_case_insensitive(self, client, project, case, data_type):
238238
highlight = self._get_highlight(first_result, data_type)
239239
assert len(highlight) == 1
240240
highlighted_words = self._get_highlighted_words(highlight[0])
241+
assert len(highlighted_words) >= 1
241242
for word in highlighted_words:
242243
assert word.lower() in query.lower()
243244

@@ -271,6 +272,7 @@ def test_file_search_exact_match(self, client, project):
271272
highlight = self._get_highlight(results[0], 'sections.content')
272273
assert len(highlight) == 1
273274
highlighted_words = self._get_highlighted_words(highlight[0])
275+
assert len(highlighted_words) >= 1
274276
for word in highlighted_words:
275277
assert word.lower() in query.lower()
276278

readthedocs/search/tests/test_xss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def test_facted_page_xss(self, client, project):
1212
page_search = PageDocument.faceted_search(query=query, user='')
1313
results = page_search.execute()
1414
expected = """
15-
&lt;h3&gt;<em>XSS</em> exploit&lt;&#x2F;h3&gt;
15+
&lt;h3&gt;<span>XSS</span> exploit&lt;&#x2F;h3&gt;
1616
""".strip()
1717

1818
hits = results.hits.hits

0 commit comments

Comments
 (0)