@@ -6,6 +6,38 @@ const FETCH_RESULTS_DELAY = 250;
66const CLEAR_RESULTS_DELAY = 300 ;
77const RTD_SEARCH_PARAMETER = "rtd_search" ;
88
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+
941/**
1042 * Debounce the function.
1143 * Usage::
@@ -68,14 +100,14 @@ const debounce = (func, wait) => {
68100 */
69101const buildSection = function ( id , title , link , contents ) {
70102 let span_element = createDomNode ( "span" , { class : "search__result__subheading" } ) ;
71- span_element . innerHTML = title ;
103+ setElementContent ( span_element , title )
72104
73105 let div_element = createDomNode ( "div" , { class : "outer_div_page_results" , id : id } ) ;
74106 div_element . appendChild ( span_element ) ;
75107
76108 for ( var i = 0 ; i < contents . length ; i += 1 ) {
77109 let p_element = createDomNode ( "p" , { class : "search__result__content" } ) ;
78- p_element . innerHTML = contents [ i ] ;
110+ setElementContent ( p_element , contents [ i ] ) ;
79111 div_element . appendChild ( p_element ) ;
80112 }
81113
@@ -168,7 +200,7 @@ const get_section_html = (sectionData, page_link, id) => {
168200 let section_subheading = sectionData . title ;
169201 let highlights = sectionData . highlights ;
170202 if ( highlights . title . length ) {
171- section_subheading = highlights . title [ 0 ] ;
203+ section_subheading = markAsSafe ( highlights . title [ 0 ] ) ;
172204 }
173205
174206 let section_content = [
@@ -183,7 +215,7 @@ const get_section_html = (sectionData, page_link, id) => {
183215 j < highlight_content . length && j < MAX_SECTION_RESULTS ;
184216 ++ j
185217 ) {
186- section_content . push ( "... " + highlight_content [ j ] + " ..." ) ;
218+ section_content . push ( markAsSafe ( "... " + highlight_content [ j ] + " ..." ) ) ;
187219 }
188220 }
189221
@@ -192,43 +224,6 @@ const get_section_html = (sectionData, page_link, id) => {
192224 return buildSection ( section_id , section_subheading , section_link , section_content ) ;
193225} ;
194226
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-
232227
233228/**
234229 * Generate search results for a single page.
@@ -265,11 +260,11 @@ const generateSingleResult = (resultData, projectName, id) => {
265260 let highlights = resultData . highlights ;
266261
267262 if ( highlights . title . length ) {
268- page_title = highlights . title [ 0 ] ;
263+ page_title = markAsSafe ( highlights . title [ 0 ] ) ;
269264 }
270265
271266 let h2_element = createDomNode ( "h2" , { class : "search__result__title" } ) ;
272- h2_element . innerHTML = page_title ;
267+ setElementContent ( h2_element , page_title ) ;
273268
274269 // Results can belong to different projects.
275270 // If the result isn't from the current project, add a note about it.
@@ -301,12 +296,6 @@ const generateSingleResult = (resultData, projectName, id) => {
301296 page_link ,
302297 id ,
303298 ) ;
304- } else if ( block . type === "domain" ) {
305- section = get_domain_html (
306- block ,
307- page_link ,
308- id ,
309- ) ;
310299 }
311300
312301 if ( section !== null ) {
@@ -479,7 +468,7 @@ const getErrorDiv = err_msg => {
479468 let err_div = createDomNode ( "div" , {
480469 class : "search__result__box search__error__box"
481470 } ) ;
482- err_div . innerHTML = err_msg ;
471+ err_div . innerText = err_msg ;
483472 return err_div ;
484473} ;
485474
0 commit comments