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

@ -25,8 +25,8 @@ import { match } from 'minimatch';
// Feel free to add more core types as you see needed if present in node.js and browsers
const CORE_TYPES = [
'require', // from our AMD loader
'atob',
'btoa',
// 'atob',
// 'btoa',
'setTimeout',
'clearTimeout',
'setInterval',

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));
}

View file

@ -537,27 +537,6 @@ export function getCharContainingOffset(str: string, offset: number): [number, n
return _getCharContainingOffset(str, offset);
}
/**
* 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));
}
/**
* A manual encoding of `str` to UTF8.
* Use only in environments which do not offer native conversion methods!

View file

@ -72,6 +72,11 @@ suite('dom', () => {
assert(!element.classList.contains('bar'));
});
test('multibyteAwareBtoa', () => {
assert.equal(dom.multibyteAwareBtoa('hello world'), dom.multibyteAwareBtoa('hello world'));
assert.ok(dom.multibyteAwareBtoa('平仮名'));
});
suite('$', () => {
test('should build simple nodes', () => {
const div = $('div');

View file

@ -417,9 +417,4 @@ suite('Strings', () => {
test('getGraphemeBreakType', () => {
assert.equal(strings.getGraphemeBreakType(0xBC1), strings.GraphemeBreakType.SpacingMark);
});
test('multibyteAwareBtoa', () => {
assert.equal(strings.multibyteAwareBtoa('hello world'), strings.multibyteAwareBtoa('hello world'));
assert.ok(strings.multibyteAwareBtoa('平仮名'));
});
});

View file

@ -11,7 +11,7 @@ import { Disposable, DisposableStore, IDisposable, IReference } from 'vs/base/co
import { Schemas } from 'vs/base/common/network';
import { basename } from 'vs/base/common/path';
import { isEqual, isEqualOrParent, toLocalResource } from 'vs/base/common/resources';
import { multibyteAwareBtoa } from 'vs/base/common/strings';
import { multibyteAwareBtoa } from 'vs/base/browser/dom';
import { URI, UriComponents } from 'vs/base/common/uri';
import * as modes from 'vs/editor/common/modes';
import { localize } from 'vs/nls';
@ -348,7 +348,7 @@ class MainThreadCustomEditorModel extends Disposable implements ICustomEditorMod
private static toWorkingCopyResource(viewType: string, resource: URI) {
const authority = viewType.replace(/[^a-z0-9\-_]/gi, '-');
const path = '/' + multibyteAwareBtoa(resource.with({ query: null, fragment: null }).toString(true));
const path = `/${multibyteAwareBtoa(resource.with({ query: null, fragment: null }).toString(true))}`;
return URI.from({
scheme: Schemas.vscodeCustomEditor,
authority: authority,

View file

@ -31,7 +31,7 @@ import { CancellationToken } from 'vs/base/common/cancellation';
import { EditorActivation, IEditorOptions } from 'vs/platform/editor/common/editor';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { isEqual } from 'vs/base/common/resources';
import { multibyteAwareBtoa } from 'vs/base/common/strings';
import { multibyteAwareBtoa } from 'vs/base/browser/dom';
/**
* The text editor that leverages the diff text editor for the editing experience.