Not enough, because there are three different places in that line and each needs its own handling.
1. Text between tags: escape & < > " '. If you build DOM in the browser, simpler still: don't use innerHTML for text at all.
2. Attribute values: the same escaping, and always inside quotes. Without " escaped, a title like " onmouseover="alert(1) breaks out of the attribute.
3. URLs: escaping does not help against javascript:alert(1), it contains no special characters. Allow only http: and https:.
const ESC = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
function safeUrl(u) {
try {
const url = new URL(u, location.href);
return url.protocol === 'https:' || url.protocol === 'http:' ? url.href : '#';
} catch {
return '#';
}
}
list.innerHTML = items
.map((i) => `<li><a href="${esc(safeUrl(i.url))}">${esc(i.title)}</a></li>`)
.join('');
Or without HTML strings at all, which removes the whole class of bugs:
for (const i of items) {
const a = document.createElement('a');
a.href = safeUrl(i.url);
a.textContent = i.title; // text, never parsed as HTML
const li = document.createElement('li');
li.append(a);
list.append(li);
}
The rule to remember: escape for the context where the value lands — HTML text, attribute, URL, JavaScript, CSS are all different.