Skip to content

Commit f0dd638

Browse files
committed
Detect long code blocks by rendered height, not newline count
v1.1.1 counted "\n" in textContent to find blocks over 5 lines, but SMF renders code line breaks as <br>, so textContent has no newlines and every block looked like 1 line -> nothing collapsed and the Expand button was hidden. Decide and clip purely by rendered height (5 lines + padding) instead. Verified live on forum.siduction.org: a 433px block now collapses to ~130px and the Erweitern/Verringern toggle works.
1 parent b575936 commit f0dd638

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎scripts/theme.js‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,19 @@ $(function() {
6767
// Expand link toggles it.
6868
var CODE_MAX_LINES = 5;
6969

70-
function sidCollapseCode(code, btn) {
70+
// Height of the collapsed state, in px (CODE_MAX_LINES lines + padding).
71+
// SMF renders code line breaks as <br>, so the text has no "\n" to count
72+
// — we decide and clip purely by rendered height.
73+
function sidCollapsedHeight(code) {
7174
var cs = getComputedStyle(code);
7275
var lineHeight = parseFloat(cs.lineHeight) || 20;
7376
var padTop = parseFloat(cs.paddingTop) || 0;
74-
code.style.maxHeight = Math.round(lineHeight * CODE_MAX_LINES + padTop) + 'px';
77+
var padBot = parseFloat(cs.paddingBottom) || 0;
78+
return Math.round(lineHeight * CODE_MAX_LINES + padTop + padBot);
79+
}
80+
81+
function sidCollapseCode(code, btn) {
82+
code.style.maxHeight = sidCollapsedHeight(code) + 'px';
7583
code.style.overflow = 'auto';
7684
code.classList.add('sid_code_collapsed');
7785
if (btn)
@@ -90,9 +98,8 @@ $(function() {
9098
var header = code.previousElementSibling;
9199
var btn = header ? header.querySelector('.smf_expand_code') : null;
92100

93-
// Source-line count: "longer than 5 lines" means 5 real lines.
94-
var lines = code.textContent.replace(/\s+$/, '').split('\n').length;
95-
if (lines <= CODE_MAX_LINES) {
101+
// Decide by rendered height: taller than ~5 lines collapses.
102+
if (code.scrollHeight <= sidCollapsedHeight(code) + 4) {
96103
// Short enough to show in full; no expander needed.
97104
if (btn)
98105
btn.classList.add('hidden');

0 commit comments

Comments
 (0)