Fix #194: line-number alignment and doubled line spacing in paste viewer #202

Closed
fen wants to merge 1 commits from fix-194 into dev
Showing only changes of commit 392fa026cc - Show all commits
+13 -7
View File
@@ -12,8 +12,10 @@
return document.documentElement.hasAttribute('data-wrap');
};
// Wrap each logical line (split on newline; spans never contain newlines
// because HighlightCode highlights per line) in a .codeline block.
// #194: join with NO separator. .codeline is display:block, so a '\n'
// text node between spans is an extra line box in the white-space:pre
// container — that doubled the visual line spacing and desynced the
// gutter in BOTH modes.
function splitLines() {
var html = body.innerHTML;
var parts = html.split('\n');
@@ -21,13 +23,17 @@
for (var i = 0; i < parts.length; i++) {
out.push('<span class="codeline">' + parts[i] + '</span>');
}
body.innerHTML = out.join('\n');
body.innerHTML = out.join('');
}
function renumber() {
var lines = body.querySelectorAll('.codeline');
// #194: a paste ending in '\n' produces one trailing empty .codeline;
// number only real lines (like every editor) so the count matches.
var count = lines.length;
if (count > 1 && lines[count - 1].textContent === '') count--;
var s = '';
if (wrapOn() && lines.length) {
if (wrapOn() && count) {
// Each logical line block occupies rows = height / line-height when
// wrapped; its number sits on the first row and the remaining rows get
// blank gutter lines so numbers stay aligned with line starts.
@@ -37,7 +43,7 @@
var lh = parseFloat(getComputedStyle(body).lineHeight) || 1;
gutter.textContent = '';
var frag = document.createDocumentFragment();
for (var i = 0; i < lines.length; i++) {
for (var i = 0; i < count; i++) {
var num = document.createElement('span');
num.className = 'gutline';
num.textContent = String(i + 1);
@@ -52,8 +58,8 @@
}
gutter.appendChild(frag);
} else {
for (var k = 1; k <= lines.length; k++) s += k + '\n';
gutter.textContent = lines.length ? s.slice(0, -1) : '1';
for (var k = 1; k <= count; k++) s += k + '\n';
gutter.textContent = count ? s.slice(0, -1) : '1';
}
}