Skip to content

Fix: cursor behavior when using custom builder for anchor tags#35

Closed
4rthurmonteiro wants to merge 3 commits intoforesightmobile:mainfrom
4rthurmonteiro:fix/link/builder
Closed

Fix: cursor behavior when using custom builder for anchor tags#35
4rthurmonteiro wants to merge 3 commits intoforesightmobile:mainfrom
4rthurmonteiro:fix/link/builder

Conversation

@4rthurmonteiro
Copy link
Copy Markdown

@4rthurmonteiro 4rthurmonteiro commented Nov 16, 2025

Fix cursor behavior when using custom builder for anchor tags

Problem

When a custom MarkdownElementBuilder is provided for anchor tags ('a'), the cursor behaves incorrectly. This happens because the code was always adding and removing _linkHandlers for anchor tags, even when a custom builder was used.

The issue occurred because:

  1. In visitElementBefore, a GestureRecognizer was always added to _linkHandlers for anchor tags, regardless of whether a custom builder existed.
  2. In visitElementAfter, the _linkHandlers was always removed when closing anchor tags, even when a custom builder was handling the link rendering.
  3. This created a conflict between the default link recognizer and the custom builder's widget, causing cursor/selection issues, especially with SelectableText or other interactive widgets.

Solution

Added checks to only manage _linkHandlers when there is no custom builder for anchor tags:

  1. Line 259: Modified the condition to only add _linkHandlers when !builders.containsKey('a')
  2. Line 549: Added a check to only remove _linkHandlers when !builders.containsKey('a')

This ensures that when a custom builder is provided for anchor tags, the default link handling is skipped, preventing conflicts and cursor issues.

Changes

  • lib/src/builder.dart: Added builder checks before adding/removing _linkHandlers for anchor tags

Testing

Tested with custom anchor tag builders that return interactive widgets (e.g., SelectableText, GestureDetector). The cursor now behaves correctly in these cases.

Summary by CodeRabbit

  • Bug Fixes

    • Custom anchor builders now take precedence over the default link handler, avoiding conflicts when custom rendering is used.
  • Refactor

    • Minor formatting and structural cleanups in link handling logic.
  • Chores

    • Package version bumped to 1.0.6 and changelog updated.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 16, 2025

Walkthrough

The PR changes link handling to skip default link creation when a custom 'a' element builder is registered and conditions link recognizer removal on the absence of a custom 'a' builder; minor formatting and parameter-list reshapes accompany the change. (≤50 words)

Changes

Cohort / File(s) Summary
Link handling with custom builder guards
lib/src/builder.dart
Adds guards to avoid creating default links for <a> when a custom 'a' builder is registered; conditions removal of the last link recognizer during anchor-close processing on the same guard; minor formatting/parameter reshapes.
Changelog update
CHANGELOG.md
Adds 1.0.6 entry describing the bugfix for custom anchor builders and link handler management.
Version bump
pubspec.yaml
Bumps package version from 1.0.5 to 1.0.6.

Sequence Diagram(s)

sequenceDiagram
    participant Parser
    participant Builder
    participant LinkRecognizer

    Parser->>Builder: encounter <a> (open)
    alt custom 'a' builder exists
        Builder-->>Parser: build using custom 'a' (skip default link creation)
        note right of Builder `#DDEBF7`: No LinkRecognizer added
    else no custom 'a' builder
        Builder->>LinkRecognizer: create/add link recognizer
        LinkRecognizer-->>Builder: added
    end

    Parser->>Builder: encounter </a> (close)
    alt custom 'a' builder exists
        Builder-->>Parser: close custom 'a' (no link removal)
    else no custom 'a' builder
        Builder->>LinkRecognizer: remove last link recognizer
        LinkRecognizer-->>Builder: removed
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Areas to review:
    • Ensure the guard accurately detects registered custom 'a' builders in all relevant builder registry/lookup paths.
    • Confirm link recognizer lifecycle is correct when multiple nested/adjacent anchors exist.
    • Check formatting changes did not introduce subtle signature/parameter behavior differences.

Poem

🐰 I hopped through code with gentle care,

When anchors asked, "Who will prepare?"
If builders custom answer true,
Default links step back—a graceful cue,
I tidy lines and bind the pair.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main fix: preventing cursor behavior issues when custom builders are used for anchor tags by avoiding conflicts with default link recognizers.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 16a300c and 6540cf0.

📒 Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • pubspec.yaml (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • CHANGELOG.md
  • pubspec.yaml

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Arthur Monteiro and others added 2 commits November 16, 2025 15:58
@4rthurmonteiro
Copy link
Copy Markdown
Author

@Prototypev1 Can you merge it?

1 workflow awaiting approval
This workflow requires approval from a maintainer. Learn more about approving workflows.

@gazreese gazreese mentioned this pull request Dec 23, 2025
@gazreese
Copy link
Copy Markdown
Member

This has now been released in version 1.0.6 - thanks @4rthurmonteiro

@gazreese gazreese closed this Dec 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants