@@ -6,6 +6,38 @@ const FETCH_RESULTS_DELAY = 250;
6
6
const CLEAR_RESULTS_DELAY = 300 ;
7
7
const RTD_SEARCH_PARAMETER = "rtd_search" ;
8
8
9
+
10
+ /**
11
+ * Mark a string as safe to be used as HTML in setNodeContent.
12
+ */
13
+ function SafeHtmlString ( value ) {
14
+ this . value = value ;
15
+ this . isSafe = true ;
16
+ }
17
+
18
+ /**
19
+ * Create a SafeHtmlString instance from a string.
20
+ *
21
+ * @param {String } value
22
+ */
23
+ function markAsSafe ( value ) {
24
+ return new SafeHtmlString ( value ) ;
25
+ }
26
+
27
+ /**
28
+ * Set the content of an element as text or HTML.
29
+ *
30
+ * @param {Element } element
31
+ * @param {String|SafeHtmlString } content
32
+ */
33
+ function setElementContent ( element , content ) {
34
+ if ( content . isSafe ) {
35
+ element . innerHTML = content . value ;
36
+ } else {
37
+ element . innerText = content ;
38
+ }
39
+ }
40
+
9
41
/**
10
42
* Debounce the function.
11
43
* Usage::
@@ -68,14 +100,14 @@ const debounce = (func, wait) => {
68
100
*/
69
101
const buildSection = function ( id , title , link , contents ) {
70
102
let span_element = createDomNode ( "span" , { class : "search__result__subheading" } ) ;
71
- span_element . innerHTML = title ;
103
+ setElementContent ( span_element , title )
72
104
73
105
let div_element = createDomNode ( "div" , { class : "outer_div_page_results" , id : id } ) ;
74
106
div_element . appendChild ( span_element ) ;
75
107
76
108
for ( var i = 0 ; i < contents . length ; i += 1 ) {
77
109
let p_element = createDomNode ( "p" , { class : "search__result__content" } ) ;
78
- p_element . innerHTML = contents [ i ] ;
110
+ setElementContent ( p_element , contents [ i ] ) ;
79
111
div_element . appendChild ( p_element ) ;
80
112
}
81
113
@@ -168,7 +200,7 @@ const get_section_html = (sectionData, page_link, id) => {
168
200
let section_subheading = sectionData . title ;
169
201
let highlights = sectionData . highlights ;
170
202
if ( highlights . title . length ) {
171
- section_subheading = highlights . title [ 0 ] ;
203
+ section_subheading = markAsSafe ( highlights . title [ 0 ] ) ;
172
204
}
173
205
174
206
let section_content = [
@@ -183,7 +215,7 @@ const get_section_html = (sectionData, page_link, id) => {
183
215
j < highlight_content . length && j < MAX_SECTION_RESULTS ;
184
216
++ j
185
217
) {
186
- section_content . push ( "... " + highlight_content [ j ] + " ..." ) ;
218
+ section_content . push ( markAsSafe ( "... " + highlight_content [ j ] + " ..." ) ) ;
187
219
}
188
220
}
189
221
@@ -192,43 +224,6 @@ const get_section_html = (sectionData, page_link, id) => {
192
224
return buildSection ( section_id , section_subheading , section_link , section_content ) ;
193
225
} ;
194
226
195
- /**
196
- * Generate and return html structure
197
- * for a sphinx domain result.
198
- *
199
- * @param {Object } domainData object containing the result data
200
- * @param {String } page_link link of the main page. It is used to construct the section link
201
- * @param {Number } id to be used in for this section
202
- */
203
- const get_domain_html = ( domainData , page_link , id ) => {
204
- let domain_link = `${ page_link } #${ domainData . id } ` ;
205
- let domain_role_name = domainData . role ;
206
- let domain_name = domainData . name ;
207
- let domain_content =
208
- domainData . content . substr ( 0 , MAX_SUBSTRING_LIMIT ) + " ..." ;
209
-
210
- let highlights = domainData . highlights ;
211
- if ( highlights . name . length ) {
212
- domain_name = highlights . name [ 0 ] ;
213
- }
214
- if ( highlights . content . length ) {
215
- domain_content = highlights . content [ 0 ] ;
216
- }
217
-
218
- let domain_id = "hit__" + id ;
219
-
220
- let div_role_name = createDomNode ( "div" , { class : "search__domain_role_name" } ) ;
221
- div_role_name . innerText = `[${ domain_role_name } ]` ;
222
- domain_name += div_role_name . outerHTML ;
223
-
224
- return buildSection (
225
- domain_id ,
226
- domain_name ,
227
- domain_link ,
228
- [ domain_content ]
229
- ) ;
230
- } ;
231
-
232
227
233
228
/**
234
229
* Generate search results for a single page.
@@ -265,11 +260,11 @@ const generateSingleResult = (resultData, projectName, id) => {
265
260
let highlights = resultData . highlights ;
266
261
267
262
if ( highlights . title . length ) {
268
- page_title = highlights . title [ 0 ] ;
263
+ page_title = markAsSafe ( highlights . title [ 0 ] ) ;
269
264
}
270
265
271
266
let h2_element = createDomNode ( "h2" , { class : "search__result__title" } ) ;
272
- h2_element . innerHTML = page_title ;
267
+ setElementContent ( h2_element , page_title ) ;
273
268
274
269
// Results can belong to different projects.
275
270
// If the result isn't from the current project, add a note about it.
@@ -301,12 +296,6 @@ const generateSingleResult = (resultData, projectName, id) => {
301
296
page_link ,
302
297
id ,
303
298
) ;
304
- } else if ( block . type === "domain" ) {
305
- section = get_domain_html (
306
- block ,
307
- page_link ,
308
- id ,
309
- ) ;
310
299
}
311
300
312
301
if ( section !== null ) {
@@ -479,7 +468,7 @@ const getErrorDiv = err_msg => {
479
468
let err_div = createDomNode ( "div" , {
480
469
class : "search__result__box search__error__box"
481
470
} ) ;
482
- err_div . innerHTML = err_msg ;
471
+ err_div . innerText = err_msg ;
483
472
return err_div ;
484
473
} ;
485
474
0 commit comments