tiptap icon indicating copy to clipboard operation
tiptap copied to clipboard

Input rules cause issues when `renderText` produces text that is matched by the input rule

Open arrocke opened this issue 2 years ago • 10 comments

What’s the bug you are facing?

When renderText in a custom node produces text that is matched by a input rule, a position out of range error is triggered.

Our use case is to have dynamic nodes whose content is determined by an object of values at runtime. The node has a property name that it uses to pull off the value from that object. It's important for us that the text representation and the input rules are exactly the same.

Which browser was this experienced in? Are any special extensions installed?

chrome, no relevant extensions

How can we reproduce the bug on our side?

  1. Create a node with a renderText that outputs some text (ie 'custom-node')
  2. Add a node input rule that replaces the text with the node (ie /custom-node/)
  3. In the editor, type the text that matches rule, that should work
  4. Type it again, that should fail with the error

Can you provide a CodeSandbox?

This is primitive and non-sensical, but demonstrates the behavior: https://codesandbox.io/s/tender-hill-l74pkp

What did you expect to happen?

I would expect that node input rules should only process text nodes and not other custom nodes. Or at least be able to filter them out when matching.

Anything to add? (optional)

No response

Did you update your dependencies?

  • [X] Yes, I’ve updated my dependencies to use the latest version of all packages.

Are you sponsoring us?

  • [X] Yes, I’m a sponsor. 💖

arrocke avatar Jul 01 '22 19:07 arrocke

FWIW: this was working in @tiptap/[email protected], but it is broken in 2.0.0-beta.181

arrocke avatar Jul 01 '22 20:07 arrocke

@arrocke Thanks for your report. There are some known issues with the input rules. If you have a change, could you give https://github.com/ueberdosis/tiptap/pull/2941 a try and see if this is affect your case?

svenadlung avatar Jul 01 '22 20:07 svenadlung

I don't think it will. Since my custom node has renderText defined, toText will execute before $from.nodeBefore?.text does

arrocke avatar Jul 01 '22 20:07 arrocke

@svenadlung I wanted to add this here rather than create a new issue because I believe they are still related. Input rules still don't work quite right even after your most recent fix. Thanks for being prompt and getting right on that, by the way.

The new problem has to do with your fix. It seems like sometimes the same text nodes are being added to textBefore multiple times. You can see this behavior here. After the first equation, you can see a capital X. If you put dollar signs around that ($X$) you'll see that it repeats the same text several times. I have confirmed that that is the result of textBefore coming from the getTextContentFromNodes function. The iteration you are doing in there is iterating over the same nodes multiple times.

An alternative to your approach may be to just do what ProseMirror does. The equivalent line of code ProseMirror is

let textBefore = $from.parent.textBetween(Math.max(0, $from.parentOffset - MAX_MATCH), $from.parentOffset,
                                            null, "\ufffc") + text

You can see the full code in this discussion

Hope this helps the triage.

Oh, I also went back to @tiptap/[email protected] and input rules were still working correctly there.

brentmparker avatar Jul 07 '22 13:07 brentmparker

This issue is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 7 days

github-actions[bot] avatar Aug 22 '22 00:08 github-actions[bot]

Wondering if there is timeline for a fix on this? It's preventing us from upgrading to get fixes for other issues.

arrocke avatar Sep 12 '22 22:09 arrocke

This issue is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 7 days

github-actions[bot] avatar Feb 14 '23 00:02 github-actions[bot]

This issue is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 7 days

github-actions[bot] avatar May 24 '23 00:05 github-actions[bot]

This is still an issue as far as I am aware

arrocke avatar May 24 '23 00:05 arrocke