Render webviews in UTF-8 (#5836)

This commit is contained in:
Dave Nicolson 2023-03-15 10:48:07 +01:00 committed by GitHub
parent 707822e691
commit 55b5260aa4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals';
import { render } from '@testing-library/react';
import iconv from 'iconv-lite';
import React from 'react';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
@ -86,4 +87,18 @@ describe('<ResponseViewer />', () => {
expect(responseWebView).toBeInTheDocument();
expect(mockRenderWithProps).not.toHaveBeenCalledWith();
});
it('should decode ISO-8859-1 HTML content and render encoded in UTF-8', async () => {
const TEST_STRING = 'Viel Glück';
const responseWebView = await getResponseViewerChild({
contentType: 'text/html; charset=iso-8859-1',
getBody: () => Buffer.from(iconv.encode(TEST_STRING, 'iso-8859-1')),
}, 'ResponseWebView');
Object.assign(responseWebView, { loadURL: (url: string) => {
expect(url).toEqual(`data:text/html; charset=utf-8,${encodeURIComponent(TEST_STRING)}`);
} });
responseWebView.dispatchEvent(new Event('dom-ready'));
});
});

View File

@ -253,7 +253,6 @@ export const ResponseViewer = ({
return (
<ResponseWebView
body={getBodyAsString()}
contentType={contentType}
key={disableHtmlPreviewJs ? 'no-js' : 'yes-js'}
url={url}
webpreferences={`disableDialogs=true, javascript=${disableHtmlPreviewJs ? 'no' : 'yes'

View File

@ -2,11 +2,10 @@ import React, { FC, useEffect, useRef } from 'react';
interface Props {
body: string;
contentType: string;
url: string;
webpreferences: string;
}
export const ResponseWebView: FC<Props> = ({ webpreferences, body, contentType, url }) => {
export const ResponseWebView: FC<Props> = ({ webpreferences, body, url }) => {
const webviewRef = useRef<Electron.WebviewTag>(null);
useEffect(() => {
@ -15,7 +14,7 @@ export const ResponseWebView: FC<Props> = ({ webpreferences, body, contentType,
if (webview) {
webview.removeEventListener('dom-ready', handleDOMReady);
const bodyWithBase = body.replace('<head>', `<head><base href="${url}">`);
webview.loadURL(`data:${contentType},${encodeURIComponent(bodyWithBase)}`);
webview.loadURL(`data:text/html; charset=utf-8,${encodeURIComponent(bodyWithBase)}`);
}
};
if (webview) {
@ -26,7 +25,7 @@ export const ResponseWebView: FC<Props> = ({ webpreferences, body, contentType,
webview.removeEventListener('dom-ready', handleDOMReady);
}
};
}, [body, contentType, url]);
}, [body, url]);
return (
<webview
data-testid="ResponseWebView"