Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix instanceof checks for a cross-realm scenario #312

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 41 additions & 35 deletions src/help/getRenderProperties.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/* global HTMLImageElement */
/* global HTMLCanvasElement */
/* global SVGElement */

import getOptionsFromElement from "./getOptionsFromElement.js";
import renderers from "../renderers";

Expand All @@ -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);
Expand All @@ -31,14 +27,51 @@ 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 globalThis = typeof window !== 'undefined' ? window : global;
const ownerDocument = element && element.ownerDocument || globalThis.document;
const ownerWindow = ownerDocument && (ownerDocument.defaultView || ownerDocument.parentWindow) || globalThis;

// 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,
Expand All @@ -47,7 +80,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),
Expand All @@ -72,31 +105,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;