mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-03 02:00:43 +00:00
Replace invalid characters in attribute value; fixes the error
Unescaped '<' not allowed in attributes values
Fixes #2534
17 lines
452 B
JavaScript
17 lines
452 B
JavaScript
/**
|
|
* Cleans up a value to be used as an element ID.
|
|
*
|
|
* Encodes invalid characters to be valid in XHTML and makes it
|
|
* so that it can be reversed by {@link decodeIdAttribute}.
|
|
*/
|
|
export function encodeIdAttribute(id) {
|
|
return id.replace(/[<>&;]/gm, m => `__u${m.charCodeAt(0)}__`);
|
|
}
|
|
|
|
/**
|
|
* Reverts {@link encodeIdAttribute}.
|
|
*/
|
|
export function decodeIdAttribute(id) {
|
|
return id.replace(/__u(\d+)__/gm, (m, d) => String.fromCharCode(d));
|
|
}
|