Skip to content

Commit 2c913e5

Browse files
committed
Make hack to break up adjacent spaces work for single-space tokens
Closes codemirror#4087
1 parent fb3e1dc commit 2c913e5

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

lib/codemirror.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6944,6 +6944,7 @@
69446944
var content = elt("span", null, null, webkit ? "padding-right: .1px" : null);
69456945
var builder = {pre: elt("pre", [content], "CodeMirror-line"), content: content,
69466946
col: 0, pos: 0, cm: cm,
6947+
trailingSpace: false,
69476948
splitSpaces: (ie || webkit) && cm.getOption("lineWrapping")};
69486949
lineView.measure = {};
69496950

@@ -7005,7 +7006,7 @@
70057006
// the line map. Takes care to render special characters separately.
70067007
function buildToken(builder, text, style, startStyle, endStyle, title, css) {
70077008
if (!text) return;
7008-
var displayText = builder.splitSpaces ? text.replace(/ {3,}/g, splitSpaces) : text;
7009+
var displayText = builder.splitSpaces ? splitSpaces(text, builder.trailingSpace) : text
70097010
var special = builder.cm.state.specialChars, mustWrap = false;
70107011
if (!special.test(text)) {
70117012
builder.col += text.length;
@@ -7050,6 +7051,7 @@
70507051
builder.pos++;
70517052
}
70527053
}
7054+
builder.trailingSpace = displayText.charCodeAt(text.length - 1) == 32
70537055
if (style || startStyle || endStyle || mustWrap || css) {
70547056
var fullStyle = style || "";
70557057
if (startStyle) fullStyle += startStyle;
@@ -7061,11 +7063,17 @@
70617063
builder.content.appendChild(content);
70627064
}
70637065

7064-
function splitSpaces(old) {
7065-
var out = " ";
7066-
for (var i = 0; i < old.length - 2; ++i) out += i % 2 ? " " : "\u00a0";
7067-
out += " ";
7068-
return out;
7066+
function splitSpaces(text, trailingBefore) {
7067+
if (text.length > 1 && !/ /.test(text)) return text
7068+
var spaceBefore = trailingBefore, result = ""
7069+
for (var i = 0; i < text.length; i++) {
7070+
var ch = text.charAt(i)
7071+
if (ch == " " && spaceBefore && (i == text.length - 1 || text.charCodeAt(i + 1) == 32))
7072+
ch = "\u00a0"
7073+
result += ch
7074+
spaceBefore = ch == " "
7075+
}
7076+
return result
70697077
}
70707078

70717079
// Work around nonsense dimensions being reported for stretches of
@@ -7102,6 +7110,7 @@
71027110
builder.content.appendChild(widget);
71037111
}
71047112
builder.pos += size;
7113+
builder.trailingSpace = false
71057114
}
71067115

71077116
// Outputs a number of spans to make up a line, taking highlighting

0 commit comments

Comments
 (0)