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

Default autohighlightCallback causes freeze and "Maximum call stack size exceeded" #21

Open
jcv8000 opened this issue Sep 14, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@jcv8000
Copy link

jcv8000 commented Sep 14, 2021

My users and I noticed this issue in my program Codex, that when you create a code block with a language it works fine, but when you create a code block with no language to be auto-highlighted, it freezes the program for 10+ seconds and spits out an Uncaught RangeError: Maximum call stack size exceeded error when creating and when typing. One user reported that this caused his entire code block to be erased and he couldn't get it back using Undo

To make sure this wasn't something in my program causing this I made a completely barebones empty project with electron, prosemirror-example-setup, prosemirror-highlightjs(0.7.1), and highlightjs(11.2.0) and it does the same thing.

The solution on my end was to disable the autohighlightCallback all together in getHighlightDecorations.ts:

// if we autohighlighted and have a callback set, call it
if (!language && options?.autohighlightCallback) {

    // COMMENTING OUT THIS LINE 
    // options.autohighlightCallback(b.node, b.pos, result.language);

}

Here's the error message:

C:\Users\jcv8000\Desktop\hljstest\node_modules\highlight.js\lib\core.js:2184 Uncaught RangeError: Maximum call stack size exceeded
    at Array.sort (<anonymous>)
    at Object.highlightAuto (C:\Users\jcv8000\Desktop\hljstest\node_modules\highlight.js\lib\core.js:2184)
    at C:\Users\jcv8000\Desktop\hljstest\node_modules\prosemirror-highlightjs\dist\index.js:1
    at Array.forEach (<anonymous>)
    at r (C:\Users\jcv8000\Desktop\hljstest\node_modules\prosemirror-highlightjs\dist\index.js:1)
    at d (C:\Users\jcv8000\Desktop\hljstest\node_modules\prosemirror-highlightjs\dist\index.js:1)
    at Plugin.apply (C:\Users\jcv8000\Desktop\hljstest\node_modules\prosemirror-highlightjs\dist\index.js:1)
    at EditorState.applyInner (C:\Users\jcv8000\Desktop\hljstest\node_modules\prosemirror-state\dist\index.js:871)
    at EditorState.applyTransaction (C:\Users\jcv8000\Desktop\hljstest\node_modules\prosemirror-state\dist\index.js:835)
    at EditorState.apply (C:\Users\jcv8000\Desktop\hljstest\node_modules\prosemirror-state\dist\index.js:811)
@b-kelly
Copy link
Owner

b-kelly commented Sep 15, 2021

I was unable to reproduce the issue you describe locally. Can you perhaps give me some more info on your setup or a sample project + input that is giving this issue? I'm also a tad surprised that commenting out the autohighlightCallback did the trick for you, since the exception log you posted has the stack overflow happening in the highlight.js library, not mine.

Either way, I did some diving in my code around this line and came up with a fix for an issue that seems like it might happen in theory, but I couldn't actually get to trigger locally. The theory I came up with was that if hljs.autoHighlight returns no language, then autohighlightCallback triggers, updates the prosemirror node with an empty language param, which triggers a redraw, which calls the callback with an empty language, which triggers an update/redraw... and so forth.

Can you check to see if the fix included in version 0.7.2 fixes your issue? If not, please provide a sample setup so I can dig into this a bit more.

@b-kelly b-kelly added the bug Something isn't working label Sep 15, 2021
@jcv8000
Copy link
Author

jcv8000 commented Sep 15, 2021

Thanks for taking a look at this. The 0.7.2 patch didn't seem to fix the issue.

I uploaded the test project I was using here: https://github.com/jcv8000/pmhljs-freeze-test

The pm-example-setup.js file is a direct copy of the prosemirror-example-setup package's dist/index.js with highlightPlugin added to the plugins list. pm-schema.js is a copy of the basic schema, I just added the hljs class to the code block node

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

No branches or pull requests

2 participants