From 414eb7076f643dcbc5a2c5cb2effd310904fc45c Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Wed, 6 Mar 2019 00:06:44 +0000 Subject: [PATCH] Update element fill to replace `file` URIs in image tags --- packages/ide/src/fill/electron.ts | 37 +++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/ide/src/fill/electron.ts b/packages/ide/src/fill/electron.ts index d84742205..ac4d55614 100644 --- a/packages/ide/src/fill/electron.ts +++ b/packages/ide/src/fill/electron.ts @@ -25,18 +25,37 @@ const newCreateElement = (tagName: K): HT // tslint:disable-next-line:no-any return oldCreateElement.call(document, tagName as any); }; + // tslint:disable-next-line:no-any + const getPropertyDescriptor = (object: any, id: string): PropertyDescriptor | undefined => { + let op = Object.getPrototypeOf(object); + while (!Object.getOwnPropertyDescriptor(op, id)) { + op = Object.getPrototypeOf(op); + } + + 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"); - // tslint:disable-next-line:no-any - const getPropertyDescriptor = (object: any, id: string): PropertyDescriptor | undefined => { - let op = Object.getPrototypeOf(object); - while (!Object.getOwnPropertyDescriptor(op, id)) { - op = Object.getPrototypeOf(op); - } - - return Object.getOwnPropertyDescriptor(op, id); - }; const oldInnerHtml = getPropertyDescriptor(style, "innerHTML"); if (!oldInnerHtml) { throw new Error("Failed to find innerHTML property");