Skip to content

ReactNodeViewRenderer crashes when nodes exist in initial document and renderHTML returns text #7641

@rohmann

Description

@rohmann

Affected Packages

core, react

Version(s)

latest

Bug Description

When a custom atom node meets these conditions

  • atom: true
  • Uses ReactNodeViewRenderer for its node view
  • Has a renderHTML that returns a string (text node, not an element)
  • Exists in the initial document at parse time (via parseHTML rules or content)

ProseMirror crashes with this error

TypeError: dom.hasAttribute is not a function during EditorView construction.

Codesandbox Repro

It's not clear to me if this is a TipTap or a ProseMirror issue, but I was able to workaround it. The error fires when the component mounts, and when it unmounts.

Looking at it from the ProseMirror side, maybe this is an issue with NodeViewDesc.create not handling the case where dom could be a text node.

Here's how I worked around the errors for now.

Before

addNodeView() {
  return ReactNodeViewRenderer(TemplateTagNodeView);
}

After

// Wrap `ReactNodeViewRenderer` to fix TypeError on mount
addNodeView() {
    const reactRenderer = ReactNodeViewRenderer(TemplateTagNodeView);
    return (props, ...rest) => {
      const spec = reactRenderer(props, ...rest);
      if (!spec.dom) {
        const placeholder = document.createElement('span');
        return { dom: placeholder };
      }
      return spec;
    };
  },

addProseMirrorPlugins() {
  return [
  // Keep a placeholder element to fix TypeError on unmount
    new Plugin({
      props: {
        nodeViews: {
          [this.name]: (node: ProseMirrorNode) => {
            const dom = document.createElement('span');
            dom.textContent = node.attrs.value;
            return { dom };
          },
        },
      },
    }),
  ];
},

Browser Used

Chrome

Code Example URL

https://codesandbox.io/p/sandbox/interesting-ully-826d7l?file=%2Fpackage.json%3A9%2C6-9%2C19

Expected Behavior

No type error should be thrown.

Additional Context (Optional)

No response

Dependency Updates

  • Yes, I've updated all my dependencies.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions