Created
January 11, 2012 17:48
-
-
Save wGEric/1595828 to your computer and use it in GitHub Desktop.
CKEditor plugin that uses https://github.com/wGEric/phpBB-BBCode-Javascript-Parser to process the data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
var BBCodeWriter = function(editor) { | |
this.editor = editor; | |
this.writer = new CKEDITOR.htmlWriter(); | |
this.dataFilter = new CKEDITOR.htmlParser.filter(); | |
this.htmlFilter = new CKEDITOR.htmlParser.filter(); | |
} | |
BBCodeWriter.prototype = { | |
toHtml : function (data, fixForBody) { | |
return bbcodeParser.bbcodeToHtml(data); | |
}, | |
toDataFormat : function (html, fixForBody) { | |
var writer = this.writer, | |
fragment = CKEDITOR.htmlParser.fragment.fromHtml( html, fixForBody ); | |
writer.reset(); | |
fragment.writeHtml( writer, this.htmlFilter ); | |
var data = writer.getHtml( true ); | |
return bbcodeParser.htmlToBBCode(data); | |
} | |
} | |
var defaultHtmlFilterRules = { | |
elementNames : | |
[ | |
// Remove the "cke:" namespace prefix. | |
[ ( /^cke:/ ), '' ], | |
// Ignore <?xml:namespace> tags. | |
[ ( /^\?xml:namespace$/ ), '' ] | |
], | |
attributeNames : | |
[ | |
// Attributes saved for changes and protected attributes. | |
[ ( /^data-cke-(saved|pa)-/ ), '' ], | |
// All "data-cke-" attributes are to be ignored. | |
[ ( /^data-cke-.*/ ), '' ], | |
[ 'hidefocus', '' ] | |
], | |
elements : | |
{ | |
$ : function( element ) | |
{ | |
var attribs = element.attributes; | |
if ( attribs ) | |
{ | |
// Elements marked as temporary are to be ignored. | |
if ( attribs[ 'data-cke-temp' ] ) | |
return false; | |
// Remove duplicated attributes - #3789. | |
var attributeNames = [ 'name', 'href', 'src' ], | |
savedAttributeName; | |
for ( var i = 0 ; i < attributeNames.length ; i++ ) | |
{ | |
savedAttributeName = 'data-cke-saved-' + attributeNames[ i ]; | |
savedAttributeName in attribs && ( delete attribs[ attributeNames[ i ] ] ); | |
} | |
} | |
return element; | |
}, | |
// Remove empty link but not empty anchor.(#3829) | |
a : function( element ) | |
{ | |
if ( !( element.children.length || | |
element.attributes.name || | |
element.attributes[ 'data-cke-saved-name' ] ) ) | |
{ | |
return false; | |
} | |
}, | |
// Remove dummy span in webkit. | |
span: function( element ) | |
{ | |
if ( element.attributes[ 'class' ] == 'Apple-style-span' ) | |
delete element.name; | |
}, | |
// Empty <pre> in IE is reported with filler node ( ). | |
pre : function( element ) { CKEDITOR.env.ie && trimFillers( element ); }, | |
}, | |
attributes : | |
{ | |
'class' : function( value, element ) | |
{ | |
// Remove all class names starting with "cke_". | |
return CKEDITOR.tools.ltrim( value.replace( /(?:^|\s+)cke_[^\s]*/g, '' ) ) || false; | |
} | |
} | |
}; | |
CKEDITOR.on( 'dialogDefinition', function( ev ) | |
{ | |
var tab, name = ev.data.name, | |
definition = ev.data.definition; | |
if ( name == 'link' ) | |
{ | |
definition.removeContents( 'target' ); | |
definition.removeContents( 'upload' ); | |
definition.removeContents( 'advanced' ); | |
tab = definition.getContents( 'info' ); | |
tab.remove( 'emailSubject' ); | |
tab.remove( 'emailBody' ); | |
} | |
else if ( name == 'image' ) | |
{ | |
definition.removeContents( 'advanced' ); | |
tab = definition.getContents( 'Link' ); | |
tab.remove( 'cmbTarget' ); | |
tab = definition.getContents( 'info' ); | |
tab.remove( 'txtAlt' ); | |
tab.remove( 'basic' ); | |
} | |
}); | |
CKEDITOR.plugins.add( 'phpbb', | |
{ | |
beforeInit : function( editor ) | |
{ | |
// Adapt some critical editor configuration for better support | |
// of BBCode environment. | |
var config = editor.config; | |
CKEDITOR.tools.extend( config, | |
{ | |
enterMode : CKEDITOR.ENTER_BR, | |
basicEntities: false, | |
entities : false, | |
fillEmptyBlocks : false | |
}, true ); | |
}, | |
init : function( editor ) | |
{ | |
var dataProcessor = editor.dataProcessor = new BBCodeWriter(editor); | |
dataProcessor.htmlFilter.addRules( defaultHtmlFilterRules ); // removes anything added by use of ckeditor internally | |
}, | |
} ); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment