Skip to content

Commit 2216682

Browse files
authored
fix: avoid removing ql-* from HTML content (#7104)
* fix: avoid removing ql-* from HTML content * simplify regex
1 parent a26ec54 commit 2216682

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎packages/rich-text-editor/src/vaadin-rich-text-editor-mixin.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,12 @@ export const RichTextEditorMixin = (superClass) =>
673673
let content = editor.innerHTML;
674674

675675
// Remove Quill classes, e.g. ql-syntax, except for align
676-
content = content.replace(/\s*ql-(?!align)[\w-]*\s*/gu, '');
676+
content = content.replace(/class="([^"]*)"/gu, (match, group1) => {
677+
const classes = group1.split(' ').filter((className) => {
678+
return !className.startsWith('ql-') || className.startsWith('ql-align');
679+
});
680+
return `class="${classes.join(' ')}"`;
681+
});
677682
// Remove meta spans, e.g. cursor which are empty after Quill classes removed
678683
content = content.replace(/<\/?span[^>]*>/gu, '');
679684

‎packages/rich-text-editor/test/basic.common.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,21 @@ describe('rich text editor', () => {
727727
expect(rte.htmlValue).to.equal('<h3><em>Foo</em>Bar</h3>');
728728
});
729729

730+
it('should filter out ql-* class names', () => {
731+
// Modify the editor content directly, as setDangerouslyHtmlValue() strips
732+
// classes
733+
rte.shadowRoot.querySelector('.ql-editor').innerHTML =
734+
'<pre class="ql-syntax foo ql-cursor"><code>console.log("hello")</code></pre>';
735+
rte.__updateHtmlValue();
736+
expect(rte.htmlValue).to.equal('<pre class="foo"><code>console.log("hello")</code></pre>');
737+
});
738+
739+
it('should not filter out ql-* in content', () => {
740+
rte.dangerouslySetHtmlValue('<p>mysql-driver</p>');
741+
flushValueDebouncer();
742+
expect(rte.htmlValue).to.equal('<p>mysql-driver</p>');
743+
});
744+
730745
it('should filter out empty span elements from the resulting htmlValue', () => {
731746
rte.dangerouslySetHtmlValue(
732747
'<p><strong>Hello </strong></p><p><strong><span class="ql-cursor"></span>world</strong></p>',

0 commit comments

Comments
 (0)