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]: Tab characters are stripped in useEditor content prop #5486

Closed
1 task done
Nantris opened this issue Aug 13, 2024 · 6 comments
Closed
1 task done

[Bug]: Tab characters are stripped in useEditor content prop #5486

Nantris opened this issue Aug 13, 2024 · 6 comments
Labels
Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug

Comments

@Nantris
Copy link
Contributor

Nantris commented Aug 13, 2024

Affected Packages

core

Version(s)

2.5.9

Bug Description

Related to #4862 - I noticed I had one test marked as skip with a TODO to re-enable once #4862 was resolved. Unfortunately this test continues to fail as setContent strips \t characters out unexpectedly.

\t is being preserved inside codeblocks, but it should be preserved everywhere

Browser Used

Chrome

Code Example URL

No response

Expected Behavior

\t is preserved

Additional Context (Optional)

These are also lost when setting the initial content of the editor.

Our old editor had \t support everywhere and this makes it impossible for us to move users to the new editor because their content would be irreversibly modified.

Dependency Updates

  • Yes, I've updated all my dependencies.
@Nantris Nantris added Category: Open Source The issue or pull reuqest is related to the open source packages of Tiptap. Type: Bug The issue or pullrequest is related to a bug labels Aug 13, 2024
@github-project-automation github-project-automation bot moved this to Triage open in Tiptap Aug 13, 2024
@nperez0111
Copy link

Can you share what specifically you are testing, because from my understanding setContent should preserve tabs now:

it('should keep newlines and tabs', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.insertContent('<p>Hello\n\tworld\n\t\thow\n\t\t\tnice.\ntest\tOK</p>')
cy.get('.tiptap').should('contain.html', '<p>Hello\n\tworld\n\t\thow\n\t\t\tnice.\ntest\tOK</p>')
})
})

@Nantris
Copy link
Contributor Author

Nantris commented Aug 14, 2024

Thanks for your reply @nperez0111.

It seems this is a discrepancy in how insertContent works versus setContent.

I just tested, and insertContent works fine to insert tab characters, but setContent does not. The editor fails to preserve \t upon reload as well, I guess because the content property of useEditor is using the same logic as setContent somewhere under the hood?

  • Works: editor.commands.insertContent('<p>\t\t\t</p>')
  • Silently fails: editor.commands.setContent('<p>\t\t\t</p>')

Tested with @tiptap/[email protected]

@nperez0111
Copy link

Ah yea so this was the compromise for preserving backwards compatibility. So for setContent you need to set a flag on preserveWhitespace for the whitespace to be respected. Looking at the source code it will be obvious what you will need to change if you set preserveWhiteSpace to "full" you'll get the same behavior as insertContentAt

https://github.com/ueberdosis/tiptap/blob/develop/packages/core/src/commands/setContent.ts

@Nantris
Copy link
Contributor Author

Nantris commented Aug 15, 2024

Ah that makes sense - thank you. I wonder though about about initially setting the editor content in useEditor? Is it possible to preserveWhiteSpace in that context? I took a very quick look around the code and didn't see anything to that effect.

I could sort of try to patch that myself by just calling setContent instead of using content, but I wonder about rendering delay and maybe other edge cases that might arise from doing so.

@Nantris Nantris changed the title [Bug]: Tab characters are stripped by setContent command [Bug]: Tab characters are stripped in useEditor content prop Aug 15, 2024
@nperez0111
Copy link

I think that you should be able to preserveWhitespace by setting parseOptions on the editor options:

this.options.parseOptions,

So it should look like this:

const editor = useEditor({
    parseOptions:{
      preserveWhitespace: 'full',
    },
    content: 'content'

@Nantris
Copy link
Contributor Author

Nantris commented Aug 16, 2024

Thank you so much for the tip @nperez0111 and apologies for overlooking it in the docs. It works great.

@Nantris Nantris closed this as completed Aug 16, 2024
@github-project-automation github-project-automation bot moved this from Triage open to Done in Tiptap Aug 16, 2024
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. Type: Bug The issue or pullrequest is related to a bug
Projects
No open projects
Archived in project
Development

No branches or pull requests

2 participants