Fix #132: inline builder duplicates children#136
Merged
Conversation
When a custom MarkdownElementBuilder for an inline tag (e.g. `a`) returned a widget from visitElementAfterWithContext, only current.children[0] was replaced. If the element had multiple inline children — such as link text split on a `_` or `*` delimiter into separate text nodes — the remaining children were still appended to the parent, rendering the trailing text twice. Replace all children with the returned widget (..clear()..add(child)), matching the block-element path which already discards children. Behaviour is unchanged for empty/single-child elements. Adds regression tests covering `_` and `*` delimiter link text with a custom `a` builder. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014oVQtXCPb3bSLnL2eDwGrh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #132.
The bug
When a custom
MarkdownElementBuilderis registered for an inline tag (e.g.a) andvisitElementAfterWithContextreturns a non-null widget, the builder replaced onlycurrent.children[0]instead of all children. If the element had multiple inline children — which happens when link text contains a delimiter like_or*that the markdown parser splits into separate text nodes ([Text("_"), Text("hello world")]) — the leftover children were still appended to the parent, so the trailing text rendered twice.Reported by @Jess-Gabia, with @amungi running a patched fork in production.
The fix
In
lib/src/builder.dart, the inline builder path now discards all existing children and replaces them with the returned widget:The returned widget represents the entire element. This matches the block-element path, which already replaces all children — so inline and block behaviour are now consistent. Behaviour is unchanged for empty/single-child elements (which is why existing tests were unaffected and the bug went unnoticed).
Tests
Added two regression tests in
test/custom_syntax_test.dartusing a customabuilder that returns a marker widget:[_hello world](…)—_delimiter[*hello world](…)—*delimiterBoth assert the marker renders once and the link text does not leak through a second time. Verified red before / green after the fix — before the fix, a stray
RichText("hello world")is found; after, it's gone.Full suite: 373 passing (was 371),
flutter analyzeclean.🤖 Generated with Claude Code
https://claude.ai/code/session_014oVQtXCPb3bSLnL2eDwGrh