Skip to content

Conversation

@TaduJR
Copy link
Contributor

@TaduJR TaduJR commented Jul 31, 2025

Fixes #716

Details

This PR fixes a bug where MarkdownTextInput with multiline={false} incorrectly inserts spaces when users select all text and type a replacement character. The issue was caused by the library treating all text input as potentially multiline during processing, regardless of the multiline prop value.

Root Cause:
The library was adding BR elements (containing \n characters) and newlines during both DOM generation and text extraction phases, even for single-line inputs. These newlines then got converted to spaces during text normalization.

Changes Made:

1. Fixed BR element addition in addTextToElement() function (parserUtils.ts)

  • Made BR element addition conditional on isMultiline parameter
  • Prevents unwanted newline characters in single-line inputs

2. Updated addTextToElement() function calls (parserUtils.ts)

  • Pass isMultiline parameter to all function calls within parseRangesToHTMLNodes()
  • Ensures multiline setting is respected throughout DOM generation

3. Enhanced parseInnerHTMLToText() function (inputUtils.ts)

  • Added isMultiline = true parameter to maintain backward compatibility
  • Made newline addition conditional in both top-level component processing and BR element parsing
  • Prevents unwanted newlines during text extraction for single-line inputs

4. Connected multiline prop in MarkdownTextInput.web.tsx

  • Pass multiline prop to parseInnerHTMLToText() function
  • Ensures the component's multiline setting flows through the entire processing chain

Impact:

  • ✅ Single-line inputs no longer get unwanted spaces
  • ✅ Multiline inputs continue to work correctly
  • ✅ Markdown highlighting preserved for both input types
  • ✅ No breaking changes to existing API
  • ✅ Backward compatible (all new parameters have defaults)

Related Issues

GH_LINK

Manual Tests

Test Scenarios Covered:

  1. Single-line text replacement:

    • Create MarkdownTextInput with multiline={false}
    • Enter text, select all, type replacement
    • Verify no extra spaces are added
  2. Multiline preservation:

    • Create MarkdownTextInput with multiline={true}
    • Test that line breaks continue to work correctly
  3. Markdown highlighting:

    • Test single-line inputs with various markdown syntax
    • Verify highlighting works without adding unwanted spaces
  4. Edge cases:

    • Empty inputs, inputs with only spaces, special characters
    • Mixed markdown content (bold, italic, etc.)
  5. Regression testing:

    • Verified existing multiline functionality remains unchanged
    • Confirmed no impact on markdown parsing or rendering

Manual Testing Steps:

  1. Open WebExample or any app using the library
  2. Create a search input with multiline={false} and type="markdown"
  3. Enter text like "type:expense group-by:reports"
  4. Select all text (Ctrl+A)
  5. Type a single character like "t"
  6. Before fix: Shows "t " (with unwanted space)
  7. After fix: Shows "t" (clean, no extra space)

@jmusial jmusial merged commit eeb0075 into Expensify:main Jul 31, 2025
5 checks passed
@os-botify
Copy link
Contributor

os-botify bot commented Jul 31, 2025

🚀 Published to npm in 0.1.299 🎉

@TaduJR TaduJR deleted the fix-unwanted-space-insertion-in-single-line-MarkdownTextInput-when-replacing-selected-text branch December 21, 2025 16:24
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.

[Bug] Unwanted space insertion in single-line MarkdownTextInput when replacing selected text

2 participants