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 bug in Safari when calculating text coords of hidden nodes #3

Closed
wants to merge 1 commit into from
Closed

Conversation

alex-ketch
Copy link

Hi @marijnh, while working on a component which uses CodeMirror I noticed some errors being raised in Safari v14.0.3.

It doesn't seem to be a fatal issue, but logs a TypeError: undefined is not an object (evaluating 'h.width').

Looking into it a bit more, it seems that the issue is related to how I render the parent DOM element housing the editor.
Depending on the user supplied prop, the CodeMirror instance can start hidden, and later toggled to be seen.

The component can be seen here

Screenshot 2021-02-03 at 18 38 40


What happens is that range.getClientRects() (linked below) returns an empty array (see screenshot from safari stopped at the breakpoint. Apologies for the minified source code). Trying to access this array with an index then returns undefined.

view/src/inlineview.ts

Lines 146 to 148 in 1b92b55

let rects = range.getClientRects(), rect = rects[(flatten ? flatten < 0 : side >= 0) ? 0 : rects.length - 1]
if (browser.safari && !flatten && rect.width == 0) rect = Array.prototype.find.call(rects, r => r.width) || rect
return flatten ? flattenRect(rect!, flatten < 0) : rect!

Screenshot 2021-02-03 at 17 34 44@2x

A simple fix in this case was to check the existence of the rect variable before accessing the width property. However I couldn't quite decide what should happen regarding the fallback on line 148.
There's a bit of type coercion going on, and in this case the function might return undefined I think. Should I stub a Rect object with zero dimensions in this case?

Please let me know if you have any suggested changes, if this PR is out of scope, or if a rambled too much and failed to explain the issue.
Thanks for your time!

marijnh added a commit that referenced this pull request Feb 4, 2021
FIX: Fix a crash when the editor was hidden on Safari, due to `getClientRects`
returning an empty list.

Issue #3
@marijnh
Copy link
Member

marijnh commented Feb 4, 2021

Thanks for the report. I think returning a zero rectangle is indeed the way to go here—I've implemented that in attached patch.

@marijnh marijnh closed this Feb 4, 2021
@visnup
Copy link

visnup commented Feb 16, 2021

Would it be easy to publish a release for this fix? We're running into the same issue and would love to incorporate it.

@marijnh
Copy link
Member

marijnh commented Feb 16, 2021

Sure. I've tagged 0.17.8

x-three added a commit to x-three/codemirror-view that referenced this pull request Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants