ProseMirror / prosemirror

The ProseMirror WYSIWYM editor
http://prosemirror.net/
MIT License
7.53k stars 334 forks source link

Bug: “instanceof DecorationSet” not behaving as expected in “prosemirror-view” #1469

Closed yuri2peter closed 1 month ago

yuri2peter commented 1 month ago

The file prosemirror-view@1.33.6/dist/index.cjs uses instanceof DecorationSet to determine whether an object is of type DecorationGroup, which generally works well in most cases.

However, I encountered a situation in practical use where this instanceof DecorationSet check likely fails, ultimately leading to an error when using the emoji plugin in tiptap.

Cannot read properties of undefined (reading 'localsInner')

After debugging step by step, I confirmed that the exception occurs here:

image image

I suspect this is due to subtle differences in constructor:

image

To ensure smooth operation of my project, I temporarily resolved this issue using the following code, which works well.

function isDecorationSet(obj) {
  if (!(obj instanceof Object)) { return false; }
  return ['add', 'addInner', 'eq', 'findInner', 'forChild', 'removeInner'].every(
    t => t in obj
  );
}

I apologize for not providing a complete reproducible demo (I'm not yet proficient with the related tools.) I hope this can assist developers facing similar issues.

marijnh commented 1 month ago

Could it be you are loading multiple copies of prosemirror-view, and that is the root of this issue? You don't want that situation anyway (it wastes a lot of bandwidth and script compilation), so the solution here is to fix your dependency tree or bundler.

yuri2peter commented 1 month ago

Could it be you are loading multiple copies of prosemirror-view, and that is the root of this issue? You don't want that situation anyway (it wastes a lot of bandwidth and script compilation), so the solution here is to fix your dependency tree or bundler.

@marijnh "It shouldn't generate additional copies; I've locked its version in package.json."

marijnh commented 1 month ago

Well, the fact that we use the instanceof is intentional, and not something I consider a bug. If your DecorationSet instances are failing that check, you are somehow loading multiple instances of the library, and it's up to you to figure out why.