fix: Issues with TrustedTypes being reflected incorrectly

This commit is contained in:
JellyBrick
2023-09-18 06:42:27 +09:00
parent c90ab00c09
commit 3f3a5483ed
6 changed files with 14 additions and 229 deletions

View File

@ -5,30 +5,11 @@ import { ipcMain, ipcRenderer } from 'electron';
import { ValueOf } from '../utils/type-utils';
import type { TrustedTypePolicy } from 'trusted-types/lib';
let policyCache: Pick<TrustedTypePolicy<{ createHTML: (s: string) => string }>, 'name' | 'createHTML'> | {
createHTML: (s: string) => string,
};
export const noopTrustedHtmlPolicy = () => {
if (policyCache) {
return policyCache;
} else {
policyCache = window?.trustedTypes?.createPolicy('forceInner', {
createHTML: (s: string): string => s,
}) ?? {
createHTML: (s: string): string => s,
};
return policyCache;
}
};
// Creates a DOM element from an HTML string
export const ElementFromHtml = (html: string): HTMLElement => {
const template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = noopTrustedHtmlPolicy().createHTML(html) as unknown as string;
template.innerHTML = html;
return template.content.firstElementChild as HTMLElement;
};