Skip to content

Commit

Permalink
Returning an array from maybeCopyElementsToSiteEditorContext and not …
Browse files Browse the repository at this point in the history
…mixed return types

Testing whether we indeed have access to the parent DOM before we try to avoid fatals
  • Loading branch information
ramonjd committed Apr 22, 2021
1 parent 611b1a9 commit a27e87c
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ export function isElementInEditorIframe( elementRef ) {
return currentWindow.name === 'editor-canvas' && currentWindow.self !== currentWindow.top;
}

/**
* Returns whether a iframe has domain access to its parent.
*
* @param {HTMLElement} currentWindow - The window context for which we want to test access.
* @returns {boolean} - Whether we have access to the parent window.
*/
function canIframeAccessParentWindow( currentWindow ) {
try {
return !! currentWindow?.parent?.location.href;
} catch ( e ) {
return false;
}
}

/**
* This function will check if the current element (e.g., a block) sits inside an Iframe (e.g., the Site Editor)
* and tries to move elements from the parent window to the iframe.
Expand All @@ -42,19 +56,24 @@ export function maybeCopyElementsToSiteEditorContext(
elementRef,
shouldRemoveSource = false
) {
let results = [];
// Check to see if we're in an iframe, e.g., the Site Editor.
// If not, do nothing.
if (
! elementRef ||
( ! elementSelectors && ! elementSelectors.length ) ||
! isElementInEditorIframe( elementRef )
) {
return;
return results;
}

const { currentDoc, currentWindow } = getLoadContext( elementRef );

if ( ! canIframeAccessParentWindow( currentWindow ) ) {
return results;
}

const parentDoc = currentWindow?.parent?.document;
let results = [];

if ( currentDoc && parentDoc ) {
results = elementSelectors.filter( selector => {
Expand Down

0 comments on commit a27e87c

Please sign in to comment.