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

Fix newline stripping via insertContent #4838

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const MenuBar = () => {
<button data-test-id="html-content" onClick={() => editor.chain().insertContent(htmlContent).focus().run()}>
Insert html content
</button>
<button data-test-id="html-content-spans" onClick={() => editor.chain().insertContent('<p><b>Hello</b> <i>World</i></p>').focus().run()}>
Insert html with span tags content
</button>
<button data-test-id="text-content" onClick={() => editor.chain().insertContent(textContent).focus().run()}>
Insert text content
</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context('/src/Issues/2720/React/', () => {
context('/src/Commands/InsertContent/React/', () => {
before(() => {
cy.visit('/src/Issues/2720/React/')
})
Expand All @@ -14,6 +14,20 @@ context('/src/Issues/2720/React/', () => {
cy.get('.tiptap').should('contain.html', '<h1>Tiptap</h1><p><strong>Hello World</strong></p><p>This is a paragraph<br>with a break.</p><p>And this is some additional string content.</p>')
})

it('should keep spaces inbetween tags in html content', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.insertContent('<p><b>Hello</b> <i>World</i></p>')
cy.get('.tiptap').should('contain.html', '<p><strong>Hello</strong> <em>World</em></p>')
})
})

it('should keep empty spaces', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.insertContent(' ')
cy.get('.tiptap').should('contain.html', '<p> </p>')
})
})

it('should insert text content correctly', () => {
cy.get('button[data-test-id="text-content"]').click()

Expand All @@ -23,7 +37,7 @@ context('/src/Issues/2720/React/', () => {

it('should keep newlines in pre tag', () => {
cy.get('.tiptap').then(([{ editor }]) => {
editor.commands.setContent('<pre><code>foo\nbar</code></pre>')
editor.commands.insertContent('<pre><code>foo\nbar</code></pre>')
cy.get('.tiptap').should('contain.html', '<pre><code>foo\nbar</code></pre>')
})
})
Expand Down
102 changes: 0 additions & 102 deletions demos/src/Commands/InsertContent/Vue/index.vue

This file was deleted.

Empty file.
4 changes: 1 addition & 3 deletions packages/core/src/utilities/elementFromString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const removeWhitespaces = (node: HTMLElement) => {
for (let i = children.length - 1; i >= 0; i -= 1) {
const child = children[i]

if (child.nodeType === 3 && child.nodeValue && !/\S/.test(child.nodeValue)) {
if (child.nodeType === 3 && child.nodeValue && /^(\n\s\s|\n)$/.test(child.nodeValue)) {
node.removeChild(child)
} else if (child.nodeType === 1) {
removeWhitespaces(child as HTMLElement)
Expand All @@ -20,7 +20,5 @@ export function elementFromString(value: string): HTMLElement {

const html = new window.DOMParser().parseFromString(wrappedValue, 'text/html').body

removeWhitespaces(html)

return removeWhitespaces(html)
}
Loading