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: Maximum call stack size exceeded - Document Workaround? #205

Open
mmichaelis opened this issue Nov 7, 2023 · 1 comment
Open
Assignees
Labels
bug Something isn't working P0 Critical Issue

Comments

@mmichaelis
Copy link

If "naively" creating custom tag mappings, you may run into this exception:

Uncaught RangeError: Maximum call stack size exceeded
    at Object.code (myHtml5Preset.ts:160:12)
    at myHtml5Preset.ts:72:72
    at k8 (utils.js:7:33)

To reproduce, create a tag-rule that generates "itself as child". For example, for parsing within CKEditor 5, we need to represent [code] as <pre><code>. If we naively adapt the code from HTML5 Preset for Blockquote:

quote: (node) => toNode('blockquote', {}, [toNode('p', {}, node.content)]),

We end up with a rule like this:

  code: (node) => toNode('pre', {}, [toNode('code', {}, node.content)]),

The problem: As parents are processed first, the newly created code will be processed afterward, so that we end up in an endless loop resulting in the RangeError given above.

Workaround:

As the above "naive mapping" may be a result of not fully understanding the internal processing in BBob, the suggestion is to actually "fix" this issue by documenting this possible workaround as "best-practice":

  code: (node) => toNode('pre', {}, [toNode('htmlCode', {}, node.content)]),
  htmlCode: (node) => toNode('code', {}, node.content),

This workaround tricks the processing order by creating an intermediate node htmlCode, that is resolved once the children of <pre> created by the original code rule are processed.

@JiLiZART JiLiZART self-assigned this Nov 14, 2023
@JiLiZART JiLiZART added bug Something isn't working P0 Critical Issue labels Nov 14, 2023
@Alteras1
Copy link
Contributor

A different workaround I've found is to add an arbitrary property to the returned node that the tag rule can detect and early exit out of.

const toNode = (tag, attrs, content = []) => ({
  tag,
  attrs,
  content,
  gen: true,
});

...

code: (node) => {
  if (node.gen) {
    return node
  }
  return toNode('pre', {}, [toNode('code', {}, node.content)]);
}

This still lets a single recursion happen, but without any major issues.

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

No branches or pull requests

3 participants