Skip to content
This repository has been archived by the owner on Feb 6, 2023. It is now read-only.

[bug] Incorrect behavior of getRangeClientRects in webkit browsers #570

Closed
qrohlf opened this issue Jul 28, 2016 · 1 comment
Closed

[bug] Incorrect behavior of getRangeClientRects in webkit browsers #570

qrohlf opened this issue Jul 28, 2016 · 1 comment

Comments

@qrohlf
Copy link
Contributor

qrohlf commented Jul 28, 2016

video demo: https://www.youtube.com/watch?v=TpNzVH5jlcU
live demo: http://codepen.io/qrohlf/pen/YWjzOq?editors=1010#0

getVisibleSelectionRect does not fully work around webkit's bad behavior regarding selection range bounding boxes. Selecting the first word on a wrapped line results in an incorrect bounding box. Bug exists in Chrome 51.0.2704.106 and Safari 9.1.1 (11601.6.17).

You can see the bad behavior this causes in the inline toolbar in Facebook Notes:

normal behavior
screen shot 2016-07-28 at 4 19 37 pm

oh look, it's the webkit contenteditable selection range bounding box bug (WCESRBBB)
screen shot 2016-07-28 at 4 19 44 pm

Hopefully once I finish the other functionality in the project I'm using draft for, I can circle back around and fix this bug, but if someone else wants to fix it for me I won't argue :).

@ouchxp
Copy link
Contributor

ouchxp commented Aug 10, 2016

@qrohlf We have this problem as well. Here is my monkey patch.

import { getVisibleSelectionRect as draftVSR } from 'draft-js';
function getVisibleSelectionRect(window) {
  let selection = draftVSR(window);

  // When selection the first word of the wrapped line, chrome will return two rects
  // And the first rect has 0 width
  if (selection) {
    const selectionRects = window.getSelection().getRangeAt(0).getClientRects();
    if (selectionRects.length === 2 && selectionRects[0].width === 0) {
      selection = selectionRects[1];
    }
  }
  return selection;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants