Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate caption with basicstyles plugin #1559

Closed
wants to merge 14 commits into from
Closed
31 changes: 23 additions & 8 deletions plugins/imagebase/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,16 +554,24 @@
function listener( evt ) {
var path = evt.name === 'blur' ? editor.elementPath() : evt.data.path,
sender = path ? path.lastElement : null,
widgets = getWidgetsWithFeature( editor.widgets.instances, 'caption' );
widgets = getWidgetsWithFeature( editor.widgets.instances, 'caption' ),
focused = getFocusedWidget( editor );

if ( !editor.filter.check( 'figcaption' ) ) {
return CKEDITOR.tools.array.forEach( listeners, function( listener ) {
listener.removeListener();
} );
}

// In Firefox and Edge applying styles inside caption results
// in firing this listener without focused widget.
// However it could be obtained from element, on which the event took place (#1646).
if ( editor.focusManager.hasFocus && sender && !focused ) {
focused = editor.widgets.getByElement( sender );
}

CKEDITOR.tools.array.forEach( widgets, function( widget ) {
widget._refreshCaption( sender );
widget._refreshCaption( sender, widget === focused );
} );
}

Expand Down Expand Up @@ -593,16 +601,23 @@
* @private
* @member CKEDITOR.plugins.imagebase.featuresDefinitions.caption
* @param {CKEDITOR.dom.element} sender The element that this function should be called on.
* @param {Boolean} [forceFocus] Indicates if current widget should be treated as a focused one.
* If this parameter is omitted or set to false, the focused widget is determined by checking
* {@link CKEDITOR.plugins.widget.repository#focused} value. This parameter was added in 4.10.
*/
_refreshCaption: function( sender ) {
var isFocused = getFocusedWidget( this.editor ) === this,
caption = this.parts.caption,
editable = this.editables.caption;
_refreshCaption: function( sender, forceFocus ) {
var caption = this.parts.caption,
editable = this.editables.caption,
isFocused = forceFocus || ( getFocusedWidget( this.editor ) === this );

function isInCaption( element ) {
return element.equals( caption ) || caption.contains( element );
}

if ( isFocused ) {
if ( !editable.getData() && !sender.equals( caption ) ) {
if ( !editable.getData() && !isInCaption( sender ) ) {
addPlaceholder( this );
} else if ( !sender || ( sender.equals( caption ) && sender.data( 'cke-caption-placeholder' ) ) ) {
} else if ( !sender || ( isInCaption( sender ) && caption.data( 'cke-caption-placeholder' ) ) ) {
removePlaceholder( this );
}

Expand Down
35 changes: 31 additions & 4 deletions tests/plugins/imagebase/features/caption.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* bender-tags: editor,widget */
/* bender-ckeditor-plugins: imagebase,toolbar,easyimage */
/* bender-include: ../../widget/_helpers/tools.js */
/* global widgetTestsTools */
/* bender-ckeditor-plugins: imagebase,basicstyles,toolbar,easyimage */
/* bender-include: ../../widget/_helpers/tools.js, %BASE_PATH%/plugins/easyimage/_helpers/tools.js */
/* global widgetTestsTools, easyImageTools */

( function() {
'use strict';
Expand Down Expand Up @@ -465,7 +465,34 @@
blurHost: emptyCaptionWidget
} );
} );
}
},

// (#1646)
'test caption placeholder integration with basicstyles': createToggleTest( {
fixture: 'toggleOneEmpty',
initial: false,
focus: true,
blur: false,

customFocus: function( widget ) {
var range = widget.editor.createRange(),
caption = widget.parts.caption;

widget.focus();
caption.focus();

// In Safari and IE11 focus is not enough to move selection.
range.setStart( caption.getChild( 0 ), 0 );
range.collapse();
range.select();

widget.editor.execCommand( 'bold' );
},

onFocus: function( widget ) {
assertPlaceholder( widget, false );
}
} )
};

tests = bender.tools.createTestsForEditors( CKEDITOR.tools.object.keys( bender.editors ), tests );
Expand Down
53 changes: 53 additions & 0 deletions tests/plugins/imagebase/features/manual/captionbasicstyles.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<head>
<link rel="stylesheet" href="/apps/ckeditor/contents.css">
</head>
<body>
<h1>Classic editor</h1>

<div id="classic">
<p>Widget without caption:</p>
<figure>
<img src="../../../image2/_assets/foo.png" alt="foo">
</figure>

<p>Widget with caption:</p>
<figure>
<img src="../../../image2/_assets/foo.png" alt="foo">
<figcaption>Test caption</figcaption>
</figure>
</div>

<h1>Divarea</h1>

<div id="divarea">
<p>Widget without caption:</p>
<figure>
<img src="../../../image2/_assets/foo.png" alt="foo">
</figure>

<p>Widget with caption:</p>
<figure>
<img src="../../../image2/_assets/foo.png" alt="foo">
<figcaption>Test caption</figcaption>
</figure>
</div>

<script>
bender.tools.ignoreUnsupportedEnvironment( 'easyimage' );

var cfg = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not work (the divarea editor to be precise) on IE8. AFAIR, imagebase is not supposed to work on IE8 so it should be ignored probably.

on: {
pluginsLoaded: function( evt ) {
var editor = evt.editor,
imageBase = CKEDITOR.plugins.imagebase;

imageBase.addImageWidget( editor, 'testWidget', imageBase.addFeature( editor, 'caption', {} ) );
}
},
height: 500
};

CKEDITOR.replace( 'classic', cfg );
CKEDITOR.replace( 'divarea', CKEDITOR.tools.object.merge( cfg, { extraPlugins: 'divarea' } ) );
</script>
</body>
25 changes: 25 additions & 0 deletions tests/plugins/imagebase/features/manual/captionbasicstyles.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@bender-tags: 4.13.1, bug, 1646
@bender-ui: collapsed
@bender-ckeditor-plugins: wysiwygarea, toolbar, imagebase, basicstyles
@bender-include: %BASE_PATH%/plugins/easyimage/_helpers/tools.js

# Integration of caption and basicstyles

1. Focus widget.
2. Put cursor inside the caption. If caption contains text, delete it.
3. Apply "Bold".

## Expected

* Bold is applied.
* There is no visible placeholder.
* Selection remains inside the caption.

## Unexpected

* Placeholder shows up.
* Firefox/Edge: Selection is lost.

---

Repeat for all editors.