Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RangeError: Invalid position 4 in document of length 3 #1

Closed
josdejong opened this issue Jan 23, 2024 · 5 comments
Closed

RangeError: Invalid position 4 in document of length 3 #1

josdejong opened this issue Jan 23, 2024 · 5 comments
Assignees

Comments

@josdejong
Copy link

In josdejong/svelte-jsoneditor#381 we came across an issue which seems to originate in codemirror-wrapped-line-indent.

When creating a CodeMirror editor, and directly dispatching an update replacing the contents with new contents with a larger size, an error is thrown:

RangeError: Invalid position 4 in document of length 3
    lineAt index.cjs:18
    getVisibleLines index.js:59
    addStyleToBuilder index.js:42
    read index.js:33
    measured index.cjs:6226
    measure index.cjs:6224
    measureScheduled index.cjs:6344
    requestMeasure index.cjs:6344
    <anonymous> index.cjs:7087
    fromClass index.cjs:1802
    update index.cjs:1821
    EditorView index.cjs:6003
    ...

I was able to reproduce the issue with the following minimal code example, you can try it out in codesandbox.io/p/sandbox/pedantic-scott-4gf59f

import { basicSetup, EditorView } from "codemirror";
import { wrappedLineIndent } from "codemirror-wrapped-line-indent";

const editor = new EditorView({
  doc: "123",
  extensions: [
    basicSetup,
    wrappedLineIndent, // remove this extension -> no error anymore
  ],
  parent: document.getElementById("app"),
});

editor.dispatch({
  changes: {
    from: 0,
    to: editor.state.doc.length,
    insert: "12345",
  },
});

The error disappears when removing wrappedLineIndent from the list with extensions, so it looks like the error originates there. Any idea on how to solve this?

@fauzi9331
Copy link
Owner

@josdejong thank you for reporting this issue. It seems that in file src/index.ts the this.addStyleToBuilder call in line 45 is passed an old state parameter. It's called inside a requestMeasure to calculate initialPaddingLeft from DOM. So the view may have been updated while reading the DOM. To solve this we need to pass the new state to this.addStyleToBuilder that can be gotten from measure (which I found is actually an EditorView). So line 45 should become

this.addStyleToBuilder(builder, measure.state, this.initialPaddingLeft)

@josdejong
Copy link
Author

That makes sense. Can you publish a fix for this?

@fauzi9331
Copy link
Owner

@josdejong I have published the fix. Could you check at your end?

@fauzi9331 fauzi9331 self-assigned this Jan 24, 2024
@josdejong
Copy link
Author

That's good news, thanks! I can't reproduce the issue anymore. I've published a new version of svelte-jsoneditor with this fix, awaiting feedback.

@josdejong
Copy link
Author

The issue is indeed resolved. Thanks again!

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

No branches or pull requests

2 participants