Skip to content

Commit

Permalink
Don't trust bounding client rects for characters
Browse files Browse the repository at this point in the history
In some cases, on wrapping points, they span the whole line

Issue #4097
  • Loading branch information
marijnh committed Jul 19, 2016
1 parent a3d9a31 commit e60f4b7
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -2688,6 +2688,16 @@
return {node: node, start: start, end: end, collapse: collapse, coverStart: mStart, coverEnd: mEnd};
}

function getUsefulRect(rects, bias) {
var rect = nullRect
if (bias == "left") for (var i = 0; i < rects.length; i++) {
if ((rect = rects[i]).left != rect.right) break
} else for (var i = rects.length - 1; i >= 0; i--) {
if ((rect = rects[i]).left != rect.right) break
}
return rect
}

function measureCharInner(cm, prepared, ch, bias) {
var place = nodeAndOffsetInLineMap(prepared.map, ch, bias);
var node = place.node, start = place.start, end = place.end, collapse = place.collapse;
Expand All @@ -2697,17 +2707,10 @@
for (var i = 0; i < 4; i++) { // Retry a maximum of 4 times when nonsense rectangles are returned
while (start && isExtendingChar(prepared.line.text.charAt(place.coverStart + start))) --start;
while (place.coverStart + end < place.coverEnd && isExtendingChar(prepared.line.text.charAt(place.coverStart + end))) ++end;
if (ie && ie_version < 9 && start == 0 && end == place.coverEnd - place.coverStart) {
if (ie && ie_version < 9 && start == 0 && end == place.coverEnd - place.coverStart)
rect = node.parentNode.getBoundingClientRect();
} else if (ie && cm.options.lineWrapping) {
var rects = range(node, start, end).getClientRects();
if (rects.length)
rect = rects[bias == "right" ? rects.length - 1 : 0];
else
rect = nullRect;
} else {
rect = range(node, start, end).getBoundingClientRect() || nullRect;
}
else
rect = getUsefulRect(range(node, start, end).getClientRects(), bias)
if (rect.left || rect.right || start == 0) break;
end = start;
start = start - 1;
Expand Down

0 comments on commit e60f4b7

Please sign in to comment.