StringUtil (#1861)

* Add StringUtil class

* Use StringUtil
This commit is contained in:
toasted-nutbread 2021-07-31 13:04:16 -04:00 committed by GitHub
parent 00c5ae7983
commit 454ffaad96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 15 deletions

View File

@ -193,6 +193,7 @@
"ext/js/data/json-schema.js", "ext/js/data/json-schema.js",
"ext/js/data/options-util.js", "ext/js/data/options-util.js",
"ext/js/data/permissions-util.js", "ext/js/data/permissions-util.js",
"ext/js/data/sandbox/string-util.js",
"ext/js/dom/simple-dom-parser.js", "ext/js/dom/simple-dom-parser.js",
"ext/js/general/cache-map.js", "ext/js/general/cache-map.js",
"ext/js/general/object-property-accessor.js", "ext/js/general/object-property-accessor.js",

View File

@ -36,6 +36,7 @@
<script src="/js/data/json-schema.js"></script> <script src="/js/data/json-schema.js"></script>
<script src="/js/data/options-util.js"></script> <script src="/js/data/options-util.js"></script>
<script src="/js/data/permissions-util.js"></script> <script src="/js/data/permissions-util.js"></script>
<script src="/js/data/sandbox/string-util.js"></script>
<script src="/js/dom/native-simple-dom-parser.js"></script> <script src="/js/dom/native-simple-dom-parser.js"></script>
<script src="/js/general/cache-map.js"></script> <script src="/js/general/cache-map.js"></script>
<script src="/js/general/object-property-accessor.js"></script> <script src="/js/general/object-property-accessor.js"></script>

View File

@ -68,6 +68,7 @@
<script src="/js/comm/cross-frame-api.js"></script> <script src="/js/comm/cross-frame-api.js"></script>
<script src="/js/data/anki-util.js"></script> <script src="/js/data/anki-util.js"></script>
<script src="/js/data/permissions-util.js"></script> <script src="/js/data/permissions-util.js"></script>
<script src="/js/data/sandbox/string-util.js"></script>
<script src="/js/dom/document-focus-controller.js"></script> <script src="/js/dom/document-focus-controller.js"></script>
<script src="/js/dom/html-template-collection.js"></script> <script src="/js/dom/html-template-collection.js"></script>
<script src="/js/pages/settings/backup-controller.js"></script> <script src="/js/pages/settings/backup-controller.js"></script>

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 Yomichan Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/**
* Class containing generic string utility functions.
*/
class StringUtil {
/**
* Decodes the contents of an ArrayBuffer using UTF8.
* @param arrayBuffer The input ArrayBuffer.
* @returns A UTF8-decoded string.
*/
static arrayBufferUtf8Decode(arrayBuffer) {
try {
return new TextDecoder('utf-8').decode(arrayBuffer);
} catch (e) {
return decodeURIComponent(escape(this.arrayBufferToBinaryString(arrayBuffer)));
}
}
/**
* Converts the contents of an ArrayBuffer to a base64 string.
* @param arrayBuffer The input ArrayBuffer.
* @returns A base64 string representing the binary content.
*/
static arrayBufferToBase64(arrayBuffer) {
return btoa(this.arrayBufferToBinaryString(arrayBuffer));
}
/**
* Converts the contents of an ArrayBuffer to a binary string.
* @param arrayBuffer The input ArrayBuffer.
* @returns A string representing the binary content.
*/
static arrayBufferToBinaryString(arrayBuffer) {
const bytes = new Uint8Array(arrayBuffer);
try {
return String.fromCharCode(...bytes);
} catch (e) {
let binary = '';
for (let i = 0, ii = bytes.byteLength; i < ii; ++i) {
binary += String.fromCharCode(bytes[i]);
}
return binary;
}
}
}

View File

@ -19,6 +19,7 @@
* JsonSchema * JsonSchema
* NativeSimpleDOMParser * NativeSimpleDOMParser
* SimpleDOMParser * SimpleDOMParser
* StringUtil
*/ */
class AudioDownloader { class AudioDownloader {
@ -257,7 +258,7 @@ class AudioDownloader {
throw new Error('Could not retrieve audio'); throw new Error('Could not retrieve audio');
} }
const data = this._arrayBufferToBase64(arrayBuffer); const data = StringUtil.arrayBufferToBase64(arrayBuffer);
const contentType = response.headers.get('Content-Type'); const contentType = response.headers.get('Content-Type');
return {data, contentType}; return {data, contentType};
} }
@ -288,10 +289,6 @@ class AudioDownloader {
return digest; return digest;
} }
_arrayBufferToBase64(arrayBuffer) {
return btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
}
_createSimpleDOMParser(content) { _createSimpleDOMParser(content) {
if (typeof NativeSimpleDOMParser !== 'undefined' && NativeSimpleDOMParser.isSupported()) { if (typeof NativeSimpleDOMParser !== 'undefined' && NativeSimpleDOMParser.isSupported()) {
return new NativeSimpleDOMParser(content); return new NativeSimpleDOMParser(content);

View File

@ -18,6 +18,7 @@
/* global /* global
* DictionaryController * DictionaryController
* OptionsUtil * OptionsUtil
* StringUtil
*/ */
class BackupController { class BackupController {
@ -317,17 +318,8 @@ class BackupController {
return warnings; return warnings;
} }
_utf8Decode(arrayBuffer) {
try {
return new TextDecoder('utf-8').decode(arrayBuffer);
} catch (e) {
const binaryString = String.fromCharCode.apply(null, new Uint8Array(arrayBuffer));
return decodeURIComponent(escape(binaryString));
}
}
async _importSettingsFile(file) { async _importSettingsFile(file) {
const dataString = this._utf8Decode(await this._readFileArrayBuffer(file)); const dataString = StringUtil.arrayBufferUtf8Decode(await this._readFileArrayBuffer(file));
const data = JSON.parse(dataString); const data = JSON.parse(dataString);
// Type check // Type check

View File

@ -3438,6 +3438,7 @@
<script src="/js/data/json-schema.js"></script> <script src="/js/data/json-schema.js"></script>
<script src="/js/data/options-util.js"></script> <script src="/js/data/options-util.js"></script>
<script src="/js/data/permissions-util.js"></script> <script src="/js/data/permissions-util.js"></script>
<script src="/js/data/sandbox/string-util.js"></script>
<script src="/js/dom/document-focus-controller.js"></script> <script src="/js/dom/document-focus-controller.js"></script>
<script src="/js/dom/document-util.js"></script> <script src="/js/dom/document-util.js"></script>
<script src="/js/dom/dom-data-binder.js"></script> <script src="/js/dom/dom-data-binder.js"></script>

View File

@ -33,6 +33,7 @@ self.importScripts(
'/js/data/json-schema.js', '/js/data/json-schema.js',
'/js/data/options-util.js', '/js/data/options-util.js',
'/js/data/permissions-util.js', '/js/data/permissions-util.js',
'/js/data/sandbox/string-util.js',
'/js/dom/simple-dom-parser.js', '/js/dom/simple-dom-parser.js',
'/js/general/cache-map.js', '/js/general/cache-map.js',
'/js/general/object-property-accessor.js', '/js/general/object-property-accessor.js',