I wanted to replace text matching /\[include\|[^\]]+\]/g with fakeElement just like anchor & flashplugin but documentation on this seemed complicated and it took quite a lot of time to resolve this.
This is my plugin's afterInit, not the whole plugin.
This is my plugin's afterInit, not the whole plugin.
afterInit : function( editor )
{
var dataProcessor = editor.dataProcessor,
dataFilter = dataProcessor && dataProcessor.dataFilter;
if ( dataFilter )
{
dataFilter.addRules(
{
text : function( text )
{
return text.replace( /\[include\|[^\]]+\]/g, function( match )
{
// Create document fragment from match
var realFragment = new CKEDITOR.htmlParser.fragment.fromHtml(match);
// Document fragments first child is the node we want
var realElement = realFragment && realFragment.children[ 0 ];
// Element should have attributes but text nodes don't have them
// So we fake them
realElement.attributes={};
// Create fake element
// @param element to replace
// @param className for fake element
// @param sets data-cke-real-element-type
// @param is resizable
var tmp = editor.createFakeParserElement( realElement, 'nid_includecontent','nid_includecontent',false );
// Because this is text rule we need to return string
// String can be returnded from writer
var writer = new CKEDITOR.htmlParser.basicWriter();
// set writer for node
tmp.writeHtml( writer );
// get html string from writer
return writer.getHtml();
});
}
});
}
}
