I have an application that generates DIV content that has to automatically be inserted into a ckeditor instance. At the moment, I'm using editorInstance.model.insertContent( modelFragment ); and have a custom plugin as follows to process the insert of these elements:
// Allow <div> elements in the model.
editor.model.schema.register( 'div', {
allowWhere: '$block',
allowContentOf: '$root',
inheritAllFrom: '$block',
allowAttributes: [ 'style' ]
} );
// Allow <div> elements in the model to have all attributes.
editor.model.schema.addAttributeCheck( context => {
if ( context.endsWith( 'div' ) ) {
return true;
}
} );
// The view-to-model converter converting a view <div> with all its attributes to the model.
editor.conversion.for( 'upcast' ).elementToElement( {
view: 'div',
model: ( viewElement, { writer: modelWriter } ) => {
return modelWriter.createElement( 'div', viewElement.getAttributes() );
}
} );
// The model-to-view converter for the <div> element (attributes are converted separately).
editor.conversion.for( 'downcast' ).elementToElement( {
model: 'div',
view: 'div'
} );
// The model-to-view converter for <div> attributes.
// Note that a lower-level, event-based API is used here.
editor.conversion.for( 'downcast' ).add( dispatcher => {
dispatcher.on( 'attribute', ( evt, data, conversionApi ) => {
// Convert <div> attributes only.
if ( data.item.name != 'div' ) {
return;
}
const viewWriter = conversionApi.writer;
const viewDiv = conversionApi.mapper.toViewElement( data.item );
// In the model-to-view conversion we convert changes.
// An attribute can be added or removed or changed.
// The below code handles all 3 cases.
if ( data.attributeNewValue ) {
viewWriter.setAttribute( data.attributeKey, data.attributeNewValue, viewDiv );
} else {
viewWriter.removeAttribute( data.attributeKey, viewDiv );
}
} );
} );
}
However, some of the DIV elements that I'm inserting have custom style tags. For 'basic' stuff, such as background-image, background-color, this works OK, but when I try to insert a div with background: linear-gradient(...... it seems to strip the entire style tag empty. These elements are generated on the fly, so I can't style them any other way than inline.
Is it possible to force CKEditor5 not to parse the style tag on the div, or to allow background: linear-gradient(...?