html.mjs
18 lines and 2 functions in DrDustinEdwards/site-runtime, at commit 89e9d58 on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| escapeHtml | 18 to 20 | 1 | 100% | OkCRAP 1 | No data: mutation testing has not run on this repo yet |
| <anonymous:19> | 19 to 19 | 2 | 100% | OkCRAP 2 | No data: mutation testing has not run on this repo yet |
Imports
It imports no other module of this repo. Imported by mcp-login.mjs.
Source
/**
* HTML escaping, once, for every repo: local copies tended to miss the single quote.
*
* The five characters that can end a text run or an attribute value: & < > " and '. The result is safe as element
* content and as a single- or double-quoted attribute value. It is not safe in an unquoted attribute, in a URL
* attribute that may hold `javascript:`, or inside <script> or <style>: those need their own encoding.
*/
/** @type {Readonly<Record<string, string>>} */
const ESCAPES = { "&": "&", "<": "<", ">": ">", '"': """, "'": "'" };
/**
* `value` as HTML text: & < > " ' replaced by their entities, every other character unchanged. A value that is not a
* string is first made one with String(), as a template literal would.
* @param {unknown} value
* @returns {string}
*/
export function escapeHtml(value) {
return String(value).replace(/[&<>"']/g, (ch) => ESCAPES[ch] ?? ch);
}