From a03f2f46ee3e0797eae85e358ddcfca9169a89c1 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sat, 31 Dec 2011 17:52:47 -0700 Subject: [PATCH] Don't assume tokens match text nodes when measuring character widths --- src/lines-component.coffee | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/lines-component.coffee b/src/lines-component.coffee index 9c63bdbd6..4223811bc 100644 --- a/src/lines-component.coffee +++ b/src/lines-component.coffee @@ -72,24 +72,33 @@ LinesComponent = React.createClass {editor} = @props rangeForMeasurement = null iterator = null - iteratorIndex = -1 + charIndex = 0 for {value, scopes}, tokenIndex in tokenizedLine.tokens charWidths = editor.getScopedCharWidths(scopes) - for char, i in value + + for char in value unless charWidths[char]? - rangeForMeasurement ?= document.createRange() - iterator ?= document.createNodeIterator(lineNode, NodeFilter.SHOW_TEXT, AcceptFilter) - - while iteratorIndex < tokenIndex + unless textNode? + rangeForMeasurement ?= document.createRange() + iterator = document.createNodeIterator(lineNode, NodeFilter.SHOW_TEXT, AcceptFilter) textNode = iterator.nextNode() - iteratorIndex++ + textNodeIndex = 0 + nextTextNodeIndex = textNode.textContent.length + while nextTextNodeIndex <= charIndex + textNode = iterator.nextNode() + textNodeIndex = nextTextNodeIndex + nextTextNodeIndex = textNodeIndex + textNode.textContent.length + + i = charIndex - textNodeIndex rangeForMeasurement.setStart(textNode, i) rangeForMeasurement.setEnd(textNode, i + 1) charWidth = rangeForMeasurement.getBoundingClientRect().width editor.setScopedCharWidth(scopes, char, charWidth) + charIndex++ + @measuredLines.add(tokenizedLine) clearScopedCharWidths: ->