Skip to content

fix: prevent rte from trimming whitespace on delta conversion (#6651) #6662

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

Merged
merged 3 commits into from
Oct 17, 2023
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
19 changes: 19 additions & 0 deletions packages/rich-text-editor/src/vaadin-rich-text-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,26 @@ class RichTextEditor extends ElementMixin(ThemableMixin(PolymerElement)) {
* @param {string} htmlValue
*/
dangerouslySetHtmlValue(htmlValue) {
const whitespaceCharacters = {
'\t': '__VAADIN_RICH_TEXT_EDITOR_TAB',
' ': '__VAADIN_RICH_TEXT_EDITOR_DOUBLE_SPACE',
};
// Replace whitespace characters with placeholders before the Delta conversion to prevent Quill from trimming them
Object.entries(whitespaceCharacters).forEach(([character, replacement]) => {
htmlValue = htmlValue.replaceAll(/>[^<]*</gu, (match) => match.replaceAll(character, replacement)); // NOSONAR
});

const deltaFromHtml = this._editor.clipboard.convert(htmlValue);

// Restore whitespace characters after the conversion
Object.entries(whitespaceCharacters).forEach(([character, replacement]) => {
deltaFromHtml.ops.forEach((op) => {
if (typeof op.insert === 'string') {
op.insert = op.insert.replaceAll(replacement, character);
}
});
});

this._editor.setContents(deltaFromHtml, SOURCE.API);
}

Expand Down
35 changes: 35 additions & 0 deletions packages/rich-text-editor/test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,41 @@ describe('rich text editor', () => {
expect(rte.htmlValue).to.equal('<p><strong>Hello </strong></p><p><strong>world</strong></p>');
});

it('should not lose leading tab characters from the resulting htmlValue', () => {
const htmlWithLeadingTab = '<p>\tTab</p>';
rte.dangerouslySetHtmlValue(htmlWithLeadingTab);
flushValueDebouncer();
expect(rte.htmlValue).to.equal(htmlWithLeadingTab);
});

it('should not lose extra space characters from the resulting htmlValue', () => {
const htmlWithExtraSpaces = '<p>Extra spaces</p>';
rte.dangerouslySetHtmlValue(htmlWithExtraSpaces);
flushValueDebouncer();
expect(rte.htmlValue).to.equal(htmlWithExtraSpaces);
});

it('should not break code block attributes', () => {
const htmlWithCodeBlock = `<pre spellcheck="false">code\n</pre>`;
rte.dangerouslySetHtmlValue(htmlWithCodeBlock);
flushValueDebouncer();
expect(rte.htmlValue).to.equal(htmlWithCodeBlock);
});

it('should support double spaces inside html tags', () => {
const htmlWithCodeBlock = `<pre spellcheck="false">code\n</pre>`;
rte.dangerouslySetHtmlValue(htmlWithCodeBlock);
flushValueDebouncer();
expect(rte.htmlValue).to.equal(`<pre spellcheck="false">code\n</pre>`);
});

it('should support tabs inside html tags', () => {
const htmlWithCodeBlock = `<pre\tspellcheck="false">code\n</pre>`;
rte.dangerouslySetHtmlValue(htmlWithCodeBlock);
flushValueDebouncer();
expect(rte.htmlValue).to.equal(`<pre spellcheck="false">code\n</pre>`);
});

it('should return the quill editor innerHTML', () => {
expect(rte.htmlValue).to.equal('<p><br></p>');
});
Expand Down