diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-03-31 03:55:26 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-04-05 20:07:01 +0200 |
commit | a7ff0a1948b874c4171fd424698be033c74d333d (patch) | |
tree | 537822c30ee4605d4f8d43b0a2af95e2b63ffe0b | |
parent | Minor fixups. (diff) | |
download | calp-a7ff0a1948b874c4171fd424698be033c74d333d.tar.gz calp-a7ff0a1948b874c4171fd424698be033c74d333d.tar.xz |
Remove innerHTML from user-additions.js.
-rw-r--r-- | static/user/user-additions.js | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/static/user/user-additions.js b/static/user/user-additions.js index 6d944b86..c9ebe1a4 100644 --- a/static/user/user-additions.js +++ b/static/user/user-additions.js @@ -5,17 +5,22 @@ window.formatters.set('description', (el, d) => { let doc = parser.parseFromString(d, 'text/html'); el.replaceChildren(doc.body); } else { - /* Otherwise it should be plain(er) text, parse "all" links - (and reserved XML characters) - */ - // TODO replace with something that doesn't use innerHTML */ - el.innerHTML = d - .replaceAll(/</g, '<') - .replaceAll(/>/g, '>') - .replaceAll(/&/g, '&') - .replaceAll(/'/g, ''') - .replaceAll(/"/g, '"') - .replaceAll(/https?:\/\/\S+/g, '<a href="$&">$&</a>') + /* Otherwise it should be plain(er) text, parse "all" links */ + let rx = /https?:\/\/\S+/g + let idx = 0; + let children = [] + for (let match of d.matchAll(rx)) { + let anch = document.createElement('a') + anch.href = match[0] + anch.textContent = match[0] + + children.push(d.substring(idx, match.index)) + children.push(anch) + + idx = match.index + match[0].length + } + children.push(d.substring(idx)) + el.replaceChildren(...children); } }) |