From a7f4df0370938cc95c06fbc2af966b9b4160b9bb Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 4 Oct 2022 13:31:17 +0800 Subject: [PATCH] Add extra guarding against legacy widget preview errors (#44635) * Add extra guarding against legacy widget preview errors * Add a fallback height --- .../widgets/src/blocks/legacy-widget/edit/preview.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/widgets/src/blocks/legacy-widget/edit/preview.js b/packages/widgets/src/blocks/legacy-widget/edit/preview.js index 3d731672e87435..a23e046572ce3e 100644 --- a/packages/widgets/src/blocks/legacy-widget/edit/preview.js +++ b/packages/widgets/src/blocks/legacy-widget/edit/preview.js @@ -61,10 +61,15 @@ export default function Preview( { idBase, instance, isVisible } ) { function setHeight() { // Pick the maximum of these two values to account for margin collapsing. const height = Math.max( - iframe.contentDocument.documentElement.offsetHeight, - iframe.contentDocument.body.offsetHeight + iframe.contentDocument.documentElement?.offsetHeight ?? 0, + iframe.contentDocument.body?.offsetHeight ?? 0 ); - iframe.style.height = `${ height }px`; + + // Fallback to a height of 100px if the height cannot be determined. + // This ensures the block is still selectable. 100px should hopefully + // be not so big that it's annoying, and not so small that nothing + // can be seen. + iframe.style.height = `${ height !== 0 ? height : 100 }px`; } const { IntersectionObserver } = iframe.ownerDocument.defaultView;