Skip to content

Commit

Permalink
Merge pull request #2693 from ckeditor/t/2692
Browse files Browse the repository at this point in the history
Add `isSupportedEnvironment` plugin method
  • Loading branch information
f1ames authored Jun 17, 2019
2 parents ea17909 + 48b29cd commit 27f156c
Show file tree
Hide file tree
Showing 277 changed files with 786 additions and 745 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ API Changes:
* [#2962](https://github.com/ckeditor/ckeditor-dev/issues/2962): Added the [`CKEDITOR.tools.promise`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_promise.html) class.
* [#2924](https://github.com/ckeditor/ckeditor-dev/issues/2924): Added the [`CKEDITOR.tools.style.border`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_tools_style_border.html) object wrapping CSS border style helpers under single type.
* [#2495](https://github.com/ckeditor/ckeditor-dev/issues/2495): [Table Selection](https://ckeditor.com/cke4/addon/tableselection) plugin can be now disabled for the given table by the `data-cke-tableselection-ignored` attribute.
* [#2692](https://github.com/ckeditor/ckeditor-dev/issues/2692): Plugins can now expose information about supported environment by implementing [`pluginDefinition.isSupportedEnvironment`](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_pluginDefinition.html#method-isSupportedEnvironment) method.

Other Changes:

Expand Down
26 changes: 26 additions & 0 deletions core/plugindefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,29 @@
*
* @property {String} [icons]
*/

/**
* A function which should be implemented if a plugin is not supported on every
* available environment according to
* [Browser Compatibility](https://ckeditor.com/docs/ckeditor4/latest/guide/dev_browsers.html)
* or specific editor configuration.
*
* This function won't be called by the plugin loader itself and it's not required for a proper
* plugin initialization. However, it's recommended to implement the function if a plugin
* has environment requirements. This information may be important for related features
* and testing environment.
*
* ```javascript
* CKEDITOR.plugins.add( 'sample', {
* isSupportedEnvironment: function( editor ) {
* // Plugin supported only on modern browsers.
* return !CKEDITOR.env.ie || CKEDITOR.env.edge;
* }
* } );
* ```
*
* @since 4.12.0
* @method isSupportedEnvironment
* @param {CKEDITOR.editor} editor
* @returns {Boolean} Information if the plugin is supported in the current environment.
*/
6 changes: 6 additions & 0 deletions core/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ CKEDITOR.plugins.load = CKEDITOR.tools.override( CKEDITOR.plugins.load, function
'.png' );
}
}

// Plugin is supported by default (#2692).
plugin.isSupportedEnvironment = plugin.isSupportedEnvironment || function() {
return true;
};

initialized[ pluginName ] = 1;
}

Expand Down
5 changes: 2 additions & 3 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2020,9 +2020,8 @@

// Handle special case - fake selection of table cells.
if ( editor && editor.plugins.tableselection &&
CKEDITOR.plugins.tableselection.isSupportedEnvironment &&
isTableSelection( ranges ) &&
!isSelectingTable &&
editor.plugins.tableselection.isSupportedEnvironment() &&
isTableSelection( ranges ) && !isSelectingTable &&
!ranges[ 0 ]._getTableElement( { table: 1 } ).hasAttribute( 'data-cke-tableselection-ignored' )
) {
performFakeTableSelection.call( this, ranges );
Expand Down
3 changes: 3 additions & 0 deletions plugins/autocomplete/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
requires: 'textwatcher',
onLoad: function() {
CKEDITOR.document.appendStyleSheet( this.path + 'skins/default.css' );
},
isSupportedEnvironment: function() {
return !CKEDITOR.env.ie || CKEDITOR.env.version > 8;
}
} );

Expand Down
4 changes: 4 additions & 0 deletions plugins/balloontoolbar/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,10 @@
CKEDITOR.plugins.add( 'balloontoolbar', {
requires: 'balloonpanel',

isSupportedEnvironment: function() {
return !CKEDITOR.env.ie || CKEDITOR.env.version > 8;
},

beforeInit: function( editor ) {
if ( !cssLoaded ) {
// Load fallback styles.
Expand Down
10 changes: 6 additions & 4 deletions plugins/codesnippet/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
'use strict';

( function() {
var isBrowserSupported = !CKEDITOR.env.ie || CKEDITOR.env.version > 8;

CKEDITOR.plugins.add( 'codesnippet', {
requires: 'widget,dialog',
lang: 'ar,az,bg,ca,cs,da,de,de-ch,el,en,en-au,en-gb,eo,es,es-mx,et,eu,fa,fi,fr,fr-ca,gl,he,hr,hu,id,it,ja,km,ko,ku,lt,lv,nb,nl,no,oc,pl,pt,pt-br,ro,ru,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
icons: 'codesnippet', // %REMOVE_LINE_CORE%
hidpi: true, // %REMOVE_LINE_CORE%

isSupportedEnvironment: function() {
return !CKEDITOR.env.ie || CKEDITOR.env.version > 8;
},

beforeInit: function( editor ) {
editor._.codesnippet = {};

Expand Down Expand Up @@ -105,7 +107,7 @@
init: function( callback ) {
var that = this;

if ( isBrowserSupported ) {
if ( editor.plugins.codesnippet.isSupportedEnvironment() ) {
CKEDITOR.scriptLoader.load( path + 'lib/highlight/highlight.pack.js', function() {
that.hljs = window.hljs;
callback();
Expand Down Expand Up @@ -313,7 +315,7 @@
callback = function( formatted ) {
// IE8 (not supported browser) have issue with new line chars, when using innerHTML.
// It will simply strip it.
that.parts.code.setHtml( isBrowserSupported ?
that.parts.code.setHtml( editor.plugins.codesnippet.isSupportedEnvironment() ?
formatted : formatted.replace( newLineRegex, '<br>' ) );
};

Expand Down
12 changes: 6 additions & 6 deletions plugins/easyimage/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,10 +494,6 @@
}
};

function isSupportedBrowser() {
return !CKEDITOR.env.ie || CKEDITOR.env.version >= 11;
}

function addUploadButtonToToolbar( editor ) {
editor.ui.addButton( BUTTON_PREFIX + 'Upload', {
label: editor.lang.easyimage.commands.upload,
Expand Down Expand Up @@ -535,8 +531,12 @@
CKEDITOR.dialog.add( 'easyimageAlt', this.path + 'dialogs/easyimagealt.js' );
},

isSupportedEnvironment: function() {
return !CKEDITOR.env.ie || CKEDITOR.env.version >= 11;
},

init: function( editor ) {
if ( !isSupportedBrowser() ) {
if ( !this.isSupportedEnvironment() ) {
return;
}
loadStyles( editor, this );
Expand All @@ -545,7 +545,7 @@
// Widget must be registered after init in case that link plugin is dynamically loaded e.g. via
// `config.extraPlugins`.
afterInit: function( editor ) {
if ( !isSupportedBrowser() ) {
if ( !this.isSupportedEnvironment() ) {
return;
}
var styles = getStylesForEditor( editor );
Expand Down
8 changes: 6 additions & 2 deletions plugins/emoji/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,12 @@
icons: 'emojipanel',
hidpi: true,

isSupportedEnvironment: function() {
return !CKEDITOR.env.ie || CKEDITOR.env.version >= 11;
},

beforeInit: function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 11 ) {
if ( !this.isSupportedEnvironment() ) {
return;
}
if ( !stylesLoaded ) {
Expand All @@ -560,7 +564,7 @@
},

init: function( editor ) {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 11 ) {
if ( !this.isSupportedEnvironment() ) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions plugins/mathjax/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
icons: 'mathjax',
hidpi: true, // %REMOVE_LINE_CORE%

isSupportedEnvironment: function() {
return !CKEDITOR.env.ie || CKEDITOR.env.version > 8;
},

init: function( editor ) {
var cls = editor.config.mathJaxClass || 'math-tex';

Expand Down
3 changes: 3 additions & 0 deletions plugins/mentions/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
self.instances.push( new Mentions( editor, config ) );
} );
} );
},
isSupportedEnvironment: function( editor ) {
return editor.plugins.autocomplete.isSupportedEnvironment( editor );
}
} );

Expand Down
16 changes: 6 additions & 10 deletions plugins/tableselection/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,20 +1149,16 @@
var editable = editor.editable();
editable.attachListener( editable, 'keydown', getTableOnKeyDownListener( editor ), null, null, -1 );
editable.attachListener( editable, 'keypress', tableKeyPressListener, null, null, -1 );
},

/**
* Determines whether table selection is supported in the current environment.
*
* @property {Boolean}
* @private
*/
isSupportedEnvironment: !( CKEDITOR.env.ie && CKEDITOR.env.version < 11 )
}
};

CKEDITOR.plugins.add( 'tableselection', {
requires: 'clipboard,tabletools',

isSupportedEnvironment: function() {
return !( CKEDITOR.env.ie && CKEDITOR.env.version < 11 );
},

onLoad: function() {
// We can't alias these features earlier, as they could be still not loaded.
tabletools = CKEDITOR.plugins.tabletools;
Expand All @@ -1176,7 +1172,7 @@

init: function( editor ) {
// Disable unsupported browsers.
if ( !CKEDITOR.plugins.tableselection.isSupportedEnvironment ) {
if ( !this.isSupportedEnvironment() ) {
return;
}

Expand Down
6 changes: 5 additions & 1 deletion plugins/uploadfile/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
requires: 'uploadwidget,link',
init: function( editor ) {
// Do not execute this paste listener if it will not be possible to upload file.
if ( !CKEDITOR.plugins.clipboard.isFileApiSupported ) {
if ( !this.isSupportedEnvironment() ) {
return;
}

Expand All @@ -37,6 +37,10 @@
this.replaceWith( '<a href="' + upload.url + '" target="_blank">' + upload.fileName + '</a>' );
}
} );
},

isSupportedEnvironment: function() {
return CKEDITOR.plugins.clipboard.isFileApiSupported;
}
} );
} )();
6 changes: 5 additions & 1 deletion plugins/uploadimage/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@
);
},

isSupportedEnvironment: function() {
return CKEDITOR.plugins.clipboard.isFileApiSupported;
},

init: function( editor ) {
// Do not execute this paste listener if it will not be possible to upload file.
if ( !CKEDITOR.plugins.clipboard.isFileApiSupported ) {
if ( !this.isSupportedEnvironment() ) {
return;
}

Expand Down
4 changes: 4 additions & 0 deletions plugins/uploadwidget/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
// because otherwise wrong widget may handle upload placeholder element (e.g. image2 plugin would handle image).
// `data-widget` attribute is allowed only in the elements which has also `data-cke-upload-id` attribute.
editor.filter.allow( '*[!data-widget,!data-cke-upload-id]' );
},

isSupportedEnvironment: function() {
return CKEDITOR.plugins.clipboard.isFileApiSupported;
}
} );

Expand Down
40 changes: 40 additions & 0 deletions tests/_benderjs/ckeditor/static/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,46 @@
} );

bender.tools = {
/**
* Ignores test case when the given plugin is not supported on the testing
* environment. Uses {@link CKEDITOR.pluginDefinition#isSupportedEnvironment} to
* verify if plugin is supported.
*
* Works for both manual and unit tests.
*
* @param pluginName pluginName Plugin name to check.
* @param CKEDITOR.editor [editor] Editor instance passsed as an argument
* to the {@link CKEDITOR.pluginDefinition#isSupportedEnvironment} method.
*/
ignoreUnsupportedEnvironment: function( pluginName, editor ) {
if ( editor ) {
if ( editor.status === 'ready' ) {
ignoreUnsupportedEnvironment();
} else {
editor.once( 'instanceReady', ignoreUnsupportedEnvironment );
}
return;
}

if ( CKEDITOR.plugins.registered[ pluginName ] ) {
ignoreUnsupportedEnvironment();
} else {
CKEDITOR.once( pluginName + 'PluginReady', ignoreUnsupportedEnvironment );
}

function ignoreUnsupportedEnvironment() {
var plugin = editor ? editor.plugins[ pluginName ] : CKEDITOR.plugins.registered[ pluginName ];

if ( !plugin.isSupportedEnvironment( editor ) ) {
if ( bender.testData.manual ) {
bender.ignore();
} else {
assert.ignore();
}
}
}
},

/**
* Creates an array from an object.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/core/plugins/plugins.html
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<textarea id="errortest">test</textarea>
<textarea id="errortest">test</textarea>
<textarea id="env"></textarea>
27 changes: 27 additions & 0 deletions tests/core/plugins/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,32 @@ bender.test(

assert.isFalse( result, 'Conflicts not detected.' );
assert.isFalse( spy.called );
},

// (#2692)
'test supported environment': function() {
CKEDITOR.plugins.add( 'envdefault', {} );
CKEDITOR.plugins.add( 'envcustom', {
isSupportedEnvironment: function() {
return false;
}
} );

var editor = CKEDITOR.replace( 'env', {
plugins: 'wysiwygarea,envdefault,envcustom',
on: {
instanceReady: function() {
resume( function() {
assert.isTrue( editor.plugins.envdefault.isSupportedEnvironment(),
'Plugin should be supported by default' );

assert.isFalse( editor.plugins.envcustom.isSupportedEnvironment(),
'Plugin should allow for custom isSupportedEnvironment implementation' );
} );
}
}
} );

wait();
}
} );
4 changes: 1 addition & 3 deletions tests/plugins/autocomplete/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@
bender.test( {

setUp: function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 9 ) {
assert.ignore();
}
bender.tools.ignoreUnsupportedEnvironment( 'autocomplete' );
},

'test API exists': function() {
Expand Down
4 changes: 1 addition & 3 deletions tests/plugins/autocomplete/focus.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@

bender.test( {
setUp: function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version == 8 ) {
assert.ignore();
}
bender.tools.ignoreUnsupportedEnvironment( 'autocomplete' );
},

'test preventDefault is called on mousedown': function() {
Expand Down
4 changes: 1 addition & 3 deletions tests/plugins/autocomplete/manual/__template__.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

<script>

if ( autocompleteUtils.isUnsupportedEnvironment() ) {
bender.ignore();
}
bender.tools.ignoreUnsupportedEnvironment( 'autocomplete' );

CKEDITOR.replace( 'editor1', {
width: 600,
Expand Down
Loading

0 comments on commit 27f156c

Please sign in to comment.