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

throw 'RangeError: Invalid position 1 in document of length 0' when calling set method with JSON value under text mode #381

Closed
cloydlau opened this issue Dec 23, 2023 · 14 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@cloydlau
Copy link
Contributor

Hello jos,
Here is the minimal reproduction: https://github.com/cloydlau/reproduction.git

@thaneuk
Copy link

thaneuk commented Jan 4, 2024

A quick work around I'm using is to create it in tree mode; then immediately call jsonEditor.updateProps({ mode: Mode.text }) to convert it to text.

@josdejong josdejong added the bug Something isn't working label Jan 10, 2024
@josdejong
Copy link
Owner

Thanks for the reproduction Cloyd, that helps.

The steps to reproduce are:

  • Create a JSON editor in text mode which is empty
  • Call editor.set({ text: '123' }), then the error occurs

The error only occurs when the new contents have a large length, i.e. to get the error again, you can call editor.set({ text: '12345' }). But the error does not occur when making the contents shorter, like editor.set({ text: '12' }).

I haven't figured out what is causing this error. When using editor.set, a new instance of the code editor (CodeMirror) is created and hence should not have any knowledge about the previous contents. Help debugging this further would be welcome.

@josdejong josdejong added the help wanted Extra attention is needed label Jan 10, 2024
@josdejong
Copy link
Owner

Fixed now in v0.21.3

@thaneuk
Copy link

thaneuk commented Jan 19, 2024

I'm still seeing the issue with vanilla-jsoneditor.

"name": "vanilla-jsoneditor"
"version": "0.21.3"

new JSONEditor({
  target: element,
  props: {
    readOnly: true,
    mainMenuBar: false,
    navigationBar: false,
    statusBar: false,
    mode: Mode.text
  }
});
RangeError: Invalid position 1 in document of length 0
    at TextLeaf.lineAt (index.js:10:19)
    at WrappedLineIndent.getVisibleLines (index.js:58:40)
    at WrappedLineIndent.addStyleToBuilder (index.js:40:35)
    at Object.read (index.js:31:30)
    at index.js:7249:34
    at Array.map (<anonymous>)
    at EditorView.measure (index.js:7247:42)
    at index.js:7382:79

@josdejong
Copy link
Owner

Thanks for testing with the latest version. I've tried to reproduce your issue but without success so far:

https://jsbin.com/pufufol/edit?html,output

Can you share more details?

@josdejong josdejong reopened this Jan 22, 2024
@thaneuk
Copy link

thaneuk commented Jan 22, 2024

I added an update to the jsbin code and got the error.

editor.update({
  json: {}
})
"RangeError: Invalid position 1 in document of length 0
    at t.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:416667)
    at e.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:879553)
    at e.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:879060)
    at Object.read (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:878894)
    at https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:620350
    at Array.map (<anonymous>)
    at e.value (https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:620320)
    at https://cdn.jsdelivr.net/npm/vanilla-jsoneditor@0.21.3/standalone.min.js:25:622924"

@thaneuk
Copy link

thaneuk commented Jan 22, 2024

I also tested with an initial value, incase it was related to using update without an initial value to start but had no luck.

.update
https://jsbin.com/carasariki/1/edit?html,console,output
.set
https://jsbin.com/yexapezasi/1/edit?html,console,output

@josdejong
Copy link
Owner

Thanks, I'll do further debugging.

@josdejong
Copy link
Owner

As a workaround you can, you can just not call .update right after the editor is created (the contents is passed to along the constructor anyway). If you need that right after construction, you can maybe add a setTimeout(() => { editor.update(...) }) for the time being.

@thaneuk
Copy link

thaneuk commented Jan 22, 2024

This is the work around I'm using

https://jsbin.com/desowehibi/1/edit?html,console,output

I have an Angular component using the editor; so it's created in the component constructor and the data is set in an @input set function. So the workaround I'm using flows best in this scenario.

@josdejong
Copy link
Owner

It looks like the issue originates from the codemirror-wrapped-line-indent extension that we use, I've reported the issue there: fauzi9331/codemirror-wrapped-line-indent#1.

@josdejong
Copy link
Owner

This should be fixed now in v0.21.4, can you give it a try @thaneuk ?

@thaneuk
Copy link

thaneuk commented Jan 25, 2024

Can confirm, not having the issue now with the latest update.

Many thanks for the attention to this.

@josdejong
Copy link
Owner

Thanks for your feedback, good to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants