-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Popover: Fix issue with undefined getBoundingClientRect #27445
Popover: Fix issue with undefined getBoundingClientRect #27445
Conversation
Size Change: +7 B (0%) Total Size: 1.19 MB
ℹ️ View Unchanged
|
Oddly enough, the e2e failures do seem to be related 🤔 |
fb14e14
to
92f0164
Compare
Whoops! The same fix doesn't work for |
Can we add a unit test for the scenario you're describing in the PR desc to |
When is it throwing this error? We haven't iframed the content yet? |
This seems to be happening in an environment where we iframe the editor, and use a popover for page template selection (a custom feature on our platform). |
if ( anchorRef instanceof window.Element ) { | ||
if ( | ||
anchorRef?.ownerDocument && | ||
anchorRef instanceof anchorRef.ownerDocument.defaultView.Element |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can just check for .getBoundingClientRect
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would probably be more direct 🙂
If we decide that this fixes the issue, and if there will be a 9.5.1 to fix the 'Schedule...' button label issue -- can we include this PR also in that patch release? cc/ @ntsekouras @jorgefilipecosta |
@@ -75,7 +75,10 @@ function computeAnchorRect( | |||
return getRectangleFromRange( anchorRef ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the above be changed too? Could potentially check for .endContainer
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was looking at that and I think the range check is susceptible as well. I'll push the change in a moment.
This reverts commit 16727dc.
0724a15
to
08c4ff3
Compare
I added a check for Range that should work across the iframe boundary as well and used duck-typing (check for methods) to discover Range and Element. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks code to me 👍
Fix an issue where the `Popover` component throws a runtime `TypeError: Cannot read property 'getBoundingClientRect' of undefined.` This may happen in WordPress if a Popover is rendered in an iframe. It's been observed by rendering a `BlockList` component containing `RichText` in an iframe 😵 The issue is that `instanceof` checks fail across iframe boundaries, where `anchorRef instanceof window.Element` fails because `anchorRef` when it _is_ an instance of Element in the frame but not `window.Element`. Instead of `instanceof` checks that fail across iframe boundaries, check for expected methods as the predicate.
Fix an issue where the `Popover` component throws a runtime `TypeError: Cannot read property 'getBoundingClientRect' of undefined.` This may happen in WordPress if a Popover is rendered in an iframe. It's been observed by rendering a `BlockList` component containing `RichText` in an iframe 😵 The issue is that `instanceof` checks fail across iframe boundaries, where `anchorRef instanceof window.Element` fails because `anchorRef` when it _is_ an instance of Element in the frame but not `window.Element`. Instead of `instanceof` checks that fail across iframe boundaries, check for expected methods as the predicate.
Description
Fix an issue where the
Popover
component throws a runtimeTypeError: Cannot read property 'getBoundingClientRect' of undefined.
on this line:gutenberg/packages/components/src/popover/index.js
Line 89 in adacd0a
This may happen in WordPress if a Popover is rendered in an iframe. It's been observed by rendering a
BlockList
component containing aRichText
component in an iframe 😵This is the problematic section:
gutenberg/packages/components/src/popover/index.js
Lines 64 to 86 in adacd0a
In the issue I've observed,
anchorRef
is anElement
on line 78, but it's not an instance ofwindow.Element
. It's an instance ofanchorRef.ownerDocument.defaultView.Element
😮Both of the conditional blocks are skipped and we end up calling
anchorRef.top.getBoundingClientRect()
on an Element. This triggers the error:gutenberg/packages/components/src/popover/index.js
Lines 88 to 90 in adacd0a
You can verify this is problematic by running the following in a recent browser at
about:blank
:The issue would apparently begin to manifest after #25332. The iframed issue described above worked with Gutenberg 9.3 but began to throw errors in 9.4.
How has this been tested?
Appears to fix the issue where observed.
Popovers should continue to work in normal usage.
Types of changes
Bug fix
Checklist: