Skip to content

Commit ec63ed4

Browse files
committed
Better escape html
1 parent 681ee18 commit ec63ed4

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

sphinx/themes/basic/static/searchtools.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ const _removeChildren = (element) => {
5858
const _escapeRegExp = (string) =>
5959
string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
6060

61+
const _escapeHTML = (text) => {
62+
return text
63+
.replaceAll("&", "&")
64+
.replaceAll("<", "&lt;")
65+
.replaceAll(">", "&gt;")
66+
.replaceAll('"', "&quot;")
67+
.replaceAll("'", "&#39;");
68+
}
69+
6170
const _displayItem = (item, searchTerms, highlightTerms) => {
6271
const docBuilder = DOCUMENTATION_OPTIONS.BUILDER;
6372
const docFileSuffix = DOCUMENTATION_OPTIONS.FILE_SUFFIX;
@@ -324,14 +333,6 @@ const Search = {
324333
const titles = Search._index.titles;
325334
const allTitles = Search._index.alltitles;
326335
const indexEntries = Search._index.indexentries;
327-
const htmlEscape = (text) => {
328-
return String(text)
329-
.replaceAll("&", "&amp;")
330-
.replaceAll("<", "&lt;")
331-
.replaceAll(">", "&gt;")
332-
.replaceAll('"', "&quot;")
333-
.replaceAll("'", "&#39;");
334-
}
335336

336337
// Collect multiple result groups to be sorted separately and then ordered.
337338
// Each is an array of [docname, title, anchor, descr, score, filename, kind].
@@ -348,7 +349,7 @@ const Search = {
348349
const boost = titles[file] === title ? 1 : 0; // add a boost for document titles
349350
normalResults.push([
350351
docNames[file],
351-
htmlEscape(
352+
_escapeHTML(
352353
titles[file] !== title ? `${titles[file]} > ${title}` : title
353354
),
354355
id !== null ? "#" + id : "",
@@ -368,7 +369,7 @@ const Search = {
368369
const score = Math.round(100 * queryLower.length / entry.length);
369370
const result = [
370371
docNames[file],
371-
htmlEscape(titles[file]),
372+
_escapeHTML(titles[file]),
372373
id ? "#" + id : "",
373374
null,
374375
score,

0 commit comments

Comments
 (0)