mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-23 10:08:49 +01:00
debt - btoa/atob is not core
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user