Skip to content

fix: make Bold and Italic toggle off on a second press - #12

Closed
bero wants to merge 1 commit into
GDKsoftware:mainfrom
bero:fix/bold-toggle-outside-selection
Closed

bero wants to merge 1 commit into
GDKsoftware:mainfrom
bero:fix/bold-toggle-outside-selection

Conversation

@bero

@bero bero commented Sep 16, 2026

Copy link
Copy Markdown

Independent of #7, #8, #9, #10 and #11. Cut from main.

The bug

Select a word, press Bold, press Bold again:

Unidentified  ->  **Unidentified**  ->  ****Unidentified****

The second press should have removed the markers.

The cause

WrapOrToggle deliberately leaves the selection on the word itself, inside the markers it has just added:

FAnchor := Start + MarkerLen;
FCaret := Start + MarkerLen + System.Length(Selected);

The toggle branch then asks whether the selected text starts and ends with the marker. In the state the first press produces it does not, so the command falls through and wraps again. The toggle only ever worked if you selected the markers yourself, which is not where the first press leaves you.

The fix

The toggle now also looks immediately outside the selection, and strips the markers it finds there.

One case is deliberately left alone. A marker that continues into more of the same character belongs to a longer run, and therefore to a different construct: the single asterisks either side of a word inside **bold** are the bold, not an italic waiting to be removed. Pressing Italic there adds its own markers and gives ***word***.

Because this lives in the shared editor model, both the VCL and FMX editors are fixed by it.

Testing

Three tests, each written before the fix and confirmed failing with the exact reported strings.

  • The real sequence: select a word, Bold, Bold, expect the word back.
  • Markers sitting just outside the selection are removed.
  • Italic on a word inside bold adds rather than eats, guarding the case above.

Suite: 1087 tests, all passing, no regressions. All three projects build clean on Win32 Debug with Delphi 12 Athens.

Verified in the running studio by toggling with the keyboard and saving after each press: wraps, unwraps, wraps again.

Wrapping leaves the selection on the word itself, inside the markers it just
added. The toggle then asked whether the selected text starts and ends with the
marker, which is false in exactly the state the first press produces, so a
second press wrapped the wrapped text:

    Unidentified -> **Unidentified** -> ****Unidentified****

The toggle now also looks immediately outside the selection, and removes the
markers it finds there.

Markers that continue into more of the same character are left alone, because
they belong to a longer run and so to a different construct. Pressing Italic on
a word inside '**bold**' therefore adds its own markers, giving '***word***',
rather than taking half the bold away.

The fix is in the shared editor model, so both the VCL and FMX editors get it.

Three tests, written first and confirmed failing with the reported strings.
@bero

bero commented Sep 16, 2026

Copy link
Copy Markdown
Author

Withdrawing this for now: it needs more work before it is worth your time. I will reopen or resubmit once it is verified end to end.

@bero bero closed this Sep 16, 2026
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.

1 participant