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

Balloon toolbar on destroy doesn't destroy its content #1482

Merged
merged 29 commits into from
Sep 15, 2018
Merged

Conversation

engineering-this
Copy link
Contributor

What is the purpose of this pull request?

Bugfix

Does your PR contain necessary tests?

All patches which change the editor code must include tests. You can always read more
on PR testing,
how to set the testing environment and
how to create tests
in the official CKEditor documentation.

This PR contains

  • Unit tests
  • Manual tests

What changes did you make?

I've made balloonToolbar destroy to call also destroy on each of its _items. And added destroy method to richCombo, which removes all listeners from it.

Closes #1477

@mlewand mlewand added the review:easy Pull requests that can be reviewed by a Junior Developer before being reviewed by the Reviewer. label Aug 16, 2018
@Comandeer Comandeer self-requested a review August 23, 2018 15:29
@Comandeer Comandeer self-assigned this Aug 23, 2018
@mlewand mlewand changed the title Bugfix: Balloon toolbar on destroy doesn't destroy its content Balloon toolbar on destroy doesn't destroy its content Aug 24, 2018
@Comandeer Comandeer self-assigned this Aug 24, 2018
CHANGES.md Outdated
@@ -24,6 +24,7 @@ Fixed Issues:
* [#1356](https://github.com/ckeditor/ckeditor-dev/issues/1356): Fixed: [Border parse function](https://docs.ckeditor.com/ckeditor4/docs/#!/api/CKEDITOR.tools.style.parse-method-border) should allow spaces in the color value.
* [#1426](https://github.com/ckeditor/ckeditor-dev/issues/1426): [IE8-9] Fixed: Missing balloon toolbar background in Kama skin. Thanks to [Christian Elmer](https://github.com/keinkurt)!
* [#1010](https://github.com/ckeditor/ckeditor-dev/issues/1010): Fixed: CSS `border` shorthand property was incorrectly expanded ignoring `border-color` style.
* [#1477](https://github.com/ckeditor/ckeditor-dev/issues/1477): Fixed: Balloon toolbar on destroy doesn't destroy its content.
Copy link
Member

Choose a reason for hiding this comment

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

Please link plugin.

@@ -131,6 +131,7 @@
* @param {CKEDITOR.ui.button/CKEDITOR.ui.richCombo} element Instance of ui element.
*/
CKEDITOR.ui.balloonToolbar.prototype.addItem = function( name, element ) {
// console.log( this._items[ name ] );
Copy link
Member

Choose a reason for hiding this comment

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

What's this? 😛

* Hides the toolbar and removes it from the DOM.
* Cleares Balloon Toolbar from each menu items.
*
* @since 4.9.0
Copy link
Member

Choose a reason for hiding this comment

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

Please update version tag.

/**
* Removes all listeners from richCombo element.
*
* @since 4.9.0
Copy link
Member

Choose a reason for hiding this comment

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

Please update version tag.

@@ -0,0 +1,49 @@
/* bender-tags: balloontoolbar, 1477, 4.9.0 */
Copy link
Member

Choose a reason for hiding this comment

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

Please update version tag.

@@ -0,0 +1,15 @@
@bender-ui: collapsed
@bender-tags: 4.9.0 bug, 1477, balloontoolbar
Copy link
Member

Choose a reason for hiding this comment

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

Please update version tag.

@@ -169,9 +170,24 @@
};

/**
* Hides the toolbar and removes it from the DOM.
* Cleares Balloon Toolbar from each menu items.
Copy link
Member

Choose a reason for hiding this comment

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

Some better API docs would be nice, e.g. note that it invokes destroy method of every item.

*
* @since 4.9.0
*/
CKEDITOR.ui.balloonToolbar.prototype.clearItems = function() {
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if this method should be public. I don't see any real use-cases for it except of destroying the toolbar.

* @since 4.9.0
*/
destroy: function() {
if ( this._listeners.length ) {
Copy link
Member

Choose a reason for hiding this comment

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

IMO this check is totally unnecessary. forEach loop would work totally fine with empty array.


## Unexpected:

1. Console throws an error.
Copy link
Member

Choose a reason for hiding this comment

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

If we use console here, it's nice to include opening console inside the procedure. Also it makes this test mobile unfriendly, so it should be ignored in mobile environments.

}
panel.destroy();

items.forEach( function( item ) {
Copy link
Member

Choose a reason for hiding this comment

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

IMO such condition could be nicer expressed using CKEDITOR.tools.array.every:

var listenersDeleted = CKEDITOR.tools.array.every( items, function( item ) {
	return !( item instanceof CKEDITOR.ui.richCombo ) || item._listeners.length === 0;
};
assert.isTrue( listenersDeleted, 'All listeners are deleted' );

Please also note the move of the assertion itself. In fact we are interested in deleting all listeners, not individual ones.

*/
CKEDITOR.ui.balloonToolbar.prototype.destroy = function() {
for ( var key in this._items ) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I'm already a bit biased, but CKEDITOR.tools.forEach + CKEDITOR.tools.objectKeys looks better to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I adapted your suggestion in other place. However I think for in loop is fine here.

@@ -96,6 +96,8 @@ CKEDITOR.plugins.add( 'richcombo', {
panelDefinition: panelDefinition,
items: {}
};

this._listeners = [];
Copy link
Member

Choose a reason for hiding this comment

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

IMO it'd be nice to add some API docs for this private property.

@@ -0,0 +1,16 @@
@bender-ui: collapsed
@bender-tags: 4.11.0 bug, 1477, balloontoolbar
Copy link
Member

Choose a reason for hiding this comment

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

Missing comma after version tag.

Copy link
Member

@Comandeer Comandeer left a comment

Choose a reason for hiding this comment

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

LGTM, there is only one thing I miss: unit test for richcombo. At the moment it's tested only inside balloon toolbar.

* Array containing richCombos registered listeners.
*
* @private
*/
Copy link
Member

Choose a reason for hiding this comment

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

There is missing type of this property. It'd be ideal to use dedicated type (see #2383), but some more generic approach would be also enough.

@Comandeer Comandeer removed their assignment Sep 6, 2018
@engineering-this engineering-this self-assigned this Sep 6, 2018
@engineering-this engineering-this removed their assignment Sep 6, 2018
@Comandeer Comandeer self-assigned this Sep 11, 2018
* @type {Array}
* @private
*/
this._listeners = [];
Copy link
Member

Choose a reason for hiding this comment

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

I didn't notice it earlier 😐 but we don't need new _-variable as we already have _ namespace here to put private stuff inside. In such case we could even omit the docs. Please update the code from _listeners to _.listeners. And sorry for not noticing it earlier 😞

};

var tests = {
'test destroy toolbar with richcombo': function() {
Copy link
Member

Choose a reason for hiding this comment

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

This test does not test anything. Array passed to CKEDITOR.tools.array.every is empty, therefore test always passes.

panel.destroy();

var listenersDeleted = CKEDITOR.tools.array.every( items, function( item ) {
return !( item instanceof CKEDITOR.ui.richCombo ) || item._listeners.length === 0;
Copy link
Member

Choose a reason for hiding this comment

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

I discovered it by this typo: item._listeners instead of item._.listeners.

@@ -38,5 +38,21 @@ bender.test( {
anchorEl = CKEDITOR.document.getById( 'cke_' + combo.id ).findOne( 'a' );

assert.areEqual( anchorEl.getAttribute( 'aria-haspopup' ), 'listbox' );
},
'test destroy removes combo listeners': function() {
Copy link
Member

Choose a reason for hiding this comment

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

Please add issue reference.

* @type {Array}
* @private
*/
this._.listeners = [];
Copy link
Member

Choose a reason for hiding this comment

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

If we add it to _ namespace, API docs are not needed. It would be even weird to document only one property of it. Let's just add listeners array to the object literal above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess every piece of extra information might be helpful in future especially if it's already written, but I guess this property is so obvious we can drop docs, and move it to the object literal.

Copy link
Member

@Comandeer Comandeer left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
review:easy Pull requests that can be reviewed by a Junior Developer before being reviewed by the Reviewer.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Balloon toolbar should destroy its content properly
3 participants