diff options
author | Göktürk Yüksek <gokturk@gentoo.org> | 2019-12-10 20:23:07 -0500 |
---|---|---|
committer | Göktürk Yüksek <gokturk@gentoo.org> | 2019-12-19 15:58:11 -0500 |
commit | 690ef9d882b7cb66e7cc93409cf91175f4cc45e1 (patch) | |
tree | 786a82a2a5cf20b7f655a96af2d0fdfda5c6a573 /search.js | |
parent | search.js: highlight the search terms in results (diff) | |
download | devmanual-690ef9d882b7cb66e7cc93409cf91175f4cc45e1.tar.gz devmanual-690ef9d882b7cb66e7cc93409cf91175f4cc45e1.tar.bz2 devmanual-690ef9d882b7cb66e7cc93409cf91175f4cc45e1.zip |
search.js: escape HTML/XML tags returned in search results
build_search_documents.py unescapes the escaped tags when creating an
index. This is desired as lunr doesn't index them otherwise. For
example it indexes '<warning>' properly but not
'<warning>'. When we display them on the browser though, we need
to escape them again so that they are not interpreted as real tags.
Signed-off-by: Göktürk Yüksek <gokturk@gentoo.org>
Diffstat (limited to 'search.js')
-rw-r--r-- | search.js | 21 |
1 files changed, 18 insertions, 3 deletions
@@ -33,6 +33,21 @@ function getContents(docs, uid) { return contents; } +function escapeHTML(str) { + return str.replace(/[&<"']/g, function(m) { + switch (m) { + case '&': + return '&'; + case '<': + return '<'; + case '"': + return '"'; + default: + return '''; + } + }); +}; + function search() { var term = document.getElementById("searchInput").value; if (term !== "") { @@ -57,14 +72,14 @@ function search() { }); for (var i = 0; i < positions.length; i++) { - text += contents.text.substring(pos, positions[i][0]); + text += escapeHTML(contents.text.substring(pos, positions[i][0])); pos = positions[i][0]; text += "<span style='background-color: yellow;'>"; - text += contents.text.substring(pos, pos + positions[i][1]); + text += escapeHTML(contents.text.substring(pos, pos + positions[i][1])); pos += positions[i][1]; text += "</span>"; } - text += contents.text.substring(pos); + text += escapeHTML(contents.text.substring(pos)); $("#searchResults .modal-body").append(`<article><h5><a href="${contents.url}"> ${contents.name}</a></h5><p>${text}</p></article>`); |