From 14ef621b2430a626a0a5054d6b8040572d063e8f Mon Sep 17 00:00:00 2001 From: shau-kote Date: Thu, 3 Oct 2019 20:34:29 +0300 Subject: [PATCH 1/5] Fix instanceof checks for multi-realm environments --- src/help/getRenderProperties.js | 75 ++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/src/help/getRenderProperties.js b/src/help/getRenderProperties.js index 6d4d6bba..30c2b205 100644 --- a/src/help/getRenderProperties.js +++ b/src/help/getRenderProperties.js @@ -1,7 +1,3 @@ -/* global HTMLImageElement */ -/* global HTMLCanvasElement */ -/* global SVGElement */ - import getOptionsFromElement from "./getOptionsFromElement.js"; import renderers from "../renderers"; @@ -18,7 +14,7 @@ import {InvalidElementException} from "../exceptions/exceptions.js"; // options (optional): Options that can be defined in the element // } -function getRenderProperties(element){ +function getRenderProperties(element) { // If the element is a string, query select call again if(typeof element === "string"){ return querySelectedRenderProperties(element); @@ -31,14 +27,50 @@ function getRenderProperties(element){ } return returnArray; } + // If an element is a single instance + else{ + return elementInstanceRenderProperties(element); + } +} + +function querySelectedRenderProperties(string){ + var selector = document.querySelectorAll(string); + if(selector.length === 0){ + return undefined; + } + else{ + let returnArray = []; + for(let i = 0; i < selector.length; i++){ + returnArray.push(getRenderProperties(selector[i])); + } + return returnArray; + } +} + +function newCanvasRenderProperties(imgElement){ + var canvas = document.createElement('canvas'); + return { + element: canvas, + options: getOptionsFromElement(imgElement), + renderer: renderers.CanvasRenderer, + afterRender: function(){ + imgElement.setAttribute("src", canvas.toDataURL()); + } + }; +} + +function elementInstanceRenderProperties(element){ + const ownerDocument = element.ownerDocument || document; + const ownerWindow = ownerDocument.defaultView || ownerDocument.parentWindow || window; + // If element, render on canvas and set the uri as src - else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLImageElement){ + if(typeof HTMLCanvasElement !== 'undefined' && element instanceof ownerWindow.HTMLImageElement){ return newCanvasRenderProperties(element); } // If SVG else if( (element && element.nodeName === 'svg') || - (typeof SVGElement !== 'undefined' && element instanceof SVGElement) + (typeof SVGElement !== 'undefined' && element instanceof ownerWindow.SVGElement) ){ return { element: element, @@ -47,7 +79,7 @@ function getRenderProperties(element){ }; } // If canvas (in browser) - else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof HTMLCanvasElement){ + else if(typeof HTMLCanvasElement !== 'undefined' && element instanceof ownerWindow.HTMLCanvasElement){ return { element: element, options: getOptionsFromElement(element), @@ -72,31 +104,4 @@ function getRenderProperties(element){ } } -function querySelectedRenderProperties(string){ - var selector = document.querySelectorAll(string); - if(selector.length === 0){ - return undefined; - } - else{ - let returnArray = []; - for(let i = 0; i < selector.length; i++){ - returnArray.push(getRenderProperties(selector[i])); - } - return returnArray; - } -} - - -function newCanvasRenderProperties(imgElement){ - var canvas = document.createElement('canvas'); - return { - element: canvas, - options: getOptionsFromElement(imgElement), - renderer: renderers.CanvasRenderer, - afterRender: function(){ - imgElement.setAttribute("src", canvas.toDataURL()); - } - }; -} - export default getRenderProperties; From 6ead04dc605ce1b5ebf5f9c5d3eb8e4094a8153e Mon Sep 17 00:00:00 2001 From: shau-kote Date: Thu, 3 Oct 2019 21:30:01 +0300 Subject: [PATCH 2/5] Fix the global reference for Node.js --- src/help/getRenderProperties.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/help/getRenderProperties.js b/src/help/getRenderProperties.js index 30c2b205..5e3951b0 100644 --- a/src/help/getRenderProperties.js +++ b/src/help/getRenderProperties.js @@ -60,8 +60,9 @@ function newCanvasRenderProperties(imgElement){ } function elementInstanceRenderProperties(element){ - const ownerDocument = element.ownerDocument || document; - const ownerWindow = ownerDocument.defaultView || ownerDocument.parentWindow || window; + const global = typeof window !== 'undefined' ? window : global; + const ownerDocument = element.ownerDocument || global.document; + const ownerWindow = ownerDocument.defaultView || ownerDocument.parentWindow || global.window; // If element, render on canvas and set the uri as src if(typeof HTMLCanvasElement !== 'undefined' && element instanceof ownerWindow.HTMLImageElement){ From 3213ce4d0f9986e4322d85a24d221eba1afe3b2d Mon Sep 17 00:00:00 2001 From: shau-kote Date: Thu, 3 Oct 2019 21:47:41 +0300 Subject: [PATCH 3/5] Fix variable name collision --- src/help/getRenderProperties.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/help/getRenderProperties.js b/src/help/getRenderProperties.js index 5e3951b0..0dd4f7b0 100644 --- a/src/help/getRenderProperties.js +++ b/src/help/getRenderProperties.js @@ -60,9 +60,9 @@ function newCanvasRenderProperties(imgElement){ } function elementInstanceRenderProperties(element){ - const global = typeof window !== 'undefined' ? window : global; - const ownerDocument = element.ownerDocument || global.document; - const ownerWindow = ownerDocument.defaultView || ownerDocument.parentWindow || global.window; + const globalThis = typeof window !== 'undefined' ? window : global; + const ownerDocument = element.ownerDocument || globalThis.document; + const ownerWindow = ownerDocument.defaultView || ownerDocument.parentWindow || globalThis; // If element, render on canvas and set the uri as src if(typeof HTMLCanvasElement !== 'undefined' && element instanceof ownerWindow.HTMLImageElement){ From 6391870f6ea7e203a046cc0e6459473f183237f5 Mon Sep 17 00:00:00 2001 From: shau-kote Date: Thu, 3 Oct 2019 21:56:09 +0300 Subject: [PATCH 4/5] Fix case of element is null --- src/help/getRenderProperties.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/help/getRenderProperties.js b/src/help/getRenderProperties.js index 0dd4f7b0..e9fbf5b2 100644 --- a/src/help/getRenderProperties.js +++ b/src/help/getRenderProperties.js @@ -61,7 +61,7 @@ function newCanvasRenderProperties(imgElement){ function elementInstanceRenderProperties(element){ const globalThis = typeof window !== 'undefined' ? window : global; - const ownerDocument = element.ownerDocument || globalThis.document; + const ownerDocument = element && element.ownerDocument || globalThis.document; const ownerWindow = ownerDocument.defaultView || ownerDocument.parentWindow || globalThis; // If element, render on canvas and set the uri as src From 0ec4666bcb2aa1b9c24e9baf016ddfbd09a86dfe Mon Sep 17 00:00:00 2001 From: shau-kote Date: Thu, 3 Oct 2019 22:01:42 +0300 Subject: [PATCH 5/5] Fix case with absence of document --- src/help/getRenderProperties.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/help/getRenderProperties.js b/src/help/getRenderProperties.js index e9fbf5b2..b9e15843 100644 --- a/src/help/getRenderProperties.js +++ b/src/help/getRenderProperties.js @@ -62,7 +62,7 @@ function newCanvasRenderProperties(imgElement){ function elementInstanceRenderProperties(element){ const globalThis = typeof window !== 'undefined' ? window : global; const ownerDocument = element && element.ownerDocument || globalThis.document; - const ownerWindow = ownerDocument.defaultView || ownerDocument.parentWindow || globalThis; + const ownerWindow = ownerDocument && (ownerDocument.defaultView || ownerDocument.parentWindow) || globalThis; // If element, render on canvas and set the uri as src if(typeof HTMLCanvasElement !== 'undefined' && element instanceof ownerWindow.HTMLImageElement){