Skip to content

Commit 2343e38

Browse files
knsv-botclaude
andcommitted
fix(sequence): add label box background for alt/else section titles
Section titles like "else" in alt/else blocks were rendered without a .labelBox background polygon, unlike the main loop label. When custom CSS set .loopText to white, section titles became invisible against the white SVG background. Resolves #7546 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent b4d7dcf commit 2343e38

4 files changed

Lines changed: 184 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mermaid': patch
3+
---
4+
5+
fix: add background box behind alt/else section title labels in sequence diagrams

‎cypress/integration/rendering/sequencediagram.spec.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,4 +1228,26 @@ describe('Sequence diagram', () => {
12281228
);
12291229
});
12301230
});
1231+
1232+
it('should render alt/else section titles with label box backgrounds', () => {
1233+
imgSnapshotTest(
1234+
`
1235+
sequenceDiagram
1236+
participant Alice
1237+
participant Bob
1238+
opt Outer
1239+
alt Command A
1240+
Alice->>Bob: Request A
1241+
else Command B
1242+
Alice->>Bob: Request B
1243+
else Command C
1244+
Alice->>Bob: Request C
1245+
end
1246+
end
1247+
`,
1248+
{
1249+
themeCSS: '.loopText { fill: #ffffff !important; } .labelBox { fill: red !important; }',
1250+
}
1251+
);
1252+
});
12311253
});

‎packages/mermaid/src/diagrams/sequence/svgDraw.js‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,24 @@ export const drawLoop = async function (elem, loopModel, labelText, conf, msg) {
14561456
if (loopModel.sectionTitles !== undefined) {
14571457
for (const [idx, item] of Object.entries(loopModel.sectionTitles)) {
14581458
if (item.message) {
1459+
// Draw a labelBox background behind the section title (e.g., "else")
1460+
let sectionLabelTxt = svgDrawCommon.getTextObj();
1461+
sectionLabelTxt.text = item.message;
1462+
sectionLabelTxt.x = loopModel.startx;
1463+
sectionLabelTxt.y = loopModel.sections[idx].y;
1464+
sectionLabelTxt.fontFamily = fontFamily;
1465+
sectionLabelTxt.fontSize = fontSize;
1466+
sectionLabelTxt.fontWeight = fontWeight;
1467+
sectionLabelTxt.anchor = 'middle';
1468+
sectionLabelTxt.valign = 'middle';
1469+
sectionLabelTxt.tspan = false;
1470+
sectionLabelTxt.width = Math.max(labelBoxWidth ?? 0, 50);
1471+
sectionLabelTxt.height = labelBoxHeight + (conf.look === 'neo' ? 15 : 0) || 20;
1472+
sectionLabelTxt.textMargin = boxTextMargin;
1473+
sectionLabelTxt.class = 'labelText';
1474+
1475+
drawLabel(g, sectionLabelTxt);
1476+
14591477
txt.text = item.message;
14601478
txt.x = loopModel.startx + (loopModel.stopx - loopModel.startx) / 2;
14611479
txt.y = loopModel.sections[idx].y + boxMargin + boxTextMargin;

‎packages/mermaid/src/diagrams/sequence/svgDraw.spec.js‎

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,145 @@ describe('svgDraw', function () {
189189
expect(text.style).toHaveBeenCalledWith('font-weight', '500');
190190
});
191191
});
192+
describe('drawLoop', function () {
193+
/**
194+
* Extended MockD3 that supports getBBox (needed by drawLoop's text measurement).
195+
*/
196+
const MockD3WithBBox = (name, parent) => {
197+
const children = [];
198+
const elem = {
199+
get __children() {
200+
return children;
201+
},
202+
get __name() {
203+
return name;
204+
},
205+
get __parent() {
206+
return parent;
207+
},
208+
_groups: [[{ getBBox: () => ({ x: 0, y: 0, width: 100, height: 20 }) }]],
209+
};
210+
elem.append = (childName) => {
211+
const mockElem = MockD3WithBBox(childName, elem);
212+
children.push(mockElem);
213+
return mockElem;
214+
};
215+
elem.lower = vi.fn(() => elem);
216+
elem.attr = vi.fn(() => elem);
217+
elem.text = vi.fn(() => elem);
218+
elem.style = vi.fn(() => elem);
219+
elem.node = vi.fn(() => ({
220+
querySelectorAll: (selector) => {
221+
// Collect all matching children recursively
222+
const results = [];
223+
const walk = (node) => {
224+
for (const child of node.__children || []) {
225+
if (selector === '.labelBox' && child.__name === 'polygon') {
226+
// Check if it was called with class labelBox
227+
const calls = child.attr.mock.calls;
228+
if (calls.some((c) => c[0] === 'class' && c[1] === 'labelBox')) {
229+
results.push(child);
230+
}
231+
}
232+
walk(child);
233+
}
234+
};
235+
walk(elem);
236+
return results;
237+
},
238+
}));
239+
return elem;
240+
};
241+
242+
it('should create a labelBox polygon for the main loop label', async function () {
243+
const svg = MockD3WithBBox('svg');
244+
const loopModel = {
245+
startx: 10,
246+
starty: 10,
247+
stopx: 200,
248+
stopy: 200,
249+
title: 'Yes',
250+
wrap: false,
251+
sectionTitles: [],
252+
sections: [],
253+
};
254+
const conf = {
255+
boxMargin: 10,
256+
boxTextMargin: 5,
257+
labelBoxHeight: 20,
258+
labelBoxWidth: 50,
259+
messageFontFamily: 'Arial',
260+
messageFontSize: 12,
261+
messageFontWeight: 'normal',
262+
};
263+
const msg = { id: 'test1' };
264+
265+
const g = await svgDraw.drawLoop(svg, loopModel, 'alt', conf, msg);
266+
const labelBoxes = g.node().querySelectorAll('.labelBox');
267+
expect(labelBoxes.length).toBeGreaterThanOrEqual(1);
268+
});
269+
270+
it('should create labelBox polygons for section titles (else labels)', async function () {
271+
const svg = MockD3WithBBox('svg');
272+
const loopModel = {
273+
startx: 10,
274+
starty: 10,
275+
stopx: 200,
276+
stopy: 200,
277+
title: 'Yes',
278+
wrap: false,
279+
sectionTitles: [{ message: 'else Command' }],
280+
sections: [{ y: 100, height: 50 }],
281+
};
282+
const conf = {
283+
boxMargin: 10,
284+
boxTextMargin: 5,
285+
labelBoxHeight: 20,
286+
labelBoxWidth: 50,
287+
messageFontFamily: 'Arial',
288+
messageFontSize: 12,
289+
messageFontWeight: 'normal',
290+
};
291+
const msg = { id: 'test2' };
292+
293+
const g = await svgDraw.drawLoop(svg, loopModel, 'alt', conf, msg);
294+
const labelBoxes = g.node().querySelectorAll('.labelBox');
295+
// Should have 2 labelBox elements: one for "alt" label, one for "else Command"
296+
expect(labelBoxes.length).toBe(2);
297+
});
298+
299+
it('should create labelBox for each section title when multiple exist', async function () {
300+
const svg = MockD3WithBBox('svg');
301+
const loopModel = {
302+
startx: 10,
303+
starty: 10,
304+
stopx: 300,
305+
stopy: 300,
306+
title: 'Yes',
307+
wrap: false,
308+
sectionTitles: [{ message: 'else A' }, { message: 'else B' }],
309+
sections: [
310+
{ y: 100, height: 50 },
311+
{ y: 200, height: 50 },
312+
],
313+
};
314+
const conf = {
315+
boxMargin: 10,
316+
boxTextMargin: 5,
317+
labelBoxHeight: 20,
318+
labelBoxWidth: 50,
319+
messageFontFamily: 'Arial',
320+
messageFontSize: 12,
321+
messageFontWeight: 'normal',
322+
};
323+
const msg = { id: 'test3' };
324+
325+
const g = await svgDraw.drawLoop(svg, loopModel, 'alt', conf, msg);
326+
const labelBoxes = g.node().querySelectorAll('.labelBox');
327+
// 1 main label + 2 section titles = 3 labelBox elements
328+
expect(labelBoxes.length).toBe(3);
329+
});
330+
});
192331
describe('drawBackgroundRect', function () {
193332
it('should append a rect before the previous element within a given bound', function () {
194333
const svg = MockD3('svg');

0 commit comments

Comments
 (0)