diff --git a/lib/codemirror.js b/lib/codemirror.js index 570eba1b35..476ab2e6e5 100644 --- a/lib/codemirror.js +++ b/lib/codemirror.js @@ -6944,6 +6944,7 @@ var content = elt("span", null, null, webkit ? "padding-right: .1px" : null); var builder = {pre: elt("pre", [content], "CodeMirror-line"), content: content, col: 0, pos: 0, cm: cm, + trailingSpace: false, splitSpaces: (ie || webkit) && cm.getOption("lineWrapping")}; lineView.measure = {}; @@ -7005,7 +7006,7 @@ // the line map. Takes care to render special characters separately. function buildToken(builder, text, style, startStyle, endStyle, title, css) { if (!text) return; - var displayText = builder.splitSpaces ? text.replace(/ {3,}/g, splitSpaces) : text; + var displayText = builder.splitSpaces ? splitSpaces(text, builder.trailingSpace) : text var special = builder.cm.state.specialChars, mustWrap = false; if (!special.test(text)) { builder.col += text.length; @@ -7050,6 +7051,7 @@ builder.pos++; } } + builder.trailingSpace = displayText.charCodeAt(text.length - 1) == 32 if (style || startStyle || endStyle || mustWrap || css) { var fullStyle = style || ""; if (startStyle) fullStyle += startStyle; @@ -7061,11 +7063,17 @@ builder.content.appendChild(content); } - function splitSpaces(old) { - var out = " "; - for (var i = 0; i < old.length - 2; ++i) out += i % 2 ? " " : "\u00a0"; - out += " "; - return out; + function splitSpaces(text, trailingBefore) { + if (text.length > 1 && !/ /.test(text)) return text + var spaceBefore = trailingBefore, result = "" + for (var i = 0; i < text.length; i++) { + var ch = text.charAt(i) + if (ch == " " && spaceBefore && (i == text.length - 1 || text.charCodeAt(i + 1) == 32)) + ch = "\u00a0" + result += ch + spaceBefore = ch == " " + } + return result } // Work around nonsense dimensions being reported for stretches of @@ -7102,6 +7110,7 @@ builder.content.appendChild(widget); } builder.pos += size; + builder.trailingSpace = false } // Outputs a number of spans to make up a line, taking highlighting