-
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUGFIX: Fix cleanupContentBeforeCommit and add test
Fix bug in the case you had multiple paragraphs and a headline and switched to autoparagrahp: false
- Loading branch information
Showing
4 changed files
with
73 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
packages/neos-ui-ckeditor5-bindings/src/cleanupContentBeforeCommit.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// We remove opening and closing span tags that are produced by the inlineMode plugin | ||
/** @param {String} content */ | ||
export const cleanupContentBeforeCommit = content => { | ||
// TODO: remove when this is fixed: https://github.com/ckeditor/ckeditor5/issues/401 | ||
if (content.match(/^<([a-z][a-z0-9]*)\b[^>]*> <\/\1>$/)) { | ||
return ''; | ||
} | ||
|
||
if (content.includes('<neos-inline-wrapper>')) { | ||
let contentWithoutOuterInlineWrapper = content; | ||
|
||
if (content.startsWith('<neos-inline-wrapper>') && content.endsWith('</neos-inline-wrapper>')) { | ||
contentWithoutOuterInlineWrapper = content | ||
.replace(/^<neos-inline-wrapper>/, '') | ||
.replace(/<\/neos-inline-wrapper>$/, ''); | ||
} | ||
|
||
if (contentWithoutOuterInlineWrapper.includes('<neos-inline-wrapper>')) { | ||
// in the case, multiple root paragraph elements were inserted into the ckeditor (wich is currently not prevented if the html is modified from outside) | ||
// we have multiple root elements of type <neos-inline-wrapper>. We will convert all of them into spans. | ||
return content | ||
.replace(/<neos-inline-wrapper>/g, '<span>') | ||
.replace(/<\/neos-inline-wrapper>/g, '</span>'); | ||
} | ||
return contentWithoutOuterInlineWrapper; | ||
} | ||
return content; | ||
}; |
36 changes: 36 additions & 0 deletions
36
packages/neos-ui-ckeditor5-bindings/src/cleanupContentBeforeCommit.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {cleanupContentBeforeCommit} from './cleanupContentBeforeCommit' | ||
|
||
const assertCleanedUpContent = (input, expected) => { | ||
expect(cleanupContentBeforeCommit(input)).toBe(expected); | ||
} | ||
|
||
test('remove empty nbsp', () => { | ||
assertCleanedUpContent('<p> </p>', ''); | ||
assertCleanedUpContent('<span> </span>', ''); | ||
}) | ||
|
||
describe('ckeditor inline mode hack, cleanup <neos-inline-wrapper>', () => { | ||
test('noop', () => { | ||
assertCleanedUpContent('<p></p>', '<p></p>'); | ||
|
||
assertCleanedUpContent('', ''); | ||
}) | ||
|
||
test('cleanup single <neos-inline-wrapper>', () => { | ||
assertCleanedUpContent('<neos-inline-wrapper></neos-inline-wrapper>', ''); | ||
assertCleanedUpContent('<neos-inline-wrapper>foo</neos-inline-wrapper>', 'foo'); | ||
|
||
assertCleanedUpContent('<neos-inline-wrapper><span>foo</span></neos-inline-wrapper>', '<span>foo</span>'); | ||
}) | ||
|
||
test('cleanup multiple <neos-inline-wrapper>', () => { | ||
assertCleanedUpContent('<neos-inline-wrapper>foo</neos-inline-wrapper><neos-inline-wrapper>bar</neos-inline-wrapper>', '<span>foo</span><span>bar</span>'); | ||
|
||
assertCleanedUpContent('<neos-inline-wrapper>foo</neos-inline-wrapper><neos-inline-wrapper>bar</neos-inline-wrapper>', '<span>foo</span><span>bar</span>'); | ||
}) | ||
|
||
test('cleanup <neos-inline-wrapper> after other root', () => { | ||
// in the case you had multiple paragraphs and a headline and switched to autoparagrahp: false | ||
assertCleanedUpContent('<h1>foo</h1><neos-inline-wrapper>bar</neos-inline-wrapper>', '<h1>foo</h1><span>bar</span>'); | ||
}) | ||
}) |