Skip to content
Snippets Groups Projects
Commit 147cfae6 authored by Marius Dumitru Florea's avatar Marius Dumitru Florea
Browse files

XWIKI-20484: The source syntax used by CKEditor is always the syntax of the...

XWIKI-20484: The source syntax used by CKEditor is always the syntax of the current / edited document
* Bulletproof the code (it was failing in the CKEditor administration section)
* Refactor the code to pass jshint
parent ccb9578e
No related branches found
No related tags found
No related merge requests found
......@@ -28,47 +28,63 @@
// The editor instance was created but it not yet initialized. The configuration object passed when the instance was
// created has not been merged with the global configuration yet.
event.editor.once('configLoaded', function(event) {
// TODO: The the complexity can only be lowered. Once below the default maxcomplexity (10 at the time of writing),
// the jshint annotation can be removed.
/*jshint maxcomplexity:16 */
// The editor configuration has been loaded (the instance configuration has been merged with the global
// configuration) but the editor has not been fully initialized yet so we can modify the configuration.
var config = event.editor.config;
var isHTML5 = config.htmlSyntax !== 'annotatedxhtml/1.0';
setDefaultConfig(event.editor);
});
});
var setDefaultConfig = function(editor) {
var config = editor.config;
config.sourceSyntax = config.sourceSyntax || (XWiki || {}).docsyntax;
config.sourceDocument = config.sourceDocument || (XWiki || {}).currentDocument;
maybeSetAllowedContent(config);
};
var maybeSetAllowedContent = function(config) {
var allowedContent = getAllowedContent(config);
if (allowedContent.rules && !config.hasOwnProperty('allowedContent')) {
config.allowedContent = allowedContent.rules;
}
var allowedContent;
var allowedContentWithoutFigure;
if (config.sourceSyntax in config.allowedContentBySyntax) {
allowedContent = $.extend(true, {}, config.allowedContentBySyntax[config.sourceSyntax]);
var imageConfig = config['xwiki-image'] = config['xwiki-image'] || {};
if (allowedContent.rulesWithoutFigure && !imageConfig.hasOwnProperty('captionAllowedContent')) {
imageConfig.captionAllowedContent = allowedContent.rulesWithoutFigure;
}
};
// Forbid script tags if JavaScript skin extensions are not loaded.
if (!config.loadJavaScriptSkinExtensions && '$1' in allowedContent && 'elements' in allowedContent.$1) {
delete allowedContent.$1.elements.script;
}
var getAllowedContent = function(config) {
var isHTML5 = config.htmlSyntax !== 'annotatedxhtml/1.0';
// Removing the figure/figcaption tags for caption content to forbid nested figures.
allowedContentWithoutFigure = $.extend(true, {}, allowedContent);
if ('$1' in allowedContentWithoutFigure && 'elements' in allowedContentWithoutFigure.$1) {
delete allowedContentWithoutFigure.$1.elements.figure;
}
if ('$2' in allowedContentWithoutFigure && 'elements' in allowedContentWithoutFigure.$2) {
delete allowedContentWithoutFigure.$2.elements.figcaption;
}
var allowedContent;
var allowedContentWithoutFigure;
if (config.sourceSyntax in config.allowedContentBySyntax) {
allowedContent = $.extend(true, {}, config.allowedContentBySyntax[config.sourceSyntax]);
// Disable figure support if the syntax isn't HTML 5.
if (!isHTML5) {
allowedContent = allowedContentWithoutFigure;
}
// Forbid script tags if JavaScript skin extensions are not loaded.
if (!config.loadJavaScriptSkinExtensions && '$1' in allowedContent && 'elements' in allowedContent.$1) {
delete allowedContent.$1.elements.script;
}
if (!config.hasOwnProperty('allowedContent') && allowedContent) {
config.allowedContent = allowedContent;
// Removing the figure/figcaption tags for caption content to forbid nested figures.
allowedContentWithoutFigure = $.extend(true, {}, allowedContent);
if ('$1' in allowedContentWithoutFigure && 'elements' in allowedContentWithoutFigure.$1) {
delete allowedContentWithoutFigure.$1.elements.figure;
}
if ('$2' in allowedContentWithoutFigure && 'elements' in allowedContentWithoutFigure.$2) {
delete allowedContentWithoutFigure.$2.elements.figcaption;
}
var imageConfig = config['xwiki-image'] = config['xwiki-image'] || {};
if (!imageConfig.hasOwnProperty('captionAllowedContent') && allowedContentWithoutFigure) {
imageConfig.captionAllowedContent = allowedContentWithoutFigure;
// Disable figure support if the syntax isn't HTML 5.
if (!isHTML5) {
allowedContent = allowedContentWithoutFigure;
}
});
});
}
return {
rules: allowedContent,
rulesWithoutFigure: allowedContentWithoutFigure
};
};
})();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment