tiptap
tiptap copied to clipboard
[Bug]: Difference in behavior of setContent and insertContent commands
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:
But, for
insertContent
it is coming with two blank lines in-between the content
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:
Ideally, behavior should be same for both the cases.
I have some observations regarding this:
- 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
.
- On taking a cursory at the internal implementation of the two commands,
setContent
is using parseSlice andinsertContent
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?
- [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. 💖
In order to maintain consistency between the behaviors of "insertContent" and "setContent", this is what I did:
const htmlWithoutNewLine = html.replace(/\n/g, '');
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
Thanks for reporting. I'll take a look tomorrow
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?
https://github.com/ueberdosis/tiptap/pull/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.
Should be resolved with the latest beta, let us know: v2.5.0-beta.5
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.
~~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.