CKEditor: PasteFromWord ignores pasteFilter
In CKEditor 4.6.2 (currently bundled with Drupal 8), ACF is disabled by default to ensure that some of the custom plugins work correctly. So, for the record: I don't want to enable ACF and cannot use allowedContent
or disallowedContent
. I am trying to prevent some elements from being entered when pasting from Word (like h1
and p[styles]
).
To accomplish this, I'm trying to add them to pasteFilter
, which works fine with non-Word content, but pasteFilter
seems to be ignored when pasting from Word ? This is mistake?
So how can I:
- Keep ACF disabled - in favor of Drupal custom elements
- Keep
pastefromword
On - to detect a special Word style, such as indents and lists. - Add additional filtering to all (including from Word) inserts - remove some elements and attributes, such as
h1
,style="font-family: Verdana"
etc.
source to share
Processing content pasted from Word requires some serious amount of markup processing to translate it into pure semantic content. The paste from the Word filter is very specific to many edge cases (especially nested lists). The reason Paste from Word has its own filter and does not reuse ACF rules is because it can cause some conflicts - this is covered in this issue .
There is now a ready-made approach for adding additional filtering to content pasted from Word. However, you can use an afterPasteFromWord
event to filter out nested data, for example:
var editor = CKEDITOR.replace( 'editor1' );
editor.on( 'afterPasteFromWord', function( evt ) {
var filter = editor.activeFilter, // Use activeFilter so it reflects ACF settings.
// var filter = new CKEDITOR.filter( 'p b' ), // Use custom new filter.
fragment = CKEDITOR.htmlParser.fragment.fromHtml( evt.data.dataValue ),
writer = new CKEDITOR.htmlParser.basicWriter();
filter.applyTo( fragment );
fragment.writeHtml( writer );
evt.data.dataValue = writer.getHtml();
} );
See this demo of codef .
You can also refer to the official docs at CKEDITOR.filter.applyTo
and CKEDITOR.editor.activeProperty
.
source to share