Update element fill to replace `file` URIs in image tags

This commit is contained in:
Kyle Carberry 2019-03-06 00:06:44 +00:00
parent 2b3d2933eb
commit 414eb7076f
No known key found for this signature in database
GPG Key ID: 42E8F51E81E8201C
1 changed files with 28 additions and 9 deletions

View File

@ -25,9 +25,6 @@ const newCreateElement = <K extends keyof HTMLElementTagNameMap>(tagName: K): HT
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
return oldCreateElement.call(document, tagName as any); return oldCreateElement.call(document, tagName as any);
}; };
if (tagName === "style") {
const style = createElement("style");
// tslint:disable-next-line:no-any // tslint:disable-next-line:no-any
const getPropertyDescriptor = (object: any, id: string): PropertyDescriptor | undefined => { const getPropertyDescriptor = (object: any, id: string): PropertyDescriptor | undefined => {
let op = Object.getPrototypeOf(object); let op = Object.getPrototypeOf(object);
@ -37,6 +34,28 @@ const newCreateElement = <K extends keyof HTMLElementTagNameMap>(tagName: K): HT
return Object.getOwnPropertyDescriptor(op, id); return Object.getOwnPropertyDescriptor(op, id);
}; };
if (tagName === "img") {
const img = createElement("img");
const oldSrc = getPropertyDescriptor(img, "src");
if (!oldSrc) {
throw new Error("Failed to find src property");
}
Object.defineProperty(img, "src", {
get: (): string => {
return oldSrc!.get!.call(img);
},
set: (value: string): void => {
value = value.replace(/file:\/\//g, "/resource");
oldSrc!.set!.call(img, value);
},
});
return img;
}
if (tagName === "style") {
const style = createElement("style");
const oldInnerHtml = getPropertyDescriptor(style, "innerHTML"); const oldInnerHtml = getPropertyDescriptor(style, "innerHTML");
if (!oldInnerHtml) { if (!oldInnerHtml) {
throw new Error("Failed to find innerHTML property"); throw new Error("Failed to find innerHTML property");