Merge pull request #13419 from Snuffleupagus/more-stringToBytes

Use the `stringToBytes` helper function in more places
This commit is contained in:
Tim van der Meij 2021-05-22 13:00:16 +02:00 committed by GitHub
commit 92e731011d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 8 deletions

View File

@ -57,7 +57,7 @@ import {
getRelevant,
getStringOption,
} from "./utils.js";
import { Util, warn } from "../../shared/util.js";
import { stringToBytes, Util, warn } from "../../shared/util.js";
const TEMPLATE_NS_ID = NamespaceIds.template.id;
@ -2262,9 +2262,7 @@ class Image extends StringObject {
}
if (this.transferEncoding === "base64") {
const buffer = Uint8Array.from(atob(this[$content]), c =>
c.charCodeAt(0)
);
const buffer = stringToBytes(atob(this[$content]));
const blob = new Blob([buffer], { type: this.contentType });
html.attributes.src = URL.createObjectURL(blob);
return html;

View File

@ -13,6 +13,8 @@
* limitations under the License.
*/
import { stringToBytes } from "../shared/util.js";
// This getFilenameFromContentDispositionHeader function is adapted from
// https://github.com/Rob--W/open-in-browser/blob/7e2e35a38b8b4e981b11da7b2f01df0149049e92/extension/content-disposition.js
// with the following changes:
@ -85,10 +87,8 @@ function getFilenameFromContentDispositionHeader(contentDisposition) {
}
try {
const decoder = new TextDecoder(encoding, { fatal: true });
const bytes = Array.from(value, function (ch) {
return ch.charCodeAt(0) & 0xff;
});
value = decoder.decode(new Uint8Array(bytes));
const buffer = stringToBytes(value);
value = decoder.decode(buffer);
needsEncodingFixup = false;
} catch (e) {
// TextDecoder constructor threw - unrecognized encoding.