Skip to content

fix: clamp markdown cursor range offsets - #770

Merged
jmusial merged 2 commits into
Expensify:mainfrom
x-dev90:fix/clamp-cursor-range-offset
Jul 29, 2026
Merged

fix: clamp markdown cursor range offsets#770
jmusial merged 2 commits into
Expensify:mainfrom
x-dev90:fix/clamp-cursor-range-offset

Conversation

@x-dev90

@x-dev90 x-dev90 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Details

This fixes a web crash during markdown cursor restoration. setCursorPosition clamped the cursor index against the markdown tree length, but then passed the derived offset directly to the actual DOM node used by Range.setStart / Range.setEnd.

When the markdown tree and rendered DOM are temporarily out of sync, the offset can be valid for the tree but invalid for the DOM node, causing IndexSizeError.

This change clamps the final offset against the actual Range container node bounds before applying it.

Related Issues

Expensify/App#95347

Manual Tests

  1. Use the IOU Description repro from Expensify/App.
  2. Type text in the markdown description input.
  3. Force a stale tree/DOM state by saving a caret, blurring the input, emptying the rendered text node, then focusing/clicking the input again.
  4. Verify no IndexSizeError is thrown and the editor remains usable.

Linked PRs

Expensify/App PR: Expensify/App#96638

@github-actions

github-actions Bot commented Jul 24, 2026

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@x-dev90

x-dev90 commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

exfy-clabot Bot added a commit to Expensify/CLA that referenced this pull request Jul 24, 2026
@tomekzaw
tomekzaw requested a review from war-in July 24, 2026 14:32
@mountiny
mountiny requested review from mountiny and suneox July 25, 2026 15:09
@mountiny

Copy link
Copy Markdown

@suneox can you please test the change in the App?

@suneox

suneox commented Jul 26, 2026

Copy link
Copy Markdown

@suneox can you please test the change in the App?

Yes, I'll take a look at this one soon

@suneox suneox left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changeset LGTM, the functionality still works as expected

@jmusial

jmusial commented Jul 27, 2026

Copy link
Copy Markdown
Collaborator

will review & test today / tomorrow

@jmusial jmusial left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, just one comment, will merge after it's resolved.

return node.nodeType === Node.TEXT_NODE ? (node.textContent ?? '').length : node.childNodes.length;
}

function getClampedRangeOffset(node: ChildNode, offset: number): number {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to have jsdoc here with some context.

@x-dev90

x-dev90 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@jmusial Thanks for the feedback and it has been resolved, you can take another look

@jmusial
jmusial merged commit aee53e2 into Expensify:main Jul 29, 2026
8 checks passed
@jmusial

jmusial commented Jul 29, 2026

Copy link
Copy Markdown
Collaborator

@jmusial Thanks for the feedback and it has been resolved, you can take another look

Awesome, merged. Thank you for your contribution :)

@os-botify

os-botify Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

🚀 Published to npm in 0.1.334 🎉

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.

4 participants