Don't remeasure the first character of a line

Previously we were ignoring the measurement cache for characters located
at `left: 0px` because `0` is evaluated as falsy in Javascript, causing
those character to be constantly re-measured.

This commit fixes it so that we explicitly check for null values when
consulting the cache.
This commit is contained in:
Antonio Scandurra
2016-09-20 15:03:41 +02:00
parent a2e8d1a53a
commit fae507d70d

View File

@@ -91,34 +91,34 @@ class LinesYardstick
lineNode = @lineNodesProvider.lineNodeForScreenRow(row)
lineId = @lineNodesProvider.lineIdForScreenRow(row)
return 0 unless lineNode?
if cachedPosition = @leftPixelPositionCache[lineId]?[column]
return cachedPosition
textNodes = @lineNodesProvider.textNodesForScreenRow(row)
textNodeStartColumn = 0
for textNode in textNodes
textNodeEndColumn = textNodeStartColumn + textNode.textContent.length
if textNodeEndColumn > column
indexInTextNode = column - textNodeStartColumn
break
if lineNode?
if @leftPixelPositionCache[lineId]?[column]?
@leftPixelPositionCache[lineId][column]
else
textNodeStartColumn = textNodeEndColumn
textNodes = @lineNodesProvider.textNodesForScreenRow(row)
textNodeStartColumn = 0
for textNode in textNodes
textNodeEndColumn = textNodeStartColumn + textNode.textContent.length
if textNodeEndColumn > column
indexInTextNode = column - textNodeStartColumn
break
else
textNodeStartColumn = textNodeEndColumn
if textNode?
indexInTextNode ?= textNode.textContent.length
lineOffset = lineNode.getBoundingClientRect().left
if indexInTextNode is 0
leftPixelPosition = @clientRectForRange(textNode, 0, 1).left
else
leftPixelPosition = @clientRectForRange(textNode, 0, indexInTextNode).right
leftPixelPosition -= lineOffset
if textNode?
indexInTextNode ?= textNode.textContent.length
lineOffset = lineNode.getBoundingClientRect().left
if indexInTextNode is 0
leftPixelPosition = @clientRectForRange(textNode, 0, 1).left
else
leftPixelPosition = @clientRectForRange(textNode, 0, indexInTextNode).right
leftPixelPosition -= lineOffset
@leftPixelPositionCache[lineId] ?= {}
@leftPixelPositionCache[lineId][column] = leftPixelPosition
leftPixelPosition
@leftPixelPositionCache[lineId] ?= {}
@leftPixelPositionCache[lineId][column] = leftPixelPosition
leftPixelPosition
else
0
else
0