codemirror / dev

Development repository for the CodeMirror editor project
https://codemirror.net/
Other
5.94k stars 376 forks source link

Crash when @codemirror/merge diffs a document against an empty document #1452

Closed tmcw closed 1 month ago

tmcw commented 1 month ago

Describe the issue

Repro link included, which produces the error

@codemirror-state.js:10 Uncaught RangeError: Invalid position -3 in document of length 40
    at TextLeaf.lineAt (@codemirror-state.js:10:19)
    at fromLine (@codemirror-merge.js:648:19)
    at toChunks (@codemirror-merge.js:660:30)
    at Chunk.build (@codemirror-merge.js:631:16)
    at new MergeView (@codemirror-merge.js:1192:29)
    at <anonymous>:7:12

Browser and platform

All, afaict

Reproduction link

https://codemirror.net/try/?c=aW1wb3J0IHtNZXJnZVZpZXd9IGZyb20gIkBjb2RlbWlycm9yL21lcmdlIgppbXBvcnQge0VkaXRvclZpZXcsIGJhc2ljU2V0dXB9IGZyb20gImNvZGVtaXJyb3IiCmltcG9ydCB7RWRpdG9yU3RhdGV9IGZyb20gIkBjb2RlbWlycm9yL3N0YXRlIgoKbGV0IGRvYyA9ICd4eHhcbicucmVwZWF0KDEwKQoKbGV0IHZpZXcgPSBuZXcgTWVyZ2VWaWV3KHsKICBhOiB7CiAgICBkb2MKICB9LAogIGI6IHsKICAgIGRvYzogJ1xuJwogIH0sCiAgcGFyZW50OiBkb2N1bWVudC5ib2R5Cn0p

marijnh commented 1 month ago

This was an embarrassing copy-paste error in the line-break-alignment code added in 6.7.1. Attached patch should help.