fix: keep trailing content out of unmatched emphasis closer (#743)

When EnableTrackTrivia() is enabled, an emphasis closer whose delimiter
run is only partially consumed (its leftover dropping below the minimum
count) kept the inlines that follow it nested inside the emphasis. The
roundtrip renderer then emitted the closing delimiters after that
trailing content, corrupting the output, for example a grid table
separator row using the "marked" (==) delimiter from EmphasisExtras.

Move the trailing content out to the outermost enclosing emphasis when
the closer can no longer be matched, mirroring the existing handling for
a fully consumed opener. The leftover closer keeps its position, so
cases without trailing content are unaffected. Adds a regression test.
This commit is contained in:
dualfroz committed 2026-09-02 11:59:02 +02:00
1 parent 165ebe1cf1
commit 83afc8fa26
2 files changed
+91

No files matched your search

@@ -419,6 +419,27 @@ public class EmphasisInlineParser : InlineParser, IPostInlineProcessor
break;
}
}
// The closer could not be fully consumed and still holds leftover delimiter
// characters that cannot match any opener. When the closer was embraced as the
// closing side of an emphasis, the inlines that follow the closer belong after
// that emphasis, not nested inside it. This is already handled when the opener is
// fully consumed, but not when the opener also keeps leftover characters. Move the
// trailing content out to the outermost enclosing emphasis so it keeps its original
// position; otherwise it is rendered before the closing delimiters, which corrupts
// round-tripped output when EnableTrackTrivia() is used (for example a grid table
// separator, #743). The closer itself keeps its position, matching the behaviour
// for a leftover closer that has no trailing content.
if (closeDelimiter.DelimiterCount > 0 && closeDelimiter.Parent is EmphasisInline closerParentEmphasis)
{
var outermostEmphasis = closerParentEmphasis;
while (outermostEmphasis.Parent is EmphasisInline ancestorEmphasis)
{
outermostEmphasis = ancestorEmphasis;
}
closeDelimiter.MoveChildrenAfter(outermostEmphasis);
}
}
}