Sfoglia il codice sorgente

Add 'is' method to model's classes.

Mateusz Samsel 6 anni fa
parent
commit
f642a04a0b

+ 10 - 0
packages/ckeditor5-engine/src/model/batch.js

@@ -79,4 +79,14 @@ export default class Batch {
 
 		return operation;
 	}
+
+	/**
+	 * Checks whether given object is of `batch` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'batch';
+	}
 }

+ 10 - 0
packages/ckeditor5-engine/src/model/differ.js

@@ -520,6 +520,16 @@ export default class Differ {
 	}
 
 	/**
+	 * Checks whether given object is of `differ` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'differ';
+	}
+
+	/**
 	 * Resets `Differ`. Removes all buffered changes.
 	 */
 	reset() {

+ 10 - 0
packages/ckeditor5-engine/src/model/document.js

@@ -242,6 +242,16 @@ export default class Document {
 	}
 
 	/**
+	 * Checks whether given object is of `document` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'document' || type == 'model:document';
+	}
+
+	/**
 	 * Used to register a post-fixer callback. A post-fixer mechanism guarantees that the features
 	 * will operate on a correct model state.
 	 *

+ 1 - 1
packages/ckeditor5-engine/src/model/documentfragment.js

@@ -124,7 +124,7 @@ export default class DocumentFragment {
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'documentFragment';
+		return type == 'documentFragment' || type == 'model:documentFragment';
 	}
 
 	/**

+ 8 - 1
packages/ckeditor5-engine/src/model/documentselection.js

@@ -374,15 +374,22 @@ export default class DocumentSelection {
 	 *		const selection = new DocumentSelection( ... );
 	 *
 	 *		selection.is( 'selection' ); // true
+	 *		selection.is( 'model:selection' ); // true
 	 *		selection.is( 'documentSelection' ); // true
+	 *		selection.is( 'model:documentSelection' ); // true
+	 *		selection.is( 'view:selection' ); // false
 	 *		selection.is( 'node' ); // false
+	 *		selection.is( 'model:node' ); // false
 	 *		selection.is( 'element' ); // false
 	 *
 	 * @param {String} type
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'selection' || type == 'documentSelection';
+		return type == 'selection' ||
+			type == 'model:selection' ||
+			type == 'documentSelection' ||
+			type == 'model:documentSelection';
 	}
 
 	/**

+ 8 - 2
packages/ckeditor5-engine/src/model/element.js

@@ -95,9 +95,14 @@ export default class Element extends Node {
 	 *		obj instanceof Element; // true
 	 *
 	 *		obj.is( 'element' ); // true
+	 *		obj.is( 'model:element' ); // true
 	 *		obj.is( 'listItem' ); // true
+	 *		obj.is( 'model:listItem' ); // true
 	 *		obj.is( 'element', 'listItem' ); // true
+	 *		obj.is( 'model:element', 'listItem' ); // true
 	 *		obj.is( 'text' ); // false
+	 *		obj.is( 'model:text' ); // false
+	 *		obj.is( 'view:element' ); // false
 	 *		obj.is( 'element', 'image' ); // false
 	 *
 	 * Read more in {@link module:engine/model/node~Node#is `Node#is()`}.
@@ -108,10 +113,11 @@ export default class Element extends Node {
 	 * @returns {Boolean}
 	 */
 	is( type, name = null ) {
+		const cutType = type && 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;
 		}
 	}
 

+ 10 - 0
packages/ckeditor5-engine/src/model/history.js

@@ -98,6 +98,16 @@ export default class History {
 	}
 
 	/**
+	 * Checks whether given object is of `history` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'history';
+	}
+
+	/**
 	 * Checks whether given `operation` is undoing any other operation.
 	 *
 	 * @param {module:engine/model/operation/operation~Operation} operation Operation to check.

+ 7 - 0
packages/ckeditor5-engine/src/model/liveposition.js

@@ -64,6 +64,13 @@ export default class LivePosition extends Position {
 	}
 
 	/**
+	 * @inheritDoc
+	 */
+	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.
 	 *
 	 * @returns {module:engine/model/position~Position}

+ 7 - 0
packages/ckeditor5-engine/src/model/liverange.js

@@ -41,6 +41,13 @@ export default class LiveRange extends Range {
 	}
 
 	/**
+	 * @inheritDoc
+	 */
+	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.
 	 *
 	 * @returns {module:engine/model/range~Range}

+ 20 - 0
packages/ckeditor5-engine/src/model/markercollection.js

@@ -73,6 +73,16 @@ export default class MarkerCollection {
 	}
 
 	/**
+	 * Checks whether given object is of `markerCollection` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'markerCollection';
+	}
+
+	/**
 	 * Creates and adds a {@link ~Marker marker} to the `MarkerCollection` with given name on given
 	 * {@link module:engine/model/range~Range range}.
 	 *
@@ -449,6 +459,16 @@ class Marker {
 	}
 
 	/**
+	 * Checks whether given object is of `marker` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'marker';
+	}
+
+	/**
 	 * Binds new live range to the marker and detach the old one if is attached.
 	 *
 	 * @protected

+ 10 - 0
packages/ckeditor5-engine/src/model/model.js

@@ -718,6 +718,16 @@ export default class Model {
 	}
 
 	/**
+	 * Checks whether given object is of `model` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'model';
+	}
+
+	/**
 	 * Common part of {@link module:engine/model/model~Model#change} and {@link module:engine/model/model~Model#enqueueChange}
 	 * which calls callbacks and returns array of values returned by these callbacks.
 	 *

+ 4 - 2
packages/ckeditor5-engine/src/model/node.js

@@ -467,6 +467,8 @@ export default class Node {
 	 * 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.
+	 * All checked types might be prefixed with `model:` to narrow search exclusively to model's objects.
+	 * That should prevent of situation where `view:node` accidentally might be considered as `model:node`.
 	 *
 	 *		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
@@ -477,11 +479,11 @@ export default class Node {
 	 *		obj.is( 'textProxy' ); // true for text proxy object
 	 *
 	 * @method #is
-	 * @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
+	 * @param {String} type
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'node';
+		return type == 'node' || type == 'model:node';
 	}
 }
 

+ 10 - 0
packages/ckeditor5-engine/src/model/nodelist.js

@@ -171,6 +171,16 @@ export default class NodeList {
 	}
 
 	/**
+	 * Checks whether given object is of `nodeList` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'nodeList' || type == 'model:nodeList';
+	}
+
+	/**
 	 * Inserts given nodes at given index.
 	 *
 	 * @protected

+ 10 - 0
packages/ckeditor5-engine/src/model/position.js

@@ -503,6 +503,16 @@ export default class Position {
 	}
 
 	/**
+	 * Checks whether given object is of `position` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'position' || type == 'model:position';
+	}
+
+	/**
 	 * Checks if two positions are in the same parent.
 	 *
 	 * This method is safe to use it on non-existing positions (for example during operational transformation).

+ 10 - 0
packages/ckeditor5-engine/src/model/range.js

@@ -144,6 +144,16 @@ export default class Range {
 	}
 
 	/**
+	 * Checks whether given object is of `range` type.
+	 *
+	 * @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.
 	 *
 	 * @param {module:engine/model/range~Range} otherRange Range to compare with.

+ 3 - 2
packages/ckeditor5-engine/src/model/rootelement.js

@@ -58,10 +58,11 @@ export default class RootElement extends Element {
 	 * @inheritDoc
 	 */
 	is( type, name ) {
+		const cutType = type && 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 );
 		}
 	}
 

+ 10 - 0
packages/ckeditor5-engine/src/model/schema.js

@@ -188,6 +188,16 @@ export default class Schema {
 	}
 
 	/**
+	 * Checks whether given object is of `schema` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'schema';
+	}
+
+	/**
 	 * Returns `true` if the given item is registered in the schema.
 	 *
 	 *		schema.isRegistered( 'paragraph' ); // -> true

+ 3 - 1
packages/ckeditor5-engine/src/model/selection.js

@@ -629,14 +629,16 @@ export default class Selection {
 	 *		const selection = new Selection( ... );
 	 *
 	 *		selection.is( 'selection' ); // true
+	 *		selection.is( 'model:selection' ); // true
 	 *		selection.is( 'node' ); // false
 	 *		selection.is( 'element' ); // false
+	 *		selection.is( 'view:selection' ); // false
 	 *
 	 * @param {String} type
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'selection';
+		return type == 'selection' || type == 'model:selection';
 	}
 
 	/**

+ 1 - 1
packages/ckeditor5-engine/src/model/text.js

@@ -66,7 +66,7 @@ export default class Text extends Node {
 	 * @inheritDoc
 	 */
 	is( type ) {
-		return type == 'text' || super.is( type );
+		return type == 'text' || type == 'model:text' || super.is( type );
 	}
 
 	/**

+ 2 - 2
packages/ckeditor5-engine/src/model/textproxy.js

@@ -173,7 +173,7 @@ export default class TextProxy {
 	}
 
 	/**
-	 * Checks whether given model tree object is of given type.
+	 * Checks whether given object is of `textProxy` type.
 	 *
 	 * Read more in {@link module:engine/model/node~Node#is}.
 	 *
@@ -181,7 +181,7 @@ export default class TextProxy {
 	 * @returns {Boolean}
 	 */
 	is( type ) {
-		return type == 'textProxy';
+		return type == 'textProxy' || type == 'model:textProxy';
 	}
 
 	/**

+ 10 - 0
packages/ckeditor5-engine/src/model/treewalker.js

@@ -153,6 +153,16 @@ export default class TreeWalker {
 	}
 
 	/**
+	 * Checks whether given object is of `treeWalker` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'treeWalker' || type == 'model:treeWalker';
+	}
+
+	/**
 	 * Iterable interface.
 	 *
 	 * @returns {Iterable.<module:engine/model/treewalker~TreeWalkerValue>}

+ 10 - 0
packages/ckeditor5-engine/src/model/writer.js

@@ -80,6 +80,16 @@ export default class Writer {
 	}
 
 	/**
+	 * Checks whether given object is of `writer` type.
+	 *
+	 * @param {String} type
+	 * @returns {Boolean}
+	 */
+	is( type ) {
+		return type == 'writer' || type == 'model:writer';
+	}
+
+	/**
 	 * Creates a new {@link module:engine/model/text~Text text node}.
 	 *
 	 *		writer.createText( 'foo' );