debt - btoa/atob is not core

This commit is contained in:
Benjamin Pasero
2020-10-15 13:55:12 +02:00
parent aa4a787851
commit 50ee84f780
7 changed files with 31 additions and 31 deletions

View File

@@ -1362,3 +1362,24 @@ export function safeInnerHtml(node: HTMLElement, value: string): void {
const html = _ttpSafeInnerHtml?.createHTML(value, options) ?? insane(value, options);
node.innerHTML = html as unknown as string;
}
/**
* Convert a Unicode string to a string in which each 16-bit unit occupies only one byte
*
* From https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/btoa
*/
function toBinary(str: string): string {
const codeUnits = new Uint16Array(str.length);
for (let i = 0; i < codeUnits.length; i++) {
codeUnits[i] = str.charCodeAt(i);
}
return String.fromCharCode(...new Uint8Array(codeUnits.buffer));
}
/**
* Version of the global `btoa` function that handles multi-byte characters instead
* of throwing an exception.
*/
export function multibyteAwareBtoa(str: string): string {
return btoa(toBinary(str));
}