Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Implementing 'is' methods to classes related to view and to model #1736

Merged
merged 33 commits into from
Aug 14, 2019

Conversation

msamsel
Copy link
Contributor

@msamsel msamsel commented May 10, 2019

Suggested merge commit message (convention)

Other: Implementing 'is' method to classes related to view and model. Closes ckeditor/ckeditor5#4479 .

@coveralls
Copy link

coveralls commented May 10, 2019

Coverage Status

Coverage remained the same at 100.0% when pulling f33f445 on t/1667 into 44068ac on master.

@@ -58,10 +58,11 @@ export default class RootElement extends Element {
* @inheritDoc
*/
is( type, name ) {
const cutType = type.replace( 'model:', '' );
Copy link
Member

Choose a reason for hiding this comment

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

Missing blank line after var def.

Copy link
Member

Choose a reason for hiding this comment

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

Also, what's "cut type"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's type without module: at the beginning to simplify logic in further lines.
Statement like:

( ( type == 'rootElement' || type == 'module:rootElement'  ) && name == this.name ) || super.is( type, name );

seems to be a little to complex in my opinion.

@Reinmar
Copy link
Member

Reinmar commented May 22, 2019

I'm unsure whether we need this method on classes such as Batch, Model, Differ, Schema. I think that the rule of thumb could be that if there's no instanceof X yet (anywhere), it's unnecessary.

WDYT, @scofalik @pjasiun?

@msamsel
Copy link
Contributor Author

msamsel commented May 29, 2019

I'm unsure whether we need this method on classes such as Batch, Model, Differ, Schema. I think that the rule of thumb could be that if there's no instanceof X yet (anywhere), it's unnecessary.

@Reinmar, should I remove is() method from mentioned classes and put this PR on review again?

@pjasiun
Copy link

pjasiun commented May 29, 2019

I'm unsure whether we need this method on classes such as Batch, Model, Differ, Schema.

It does not make sense to have is method there.

@scofalik
Copy link
Contributor

As a rule of thumb, I'd say that we need those methods where we need those methods.

@Reinmar
Copy link
Member

Reinmar commented May 29, 2019

@Reinmar, should I remove is() method from mentioned classes and put this PR on review again?

Can you check which classes are used with instanceof and which not? We need this method for objects that we know we that we needed or may need to check the type of.

@scofalik
Copy link
Contributor

scofalik commented May 29, 2019

Basically, classes like Differ or Schema aren't similar to other classes and are not returned / expected as a parameter next to (together) with other classes, so we don't need .is() for them cause we always know what are we dealing with. There's nothing like function foo( modelOrDocument ).

@msamsel
Copy link
Contributor Author

msamsel commented May 29, 2019

Can you check which classes are used with instanceof and which not? We need this method for objects that we know we that we needed or may need to check the type of.

I take only set from engine as there is no other place where we check for model/view classes.

instanceof Array
instanceof AttributeElement
instanceof CKEditorError
instanceof DocumentFragment
instanceof DocumentSelection
instanceof DropdownButtonView
instanceof EditableElement
instanceof Element
instanceof EventInfo
instanceof FileLoader
instanceof Function
instanceof HTMLElement
instanceof HTMLTextAreaElement
instanceof Marker
instanceof Model
instanceof ModelDocumentFragment
instanceof ModelElement
instanceof ModelPosition
instanceof ModelRange
instanceof ModelSelection
instanceof Node
instanceof NodeList
instanceof Position
instanceof Promise
instanceof ProxyEmitter
instanceof Range
instanceof RegExp
instanceof RootElement
instanceof SchemaContext
instanceof Selection
instanceof SwitchButtonView
instanceof Template
instanceof TemplateBinding
instanceof TemplateIfBinding
instanceof Text
instanceof TextProxy
instanceof View
instanceof ViewCollection
instanceof ViewText

@Reinmar
Copy link
Member

Reinmar commented May 29, 2019

instanceof TemplateBinding

This is ckeditor5-ui. Could you limit this list to the things from ckeditor5-engine/src only? I mean – instanceof calls used in code located in ckeditor5-engine/src but also instanceof checks outside the engine which check stuff from the engine.

@Reinmar
Copy link
Member

Reinmar commented May 29, 2019

Also, please exclude any code from src/dev-utils from this check.

@msamsel
Copy link
Contributor Author

msamsel commented May 30, 2019

instanceof AttributeElement
instanceof DocumentFragment
instanceof DocumentSelection
instanceof EditableElement
instanceof Element
instanceof Marker
instanceof ModelElement
instanceof ModelRange
instanceof ModelSelection
instanceof Node
instanceof NodeList
instanceof Position
instanceof Range
instanceof SchemaContext
instanceof Selection
instanceof Text
instanceof TextProxy
instanceof ViewText

@scofalik
Copy link
Contributor

I'd guess that SchemaContext and NodeList are used together with Arrays. I wonder if we need .is() there if that's the case.

@msamsel
Copy link
Contributor Author

msamsel commented May 30, 2019

@scofalik only those entries exists for those two:

if ( context instanceof SchemaContext ) {

} else if ( nodes[ i ] instanceof DocumentFragment || nodes[ i ] instanceof NodeList ) {

@Reinmar
Copy link
Member

Reinmar commented May 30, 2019

Agree with @scofalik .SchemaContext and NodeList are mostly internal. So we can exclude those.

@Reinmar
Copy link
Member

Reinmar commented May 30, 2019

So, to conclude:

  • all types of view and model nodes,
  • selection, doc selection, range, position classes,
  • marker,
  • text proxy,
  • doc frags

And that's all for now.

@msamsel
Copy link
Contributor Author

msamsel commented Jun 26, 2019

I've adjusted scope of this PR to cover only mentioned classes.

@@ -40,6 +40,13 @@ export default class LiveRange extends Range {
this.stopListening();
}

/**
* @inheritDoc
Copy link
Member

Choose a reason for hiding this comment

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

You send people to Range#is() which documents that:

Checks whether given object is of range type.

Which means that you still don't know how to use this method to check for a live range

/**
* Checks whether given object is of `position` type.
*
* Read more at {@link module:engine/model/node~Node#is `Node#is()`}.
Copy link
Member

Choose a reason for hiding this comment

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

Sending people to Node#is() from Position (which isn't a node) seems confusing.

Copy link
Member

Choose a reason for hiding this comment

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

But see my other comment below about mentioning that this is a convention.

* @param {module:engine/view/attributeelement~AttributeElement} attribute Attribute element to use as wrapper.
* @returns {module:engine/view/range~Range} range Range after wrapping, spanning over wrapping attribute element.
*/
* Wraps elements within range with provided {@link module:engine/view/attributeelement~AttributeElement AttributeElement}.
Copy link
Member

Choose a reason for hiding this comment

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

Why touching this file in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the first stage when I add is() method, my editor fix those spaces by replacing them with tabs. So it sees to be leftover. I'll revert it.

@@ -394,6 +394,17 @@ export default class Range {
}
}

/**
* Checks whether given object is of `range` type following the convention set by
Copy link
Member

Choose a reason for hiding this comment

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

This wording is a lot better. I complained in the other place that you link to Node#is() without much of an explanation. Saying that "following the convention set by" is a good way out.

Copy link
Member

@Reinmar Reinmar left a comment

Choose a reason for hiding this comment

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

Requires documentation polishing.

Please build the API docs and try to read them as the developer would. Land in various places in these API docs and try to figure out whether it's clear from the method description how to use it.

@msamsel msamsel requested a review from Reinmar July 26, 2019 14:59
@msamsel
Copy link
Contributor Author

msamsel commented Jul 26, 2019

I've rewritten the description. I hope it's better now. :)

@Reinmar
Copy link
Member

Reinmar commented Aug 9, 2019

Yes, it's definitely better. But there's now a lot of repetition of things which can be centralised. It's hard to find the place where this method could be described in a bit deeper way, but Node#is sounds acceptable. In all other places, I'd keep the documentation as short as possible, but still very easy to parse thanks to examples. Because of them you don't have to necessarily go to any other place to understand how to use it. But you can if you want more details.

I applied changes to a couple of model objects. Could you update all the other model objects and the view ones accordingly?

Copy link
Member

@Reinmar Reinmar left a comment

Choose a reason for hiding this comment

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

As commented.

@Reinmar Reinmar merged commit 89dbe43 into master Aug 14, 2019
@Reinmar Reinmar deleted the t/1667 branch August 14, 2019 12:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
5 participants