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

Commit

Permalink
Merge pull request #1736 from ckeditor/t/1667
Browse files Browse the repository at this point in the history
Feature: Introduced the `is()` method to additional objects from the model and view realms. Closes #1667.
  • Loading branch information
Reinmar authored Aug 14, 2019
2 parents b87e57b + 4315a6b commit 89dbe43
Show file tree
Hide file tree
Showing 56 changed files with 968 additions and 155 deletions.
13 changes: 10 additions & 3 deletions src/model/documentfragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,22 @@ export default class DocumentFragment {
}

/**
* Checks whether given model tree object is of given type.
* Checks whether this object is of the given type.
*
* Read more in {@link module:engine/model/node~Node#is}.
* docFrag.is( 'documentFragment' ); // -> true
* docFrag.is( 'model:documentFragment' ); // -> true
*
* docFrag.is( 'view:documentFragment' ); // -> false
* docFrag.is( 'element' ); // -> false
* docFrag.is( 'node' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'documentFragment';
return type == 'documentFragment' || type == 'model:documentFragment';
}

/**
Expand Down
22 changes: 14 additions & 8 deletions src/model/documentselection.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,21 +367,27 @@ export default class DocumentSelection {
}

/**
* Checks whether object is of given type following the convention set by
* {@link module:engine/model/node~Node#is `Node#is()`}.
* Checks whether this object is of the given type.
*
* const selection = new DocumentSelection( ... );
* selection.is( 'selection' ); // -> true
* selection.is( 'documentSelection' ); // -> true
* selection.is( 'model:selection' ); // -> true
* selection.is( 'model:documentSelection' ); // -> true
*
* selection.is( 'selection' ); // true
* selection.is( 'documentSelection' ); // true
* selection.is( 'node' ); // false
* selection.is( 'element' ); // false
* selection.is( 'view:selection' ); // -> false
* selection.is( 'element' ); // -> false
* selection.is( 'node' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'selection' || type == 'documentSelection';
return type == 'selection' ||
type == 'model:selection' ||
type == 'documentSelection' ||
type == 'model:documentSelection';
}

/**
Expand Down
30 changes: 19 additions & 11 deletions src/model/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,37 @@ export default class Element extends Node {
}

/**
* Checks whether this model object is of the given type.
* Checks whether this object is of the given.
*
* obj.name; // 'listItem'
* obj instanceof Element; // true
* element.is( 'element' ); // -> true
* element.is( 'node' ); // -> true
* element.is( 'model:element' ); // -> true
* element.is( 'model:node' ); // -> true
*
* obj.is( 'element' ); // true
* obj.is( 'listItem' ); // true
* obj.is( 'element', 'listItem' ); // true
* obj.is( 'text' ); // false
* obj.is( 'element', 'image' ); // false
* element.is( 'view:element' ); // -> false
* element.is( 'documentSelection' ); // -> false
*
* Read more in {@link module:engine/model/node~Node#is `Node#is()`}.
* Assuming that the object being checked is an element, you can also check its
* {@link module:engine/model/element~Element#name name}:
*
* element.is( 'image' ); // -> true if this is an <image> element
* element.is( 'element', 'image' ); // -> same as above
* text.is( 'image' ); -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type Type to check when `name` parameter is present.
* Otherwise, it acts like the `name` parameter.
* @param {String} [name] Element name.
* @returns {Boolean}
*/
is( type, name = null ) {
const cutType = type.replace( /^model:/, '' );

if ( !name ) {
return type == 'element' || type == this.name || super.is( type );
return cutType == 'element' || cutType == this.name || super.is( type );
} else {
return type == 'element' && name == this.name;
return cutType == 'element' && name == this.name;
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/model/liveposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ export default class LivePosition extends Position {
this.stopListening();
}

/**
* Checks whether this object is of the given.
*
* livePosition.is( 'position' ); // -> true
* livePosition.is( 'model:position' ); // -> true
* livePosition.is( 'liveposition' ); // -> true
* livePosition.is( 'model:livePosition' ); // -> true
*
* livePosition.is( 'view:position' ); // -> false
* livePosition.is( 'documentSelection' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'livePosition' || type == 'model:livePosition' || super.is( type );
}

/**
* Creates a {@link module:engine/model/position~Position position instance}, which is equal to this live position.
*
Expand Down
20 changes: 20 additions & 0 deletions src/model/liverange.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ export default class LiveRange extends Range {
this.stopListening();
}

/**
* Checks whether this object is of the given.
*
* liveRange.is( 'range' ); // -> true
* liveRange.is( 'model:range' ); // -> true
* liveRange.is( 'liveRange' ); // -> true
* liveRange.is( 'model:liveRange' ); // -> true
*
* liveRange.is( 'view:range' ); // -> false
* liveRange.is( 'documentSelection' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'liveRange' || type == 'model:liveRange' || super.is( type );
}

/**
* Creates a {@link module:engine/model/range~Range range instance} that is equal to this live range.
*
Expand Down
18 changes: 18 additions & 0 deletions src/model/markercollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,24 @@ class Marker {
return this._liveRange.toRange();
}

/**
* Checks whether this object is of the given.
*
* marker.is( 'marker' ); // -> true
* marker.is( 'model:marker' ); // -> true
*
* marker.is( 'view:element' ); // -> false
* marker.is( 'documentSelection' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'marker' || type == 'model:marker';
}

/**
* Binds new live range to the marker and detach the old one if is attached.
*
Expand Down
70 changes: 47 additions & 23 deletions src/model/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,53 @@ export default class Node {
return json;
}

/**
* Checks whether this object is of the given type.
*
* This method is useful when processing model objects that are of unknown type. For example, a function
* may return a {@link module:engine/model/documentfragment~DocumentFragment} or a {@link module:engine/model/node~Node}
* that can be either a text node or an element. This method can be used to check what kind of object is returned.
*
* someObject.is( 'element' ); // -> true if this is an element
* someObject.is( 'node' ); // -> true if this is a node (a text node or an element)
* someObject.is( 'documentFragment' ); // -> true if this is a document fragment
*
* Since this method is also available on a range of view objects, you can prefix the type of the object with
* `model:` or `view:` to check, for example, if this is the model's or view's element:
*
* modelElement.is( 'model:element' ); // -> true
* modelElement.is( 'view:element' ); // -> false
*
* By using this method it is also possible to check a name of an element:
*
* imageElement.is( 'image' ); // -> true
* imageElement.is( 'element', 'image' ); // -> same as above
* imageElement.is( 'model:element', 'image' ); // -> same as above, but more precise
*
* The list of model objects which implement the `is()` method:
*
* * {@link module:engine/model/node~Node#is `Node#is()`}
* * {@link module:engine/model/text~Text#is `Text#is()`}
* * {@link module:engine/model/element~Element#is `Element#is()`}
* * {@link module:engine/model/rootelement~RootElement#is `RootElement#is()`}
* * {@link module:engine/model/position~Position#is `Position#is()`}
* * {@link module:engine/model/liveposition~LivePosition#is `LivePosition#is()`}
* * {@link module:engine/model/range~Range#is `Range#is()`}
* * {@link module:engine/model/liverange~LiveRange#is `LiveRange#is()`}
* * {@link module:engine/model/documentfragment~DocumentFragment#is `DocumentFragment#is()`}
* * {@link module:engine/model/selection~Selection#is `Selection#is()`}
* * {@link module:engine/model/documentselection~DocumentSelection#is `DocumentSelection#is()`}
* * {@link module:engine/model/markercollection~Marker#is `Marker#is()`}
* * {@link module:engine/model/textproxy~TextProxy#is `TextProxy#is()`}
*
* @method #is
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'node' || type == 'model:node';
}

/**
* Creates a copy of this node, that is a node with exactly same attributes, and returns it.
*
Expand Down Expand Up @@ -460,29 +507,6 @@ export default class Node {
_clearAttributes() {
this._attrs.clear();
}

/**
* Checks whether given model tree object is of given type.
*
* This method is useful when processing model tree objects that are of unknown type. For example, a function
* may return {@link module:engine/model/documentfragment~DocumentFragment} or {@link module:engine/model/node~Node}
* that can be either text node or element. This method can be used to check what kind of object is returned.
*
* obj.is( 'node' ); // true for any node, false for document fragment and text fragment
* obj.is( 'documentFragment' ); // true for document fragment, false for any node
* obj.is( 'element' ); // true for any element, false for text node or document fragment
* obj.is( 'element', 'paragraph' ); // true only for element which name is 'paragraph'
* obj.is( 'paragraph' ); // shortcut for obj.is( 'element', 'paragraph' )
* obj.is( 'text' ); // true for text node, false for element and document fragment
* obj.is( 'textProxy' ); // true for text proxy object
*
* @method #is
* @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
* @returns {Boolean}
*/
is( type ) {
return type == 'node';
}
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/model/position.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,24 @@ export default class Position {
}
}

/**
* Checks whether this object is of the given.
*
* position.is( 'position' ); // -> true
* position.is( 'model:position' ); // -> true
*
* position.is( 'view:position' ); // -> false
* position.is( 'documentSelection' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'position' || type == 'model:position';
}

/**
* Checks if two positions are in the same parent.
*
Expand Down
18 changes: 18 additions & 0 deletions src/model/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,24 @@ export default class Range {
return this.containsPosition( pos ) || this.start.isEqual( pos );
}

/**
* Checks whether this object is of the given.
*
* range.is( 'range' ); // -> true
* range.is( 'model:range' ); // -> true
*
* range.is( 'view:range' ); // -> false
* range.is( 'documentSelection' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'range' || type == 'model:range';
}

/**
* Two ranges are equal if their {@link #start} and {@link #end} positions are equal.
*
Expand Down
31 changes: 28 additions & 3 deletions src/model/rootelement.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,38 @@ export default class RootElement extends Element {
}

/**
* @inheritDoc
* Checks whether this object is of the given.
*
* rootElement.is( 'rootElement' ); // -> true
* rootElement.is( 'element' ); // -> true
* rootElement.is( 'node' ); // -> true
* rootElement.is( 'model:rootElement' ); // -> true
* rootElement.is( 'model:element' ); // -> true
* rootElement.is( 'model:node' ); // -> true
*
* rootElement.is( 'view:element' ); // -> false
* rootElement.is( 'documentFragment' ); // -> false
*
* Assuming that the object being checked is an element, you can also check its
* {@link module:engine/model/element~Element#name name}:
*
* rootElement.is( '$root' ); // -> true if this is a $root element
* rootElement.is( 'rootElement', '$root' ); // -> same as above
* text.is( '$root' ); -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type Type to check when `name` parameter is present.
* Otherwise, it acts like the `name` parameter.
* @param {String} [name] Element name.
* @returns {Boolean}
*/
is( type, name ) {
const cutType = type.replace( 'model:', '' );
if ( !name ) {
return type == 'rootElement' || super.is( type );
return cutType == 'rootElement' || super.is( type );
} else {
return ( type == 'rootElement' && name == this.name ) || super.is( type, name );
return ( cutType == 'rootElement' && name == this.name ) || super.is( type, name );
}
}

Expand Down
15 changes: 8 additions & 7 deletions src/model/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,20 +623,21 @@ export default class Selection {
}

/**
* Checks whether object is of given type following the convention set by
* {@link module:engine/model/node~Node#is `Node#is()`}.
* Checks whether this object is of the given.
*
* const selection = new Selection( ... );
* selection.is( 'selection' ); // -> true
* selection.is( 'model:selection' ); // -> true
*
* selection.is( 'selection' ); // true
* selection.is( 'node' ); // false
* selection.is( 'element' ); // false
* selection.is( 'view:selection' ); // -> false
* selection.is( 'range' ); // -> false
*
* {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
*
* @param {String} type
* @returns {Boolean}
*/
is( type ) {
return type == 'selection';
return type == 'selection' || type == 'model:selection';
}

/**
Expand Down
Loading

0 comments on commit 89dbe43

Please sign in to comment.