Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape HTML characters in the indicator search variable tooltip. #1527

Merged
merged 6 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ New features and enhancements
Bug fixes
^^^^^^^^^
* Fixed a bug with ``n_escore=-1`` in ``xclim.sdba.adjustment.NpdfTransform`` (:issue:`1515`, :pull:`1515`).
* In the documentaion, fix the tooltips in the indicator search results (:issue:`1524`, :pull:`1527`).

Internal changes
^^^^^^^^^^^^^^^^
Expand Down
30 changes: 18 additions & 12 deletions docs/_static/indsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ fetch('indicators.json')
});


// Populate list of variables
//function getVariables() {
// fetch('variables.json')
// .then((res) => {
// return res.json();
// })
//}
//const variables = getVariables();

function escapeHTML(str){
/* Escape HTML characters in a string. */
var map =
{
'&': '&',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#039;'
};
return str.replace(/[&<>"']/g, function(m) {return map[m];});
}

function makeKeywordLabel(ind) {
/* Print list of keywords only if there is at least one. */
Expand All @@ -55,7 +58,10 @@ function makeKeywordLabel(ind) {
function makeVariableList(ind) {
/* Print list of variables and include mouse-hover tooltip with variable description. */
return Object.entries(ind.vars).map((kv) => {
const tooltip = `<button class="indVarname" title="${kv[1]}" alt="${kv[1]}">${kv[0]}</button>`;
/* kv[0] is the variable name, kv[1] is the variable description. */
/* Convert kv[1] to a string literal */
const text = escapeHTML(kv[1]);
const tooltip = `<button class="indVarname" title="${text}" alt="${text}">${kv[0]}</button>`;
huard marked this conversation as resolved.
Show resolved Hide resolved
return tooltip
}).join('');
}
Expand All @@ -66,13 +72,13 @@ function indTemplate(ind) {
return `
<div class="indElem" id="${ind.id}">
<div class="indHeader">
<b class="indTitle">${ind.title}</b>
<b class="indTitle">${escapeHTML(ind.title)}</b>
<a class="reference_internal indName" href="api_indicators.html#xclim.indicators.${ind.module}.${ind.name}" title="${ind.name}">
<code>${ind.module}.${ind.name}</code>
</a>
</div>
<div class="indVars">Uses: ${varlist}</div>
<div class="indDesc"><p>${ind.abstract}</p></div>
<div class="indDesc"><p>${escapeHTML(ind.abstract)}</p></div>
${makeKeywordLabel(ind)}
<div class="indID">Yaml ID: <code>${ind.id}</code></div>
</div>
Expand Down