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

[Bug]: Difference in behavior of setContent and insertContent commands #4034

Closed
2 tasks done
ashu12chi opened this issue May 10, 2023 · 8 comments
Closed
2 tasks done
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Triage: Open A new issue or pullrequest that requires triage (added by default) Type: Bug The issue or pullrequest is related to a bug

Comments

@ashu12chi
Copy link

Which packages did you experience the bug in?

core

What Tiptap version are you using?

latest

What’s the bug you are facing?

In our application, we are using insertContent and setContent command. The issue we are facing is the final output for the same content is different for the two commands.

Example to this:

const content = `<h1>Welcome to your fresh Tiptap Code Sandbox</h1>
  <p>You can create a demo for your issue inside of this sandbox and share it with us.</p>`

For above content, output of setContent is:
image
But, for insertContent it is coming with two blank lines in-between the content
image
if I pass parseOptions in insertContent command as

editor.commands.insertContent(content, {
    parseOptions: { preserveWhitespace: false }
})

It will still come with one blank line like this:
image

Ideally, behavior should be same for both the cases.

I have some observations regarding this:

  1. If I modify my content to have no new line characters like this:
const content = `<h1>Welcome to your fresh Tiptap Code Sandbox</h1><p>You can create a demo for your issue inside of this sandbox and share it with us.</p>`

Behavior will be same for both insertContent and setContent.

  1. On taking a cursory at the internal implementation of the two commands, setContent is using parseSlice and insertContent is using parse function from Prosmirror.

Since, external content can possibly have new lines in them, we want behavior of both the commands to be same (Ideally the behavior of setContent)

What browser are you using?

Chrome

Code example

https://codesandbox.io/s/green-monad-surcnd?file=/src/App.js

What did you expect to happen?

setContent and insertContent should produce same output for given content.

Anything to add? (optional)

NA

Did you update your dependencies?

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

Are you sponsoring us?

  • Yes, I’m a sponsor. 💖
@ashu12chi ashu12chi added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Triage: Open A new issue or pullrequest that requires triage (added by default) Type: Bug The issue or pullrequest is related to a bug labels May 10, 2023
@Adonis0123
Copy link

Adonis0123 commented Jul 3, 2023

In order to maintain consistency between the behaviors of "insertContent" and "setContent", this is what I did:

const htmlWithoutNewLine = html.replace(/\n/g, '');

@kaikun213
Copy link

Experiencing the same issue. Even when replacing the newline characters the commands do not produce the same results for more complex html imports.

Seems like quite a core functionality that is important to have reliable import. Is it still on the roadmap? @bdbch

@bdbch
Copy link
Contributor

bdbch commented Feb 14, 2024

Thanks for reporting. I'll take a look tomorrow

@Nantris
Copy link
Contributor

Nantris commented Feb 14, 2024

Could this have any relation to #4862?

Edit: Oh sorry, probably not given the issue's creation date - although it may be related to what @kaikun213 is saying specifically?

@bdbch
Copy link
Contributor

bdbch commented Feb 16, 2024

#4895 created a fix here which should bring setContent and insertContent in line with each other. setContent was using a different DOM parsing approach then insertContent.

@nperez0111
Copy link
Contributor

Should be resolved with the latest beta, let us know: v2.5.0-beta.5

@Nantris
Copy link
Contributor

Nantris commented Jun 25, 2024

Not OP, but I did a quick test in our editor and it looks good to me. It's always possible our custom code is interfering but I think this can be closed unless someone reports otherwise.

@Nantris
Copy link
Contributor

Nantris commented Jun 25, 2024

I'm a little confused about preserveWhitespace though. I'd expect false NOT to insert the empty line, but instead it does insert it. (at the end, not in the middle as shown in the image) Is that right? Nevermind, bad test on my part. I think we're good here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Triage: Open A new issue or pullrequest that requires triage (added by default) Type: Bug The issue or pullrequest is related to a bug
Projects
Status: Done
Development

No branches or pull requests

7 participants