Skip to content

Commit

Permalink
Merge pull request #4370 from ckeditor/t/4253
Browse files Browse the repository at this point in the history
Fix: Editor Placeholder plugin throws an error during editor initialization when used with bbcode and fullpage
  • Loading branch information
f1ames authored Nov 20, 2020
2 parents d7c0f1f + 9751b13 commit 6e145e4
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ CKEditor 4 Changelog

## CKEditor 4.15.2

Fixed Issues:

* [#4253](https://github.com/ckeditor/ckeditor4/issues/4253): Fixed: [Editor Placeholder](https://ckeditor.com/cke4/addon/editorplaceholder) plugin throws an error during editor initialization with [fullpage](https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_config.html#cfg-fullPage) enabled when there is no `body` tag in editor content.

## CKEditor 4.15.1

**Security Updates:**
Expand Down
7 changes: 6 additions & 1 deletion plugins/editorplaceholder/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@
data = editor.getData();

if ( isFullPage ) {
data = data.match( fullPageRegex )[ 1 ];
var bodyDataMatched = data.match( fullPageRegex );

// Check if body element exists in editor HTML (#4253).
if ( bodyDataMatched && bodyDataMatched.length > 1 ) {
data = bodyDataMatched[ 1 ];
}
}

return data.length === 0;
Expand Down
114 changes: 114 additions & 0 deletions tests/plugins/editorplaceholder/editorplaceholder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* bender-tags: editor */
/* bender-ckeditor-plugins: wysiwygarea,editorplaceholder,toolbar,undo */
/* global console */

( function() {
'use strict';
Expand Down Expand Up @@ -27,8 +28,22 @@
};

var tests = {

setUp: function() {
bender.tools.ignoreUnsupportedEnvironment( 'editorplaceholder' );

this.consoleErrorStub = null;
this.runtimeErrorListener = null;
},

tearDown: function() {
if ( this.consoleErrorStub ) {
this.consoleErrorStub.restore();
}

if ( this.runtimeErrorListener ) {
window.removeEventListener( 'error', this.runtimeErrorListener );
}
},

'test getting data from editor': function( editor ) {
Expand Down Expand Up @@ -205,5 +220,104 @@
} );
};

// (#4253)
tests[ 'test placeholder loads correctly in full-page editor with bbcode plugin' ] = function() {
if ( CKEDITOR.env.ie && CKEDITOR.env.version < 10 ) {
// Runtime error assert tries to invoke console.error() which is unavailable in IE9.
assert.ignore();
}
addErrorAsserts( this );

bender.editorBot.create( {
name: 'bbcodeFailTest',
config: {
extraPlugins: 'bbcode',
editorplaceholder: 'any',
fullPage: true
}
}, function( bot ) {
// Don't check if editor placeholder was added, since combination of bbcode plugin with fullPage config results
// in lack editor placeholder due to https://github.com/ckeditor/ckeditor4/pull/4251#pullrequestreview-481525255 and #4386.
assert.isFalse( bot.testCase.consoleErrorStub.called, 'There should be no errors during initialization' );

assert.pass( 'Error was not thrown' );
} );
};

// (#4253)
tests[ 'test placeholder loads correctly in full-page editor with bbcode plugin and initial content' ] = function() {
bender.editorBot.create( {
name: 'bbcodeFailTestContent',
config: {
extraPlugins: 'bbcode',
editorplaceholder: 'any',
fullPage: true,
startupData: '<p>Initialized content</p>'
}
}, function( bot ) {
var editor = bot.editor;

assert.isFalse( editor.editable().hasAttribute( 'data-cke-editorplaceholder' ) );
} );
};

// (#4253)
tests[ 'test placeholder is visible with data filter to no content' ] = function() {
bender.editorBot.create( {
name: 'placeholderFilterToNoContent',
config: {
editorplaceholder: 'any',
fullPage: true
}
}, function( bot ) {
var editor = bot.editor;

editor.on( 'toDataFormat', function( evt ) {
evt.data.dataValue = '';
}, null, null, 16 );

editor.fire( 'change' );

assert.isTrue( editor.editable().hasAttribute( 'data-cke-editorplaceholder' ) );
} );
};

// (#4253)
tests[ 'test placeholder is not visible with data filter to initial content' ] = function() {
var startupData = '<p>Initialized content</p>';

bender.editorBot.create( {
name: 'placeholderFilterPreinitialized',
config: {
editorplaceholder: 'any',
fullPage: true,
startupData: startupData
}
}, function( bot ) {
var editor = bot.editor;

editor.on( 'toDataFormat', function( evt ) {
evt.data.dataValue = startupData;
}, null, null, 16 );

editor.fire( 'change' );

assert.isFalse( editor.editable().hasAttribute( 'data-cke-editorplaceholder' ) );
} );
};

bender.test( tests );

function addErrorAsserts( benderScope ) {
benderScope.consoleErrorStub = sinon.stub( console, 'error' );

benderScope.runtimeErrorListener = function() {
resume( function() {
assert.fail( 'There should be no runtime errors!' );
} );
};

window.addEventListener( 'error', benderScope.runtimeErrorListener );
}

}() );

0 comments on commit 6e145e4

Please sign in to comment.