diff --git a/lib/editor.js b/lib/editor.js index 40c8357..460be7f 100644 --- a/lib/editor.js +++ b/lib/editor.js @@ -384,18 +384,17 @@ Editor.prototype.size = function () { Editor._markupRegExp = /{(\/?)([\w\-,;!#]*)}/g; Editor._markupIndex = function (markup, index) { - var i = 0; - Editor._markupRegExp.lastIndex = 0; - while (index > 0) { - var match = Editor._markupRegExp.exec(markup); - if (!match || match.index >= index) { break; } - var accounted = match.index + match[0].length; - i += accounted; - if (/^(open|close)$/g.test(match[2])) { i--; } // Account for { and } which replace {open} and {close} - index -= match.index; - } - return i + index; -} + var markupLength = 0; + var textLength = markup + .replace(Editor._markupRegExp, function (match, close, tag, i) { + var replace = {open: '{', close: '}'}[tag] || ''; + if (i < index + markupLength) { markupLength += match.length - replace.length; } + return replace; + }) + .slice(0, index) + .length; + return textLength + markupLength; +}; Editor.prototype._updateCursor = function () { var self = this; @@ -419,10 +418,7 @@ Editor.prototype._editorRender = function () { line = (line.replace(Editor._lineRegExp, '') + _.repeat(' ', self.size().x).join('')) .slice(scroll.x, scroll.x + self.size().x) .replace(/[{}]/g, function (match) { - return { - '{': '{open}', - '}': '{close}' - }[match]; + return {'{': '{open}', '}': '{close}'}[match]; }); if (selection && selection.start.y <= y && y <= selection.end.y) {