Skip to content

Commit 79d192a

Browse files
content: Filter negative margin styles if present on a vlist row
This fixes a bug where if negative margin on a vlist row is present the widget side code would hit an assert. Displaying the error (red screen) in debug mode, but in release mode the negative padding would be applied twice, once by `_KatexNegativeMargin` and another by `Padding` widget.
1 parent 44da4e2 commit 79d192a

File tree

3 files changed

+76
-9
lines changed

3 files changed

+76
-9
lines changed

lib/model/katex.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ class _KatexParser {
332332
}
333333
final pstrutHeight = pstrutStyles.heightEm ?? 0;
334334

335-
KatexSpanNode innerSpanNode = KatexSpanNode(
336-
styles: styles,
337-
text: null,
338-
nodes: _parseChildSpans(otherSpans));
335+
final KatexSpanNode innerSpanNode;
339336

340337
final marginRightEm = styles.marginRightEm;
341338
final marginLeftEm = styles.marginLeftEm;
@@ -346,11 +343,17 @@ class _KatexParser {
346343
innerSpanNode = KatexSpanNode(
347344
styles: KatexSpanStyles(),
348345
text: null,
349-
nodes: [
350-
KatexNegativeMarginNode(
351-
leftOffsetEm: marginLeftEm,
352-
nodes: [innerSpanNode]),
353-
]);
346+
nodes: [KatexNegativeMarginNode(
347+
leftOffsetEm: marginLeftEm,
348+
nodes: [KatexSpanNode(
349+
styles: styles.filter(marginLeftEm: false),
350+
text: null,
351+
nodes: _parseChildSpans(otherSpans))])]);
352+
} else {
353+
innerSpanNode = KatexSpanNode(
354+
styles: styles,
355+
text: null,
356+
nodes: _parseChildSpans(otherSpans));
354357
}
355358

356359
rows.add(KatexVlistRowNode(

test/model/content_test.dart

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,65 @@ class ContentExample {
12801280
]),
12811281
]);
12821282

1283+
static const mathBlockKatexNegativeMarginsOnVlistRow = ContentExample(
1284+
'math block, KaTeX negative margins on a vlist row',
1285+
'```math\nX_n\n```',
1286+
'<p>'
1287+
'<span class="katex-display"><span class="katex">'
1288+
'<span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><semantics><mrow><msub><mi>X</mi><mi>n</mi></msub></mrow><annotation encoding="application/x-tex">X_n</annotation></semantics></math></span>'
1289+
'<span class="katex-html" aria-hidden="true">'
1290+
'<span class="base">'
1291+
'<span class="strut" style="height:0.8333em;vertical-align:-0.15em;"></span>'
1292+
'<span class="mord">'
1293+
'<span class="mord mathnormal" style="margin-right:0.07847em;">X</span>'
1294+
'<span class="msupsub">'
1295+
'<span class="vlist-t vlist-t2">'
1296+
'<span class="vlist-r">'
1297+
'<span class="vlist" style="height:0.1514em;">'
1298+
'<span style="top:-2.55em;margin-left:-0.0785em;margin-right:0.05em;">'
1299+
'<span class="pstrut" style="height:2.7em;"></span>'
1300+
'<span class="sizing reset-size6 size3 mtight">'
1301+
'<span class="mord mathnormal mtight">n</span></span></span></span>'
1302+
'<span class="vlist-s">​</span></span>'
1303+
'<span class="vlist-r">'
1304+
'<span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span></span></span></span></span></p>', [
1305+
MathBlockNode(texSource: 'X_n', nodes: [
1306+
KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
1307+
KatexStrutNode(heightEm: 0.8333, verticalAlignEm: -0.15),
1308+
KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
1309+
KatexSpanNode(
1310+
styles: KatexSpanStyles(
1311+
marginRightEm: 0.07847,
1312+
fontFamily: 'KaTeX_Math', fontStyle: KatexSpanFontStyle.italic),
1313+
text: 'X', nodes: null),
1314+
KatexSpanNode(
1315+
styles: KatexSpanStyles(textAlign: KatexSpanTextAlign.left),
1316+
text: null, nodes: [
1317+
KatexVlistNode(rows: [
1318+
KatexVlistRowNode(
1319+
verticalOffsetEm: -2.55 + 2.7,
1320+
node: KatexSpanNode(styles: KatexSpanStyles(), text: null, nodes: [
1321+
KatexNegativeMarginNode(leftOffsetEm: -0.0785, nodes: [
1322+
KatexSpanNode(
1323+
styles: KatexSpanStyles(marginRightEm: 0.05),
1324+
text: null, nodes: [
1325+
KatexSpanNode(
1326+
styles: KatexSpanStyles(fontSizeEm: 0.7), // .reset-size6.size3
1327+
text: null, nodes: [
1328+
KatexSpanNode(
1329+
styles: KatexSpanStyles(fontFamily: 'KaTeX_Math', fontStyle: KatexSpanFontStyle.italic),
1330+
text: 'n', nodes: null),
1331+
]),
1332+
]),
1333+
]),
1334+
])),
1335+
]),
1336+
]),
1337+
]),
1338+
]),
1339+
]),
1340+
]);
1341+
12831342
static const imageSingle = ContentExample(
12841343
'single image',
12851344
// https://chat.zulip.org/#narrow/stream/7-test-here/topic/Thumbnails/near/1900103
@@ -2376,6 +2435,7 @@ void main() async {
23762435
testParseExample(ContentExample.mathBlockKatexRaisebox);
23772436
testParseExample(ContentExample.mathBlockKatexNegativeMargin);
23782437
testParseExample(ContentExample.mathBlockKatexLogo);
2438+
testParseExample(ContentExample.mathBlockKatexNegativeMarginsOnVlistRow);
23792439

23802440
testParseExample(ContentExample.imageSingle);
23812441
testParseExample(ContentExample.imageSingleNoDimensions);

test/widgets/content_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,10 @@ void main() {
630630
('E', Offset(31.63, 14.52), Size(14.0, 25.0)),
631631
('X', Offset(43.06, 9.85), Size(15.42, 25.0)),
632632
]),
633+
(ContentExample.mathBlockKatexNegativeMarginsOnVlistRow, skip: true, [
634+
('X', Offset(0.00, 7.04), Size(17.03, 25.00)),
635+
('n', Offset(17.03, 15.90), Size(8.63, 17.00)),
636+
]),
633637
];
634638

635639
for (final testCase in testCases) {

0 commit comments

Comments
 (0)