From 3816afb98d7273a6418538c788bc8e91a1921589 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 8 May 2019 13:36:56 +0200 Subject: [PATCH 01/30] Add 'is' method to model's classes. --- src/model/batch.js | 10 ++++++++++ src/model/differ.js | 10 ++++++++++ src/model/document.js | 10 ++++++++++ src/model/documentfragment.js | 2 +- src/model/documentselection.js | 9 ++++++++- src/model/element.js | 10 ++++++++-- src/model/history.js | 10 ++++++++++ src/model/liveposition.js | 7 +++++++ src/model/liverange.js | 7 +++++++ src/model/markercollection.js | 20 ++++++++++++++++++++ src/model/model.js | 10 ++++++++++ src/model/node.js | 6 ++++-- src/model/nodelist.js | 10 ++++++++++ src/model/position.js | 10 ++++++++++ src/model/range.js | 10 ++++++++++ src/model/rootelement.js | 5 +++-- src/model/schema.js | 10 ++++++++++ src/model/selection.js | 4 +++- src/model/text.js | 2 +- src/model/textproxy.js | 4 ++-- src/model/treewalker.js | 10 ++++++++++ src/model/writer.js | 10 ++++++++++ 22 files changed, 174 insertions(+), 12 deletions(-) diff --git a/src/model/batch.js b/src/model/batch.js index ed5a5c517..67bc16c51 100644 --- a/src/model/batch.js +++ b/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'; + } } diff --git a/src/model/differ.js b/src/model/differ.js index b1dfce3e4..c5d7316cd 100644 --- a/src/model/differ.js +++ b/src/model/differ.js @@ -519,6 +519,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. */ diff --git a/src/model/document.js b/src/model/document.js index 9f0d646c9..ade127b14 100644 --- a/src/model/document.js +++ b/src/model/document.js @@ -241,6 +241,16 @@ export default class Document { return Array.from( this.roots, root => root.rootName ).filter( name => name != graveyardName ); } + /** + * 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. diff --git a/src/model/documentfragment.js b/src/model/documentfragment.js index f6eff72d2..ca6bd008c 100644 --- a/src/model/documentfragment.js +++ b/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'; } /** diff --git a/src/model/documentselection.js b/src/model/documentselection.js index 4cbddca10..d339a3485 100644 --- a/src/model/documentselection.js +++ b/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'; } /** diff --git a/src/model/element.js b/src/model/element.js index 412575782..4aa6c38da 100644 --- a/src/model/element.js +++ b/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; } } diff --git a/src/model/history.js b/src/model/history.js index d51d54e29..2b2df4c88 100644 --- a/src/model/history.js +++ b/src/model/history.js @@ -97,6 +97,16 @@ export default class History { this._undoneOperations.add( undoneOperation ); } + /** + * 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. * diff --git a/src/model/liveposition.js b/src/model/liveposition.js index fba8d41c1..4bfbbffd2 100644 --- a/src/model/liveposition.js +++ b/src/model/liveposition.js @@ -63,6 +63,13 @@ export default class LivePosition extends Position { this.stopListening(); } + /** + * @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. * diff --git a/src/model/liverange.js b/src/model/liverange.js index 62385ea0b..98b57b781 100644 --- a/src/model/liverange.js +++ b/src/model/liverange.js @@ -40,6 +40,13 @@ export default class LiveRange extends Range { this.stopListening(); } + /** + * @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. * diff --git a/src/model/markercollection.js b/src/model/markercollection.js index 8909026b4..3a565904f 100644 --- a/src/model/markercollection.js +++ b/src/model/markercollection.js @@ -72,6 +72,16 @@ export default class MarkerCollection { return this._markers.get( markerName ) || null; } + /** + * 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}. @@ -448,6 +458,16 @@ class Marker { return this._liveRange.toRange(); } + /** + * 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. * diff --git a/src/model/model.js b/src/model/model.js index 61a6b73f0..bddede4a4 100644 --- a/src/model/model.js +++ b/src/model/model.js @@ -717,6 +717,16 @@ export default class Model { this.stopListening(); } + /** + * 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. diff --git a/src/model/node.js b/src/model/node.js index feb9cba38..d426b8b43 100644 --- a/src/model/node.js +++ b/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'; } } diff --git a/src/model/nodelist.js b/src/model/nodelist.js index d6c631c6f..b57383007 100644 --- a/src/model/nodelist.js +++ b/src/model/nodelist.js @@ -170,6 +170,16 @@ export default class NodeList { return this.length; } + /** + * 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. * diff --git a/src/model/position.js b/src/model/position.js index 861ad9ea5..2ed8d8110 100644 --- a/src/model/position.js +++ b/src/model/position.js @@ -502,6 +502,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. * diff --git a/src/model/range.js b/src/model/range.js index 2e7af53da..1974c787e 100644 --- a/src/model/range.js +++ b/src/model/range.js @@ -143,6 +143,16 @@ export default class Range { return this.containsPosition( pos ) || this.start.isEqual( pos ); } + /** + * 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. * diff --git a/src/model/rootelement.js b/src/model/rootelement.js index 72e002d3e..d00a44b51 100644 --- a/src/model/rootelement.js +++ b/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 ); } } diff --git a/src/model/schema.js b/src/model/schema.js index 3c407fe94..8c2cef269 100644 --- a/src/model/schema.js +++ b/src/model/schema.js @@ -187,6 +187,16 @@ export default class Schema { return this.getDefinitions()[ itemName ]; } + /** + * 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. * diff --git a/src/model/selection.js b/src/model/selection.js index 83ee0bc44..f5415fcae 100644 --- a/src/model/selection.js +++ b/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'; } /** diff --git a/src/model/text.js b/src/model/text.js index afc7560bc..ea487552f 100644 --- a/src/model/text.js +++ b/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 ); } /** diff --git a/src/model/textproxy.js b/src/model/textproxy.js index a59e99df4..004f8c4d4 100644 --- a/src/model/textproxy.js +++ b/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'; } /** diff --git a/src/model/treewalker.js b/src/model/treewalker.js index d56004513..7dad34271 100644 --- a/src/model/treewalker.js +++ b/src/model/treewalker.js @@ -152,6 +152,16 @@ export default class TreeWalker { this._visitedParent = this.position.parent; } + /** + * Checks whether given object is of `treeWalker` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'treeWalker' || type == 'model:treeWalker'; + } + /** * Iterable interface. * diff --git a/src/model/writer.js b/src/model/writer.js index 9e28394ac..df2580f6c 100644 --- a/src/model/writer.js +++ b/src/model/writer.js @@ -79,6 +79,16 @@ export default class Writer { this.batch = batch; } + /** + * 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}. * From b8c10d84b1d1e4aedf91888200907b8742775506 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 8 May 2019 16:49:27 +0200 Subject: [PATCH 02/30] Add unit test for 'is' methods. --- tests/model/batch.js | 20 +++++++++++++++++++ tests/model/differ.js | 14 +++++++++++++ tests/model/document.js | 16 +++++++++++++++ tests/model/documentfragment.js | 5 ++++- tests/model/documentselection.js | 7 +++++++ tests/model/element.js | 12 +++++++++++ tests/model/history.js | 14 +++++++++++++ tests/model/liveposition.js | 24 ++++++++++++++++++++++ tests/model/liverange.js | 23 +++++++++++++++++++++ tests/model/markercollection.js | 34 ++++++++++++++++++++++++++++++++ tests/model/model.js | 14 +++++++++++++ tests/model/node.js | 9 +++++++++ tests/model/nodelist.js | 15 ++++++++++++++ tests/model/position.js | 20 +++++++++++++++++++ tests/model/range.js | 15 ++++++++++++++ tests/model/rootelement.js | 11 +++++++++++ tests/model/schema.js | 14 +++++++++++++ tests/model/selection.js | 21 ++++++++++++++++++++ tests/model/text.js | 4 ++++ tests/model/textproxy.js | 4 ++++ tests/model/treewalker.js | 21 ++++++++++++++++++++ tests/model/writer.js | 21 ++++++++++++++++++++ 22 files changed, 337 insertions(+), 1 deletion(-) diff --git a/tests/model/batch.js b/tests/model/batch.js index b6f64baa2..9b9d62a0e 100644 --- a/tests/model/batch.js +++ b/tests/model/batch.js @@ -60,4 +60,24 @@ describe( 'Batch', () => { expect( batch.baseVersion ).to.equal( null ); } ); } ); + + describe( 'is()', () => { + let batch; + + beforeEach( () => { + batch = new Batch(); + } ); + + it( 'should return true for "batch"', () => { + expect( batch.is( 'batch' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( batch.is( 'model' ) ).to.be.false; + expect( batch.is( 'node' ) ).to.be.false; + expect( batch.is( 'model:element' ) ).to.be.false; + expect( batch.is( 'element', 'paragraph' ) ).to.be.false; + expect( batch.is() ).to.be.false; + } ); + } ); } ); diff --git a/tests/model/differ.js b/tests/model/differ.js index 2ac12d0dc..4ef97ca54 100644 --- a/tests/model/differ.js +++ b/tests/model/differ.js @@ -36,6 +36,20 @@ describe( 'Differ', () => { ] ); } ); + describe( 'is()', () => { + it( 'should return true for "differ"', () => { + expect( differ.is( 'differ' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( differ.is( 'model' ) ).to.be.false; + expect( differ.is( 'model:node' ) ).to.be.false; + expect( differ.is( 'text' ) ).to.be.false; + expect( differ.is( 'element', 'paragraph' ) ).to.be.false; + expect( differ.is() ).to.be.false; + } ); + } ); + describe( 'insert', () => { // Simple. it( 'an element', () => { diff --git a/tests/model/document.js b/tests/model/document.js index e7c79d71b..04b75a591 100644 --- a/tests/model/document.js +++ b/tests/model/document.js @@ -499,4 +499,20 @@ describe( 'Document', () => { expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' ); expect( serialized.model ).to.equal( '[engine.model.Model]' ); } ); + + describe( 'is()', () => { + it( 'should return true for "document"', () => { + expect( doc.is( 'document' ) ).to.be.true; + expect( doc.is( 'model:document' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( doc.is( 'model' ) ).to.be.false; + expect( doc.is( 'node' ) ).to.be.false; + expect( doc.is( 'model:node' ) ).to.be.false; + expect( doc.is( 'view:document' ) ).to.be.false; + expect( doc.is( 'element', 'text' ) ).to.be.false; + expect( doc.is() ).to.be.false; + } ); + } ); } ); diff --git a/tests/model/documentfragment.js b/tests/model/documentfragment.js index e9c07e797..0608c9916 100644 --- a/tests/model/documentfragment.js +++ b/tests/model/documentfragment.js @@ -70,14 +70,17 @@ describe( 'DocumentFragment', () => { it( 'should return true for documentFragment', () => { expect( frag.is( 'documentFragment' ) ).to.be.true; + expect( frag.is( 'model:documentFragment' ) ).to.be.true; } ); - it( 'should return false for other accept values', () => { + it( 'should return false for other values', () => { expect( frag.is( 'node' ) ).to.be.false; expect( frag.is( 'text' ) ).to.be.false; expect( frag.is( 'textProxy' ) ).to.be.false; expect( frag.is( 'element' ) ).to.be.false; expect( frag.is( 'rootElement' ) ).to.be.false; + expect( frag.is( 'view:documentFragment' ) ).to.be.false; + expect( frag.is() ).to.be.false; } ); } ); diff --git a/tests/model/documentselection.js b/tests/model/documentselection.js index b25cd66a8..2a86b0680 100644 --- a/tests/model/documentselection.js +++ b/tests/model/documentselection.js @@ -493,18 +493,25 @@ describe( 'DocumentSelection', () => { describe( 'is', () => { it( 'should return true for selection', () => { expect( selection.is( 'selection' ) ).to.be.true; + expect( selection.is( 'model:selection' ) ).to.be.true; } ); it( 'should return true for documentSelection', () => { expect( selection.is( 'documentSelection' ) ).to.be.true; + expect( selection.is( 'model:documentSelection' ) ).to.be.true; } ); it( 'should return false for other values', () => { expect( selection.is( 'node' ) ).to.be.false; + expect( selection.is( 'model:node' ) ).to.be.false; expect( selection.is( 'text' ) ).to.be.false; expect( selection.is( 'textProxy' ) ).to.be.false; expect( selection.is( 'element' ) ).to.be.false; + expect( selection.is( 'element', 'paragraph' ) ).to.be.false; expect( selection.is( 'rootElement' ) ).to.be.false; + expect( selection.is( 'view:selection' ) ).to.be.false; + expect( selection.is( 'view:documentSelection' ) ).to.be.false; + expect( selection.is() ).to.be.false; } ); } ); diff --git a/tests/model/element.js b/tests/model/element.js index cac079910..e841b620a 100644 --- a/tests/model/element.js +++ b/tests/model/element.js @@ -47,18 +47,30 @@ describe( 'Element', () => { it( 'should return true for node, element, element with same name and element name', () => { expect( element.is( 'node' ) ).to.be.true; + expect( element.is( 'model:node' ) ).to.be.true; expect( element.is( 'element' ) ).to.be.true; + expect( element.is( 'model:element' ) ).to.be.true; expect( element.is( 'element', 'paragraph' ) ).to.be.true; + expect( element.is( 'model:element', 'paragraph' ) ).to.be.true; expect( element.is( 'paragraph' ) ).to.be.true; + expect( element.is( 'model:paragraph' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( element.is( 'element', 'image' ) ).to.be.false; + expect( element.is( 'model:element', 'image' ) ).to.be.false; expect( element.is( 'image' ) ).to.be.false; + expect( element.is( 'model:image' ) ).to.be.false; expect( element.is( 'text' ) ).to.be.false; + expect( element.is( 'model:text' ) ).to.be.false; expect( element.is( 'textProxy' ) ).to.be.false; expect( element.is( 'documentFragment' ) ).to.be.false; expect( element.is( 'rootElement' ) ).to.be.false; + expect( element.is( 'model:rootElement' ) ).to.be.false; + expect( element.is( 'view:node' ) ).to.be.false; + expect( element.is( 'view:element' ) ).to.be.false; + expect( element.is( 'view:element' ) ).to.be.false; + expect( element.is() ).to.be.false; } ); } ); diff --git a/tests/model/history.js b/tests/model/history.js index da028a6d6..5135c8a7a 100644 --- a/tests/model/history.js +++ b/tests/model/history.js @@ -178,6 +178,20 @@ describe( 'History', () => { expect( history.getUndoneOperation( op ) ).to.be.undefined; } ); } ); + + describe( 'is()', () => { + it( 'should return true for "history"', () => { + expect( history.is( 'history' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( history.is( 'model' ) ).to.be.false; + expect( history.is( 'model:node' ) ).to.be.false; + expect( history.is( 'text' ) ).to.be.false; + expect( history.is( 'element', 'paragraph' ) ).to.be.false; + expect( history.is() ).to.be.false; + } ); + } ); } ); function getOperations() { diff --git a/tests/model/liveposition.js b/tests/model/liveposition.js index e2115d904..93e387e11 100644 --- a/tests/model/liveposition.js +++ b/tests/model/liveposition.js @@ -41,6 +41,30 @@ describe( 'LivePosition', () => expect( live ).to.be.instanceof( Position ); } ); + describe( 'is()', () => { + let live; + + beforeEach( () => { + live = new LivePosition( root, [ 0 ] ); + live.detach(); + } ); + + it( 'should return true for "livePosition" and "position"', () => { + expect( live.is( 'livePosition' ) ).to.be.true; + expect( live.is( 'model:livePosition' ) ).to.be.true; + expect( live.is( 'position' ) ).to.be.true; + expect( live.is( 'model:position' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( live.is( 'model' ) ).to.be.false; + expect( live.is( 'model:node' ) ).to.be.false; + expect( live.is( 'text' ) ).to.be.false; + expect( live.is( 'element', 'paragraph' ) ).to.be.false; + expect( live.is() ).to.be.false; + } ); + } ); + it( 'should throw if given root is not a RootElement', () => { const docFrag = new DocumentFragment(); diff --git a/tests/model/liverange.js b/tests/model/liverange.js index 67e8a53cf..fb9a6c66b 100644 --- a/tests/model/liverange.js +++ b/tests/model/liverange.js @@ -183,6 +183,29 @@ describe( 'LiveRange', () => { expect( spy.args[ 1 ][ 2 ].deletionPosition.isEqual( new Position( root, [ 0 ] ) ) ).to.be.true; } ); + describe( 'is()', () => { + let live; + beforeEach( () => { + live = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ); + live.detach(); + } ); + + it( 'should return true for "liveRange" and "range"', () => { + expect( live.is( 'liveRange' ) ).to.be.true; + expect( live.is( 'model:liveRange' ) ).to.be.true; + expect( live.is( 'range' ) ).to.be.true; + expect( live.is( 'model:range' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( live.is( 'model' ) ).to.be.false; + expect( live.is( 'model:node' ) ).to.be.false; + expect( live.is( 'text' ) ).to.be.false; + expect( live.is( 'element', 'paragraph' ) ).to.be.false; + expect( live.is() ).to.be.false; + } ); + } ); + describe( 'should get transformed and fire change:range if', () => { let live, spy; diff --git a/tests/model/markercollection.js b/tests/model/markercollection.js index a5e730677..53500de46 100644 --- a/tests/model/markercollection.js +++ b/tests/model/markercollection.js @@ -146,6 +146,20 @@ describe( 'MarkerCollection', () => { } ); } ); + describe( 'is()', () => { + it( 'should return true for "markerCollection"', () => { + expect( markers.is( 'markerCollection' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( markers.is( 'model' ) ).to.be.false; + expect( markers.is( 'model:node' ) ).to.be.false; + expect( markers.is( 'text' ) ).to.be.false; + expect( markers.is( 'element', 'paragraph' ) ).to.be.false; + expect( markers.is() ).to.be.false; + } ); + } ); + describe( '_remove', () => { it( 'should remove marker, return true and fire update: event', () => { const marker = markers._set( 'name', range ); @@ -410,4 +424,24 @@ describe( 'Marker', () => { expect( marker.affectsData ).to.be.false; } ); + + describe( 'is()', () => { + let marker; + beforeEach( () => { + const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ); + marker = model.markers._set( 'name', range ); + } ); + + it( 'should return true for "marker"', () => { + expect( marker.is( 'marker' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( marker.is( 'model' ) ).to.be.false; + expect( marker.is( 'model:node' ) ).to.be.false; + expect( marker.is( 'text' ) ).to.be.false; + expect( marker.is( 'element', 'paragraph' ) ).to.be.false; + expect( marker.is() ).to.be.false; + } ); + } ); } ); diff --git a/tests/model/model.js b/tests/model/model.js index 2762e8163..cd483e6be 100644 --- a/tests/model/model.js +++ b/tests/model/model.js @@ -808,4 +808,18 @@ describe( 'Model', () => { sinon.assert.notCalled( spy ); } ); } ); + + describe( 'is()', () => { + it( 'should return true for "model"', () => { + expect( model.is( 'model' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( model.is( 'view' ) ).to.be.false; + expect( model.is( 'model:node' ) ).to.be.false; + expect( model.is( 'text' ) ).to.be.false; + expect( model.is( 'element', 'paragraph' ) ).to.be.false; + expect( model.is() ).to.be.false; + } ); + } ); } ); diff --git a/tests/model/node.js b/tests/model/node.js index 54bd6f545..7f3bfb263 100644 --- a/tests/model/node.js +++ b/tests/model/node.js @@ -165,6 +165,15 @@ describe( 'Node', () => { describe( 'is()', () => { it( 'should return true for node', () => { expect( node.is( 'node' ) ).to.be.true; + expect( node.is( 'model:node' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( node.is( 'model' ) ).to.be.false; + expect( node.is( 'model:text' ) ).to.be.false; + expect( node.is( 'text' ) ).to.be.false; + expect( node.is( 'element', 'paragraph' ) ).to.be.false; + expect( node.is() ).to.be.false; } ); } ); diff --git a/tests/model/nodelist.js b/tests/model/nodelist.js index fe9ca7f13..9578ad2ab 100644 --- a/tests/model/nodelist.js +++ b/tests/model/nodelist.js @@ -32,6 +32,21 @@ describe( 'NodeList', () => { } ); } ); + describe( 'is()', () => { + it( 'should return true for "nodeList"', () => { + expect( nodes.is( 'nodeList' ) ).to.be.true; + expect( nodes.is( 'model:nodeList' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( nodes.is( 'model' ) ).to.be.false; + expect( nodes.is( 'model:node' ) ).to.be.false; + expect( nodes.is( 'text' ) ).to.be.false; + expect( nodes.is( 'element', 'paragraph' ) ).to.be.false; + expect( nodes.is() ).to.be.false; + } ); + } ); + describe( 'iterator', () => { it( 'should iterate over all nodes from node list', () => { expect( Array.from( nodes ) ).to.deep.equal( [ p, foo, img ] ); diff --git a/tests/model/position.js b/tests/model/position.js index 3f7bb9c5b..67433929c 100644 --- a/tests/model/position.js +++ b/tests/model/position.js @@ -128,6 +128,26 @@ describe( 'Position', () => { } ); } ); + describe( 'is()', () => { + let position; + beforeEach( () => { + position = new Position( root, [ 0 ] ); + } ); + + it( 'should return true for "position"', () => { + expect( position.is( 'position' ) ).to.be.true; + expect( position.is( 'model:position' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( position.is( 'model' ) ).to.be.false; + expect( position.is( 'model:node' ) ).to.be.false; + expect( position.is( 'text' ) ).to.be.false; + expect( position.is( 'element', 'paragraph' ) ).to.be.false; + expect( position.is() ).to.be.false; + } ); + } ); + describe( 'static creators', () => { describe( '_createAt()', () => { it( 'should throw if no offset is passed', () => { diff --git a/tests/model/range.js b/tests/model/range.js index 7409c0617..2a0b0ac5a 100644 --- a/tests/model/range.js +++ b/tests/model/range.js @@ -50,6 +50,21 @@ describe( 'Range', () => { } ); } ); + describe( 'is()', () => { + it( 'should return true for "range"', () => { + expect( range.is( 'range' ) ).to.be.true; + expect( range.is( 'model:range' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( range.is( 'model' ) ).to.be.false; + expect( range.is( 'model:node' ) ).to.be.false; + expect( range.is( 'text' ) ).to.be.false; + expect( range.is( 'element', 'paragraph' ) ).to.be.false; + expect( range.is() ).to.be.false; + } ); + } ); + describe( 'root', () => { it( 'should be equal to start position root', () => { expect( range.root ).to.equal( start.root ); diff --git a/tests/model/rootelement.js b/tests/model/rootelement.js index 2bed59cac..7547345ad 100644 --- a/tests/model/rootelement.js +++ b/tests/model/rootelement.js @@ -34,19 +34,30 @@ describe( 'RootElement', () => { it( 'should return true for rootElement, element, element with same name and element name', () => { expect( root.is( 'element', '$root' ) ).to.be.true; + expect( root.is( 'model:element', '$root' ) ).to.be.true; expect( root.is( 'element' ) ).to.be.true; + expect( root.is( 'model:element' ) ).to.be.true; expect( root.is( '$root' ) ).to.be.true; + expect( root.is( 'model:$root' ) ).to.be.true; expect( root.is( 'rootElement', '$root' ) ).to.be.true; + expect( root.is( 'model:rootElement', '$root' ) ).to.be.true; expect( root.is( 'rootElement' ) ).to.be.true; + expect( root.is( 'model:rootElement' ) ).to.be.true; + expect( root.is( 'node' ) ).to.be.true; + expect( root.is( 'model:node' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( root.is( 'element', '$graveyard' ) ).to.be.false; + expect( root.is( 'model:element', '$graveyard' ) ).to.be.false; expect( root.is( 'rootElement', '$graveyard' ) ).to.be.false; + expect( root.is( 'model:rootElement', '$graveyard' ) ).to.be.false; expect( root.is( '$graveyard' ) ).to.be.false; expect( root.is( 'text' ) ).to.be.false; expect( root.is( 'textProxy' ) ).to.be.false; expect( root.is( 'documentFragment' ) ).to.be.false; + expect( root.is( 'view:element' ) ).to.be.false; + expect( root.is() ).to.be.false; } ); } ); } ); diff --git a/tests/model/schema.js b/tests/model/schema.js index ce52e35ef..441095ca5 100644 --- a/tests/model/schema.js +++ b/tests/model/schema.js @@ -311,6 +311,20 @@ describe( 'Schema', () => { } ); } ); + describe( 'is()', () => { + it( 'should return true for "schema"', () => { + expect( schema.is( 'schema' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( schema.is( 'model' ) ).to.be.false; + expect( schema.is( 'model:node' ) ).to.be.false; + expect( schema.is( 'text' ) ).to.be.false; + expect( schema.is( 'element', 'paragraph' ) ).to.be.false; + expect( schema.is() ).to.be.false; + } ); + } ); + describe( 'isRegistered()', () => { it( 'returns true if an item was registered', () => { schema.register( 'foo' ); diff --git a/tests/model/selection.js b/tests/model/selection.js index 4586001fc..b387079e0 100644 --- a/tests/model/selection.js +++ b/tests/model/selection.js @@ -127,6 +127,27 @@ describe( 'Selection', () => { } ); } ); + describe( 'is()', () => { + let selection; + + beforeEach( () => { + selection = new Selection(); + } ); + + it( 'should return true for "selection"', () => { + expect( selection.is( 'selection' ) ).to.be.true; + expect( selection.is( 'model:selection' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( selection.is( 'model' ) ).to.be.false; + expect( selection.is( 'model:node' ) ).to.be.false; + expect( selection.is( 'text' ) ).to.be.false; + expect( selection.is( 'element', 'paragraph' ) ).to.be.false; + expect( selection.is() ).to.be.false; + } ); + } ); + describe( 'isCollapsed', () => { it( 'should return false for empty selection', () => { expect( selection.isCollapsed ).to.be.false; diff --git a/tests/model/text.js b/tests/model/text.js index 16973c119..190d55060 100644 --- a/tests/model/text.js +++ b/tests/model/text.js @@ -41,14 +41,18 @@ describe( 'Text', () => { it( 'should return true for node, text', () => { expect( text.is( 'node' ) ).to.be.true; + expect( text.is( 'model:node' ) ).to.be.true; expect( text.is( 'text' ) ).to.be.true; + expect( text.is( 'model:text' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( text.is( 'textProxy' ) ).to.be.false; expect( text.is( 'element' ) ).to.be.false; + expect( text.is( 'model:element' ) ).to.be.false; expect( text.is( 'rootElement' ) ).to.be.false; expect( text.is( 'documentFragment' ) ).to.be.false; + expect( text.is() ).to.be.false; } ); } ); diff --git a/tests/model/textproxy.js b/tests/model/textproxy.js index 3602f7b72..c9101cb86 100644 --- a/tests/model/textproxy.js +++ b/tests/model/textproxy.js @@ -105,14 +105,18 @@ describe( 'TextProxy', () => { describe( 'is', () => { it( 'should return true for textProxy', () => { expect( textProxy.is( 'textProxy' ) ).to.be.true; + expect( textProxy.is( 'model:textProxy' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( textProxy.is( 'node' ) ).to.be.false; + expect( textProxy.is( 'model:node' ) ).to.be.false; expect( textProxy.is( 'text' ) ).to.be.false; expect( textProxy.is( 'element' ) ).to.be.false; + expect( textProxy.is( 'model:element', 'image' ) ).to.be.false; expect( textProxy.is( 'documentFragment' ) ).to.be.false; expect( textProxy.is( 'rootElement' ) ).to.be.false; + expect( textProxy.is() ).to.be.false; } ); } ); diff --git a/tests/model/treewalker.js b/tests/model/treewalker.js index f865ff237..e249dcb7f 100644 --- a/tests/model/treewalker.js +++ b/tests/model/treewalker.js @@ -69,6 +69,27 @@ describe( 'TreeWalker', () => { } ); } ); + describe( 'is()', () => { + let treeWalker; + + beforeEach( () => { + treeWalker = new TreeWalker( { startPosition: rootBeginning } ); + } ); + + it( 'should return true for "treeWalker"', () => { + expect( treeWalker.is( 'treeWalker' ) ).to.be.true; + expect( treeWalker.is( 'model:treeWalker' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( treeWalker.is( 'model' ) ).to.be.false; + expect( treeWalker.is( 'model:node' ) ).to.be.false; + expect( treeWalker.is( 'text' ) ).to.be.false; + expect( treeWalker.is( 'element', 'paragraph' ) ).to.be.false; + expect( treeWalker.is() ).to.be.false; + } ); + } ); + describe( 'iterate from start position `startPosition`', () => { let expected; diff --git a/tests/model/writer.js b/tests/model/writer.js index e557d7ba3..d151fd64a 100644 --- a/tests/model/writer.js +++ b/tests/model/writer.js @@ -86,6 +86,27 @@ describe( 'Writer', () => { } ); } ); + describe( 'is()', () => { + let writer; + + beforeEach( () => { + writer = new Writer( model, batch ); + } ); + + it( 'should return true for "writer"', () => { + expect( writer.is( 'writer' ) ).to.be.true; + expect( writer.is( 'model:writer' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( writer.is( 'model' ) ).to.be.false; + expect( writer.is( 'model:node' ) ).to.be.false; + expect( writer.is( 'text' ) ).to.be.false; + expect( writer.is( 'element', 'paragraph' ) ).to.be.false; + expect( writer.is() ).to.be.false; + } ); + } ); + describe( 'insert()', () => { it( 'should insert node at given position', () => { const parent = createDocumentFragment(); From 960bb38ccb28b93c594b8382ec0380c6959ffafa Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 9 May 2019 09:48:52 +0200 Subject: [PATCH 03/30] Replace spaces with tabs. --- src/view/downcastwriter.js | 40 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/view/downcastwriter.js b/src/view/downcastwriter.js index 6abdf3316..e12e381b5 100644 --- a/src/view/downcastwriter.js +++ b/src/view/downcastwriter.js @@ -788,26 +788,26 @@ export default class DowncastWriter { } /** - * Wraps elements within range with provided {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. - * If a collapsed range is provided, it will be wrapped only if it is equal to view selection. - * - * If a collapsed range was passed and is same as selection, the selection - * will be moved to the inside of the wrapped attribute element. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-invalid-range-container` - * when {@link module:engine/view/range~Range#start} - * and {@link module:engine/view/range~Range#end} positions are not placed inside same parent container. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-invalid-attribute` when passed attribute element is not - * an instance of {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-nonselection-collapsed-range` when passed range - * is collapsed and different than view selection. - * - * @param {module:engine/view/range~Range} range Range to wrap. - * @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}. + * If a collapsed range is provided, it will be wrapped only if it is equal to view selection. + * + * If a collapsed range was passed and is same as selection, the selection + * will be moved to the inside of the wrapped attribute element. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-invalid-range-container` + * when {@link module:engine/view/range~Range#start} + * and {@link module:engine/view/range~Range#end} positions are not placed inside same parent container. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-invalid-attribute` when passed attribute element is not + * an instance of {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-nonselection-collapsed-range` when passed range + * is collapsed and different than view selection. + * + * @param {module:engine/view/range~Range} range Range to wrap. + * @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. + */ wrap( range, attribute ) { if ( !( attribute instanceof AttributeElement ) ) { throw new CKEditorError( 'view-writer-wrap-invalid-attribute', this.document ); From 87dc29b64eff48e4cc1a19c1be701bcfc4a7c848 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 9 May 2019 09:59:46 +0200 Subject: [PATCH 04/30] Remove checks for no parameters, as those shouldn't be supported. --- src/model/element.js | 2 +- src/model/rootelement.js | 2 +- tests/model/batch.js | 1 - tests/model/differ.js | 1 - tests/model/document.js | 1 - tests/model/documentfragment.js | 1 - tests/model/documentselection.js | 1 - tests/model/element.js | 1 - tests/model/history.js | 1 - tests/model/liveposition.js | 1 - tests/model/liverange.js | 1 - tests/model/markercollection.js | 2 -- tests/model/model.js | 1 - tests/model/node.js | 1 - tests/model/nodelist.js | 1 - tests/model/position.js | 1 - tests/model/range.js | 1 - tests/model/rootelement.js | 1 - tests/model/schema.js | 1 - tests/model/selection.js | 1 - tests/model/text.js | 1 - tests/model/textproxy.js | 1 - tests/model/treewalker.js | 1 - tests/model/writer.js | 1 - 24 files changed, 2 insertions(+), 25 deletions(-) diff --git a/src/model/element.js b/src/model/element.js index 4aa6c38da..cff992681 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -113,7 +113,7 @@ export default class Element extends Node { * @returns {Boolean} */ is( type, name = null ) { - const cutType = type && type.replace( 'model:', '' ); + const cutType = type.replace( 'model:', '' ); if ( !name ) { return cutType == 'element' || cutType == this.name || super.is( type ); } else { diff --git a/src/model/rootelement.js b/src/model/rootelement.js index d00a44b51..62bd3b45c 100644 --- a/src/model/rootelement.js +++ b/src/model/rootelement.js @@ -58,7 +58,7 @@ export default class RootElement extends Element { * @inheritDoc */ is( type, name ) { - const cutType = type && type.replace( 'model:', '' ); + const cutType = type.replace( 'model:', '' ); if ( !name ) { return cutType == 'rootElement' || super.is( type ); } else { diff --git a/tests/model/batch.js b/tests/model/batch.js index 9b9d62a0e..28d816101 100644 --- a/tests/model/batch.js +++ b/tests/model/batch.js @@ -77,7 +77,6 @@ describe( 'Batch', () => { expect( batch.is( 'node' ) ).to.be.false; expect( batch.is( 'model:element' ) ).to.be.false; expect( batch.is( 'element', 'paragraph' ) ).to.be.false; - expect( batch.is() ).to.be.false; } ); } ); } ); diff --git a/tests/model/differ.js b/tests/model/differ.js index 4ef97ca54..781e766a3 100644 --- a/tests/model/differ.js +++ b/tests/model/differ.js @@ -46,7 +46,6 @@ describe( 'Differ', () => { expect( differ.is( 'model:node' ) ).to.be.false; expect( differ.is( 'text' ) ).to.be.false; expect( differ.is( 'element', 'paragraph' ) ).to.be.false; - expect( differ.is() ).to.be.false; } ); } ); diff --git a/tests/model/document.js b/tests/model/document.js index 04b75a591..1e8d4b083 100644 --- a/tests/model/document.js +++ b/tests/model/document.js @@ -512,7 +512,6 @@ describe( 'Document', () => { expect( doc.is( 'model:node' ) ).to.be.false; expect( doc.is( 'view:document' ) ).to.be.false; expect( doc.is( 'element', 'text' ) ).to.be.false; - expect( doc.is() ).to.be.false; } ); } ); } ); diff --git a/tests/model/documentfragment.js b/tests/model/documentfragment.js index 0608c9916..b110e5860 100644 --- a/tests/model/documentfragment.js +++ b/tests/model/documentfragment.js @@ -80,7 +80,6 @@ describe( 'DocumentFragment', () => { expect( frag.is( 'element' ) ).to.be.false; expect( frag.is( 'rootElement' ) ).to.be.false; expect( frag.is( 'view:documentFragment' ) ).to.be.false; - expect( frag.is() ).to.be.false; } ); } ); diff --git a/tests/model/documentselection.js b/tests/model/documentselection.js index 2a86b0680..5ac47af66 100644 --- a/tests/model/documentselection.js +++ b/tests/model/documentselection.js @@ -511,7 +511,6 @@ describe( 'DocumentSelection', () => { expect( selection.is( 'rootElement' ) ).to.be.false; expect( selection.is( 'view:selection' ) ).to.be.false; expect( selection.is( 'view:documentSelection' ) ).to.be.false; - expect( selection.is() ).to.be.false; } ); } ); diff --git a/tests/model/element.js b/tests/model/element.js index e841b620a..8c3132fdd 100644 --- a/tests/model/element.js +++ b/tests/model/element.js @@ -70,7 +70,6 @@ describe( 'Element', () => { expect( element.is( 'view:node' ) ).to.be.false; expect( element.is( 'view:element' ) ).to.be.false; expect( element.is( 'view:element' ) ).to.be.false; - expect( element.is() ).to.be.false; } ); } ); diff --git a/tests/model/history.js b/tests/model/history.js index 5135c8a7a..ad2e7294b 100644 --- a/tests/model/history.js +++ b/tests/model/history.js @@ -189,7 +189,6 @@ describe( 'History', () => { expect( history.is( 'model:node' ) ).to.be.false; expect( history.is( 'text' ) ).to.be.false; expect( history.is( 'element', 'paragraph' ) ).to.be.false; - expect( history.is() ).to.be.false; } ); } ); } ); diff --git a/tests/model/liveposition.js b/tests/model/liveposition.js index 93e387e11..bb10dae63 100644 --- a/tests/model/liveposition.js +++ b/tests/model/liveposition.js @@ -61,7 +61,6 @@ describe( 'LivePosition', () => expect( live.is( 'model:node' ) ).to.be.false; expect( live.is( 'text' ) ).to.be.false; expect( live.is( 'element', 'paragraph' ) ).to.be.false; - expect( live.is() ).to.be.false; } ); } ); diff --git a/tests/model/liverange.js b/tests/model/liverange.js index fb9a6c66b..00c6edaa2 100644 --- a/tests/model/liverange.js +++ b/tests/model/liverange.js @@ -202,7 +202,6 @@ describe( 'LiveRange', () => { expect( live.is( 'model:node' ) ).to.be.false; expect( live.is( 'text' ) ).to.be.false; expect( live.is( 'element', 'paragraph' ) ).to.be.false; - expect( live.is() ).to.be.false; } ); } ); diff --git a/tests/model/markercollection.js b/tests/model/markercollection.js index 53500de46..8e678444c 100644 --- a/tests/model/markercollection.js +++ b/tests/model/markercollection.js @@ -156,7 +156,6 @@ describe( 'MarkerCollection', () => { expect( markers.is( 'model:node' ) ).to.be.false; expect( markers.is( 'text' ) ).to.be.false; expect( markers.is( 'element', 'paragraph' ) ).to.be.false; - expect( markers.is() ).to.be.false; } ); } ); @@ -441,7 +440,6 @@ describe( 'Marker', () => { expect( marker.is( 'model:node' ) ).to.be.false; expect( marker.is( 'text' ) ).to.be.false; expect( marker.is( 'element', 'paragraph' ) ).to.be.false; - expect( marker.is() ).to.be.false; } ); } ); } ); diff --git a/tests/model/model.js b/tests/model/model.js index cd483e6be..1a10269fb 100644 --- a/tests/model/model.js +++ b/tests/model/model.js @@ -819,7 +819,6 @@ describe( 'Model', () => { expect( model.is( 'model:node' ) ).to.be.false; expect( model.is( 'text' ) ).to.be.false; expect( model.is( 'element', 'paragraph' ) ).to.be.false; - expect( model.is() ).to.be.false; } ); } ); } ); diff --git a/tests/model/node.js b/tests/model/node.js index 7f3bfb263..a0d9baf82 100644 --- a/tests/model/node.js +++ b/tests/model/node.js @@ -173,7 +173,6 @@ describe( 'Node', () => { expect( node.is( 'model:text' ) ).to.be.false; expect( node.is( 'text' ) ).to.be.false; expect( node.is( 'element', 'paragraph' ) ).to.be.false; - expect( node.is() ).to.be.false; } ); } ); diff --git a/tests/model/nodelist.js b/tests/model/nodelist.js index 9578ad2ab..e2c53cab3 100644 --- a/tests/model/nodelist.js +++ b/tests/model/nodelist.js @@ -43,7 +43,6 @@ describe( 'NodeList', () => { expect( nodes.is( 'model:node' ) ).to.be.false; expect( nodes.is( 'text' ) ).to.be.false; expect( nodes.is( 'element', 'paragraph' ) ).to.be.false; - expect( nodes.is() ).to.be.false; } ); } ); diff --git a/tests/model/position.js b/tests/model/position.js index 67433929c..d64ba4d0b 100644 --- a/tests/model/position.js +++ b/tests/model/position.js @@ -144,7 +144,6 @@ describe( 'Position', () => { expect( position.is( 'model:node' ) ).to.be.false; expect( position.is( 'text' ) ).to.be.false; expect( position.is( 'element', 'paragraph' ) ).to.be.false; - expect( position.is() ).to.be.false; } ); } ); diff --git a/tests/model/range.js b/tests/model/range.js index 2a0b0ac5a..a15deeafd 100644 --- a/tests/model/range.js +++ b/tests/model/range.js @@ -61,7 +61,6 @@ describe( 'Range', () => { expect( range.is( 'model:node' ) ).to.be.false; expect( range.is( 'text' ) ).to.be.false; expect( range.is( 'element', 'paragraph' ) ).to.be.false; - expect( range.is() ).to.be.false; } ); } ); diff --git a/tests/model/rootelement.js b/tests/model/rootelement.js index 7547345ad..34bd4b389 100644 --- a/tests/model/rootelement.js +++ b/tests/model/rootelement.js @@ -57,7 +57,6 @@ describe( 'RootElement', () => { expect( root.is( 'textProxy' ) ).to.be.false; expect( root.is( 'documentFragment' ) ).to.be.false; expect( root.is( 'view:element' ) ).to.be.false; - expect( root.is() ).to.be.false; } ); } ); } ); diff --git a/tests/model/schema.js b/tests/model/schema.js index 441095ca5..dcfc381a4 100644 --- a/tests/model/schema.js +++ b/tests/model/schema.js @@ -321,7 +321,6 @@ describe( 'Schema', () => { expect( schema.is( 'model:node' ) ).to.be.false; expect( schema.is( 'text' ) ).to.be.false; expect( schema.is( 'element', 'paragraph' ) ).to.be.false; - expect( schema.is() ).to.be.false; } ); } ); diff --git a/tests/model/selection.js b/tests/model/selection.js index b387079e0..fd440e61f 100644 --- a/tests/model/selection.js +++ b/tests/model/selection.js @@ -144,7 +144,6 @@ describe( 'Selection', () => { expect( selection.is( 'model:node' ) ).to.be.false; expect( selection.is( 'text' ) ).to.be.false; expect( selection.is( 'element', 'paragraph' ) ).to.be.false; - expect( selection.is() ).to.be.false; } ); } ); diff --git a/tests/model/text.js b/tests/model/text.js index 190d55060..ab812c4af 100644 --- a/tests/model/text.js +++ b/tests/model/text.js @@ -52,7 +52,6 @@ describe( 'Text', () => { expect( text.is( 'model:element' ) ).to.be.false; expect( text.is( 'rootElement' ) ).to.be.false; expect( text.is( 'documentFragment' ) ).to.be.false; - expect( text.is() ).to.be.false; } ); } ); diff --git a/tests/model/textproxy.js b/tests/model/textproxy.js index c9101cb86..be314c961 100644 --- a/tests/model/textproxy.js +++ b/tests/model/textproxy.js @@ -116,7 +116,6 @@ describe( 'TextProxy', () => { expect( textProxy.is( 'model:element', 'image' ) ).to.be.false; expect( textProxy.is( 'documentFragment' ) ).to.be.false; expect( textProxy.is( 'rootElement' ) ).to.be.false; - expect( textProxy.is() ).to.be.false; } ); } ); diff --git a/tests/model/treewalker.js b/tests/model/treewalker.js index e249dcb7f..347e77247 100644 --- a/tests/model/treewalker.js +++ b/tests/model/treewalker.js @@ -86,7 +86,6 @@ describe( 'TreeWalker', () => { expect( treeWalker.is( 'model:node' ) ).to.be.false; expect( treeWalker.is( 'text' ) ).to.be.false; expect( treeWalker.is( 'element', 'paragraph' ) ).to.be.false; - expect( treeWalker.is() ).to.be.false; } ); } ); diff --git a/tests/model/writer.js b/tests/model/writer.js index d151fd64a..e796af3a6 100644 --- a/tests/model/writer.js +++ b/tests/model/writer.js @@ -103,7 +103,6 @@ describe( 'Writer', () => { expect( writer.is( 'model:node' ) ).to.be.false; expect( writer.is( 'text' ) ).to.be.false; expect( writer.is( 'element', 'paragraph' ) ).to.be.false; - expect( writer.is() ).to.be.false; } ); } ); From cfe3b863391e78ffb21a132667dfd446d507c26a Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 9 May 2019 10:21:53 +0200 Subject: [PATCH 05/30] Add and update 'is()' method for view components. --- src/view/attributeelement.js | 5 +++-- src/view/containerelement.js | 5 +++-- src/view/document.js | 10 ++++++++++ src/view/documentfragment.js | 2 +- src/view/documentselection.js | 9 ++++++++- src/view/domconverter.js | 10 ++++++++++ src/view/downcastwriter.js | 10 ++++++++++ src/view/editableelement.js | 5 +++-- src/view/element.js | 5 +++-- src/view/emptyelement.js | 5 +++-- src/view/matcher.js | 10 ++++++++++ src/view/node.js | 2 +- src/view/position.js | 10 ++++++++++ src/view/range.js | 10 ++++++++++ src/view/renderer.js | 10 ++++++++++ src/view/rooteditableelement.js | 5 +++-- src/view/selection.js | 2 +- src/view/text.js | 2 +- src/view/textproxy.js | 2 +- src/view/treewalker.js | 10 ++++++++++ src/view/uielement.js | 5 +++-- src/view/upcastwriter.js | 10 ++++++++++ src/view/view.js | 10 ++++++++++ 23 files changed, 134 insertions(+), 20 deletions(-) diff --git a/src/view/attributeelement.js b/src/view/attributeelement.js index 01e63195d..ef74cedba 100644 --- a/src/view/attributeelement.js +++ b/src/view/attributeelement.js @@ -128,10 +128,11 @@ export default class AttributeElement extends Element { * @inheritDoc */ is( type, name = null ) { + const cutType = type && type.replace( 'view:', '' ); if ( !name ) { - return type == 'attributeElement' || super.is( type ); + return cutType == 'attributeElement' || super.is( type ); } else { - return ( type == 'attributeElement' && name == this.name ) || super.is( type, name ); + return ( cutType == 'attributeElement' && name == this.name ) || super.is( type, name ); } } diff --git a/src/view/containerelement.js b/src/view/containerelement.js index 8333b9362..bb8d3c2a4 100644 --- a/src/view/containerelement.js +++ b/src/view/containerelement.js @@ -54,10 +54,11 @@ export default class ContainerElement extends Element { * @inheritDoc */ is( type, name = null ) { + const cutType = type && type.replace( 'view:', '' ); if ( !name ) { - return type == 'containerElement' || super.is( type ); + return cutType == 'containerElement' || super.is( type ); } else { - return ( type == 'containerElement' && name == this.name ) || super.is( type, name ); + return ( cutType == 'containerElement' && name == this.name ) || super.is( type, name ); } } } diff --git a/src/view/document.js b/src/view/document.js index 376abdde3..28e576f59 100644 --- a/src/view/document.js +++ b/src/view/document.js @@ -132,6 +132,16 @@ export default class Document { this.stopListening(); } + /** + * Checks whether given object is of `document` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'document' || type == 'view:document'; + } + /** * Performs post-fixer loops. Executes post-fixer callbacks as long as none of them has done any changes to the model. * diff --git a/src/view/documentfragment.js b/src/view/documentfragment.js index 447086b64..238db705e 100644 --- a/src/view/documentfragment.js +++ b/src/view/documentfragment.js @@ -102,7 +102,7 @@ export default class DocumentFragment { * @returns {Boolean} */ is( type ) { - return type == 'documentFragment'; + return type == 'documentFragment' || type == 'view:documentFragment'; } /** diff --git a/src/view/documentselection.js b/src/view/documentselection.js index f287f4d66..ab4225688 100644 --- a/src/view/documentselection.js +++ b/src/view/documentselection.js @@ -281,15 +281,22 @@ export default class DocumentSelection { * const selection = new DocumentSelection( ... ); * * selection.is( 'selection' ); // true + * selection.is( 'view:selection' ); // true * selection.is( 'documentSelection' ); // true + * selection.is( 'view:documentSelection' ); // true * selection.is( 'node' ); // false + * selection.is( 'view:node' ); // false + * selection.is( 'model:selection' ); // false * selection.is( 'element' ); // false * * @param {String} type * @returns {Boolean} */ is( type ) { - return type == 'selection' || type == 'documentSelection'; + return type == 'selection' || + type == 'documentSelection' || + type == 'view:selection' || + type == 'view:documentSelection'; } /** diff --git a/src/view/domconverter.js b/src/view/domconverter.js index 66505be86..f2342f191 100644 --- a/src/view/domconverter.js +++ b/src/view/domconverter.js @@ -105,6 +105,16 @@ export default class DomConverter { this._fakeSelectionMapping = new WeakMap(); } + /** + * Checks whether given object is of `domConverter` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'domConverter' || type == 'view:domConverter'; + } + /** * Binds given DOM element that represents fake selection to {@link module:engine/view/documentselection~DocumentSelection * document selection}. Document selection copy is stored and can be retrieved by diff --git a/src/view/downcastwriter.js b/src/view/downcastwriter.js index e12e381b5..27fcab852 100644 --- a/src/view/downcastwriter.js +++ b/src/view/downcastwriter.js @@ -54,6 +54,16 @@ export default class DowncastWriter { this._cloneGroups = new Map(); } + /** + * Checks whether given object is of `downcastWriter` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'downcastWriter' || type == 'view:downcastWriter'; + } + /** * Sets {@link module:engine/view/documentselection~DocumentSelection selection's} ranges and direction to the * specified location based on the given {@link module:engine/view/selection~Selectable selectable}. diff --git a/src/view/editableelement.js b/src/view/editableelement.js index c1ce04409..f702073c3 100644 --- a/src/view/editableelement.js +++ b/src/view/editableelement.js @@ -70,10 +70,11 @@ export default class EditableElement extends ContainerElement { * @inheritDoc */ is( type, name = null ) { + const cutType = type && type.replace( 'view:', '' ); if ( !name ) { - return type == 'editableElement' || super.is( type ); + return cutType == 'editableElement' || super.is( type ); } else { - return ( type == 'editableElement' && name == this.name ) || super.is( type, name ); + return ( cutType == 'editableElement' && name == this.name ) || super.is( type, name ); } } diff --git a/src/view/element.js b/src/view/element.js index 2965bfe0b..0efc962b3 100644 --- a/src/view/element.js +++ b/src/view/element.js @@ -162,10 +162,11 @@ export default class Element extends Node { * @returns {Boolean} */ is( type, name = null ) { + const cutType = type.replace( 'view:', '' ); 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; } } diff --git a/src/view/emptyelement.js b/src/view/emptyelement.js index 8973cb1c9..6d4cae9ee 100644 --- a/src/view/emptyelement.js +++ b/src/view/emptyelement.js @@ -47,10 +47,11 @@ export default class EmptyElement extends Element { * @inheritDoc */ is( type, name = null ) { + const cutType = type.replace( 'view:', '' ); if ( !name ) { - return type == 'emptyElement' || super.is( type ); + return cutType == 'emptyElement' || super.is( type ); } else { - return ( type == 'emptyElement' && name == this.name ) || super.is( type, name ); + return ( cutType == 'emptyElement' && name == this.name ) || super.is( type, name ); } } diff --git a/src/view/matcher.js b/src/view/matcher.js index e8e32e8b6..52645e2c0 100644 --- a/src/view/matcher.js +++ b/src/view/matcher.js @@ -174,6 +174,16 @@ export default class Matcher { return ( typeof pattern != 'function' && name && !( name instanceof RegExp ) ) ? name : null; } + + /** + * Checks whether given object is of `matcher` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'matcher' || type == 'view:matcher'; + } } // Returns match information if {@link module:engine/view/element~Element element} is matching provided pattern. diff --git a/src/view/node.js b/src/view/node.js index 6b9361d86..055023427 100644 --- a/src/view/node.js +++ b/src/view/node.js @@ -308,7 +308,7 @@ export default class Node { * @returns {Boolean} */ is( type ) { - return type == 'node'; + return type == 'node' || type == 'view:node'; } /** diff --git a/src/view/position.js b/src/view/position.js index d44ddc8f7..b37b7ef3b 100644 --- a/src/view/position.js +++ b/src/view/position.js @@ -206,6 +206,16 @@ export default class Position { return i === 0 ? null : ancestorsA[ i - 1 ]; } + /** + * Checks whether given object is of `position` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'position' || type == 'view:position'; + } + /** * Checks whether this position equals given position. * diff --git a/src/view/range.js b/src/view/range.js index e392ab307..cc194387f 100644 --- a/src/view/range.js +++ b/src/view/range.js @@ -394,6 +394,16 @@ export default class Range { } } + /** + * Checks whether given object is of `range` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'range' || type == 'view:range'; + } + /** * Checks and returns whether this range intersects with the given range. * diff --git a/src/view/renderer.js b/src/view/renderer.js index c8bc5cc2e..3bdf6029a 100644 --- a/src/view/renderer.js +++ b/src/view/renderer.js @@ -118,6 +118,16 @@ export default class Renderer { this._fakeSelectionContainer = null; } + /** + * Checks whether given object is of `renderer` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'renderer' || type == 'view:renderer'; + } + /** * Marks a view node to be updated in the DOM by {@link #render `render()`}. * diff --git a/src/view/rooteditableelement.js b/src/view/rooteditableelement.js index 78d9e2f63..85b183bc7 100644 --- a/src/view/rooteditableelement.js +++ b/src/view/rooteditableelement.js @@ -41,10 +41,11 @@ export default class RootEditableElement extends EditableElement { * @inheritDoc */ is( type, name = null ) { + const cutType = type.replace( 'view:', '' ); 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 ); } } diff --git a/src/view/selection.js b/src/view/selection.js index 0d23cd90d..4ec70e947 100644 --- a/src/view/selection.js +++ b/src/view/selection.js @@ -611,7 +611,7 @@ export default class Selection { * @returns {Boolean} */ is( type ) { - return type == 'selection'; + return type == 'selection' || type == 'view:selection'; } /** diff --git a/src/view/text.js b/src/view/text.js index 1d044bf62..906fe9c22 100644 --- a/src/view/text.js +++ b/src/view/text.js @@ -45,7 +45,7 @@ export default class Text extends Node { * @inheritDoc */ is( type ) { - return type == 'text' || super.is( type ); + return type == 'text' || type == 'view:text' || super.is( type ); } /** diff --git a/src/view/textproxy.js b/src/view/textproxy.js index 8c463bc5c..4b4cdadb8 100644 --- a/src/view/textproxy.js +++ b/src/view/textproxy.js @@ -149,7 +149,7 @@ export default class TextProxy { * @returns {Boolean} */ is( type ) { - return type == 'textProxy'; + return type == 'textProxy' || type == 'view:textProxy'; } /** diff --git a/src/view/treewalker.js b/src/view/treewalker.js index 40f9fe689..543d02c92 100644 --- a/src/view/treewalker.js +++ b/src/view/treewalker.js @@ -144,6 +144,16 @@ export default class TreeWalker { return this; } + /** + * Checks whether given object is of `treeWalker` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'treeWalker' || type == 'view:treeWalker'; + } + /** * Moves {@link #position} in the {@link #direction} skipping values as long as the callback function returns `true`. * diff --git a/src/view/uielement.js b/src/view/uielement.js index 663d63c23..68c2a39a5 100644 --- a/src/view/uielement.js +++ b/src/view/uielement.js @@ -60,10 +60,11 @@ export default class UIElement extends Element { * @inheritDoc */ is( type, name = null ) { + const cutType = type.replace( 'view:', '' ); if ( !name ) { - return type == 'uiElement' || super.is( type ); + return cutType == 'uiElement' || super.is( type ); } else { - return ( type == 'uiElement' && name == this.name ) || super.is( type, name ); + return ( cutType == 'uiElement' && name == this.name ) || super.is( type, name ); } } diff --git a/src/view/upcastwriter.js b/src/view/upcastwriter.js index 990e6e096..01f9af824 100644 --- a/src/view/upcastwriter.js +++ b/src/view/upcastwriter.js @@ -35,6 +35,16 @@ import Selection from './selection'; * writer.appendChild( text, someViewElement ); */ export default class UpcastWriter { + /** + * Checks whether given object is of `upcastWriter` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'upcastWriter' || type == 'view:upcastWriter'; + } + /** * Creates a new {@link module:engine/view/documentfragment~DocumentFragment} instance. * diff --git a/src/view/view.js b/src/view/view.js index c2abf2a47..0f813cb06 100644 --- a/src/view/view.js +++ b/src/view/view.js @@ -652,6 +652,16 @@ export default class View { return new Selection( selectable, placeOrOffset, options ); } + /** + * Checks whether given object is of `view` type. + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'view'; + } + /** * Disables or enables rendering. If the flag is set to `true` then the rendering will be disabled. * If the flag is set to `false` and if there was some change in the meantime, then the rendering action will be performed. From 146bd4362d770dda76bd17979820c1d28fcee3ed Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 9 May 2019 11:04:57 +0200 Subject: [PATCH 06/30] Fix editableelement test to actually use EditableElement class. --- tests/view/editableelement.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/view/editableelement.js b/tests/view/editableelement.js index 3e519204b..0b488a698 100644 --- a/tests/view/editableelement.js +++ b/tests/view/editableelement.js @@ -5,7 +5,7 @@ import createDocumentMock from '../../tests/view/_utils/createdocumentmock'; -import RootEditableElement from '../../src/view/rooteditableelement'; +import EditableElement from '../../src/view/editableelement'; import Range from '../../src/view/range'; import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; @@ -14,7 +14,7 @@ describe( 'EditableElement', () => { let element, docMock; beforeEach( () => { - element = new RootEditableElement( 'div' ); + element = new EditableElement( 'div' ); docMock = createDocumentMock(); } ); @@ -51,16 +51,16 @@ describe( 'EditableElement', () => { beforeEach( () => { docMock = createDocumentMock(); - viewMain = new RootEditableElement( 'div' ); + viewMain = new EditableElement( 'div' ); viewMain._document = docMock; - viewHeader = new RootEditableElement( 'h1' ); + viewHeader = new EditableElement( 'h1' ); viewHeader._document = docMock; viewHeader.rootName = 'header'; } ); it( 'should be observable', () => { - const root = new RootEditableElement( 'div' ); + const root = new EditableElement( 'div' ); root._document = createDocumentMock(); expect( root.isFocused ).to.be.false; @@ -114,7 +114,7 @@ describe( 'EditableElement', () => { describe( 'isReadOnly', () => { it( 'should be observable', () => { - const root = new RootEditableElement( 'div' ); + const root = new EditableElement( 'div' ); root._document = createDocumentMock(); expect( root.isReadOnly ).to.be.false; @@ -131,7 +131,7 @@ describe( 'EditableElement', () => { } ); it( 'should be bound to the document#isReadOnly', () => { - const root = new RootEditableElement( 'div' ); + const root = new EditableElement( 'div' ); root._document = createDocumentMock(); root.document.isReadOnly = false; @@ -147,7 +147,7 @@ describe( 'EditableElement', () => { describe( 'getDocument', () => { it( 'should return document', () => { const docMock = createDocumentMock(); - const root = new RootEditableElement( 'div' ); + const root = new EditableElement( 'div' ); root._document = docMock; expect( root.document ).to.equal( docMock ); From 9d80ec31f53783fe1b2a632407b932d6ce92ef1c Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 9 May 2019 12:19:50 +0200 Subject: [PATCH 07/30] Add unit tests for view components. --- tests/view/attributeelement.js | 8 ++++++ tests/view/containerelement.js | 8 ++++++ tests/view/document.js | 15 +++++++++++ tests/view/documentfragment.js | 5 +++- tests/view/documentselection.js | 6 +++++ tests/view/editableelement.js | 42 +++++++++++++++++++++++++++++++ tests/view/element.js | 9 +++++++ tests/view/emptyelement.js | 9 +++++++ tests/view/matcher.js | 26 +++++++++++++++++++ tests/view/node.js | 20 +++++++++++++-- tests/view/position.js | 33 +++++++++++++++++++++--- tests/view/range.js | 28 +++++++++++++++++++++ tests/view/renderer.js | 21 ++++++++++++++++ tests/view/rooteditableelement.js | 15 +++++++++++ tests/view/selection.js | 3 +++ tests/view/text.js | 6 +++++ tests/view/textproxy.js | 4 +++ tests/view/treewalker.js | 27 ++++++++++++++++++++ tests/view/uielement.js | 13 ++++++++++ tests/view/upcastwriter.js | 21 ++++++++++++++++ 20 files changed, 313 insertions(+), 6 deletions(-) diff --git a/tests/view/attributeelement.js b/tests/view/attributeelement.js index 3ae6870de..726112d58 100644 --- a/tests/view/attributeelement.js +++ b/tests/view/attributeelement.js @@ -30,16 +30,24 @@ describe( 'AttributeElement', () => { it( 'should return true for attributeElement/element, also with correct name and element name', () => { expect( el.is( 'attributeElement' ) ).to.be.true; + expect( el.is( 'view:attributeElement' ) ).to.be.true; expect( el.is( 'attributeElement', 'span' ) ).to.be.true; + expect( el.is( 'view:attributeElement', 'span' ) ).to.be.true; expect( el.is( 'element' ) ).to.be.true; + expect( el.is( 'view:element' ) ).to.be.true; expect( el.is( 'element', 'span' ) ).to.be.true; + expect( el.is( 'view:element', 'span' ) ).to.be.true; expect( el.is( 'span' ) ).to.be.true; + expect( el.is( 'view:span' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( el.is( 'attributeElement', 'p' ) ).to.be.false; + expect( el.is( 'view:attributeElement', 'p' ) ).to.be.false; expect( el.is( 'element', 'p' ) ).to.be.false; + expect( el.is( 'view:element', 'p' ) ).to.be.false; expect( el.is( 'p' ) ).to.be.false; + expect( el.is( 'view:p' ) ).to.be.false; expect( el.is( 'text' ) ).to.be.false; expect( el.is( 'textProxy' ) ).to.be.false; expect( el.is( 'containerElement' ) ).to.be.false; diff --git a/tests/view/containerelement.js b/tests/view/containerelement.js index b7fa31152..b38394819 100644 --- a/tests/view/containerelement.js +++ b/tests/view/containerelement.js @@ -27,16 +27,24 @@ describe( 'ContainerElement', () => { it( 'should return true for containerElement/element, also with correct name and element name', () => { expect( el.is( 'containerElement' ) ).to.be.true; + expect( el.is( 'view:containerElement' ) ).to.be.true; expect( el.is( 'containerElement', 'p' ) ).to.be.true; + expect( el.is( 'view:containerElement', 'p' ) ).to.be.true; expect( el.is( 'element' ) ).to.be.true; + expect( el.is( 'view:element' ) ).to.be.true; expect( el.is( 'element', 'p' ) ).to.be.true; + expect( el.is( 'view:element', 'p' ) ).to.be.true; expect( el.is( 'p' ) ).to.be.true; + expect( el.is( 'view:p' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( el.is( 'containerElement', 'span' ) ).to.be.false; + expect( el.is( 'view:containerElement', 'span' ) ).to.be.false; expect( el.is( 'element', 'span' ) ).to.be.false; + expect( el.is( 'view:element', 'span' ) ).to.be.false; expect( el.is( 'span' ) ).to.be.false; + expect( el.is( 'view:span' ) ).to.be.false; expect( el.is( 'text' ) ).to.be.false; expect( el.is( 'textProxy' ) ).to.be.false; expect( el.is( 'attributeElement' ) ).to.be.false; diff --git a/tests/view/document.js b/tests/view/document.js index e0dc15ae2..c664ff434 100644 --- a/tests/view/document.js +++ b/tests/view/document.js @@ -37,6 +37,21 @@ describe( 'Document', () => { } ); } ); + describe( 'is()', () => { + it( 'should return true for "document"', () => { + expect( viewDocument.is( 'document' ) ).to.be.true; + expect( viewDocument.is( 'view:document' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( viewDocument.is( 'model' ) ).to.be.false; + expect( viewDocument.is( 'model:document' ) ).to.be.false; + expect( viewDocument.is( 'node' ) ).to.be.false; + expect( viewDocument.is( 'view:node' ) ).to.be.false; + expect( viewDocument.is( 'element', 'text' ) ).to.be.false; + } ); + } ); + describe( 'getRoot()', () => { it( 'should return "main" root', () => { createViewRoot( viewDocument, 'div', 'main' ); diff --git a/tests/view/documentfragment.js b/tests/view/documentfragment.js index 9d88f5a0d..0dc35368f 100644 --- a/tests/view/documentfragment.js +++ b/tests/view/documentfragment.js @@ -71,7 +71,7 @@ describe( 'DocumentFragment', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let frag; before( () => { @@ -80,13 +80,16 @@ describe( 'DocumentFragment', () => { it( 'should return true for documentFragment', () => { expect( frag.is( 'documentFragment' ) ).to.be.true; + expect( frag.is( 'view:documentFragment' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( frag.is( 'node' ) ).to.be.false; + expect( frag.is( 'view:node' ) ).to.be.false; expect( frag.is( 'text' ) ).to.be.false; expect( frag.is( 'textProxy' ) ).to.be.false; expect( frag.is( 'element' ) ).to.be.false; + expect( frag.is( 'view:element' ) ).to.be.false; expect( frag.is( 'containerElement' ) ).to.be.false; expect( frag.is( 'attributeElement' ) ).to.be.false; expect( frag.is( 'uiElement' ) ).to.be.false; diff --git a/tests/view/documentselection.js b/tests/view/documentselection.js index fdc1fb0f5..4b6b7a371 100644 --- a/tests/view/documentselection.js +++ b/tests/view/documentselection.js @@ -728,18 +728,24 @@ describe( 'DocumentSelection', () => { describe( 'is', () => { it( 'should return true for selection', () => { expect( documentSelection.is( 'selection' ) ).to.be.true; + expect( documentSelection.is( 'view:selection' ) ).to.be.true; } ); it( 'should return true for documentSelection', () => { expect( documentSelection.is( 'documentSelection' ) ).to.be.true; + expect( documentSelection.is( 'view:documentSelection' ) ).to.be.true; } ); it( 'should return false for other values', () => { expect( documentSelection.is( 'node' ) ).to.be.false; + expect( documentSelection.is( 'view:node' ) ).to.be.false; expect( documentSelection.is( 'text' ) ).to.be.false; + expect( documentSelection.is( 'view:text' ) ).to.be.false; expect( documentSelection.is( 'textProxy' ) ).to.be.false; expect( documentSelection.is( 'element' ) ).to.be.false; expect( documentSelection.is( 'rootElement' ) ).to.be.false; + expect( documentSelection.is( 'model:selection' ) ).to.be.false; + expect( documentSelection.is( 'model:documentSelection' ) ).to.be.false; } ); } ); diff --git a/tests/view/editableelement.js b/tests/view/editableelement.js index 0b488a698..17d191091 100644 --- a/tests/view/editableelement.js +++ b/tests/view/editableelement.js @@ -10,6 +10,48 @@ import Range from '../../src/view/range'; import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils'; describe( 'EditableElement', () => { + describe( 'is', () => { + let el; + + before( () => { + el = new EditableElement( 'div' ); + } ); + + it( 'should return true for containerElement/editable/element, also with correct name and element name', () => { + expect( el.is( 'containerElement' ) ).to.be.true; + expect( el.is( 'view:containerElement' ) ).to.be.true; + expect( el.is( 'containerElement', 'div' ) ).to.be.true; + expect( el.is( 'view:containerElement', 'div' ) ).to.be.true; + expect( el.is( 'editableElement' ) ).to.be.true; + expect( el.is( 'view:editableElement' ) ).to.be.true; + expect( el.is( 'editableElement', 'div' ) ).to.be.true; + expect( el.is( 'view:editableElement', 'div' ) ).to.be.true; + expect( el.is( 'element' ) ).to.be.true; + expect( el.is( 'view:element' ) ).to.be.true; + expect( el.is( 'element', 'div' ) ).to.be.true; + expect( el.is( 'view:element', 'div' ) ).to.be.true; + expect( el.is( 'div' ) ).to.be.true; + expect( el.is( 'view:div' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( el.is( 'rootElement', 'p' ) ).to.be.false; + expect( el.is( 'view:rootElement', 'p' ) ).to.be.false; + expect( el.is( 'containerElement', 'p' ) ).to.be.false; + expect( el.is( 'view:containerElement', 'p' ) ).to.be.false; + expect( el.is( 'element', 'p' ) ).to.be.false; + expect( el.is( 'view:element', 'p' ) ).to.be.false; + expect( el.is( 'p' ) ).to.be.false; + expect( el.is( 'view:p' ) ).to.be.false; + expect( el.is( 'text' ) ).to.be.false; + expect( el.is( 'textProxy' ) ).to.be.false; + expect( el.is( 'attributeElement' ) ).to.be.false; + expect( el.is( 'uiElement' ) ).to.be.false; + expect( el.is( 'emptyElement' ) ).to.be.false; + expect( el.is( 'documentFragment' ) ).to.be.false; + } ); + } ); + describe( 'document', () => { let element, docMock; diff --git a/tests/view/element.js b/tests/view/element.js index f05ed1832..5285658ca 100644 --- a/tests/view/element.js +++ b/tests/view/element.js @@ -91,21 +91,30 @@ describe( 'Element', () => { it( 'should return true for node, element, element with correct name and element name', () => { expect( el.is( 'node' ) ).to.be.true; + expect( el.is( 'view:node' ) ).to.be.true; expect( el.is( 'element' ) ).to.be.true; + expect( el.is( 'view:element' ) ).to.be.true; expect( el.is( 'element', 'p' ) ).to.be.true; + expect( el.is( 'view:element', 'p' ) ).to.be.true; expect( el.is( 'p' ) ).to.be.true; + expect( el.is( 'view:p' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( el.is( 'element', 'span' ) ).to.be.false; + expect( el.is( 'view:element', 'span' ) ).to.be.false; expect( el.is( 'span' ) ).to.be.false; + expect( el.is( 'view:span' ) ).to.be.false; expect( el.is( 'text' ) ).to.be.false; + expect( el.is( 'view:text' ) ).to.be.false; expect( el.is( 'textProxy' ) ).to.be.false; expect( el.is( 'containerElement' ) ).to.be.false; expect( el.is( 'attributeElement' ) ).to.be.false; expect( el.is( 'uiElement' ) ).to.be.false; expect( el.is( 'emptyElement' ) ).to.be.false; + expect( el.is( 'view:emptyElement' ) ).to.be.false; expect( el.is( 'rootElement' ) ).to.be.false; + expect( el.is( 'view:ootElement' ) ).to.be.false; expect( el.is( 'documentFragment' ) ).to.be.false; } ); } ); diff --git a/tests/view/emptyelement.js b/tests/view/emptyelement.js index b216edd8e..166b905a3 100644 --- a/tests/view/emptyelement.js +++ b/tests/view/emptyelement.js @@ -29,17 +29,26 @@ describe( 'EmptyElement', () => { it( 'should return true for emptyElement/element, also with correct name and element name', () => { expect( el.is( 'emptyElement' ) ).to.be.true; + expect( el.is( 'view:emptyElement' ) ).to.be.true; expect( el.is( 'emptyElement', 'p' ) ).to.be.true; + expect( el.is( 'view:emptyElement', 'p' ) ).to.be.true; expect( el.is( 'element' ) ).to.be.true; + expect( el.is( 'view:element' ) ).to.be.true; expect( el.is( 'element', 'p' ) ).to.be.true; + expect( el.is( 'view:element', 'p' ) ).to.be.true; expect( el.is( 'p' ) ).to.be.true; + expect( el.is( 'view:p' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( el.is( 'emptyElement', 'span' ) ).to.be.false; + expect( el.is( 'view:emptyElement', 'span' ) ).to.be.false; expect( el.is( 'element', 'span' ) ).to.be.false; + expect( el.is( 'view:element', 'span' ) ).to.be.false; expect( el.is( 'span' ) ).to.be.false; + expect( el.is( 'view:span' ) ).to.be.false; expect( el.is( 'text' ) ).to.be.false; + expect( el.is( 'view:text' ) ).to.be.false; expect( el.is( 'textProxy' ) ).to.be.false; expect( el.is( 'containerElement' ) ).to.be.false; expect( el.is( 'attributeElement' ) ).to.be.false; diff --git a/tests/view/matcher.js b/tests/view/matcher.js index 5a9edff47..80e2cbe89 100644 --- a/tests/view/matcher.js +++ b/tests/view/matcher.js @@ -442,4 +442,30 @@ describe( 'Matcher', () => { expect( matcher.getElementName() ).to.be.null; } ); } ); + + describe( 'is', () => { + let matcher; + + before( () => { + matcher = new Matcher( 'div' ); + } ); + + it( 'should return true for "matcher"', () => { + expect( matcher.is( 'matcher' ) ).to.be.true; + expect( matcher.is( 'view:matcher' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( matcher.is( 'rootElement' ) ).to.be.false; + expect( matcher.is( 'containerElement' ) ).to.be.false; + expect( matcher.is( 'element' ) ).to.be.false; + expect( matcher.is( 'p' ) ).to.be.false; + expect( matcher.is( 'text' ) ).to.be.false; + expect( matcher.is( 'textProxy' ) ).to.be.false; + expect( matcher.is( 'attributeElement' ) ).to.be.false; + expect( matcher.is( 'uiElement' ) ).to.be.false; + expect( matcher.is( 'emptyElement' ) ).to.be.false; + expect( matcher.is( 'documentFragment' ) ).to.be.false; + } ); + } ); } ); diff --git a/tests/view/node.js b/tests/view/node.js index f40d6588a..6f159d2c8 100644 --- a/tests/view/node.js +++ b/tests/view/node.js @@ -31,10 +31,26 @@ describe( 'Node', () => { } ); describe( 'is()', () => { + let node; + beforeEach( () => { + node = new Node(); + } ); it( 'should return true for node', () => { - const node = new Node(); - expect( node.is( 'node' ) ).to.be.true; + expect( node.is( 'view:node' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( node.is( 'rootElement' ) ).to.be.false; + expect( node.is( 'containerElement' ) ).to.be.false; + expect( node.is( 'element' ) ).to.be.false; + expect( node.is( 'p' ) ).to.be.false; + expect( node.is( 'text' ) ).to.be.false; + expect( node.is( 'textProxy' ) ).to.be.false; + expect( node.is( 'attributeElement' ) ).to.be.false; + expect( node.is( 'uiElement' ) ).to.be.false; + expect( node.is( 'emptyElement' ) ).to.be.false; + expect( node.is( 'documentFragment' ) ).to.be.false; } ); } ); diff --git a/tests/view/position.js b/tests/view/position.js index 13405951c..e20ae286e 100644 --- a/tests/view/position.js +++ b/tests/view/position.js @@ -24,10 +24,37 @@ describe( 'Position', () => { describe( 'constructor()', () => { it( 'should create element without attributes', () => { - const elem = new Position( parentMock, 5 ); + const position = new Position( parentMock, 5 ); - expect( elem ).to.have.property( 'parent' ).that.equals( parentMock ); - expect( elem ).to.have.property( 'offset' ).that.equals( 5 ); + expect( position ).to.have.property( 'parent' ).that.equals( parentMock ); + expect( position ).to.have.property( 'offset' ).that.equals( 5 ); + } ); + } ); + + describe( 'is', () => { + let position; + + beforeEach( () => { + position = new Position( parentMock, 5 ); + } ); + + it( 'should return true for "position"', () => { + expect( position.is( 'position' ) ).to.be.true; + expect( position.is( 'view:position' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( position.is( 'rootElement' ) ).to.be.false; + expect( position.is( 'containerElement' ) ).to.be.false; + expect( position.is( 'element' ) ).to.be.false; + expect( position.is( 'p' ) ).to.be.false; + expect( position.is( 'text' ) ).to.be.false; + expect( position.is( 'textProxy' ) ).to.be.false; + expect( position.is( 'attributeElement' ) ).to.be.false; + expect( position.is( 'uiElement' ) ).to.be.false; + expect( position.is( 'emptyElement' ) ).to.be.false; + expect( position.is( 'documentFragment' ) ).to.be.false; + expect( position.is( 'model:position' ) ).to.be.false; } ); } ); diff --git a/tests/view/range.js b/tests/view/range.js index c1320282e..8588e4228 100644 --- a/tests/view/range.js +++ b/tests/view/range.js @@ -43,6 +43,34 @@ describe( 'Range', () => { } ); } ); + describe( 'is()', () => { + let range; + + before( () => { + const start = new Position( {}, 1 ); + range = new Range( start ); + } ); + + it( 'should return true for "range"', () => { + expect( range.is( 'range' ) ).to.be.true; + expect( range.is( 'view:range' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( range.is( 'rootElement' ) ).to.be.false; + expect( range.is( 'containerElement' ) ).to.be.false; + expect( range.is( 'element' ) ).to.be.false; + expect( range.is( 'p' ) ).to.be.false; + expect( range.is( 'text' ) ).to.be.false; + expect( range.is( 'textProxy' ) ).to.be.false; + expect( range.is( 'attributeElement' ) ).to.be.false; + expect( range.is( 'uiElement' ) ).to.be.false; + expect( range.is( 'emptyElement' ) ).to.be.false; + expect( range.is( 'documentFragment' ) ).to.be.false; + expect( range.is( 'model:range' ) ).to.be.false; + } ); + } ); + describe( 'iterator', () => { it( 'should iterate over the range returning tree walker values', () => { const range = getRange( '

fo{o

bar

xy}z

' ); diff --git a/tests/view/renderer.js b/tests/view/renderer.js index 617a7fda6..3dc8c5cfb 100644 --- a/tests/view/renderer.js +++ b/tests/view/renderer.js @@ -41,6 +41,27 @@ describe( 'Renderer', () => { renderer.domDocuments.add( document ); } ); + describe( 'is()', () => { + it( 'should return true for "renderer"', () => { + expect( renderer.is( 'renderer' ) ).to.be.true; + expect( renderer.is( 'view:renderer' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( renderer.is( 'rootElement' ) ).to.be.false; + expect( renderer.is( 'containerElement' ) ).to.be.false; + expect( renderer.is( 'element' ) ).to.be.false; + expect( renderer.is( 'p' ) ).to.be.false; + expect( renderer.is( 'text' ) ).to.be.false; + expect( renderer.is( 'textProxy' ) ).to.be.false; + expect( renderer.is( 'attributeElement' ) ).to.be.false; + expect( renderer.is( 'uiElement' ) ).to.be.false; + expect( renderer.is( 'emptyElement' ) ).to.be.false; + expect( renderer.is( 'documentFragment' ) ).to.be.false; + expect( renderer.is( 'model:renderer' ) ).to.be.false; + } ); + } ); + describe( 'markToSync', () => { let viewRoot; diff --git a/tests/view/rooteditableelement.js b/tests/view/rooteditableelement.js index a3cfa2e57..31d941614 100644 --- a/tests/view/rooteditableelement.js +++ b/tests/view/rooteditableelement.js @@ -47,27 +47,42 @@ describe( 'RootEditableElement', () => { it( 'should return true for rootElement/containerElement/editable/element, also with correct name and element name', () => { expect( el.is( 'rootElement' ) ).to.be.true; + expect( el.is( 'view:rootElement' ) ).to.be.true; expect( el.is( 'rootElement', 'div' ) ).to.be.true; + expect( el.is( 'view:rootElement', 'div' ) ).to.be.true; expect( el.is( 'containerElement' ) ).to.be.true; + expect( el.is( 'view:containerElement' ) ).to.be.true; expect( el.is( 'containerElement', 'div' ) ).to.be.true; + expect( el.is( 'view:containerElement', 'div' ) ).to.be.true; expect( el.is( 'editableElement' ) ).to.be.true; + expect( el.is( 'view:editableElement' ) ).to.be.true; expect( el.is( 'editableElement', 'div' ) ).to.be.true; + expect( el.is( 'view:editableElement', 'div' ) ).to.be.true; expect( el.is( 'element' ) ).to.be.true; + expect( el.is( 'view:element' ) ).to.be.true; expect( el.is( 'element', 'div' ) ).to.be.true; + expect( el.is( 'view:element', 'div' ) ).to.be.true; expect( el.is( 'div' ) ).to.be.true; + expect( el.is( 'view:div' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( el.is( 'rootElement', 'p' ) ).to.be.false; + expect( el.is( 'view:rootElement', 'p' ) ).to.be.false; expect( el.is( 'containerElement', 'p' ) ).to.be.false; + expect( el.is( 'view:containerElement', 'p' ) ).to.be.false; expect( el.is( 'element', 'p' ) ).to.be.false; + expect( el.is( 'view:element', 'p' ) ).to.be.false; expect( el.is( 'p' ) ).to.be.false; + expect( el.is( 'view:p' ) ).to.be.false; expect( el.is( 'text' ) ).to.be.false; + expect( el.is( 'view:text' ) ).to.be.false; expect( el.is( 'textProxy' ) ).to.be.false; expect( el.is( 'attributeElement' ) ).to.be.false; expect( el.is( 'uiElement' ) ).to.be.false; expect( el.is( 'emptyElement' ) ).to.be.false; expect( el.is( 'documentFragment' ) ).to.be.false; + expect( el.is( 'model:rootElement' ) ).to.be.false; } ); } ); diff --git a/tests/view/selection.js b/tests/view/selection.js index 260efe0a5..c008cd02d 100644 --- a/tests/view/selection.js +++ b/tests/view/selection.js @@ -603,15 +603,18 @@ describe( 'Selection', () => { describe( 'is', () => { it( 'should return true for selection', () => { expect( selection.is( 'selection' ) ).to.be.true; + expect( selection.is( 'view:selection' ) ).to.be.true; } ); it( 'should return false for other values', () => { expect( selection.is( 'documentSelection' ) ).to.be.false; + expect( selection.is( 'view:documentSelection' ) ).to.be.false; expect( selection.is( 'node' ) ).to.be.false; expect( selection.is( 'text' ) ).to.be.false; expect( selection.is( 'textProxy' ) ).to.be.false; expect( selection.is( 'element' ) ).to.be.false; expect( selection.is( 'rootElement' ) ).to.be.false; + expect( selection.is( 'model:selection' ) ).to.be.false; } ); } ); diff --git a/tests/view/text.js b/tests/view/text.js index c0069830f..b47e96f84 100644 --- a/tests/view/text.js +++ b/tests/view/text.js @@ -26,18 +26,24 @@ describe( 'Text', () => { it( 'should return true for node, text', () => { expect( text.is( 'node' ) ).to.be.true; + expect( text.is( 'view:node' ) ).to.be.true; expect( text.is( 'text' ) ).to.be.true; + expect( text.is( 'view:text' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( text.is( 'textProxy' ) ).to.be.false; + expect( text.is( 'view:textProxy' ) ).to.be.false; expect( text.is( 'element' ) ).to.be.false; + expect( text.is( 'view:element' ) ).to.be.false; expect( text.is( 'containerElement' ) ).to.be.false; expect( text.is( 'attributeElement' ) ).to.be.false; expect( text.is( 'uiElement' ) ).to.be.false; expect( text.is( 'emptyElement' ) ).to.be.false; expect( text.is( 'rootElement' ) ).to.be.false; expect( text.is( 'documentFragment' ) ).to.be.false; + expect( text.is( 'model:text' ) ).to.be.false; + expect( text.is( 'model:node' ) ).to.be.false; } ); } ); diff --git a/tests/view/textproxy.js b/tests/view/textproxy.js index 0c43f0816..0147fa84f 100644 --- a/tests/view/textproxy.js +++ b/tests/view/textproxy.js @@ -64,11 +64,14 @@ describe( 'TextProxy', () => { describe( 'is', () => { it( 'should return true for textProxy', () => { expect( textProxy.is( 'textProxy' ) ).to.be.true; + expect( textProxy.is( 'view:textProxy' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( textProxy.is( 'node' ) ).to.be.false; + expect( textProxy.is( 'view:node' ) ).to.be.false; expect( textProxy.is( 'text' ) ).to.be.false; + expect( textProxy.is( 'view:text' ) ).to.be.false; expect( textProxy.is( 'element' ) ).to.be.false; expect( textProxy.is( 'containerElement' ) ).to.be.false; expect( textProxy.is( 'attributeElement' ) ).to.be.false; @@ -76,6 +79,7 @@ describe( 'TextProxy', () => { expect( textProxy.is( 'emptyElement' ) ).to.be.false; expect( textProxy.is( 'rootElement' ) ).to.be.false; expect( textProxy.is( 'documentFragment' ) ).to.be.false; + expect( textProxy.is( 'model:textProxy' ) ).to.be.false; } ); } ); diff --git a/tests/view/treewalker.js b/tests/view/treewalker.js index dda9f095e..76679c9dc 100644 --- a/tests/view/treewalker.js +++ b/tests/view/treewalker.js @@ -70,6 +70,33 @@ describe( 'TreeWalker', () => { } ); } ); + describe( 'is()', () => { + let treeWalker; + + beforeEach( () => { + treeWalker = new TreeWalker(); + } ); + + it( 'should return true for "treeWalker"', () => { + expect( treeWalker.is( 'treeWalker' ) ).to.be.true; + expect( treeWalker.is( 'view:treeWalker' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( treeWalker.is( 'rootElement' ) ).to.be.false; + expect( treeWalker.is( 'containerElement' ) ).to.be.false; + expect( treeWalker.is( 'element' ) ).to.be.false; + expect( treeWalker.is( 'p' ) ).to.be.false; + expect( treeWalker.is( 'text' ) ).to.be.false; + expect( treeWalker.is( 'textProxy' ) ).to.be.false; + expect( treeWalker.is( 'attributeElement' ) ).to.be.false; + expect( treeWalker.is( 'uiElement' ) ).to.be.false; + expect( treeWalker.is( 'emptyElement' ) ).to.be.false; + expect( treeWalker.is( 'documentFragment' ) ).to.be.false; + expect( treeWalker.is( 'model:treeWalker' ) ).to.be.false; + } ); + } ); + describe( 'iterate from start position `startPosition`', () => { let expected; diff --git a/tests/view/uielement.js b/tests/view/uielement.js index 65d591371..92eeda258 100644 --- a/tests/view/uielement.js +++ b/tests/view/uielement.js @@ -46,16 +46,26 @@ describe( 'UIElement', () => { it( 'should return true for uiElement/element, also with correct name and element name', () => { expect( el.is( 'uiElement' ) ).to.be.true; + expect( el.is( 'view:uiElement' ) ).to.be.true; expect( el.is( 'uiElement', 'span' ) ).to.be.true; + expect( el.is( 'view:uiElement', 'span' ) ).to.be.true; expect( el.is( 'element' ) ).to.be.true; + expect( el.is( 'view:element' ) ).to.be.true; + expect( el.is( 'node' ) ).to.be.true; + expect( el.is( 'view:node' ) ).to.be.true; expect( el.is( 'element', 'span' ) ).to.be.true; + expect( el.is( 'view:element', 'span' ) ).to.be.true; expect( el.is( 'span' ) ).to.be.true; + expect( el.is( 'view:span' ) ).to.be.true; } ); it( 'should return false for other accept values', () => { expect( el.is( 'uiElement', 'p' ) ).to.be.false; + expect( el.is( 'view:uiElement', 'p' ) ).to.be.false; expect( el.is( 'element', 'p' ) ).to.be.false; + expect( el.is( 'view:element', 'p' ) ).to.be.false; expect( el.is( 'p' ) ).to.be.false; + expect( el.is( 'view:p' ) ).to.be.false; expect( el.is( 'text' ) ).to.be.false; expect( el.is( 'textProxy' ) ).to.be.false; expect( el.is( 'containerElement' ) ).to.be.false; @@ -63,6 +73,9 @@ describe( 'UIElement', () => { expect( el.is( 'emptyElement' ) ).to.be.false; expect( el.is( 'rootElement' ) ).to.be.false; expect( el.is( 'documentFragment' ) ).to.be.false; + expect( el.is( 'model:element' ) ).to.be.false; + expect( el.is( 'model:span' ) ).to.be.false; + expect( el.is( 'model:node' ) ).to.be.false; } ); } ); diff --git a/tests/view/upcastwriter.js b/tests/view/upcastwriter.js index 26ce26089..b361aa8d7 100644 --- a/tests/view/upcastwriter.js +++ b/tests/view/upcastwriter.js @@ -34,6 +34,27 @@ describe( 'UpcastWriter', () => { view = dataprocessor.toView( html ); } ); + describe( 'is()', () => { + it( 'should return true for "upcastWriter"', () => { + expect( writer.is( 'upcastWriter' ) ).to.be.true; + expect( writer.is( 'view:upcastWriter' ) ).to.be.true; + } ); + + it( 'should return false for other accept values', () => { + expect( writer.is( 'rootElement' ) ).to.be.false; + expect( writer.is( 'containerElement' ) ).to.be.false; + expect( writer.is( 'element' ) ).to.be.false; + expect( writer.is( 'p' ) ).to.be.false; + expect( writer.is( 'text' ) ).to.be.false; + expect( writer.is( 'textProxy' ) ).to.be.false; + expect( writer.is( 'attributeElement' ) ).to.be.false; + expect( writer.is( 'uiElement' ) ).to.be.false; + expect( writer.is( 'emptyElement' ) ).to.be.false; + expect( writer.is( 'documentFragment' ) ).to.be.false; + expect( writer.is( 'model:renderer' ) ).to.be.false; + } ); + } ); + describe( 'createDocumentFragment', () => { it( 'should create empty document fragment', () => { const df = writer.createDocumentFragment(); From 101819e0b67d9581ceeb2025f69189c1502e6ef2 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 9 May 2019 12:42:46 +0200 Subject: [PATCH 08/30] Add prefix model to missingo ones. --- src/model/batch.js | 2 +- src/model/differ.js | 2 +- src/model/history.js | 2 +- src/model/markercollection.js | 4 ++-- src/model/schema.js | 2 +- tests/model/batch.js | 1 + tests/model/differ.js | 1 + tests/model/history.js | 1 + tests/model/markercollection.js | 2 ++ tests/model/schema.js | 1 + 10 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/model/batch.js b/src/model/batch.js index 67bc16c51..a47ae055f 100644 --- a/src/model/batch.js +++ b/src/model/batch.js @@ -87,6 +87,6 @@ export default class Batch { * @returns {Boolean} */ is( type ) { - return type == 'batch'; + return type == 'batch' || type == 'model:batch'; } } diff --git a/src/model/differ.js b/src/model/differ.js index c5d7316cd..9bb0a1e38 100644 --- a/src/model/differ.js +++ b/src/model/differ.js @@ -526,7 +526,7 @@ export default class Differ { * @returns {Boolean} */ is( type ) { - return type == 'differ'; + return type == 'differ' || type == 'model:differ'; } /** diff --git a/src/model/history.js b/src/model/history.js index 2b2df4c88..3feb7021f 100644 --- a/src/model/history.js +++ b/src/model/history.js @@ -104,7 +104,7 @@ export default class History { * @returns {Boolean} */ is( type ) { - return type == 'history'; + return type == 'history' || type == 'mdoel:history'; } /** diff --git a/src/model/markercollection.js b/src/model/markercollection.js index 3a565904f..c15980c54 100644 --- a/src/model/markercollection.js +++ b/src/model/markercollection.js @@ -79,7 +79,7 @@ export default class MarkerCollection { * @returns {Boolean} */ is( type ) { - return type == 'markerCollection'; + return type == 'markerCollection' || type == 'model:markerCollection'; } /** @@ -465,7 +465,7 @@ class Marker { * @returns {Boolean} */ is( type ) { - return type == 'marker'; + return type == 'marker' || type == 'model:marker'; } /** diff --git a/src/model/schema.js b/src/model/schema.js index 8c2cef269..4017046a8 100644 --- a/src/model/schema.js +++ b/src/model/schema.js @@ -194,7 +194,7 @@ export default class Schema { * @returns {Boolean} */ is( type ) { - return type == 'schema'; + return type == 'schema' || type == 'model:schema'; } /** diff --git a/tests/model/batch.js b/tests/model/batch.js index 28d816101..a7be0ae69 100644 --- a/tests/model/batch.js +++ b/tests/model/batch.js @@ -70,6 +70,7 @@ describe( 'Batch', () => { it( 'should return true for "batch"', () => { expect( batch.is( 'batch' ) ).to.be.true; + expect( batch.is( 'model:batch' ) ).to.be.true; } ); it( 'should return false for incorrect values', () => { diff --git a/tests/model/differ.js b/tests/model/differ.js index 781e766a3..9eadaa7e9 100644 --- a/tests/model/differ.js +++ b/tests/model/differ.js @@ -39,6 +39,7 @@ describe( 'Differ', () => { describe( 'is()', () => { it( 'should return true for "differ"', () => { expect( differ.is( 'differ' ) ).to.be.true; + expect( differ.is( 'model:differ' ) ).to.be.true; } ); it( 'should return false for incorrect values', () => { diff --git a/tests/model/history.js b/tests/model/history.js index ad2e7294b..69a11bc67 100644 --- a/tests/model/history.js +++ b/tests/model/history.js @@ -182,6 +182,7 @@ describe( 'History', () => { describe( 'is()', () => { it( 'should return true for "history"', () => { expect( history.is( 'history' ) ).to.be.true; + expect( history.is( 'model:history' ) ).to.be.true; } ); it( 'should return false for incorrect values', () => { diff --git a/tests/model/markercollection.js b/tests/model/markercollection.js index 8e678444c..fff7897b3 100644 --- a/tests/model/markercollection.js +++ b/tests/model/markercollection.js @@ -149,6 +149,7 @@ describe( 'MarkerCollection', () => { describe( 'is()', () => { it( 'should return true for "markerCollection"', () => { expect( markers.is( 'markerCollection' ) ).to.be.true; + expect( markers.is( 'model:markerCollection' ) ).to.be.true; } ); it( 'should return false for incorrect values', () => { @@ -433,6 +434,7 @@ describe( 'Marker', () => { it( 'should return true for "marker"', () => { expect( marker.is( 'marker' ) ).to.be.true; + expect( marker.is( 'model:marker' ) ).to.be.true; } ); it( 'should return false for incorrect values', () => { diff --git a/tests/model/schema.js b/tests/model/schema.js index dcfc381a4..ca3eee46f 100644 --- a/tests/model/schema.js +++ b/tests/model/schema.js @@ -314,6 +314,7 @@ describe( 'Schema', () => { describe( 'is()', () => { it( 'should return true for "schema"', () => { expect( schema.is( 'schema' ) ).to.be.true; + expect( schema.is( 'model:schema' ) ).to.be.true; } ); it( 'should return false for incorrect values', () => { From c725da1c43cb591faeab275c63edd6179ea84e7e Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 9 May 2019 15:48:08 +0200 Subject: [PATCH 09/30] Add 'is()' method to operations. --- src/model/operation/attributeoperation.js | 7 +++++ src/model/operation/detachoperation.js | 7 +++++ src/model/operation/insertoperation.js | 7 +++++ src/model/operation/markeroperation.js | 7 +++++ src/model/operation/mergeoperation.js | 7 +++++ src/model/operation/moveoperation.js | 7 +++++ src/model/operation/nooperation.js | 7 +++++ src/model/operation/operation.js | 28 +++++++++++++++++ src/model/operation/renameoperation.js | 7 +++++ src/model/operation/rootattributeoperation.js | 9 ++++++ src/model/operation/splitoperation.js | 7 +++++ tests/model/operation/attributeoperation.js | 31 +++++++++++++++++++ tests/model/operation/detachoperation.js | 25 +++++++++++++++ tests/model/operation/insertoperation.js | 25 +++++++++++++++ tests/model/operation/markeroperation.js | 24 ++++++++++++++ tests/model/operation/mergeoperation.js | 26 ++++++++++++++++ tests/model/operation/moveoperation.js | 26 ++++++++++++++++ tests/model/operation/nooperation.js | 18 +++++++++++ tests/model/operation/operation.js | 22 +++++++++++++ tests/model/operation/renameoperation.js | 24 ++++++++++++++ .../model/operation/rootattributeoperation.js | 30 ++++++++++++++++++ tests/model/operation/splitoperation.js | 25 +++++++++++++++ 22 files changed, 376 insertions(+) diff --git a/src/model/operation/attributeoperation.js b/src/model/operation/attributeoperation.js index 2cc283c8f..81aecb7ab 100644 --- a/src/model/operation/attributeoperation.js +++ b/src/model/operation/attributeoperation.js @@ -107,6 +107,13 @@ export default class AttributeOperation extends Operation { return new AttributeOperation( this.range, this.key, this.newValue, this.oldValue, this.baseVersion + 1 ); } + /** + * @inheritDoc + */ + is( type ) { + return type == 'attributeOperation' || type == 'model:operation:attribute' || super.is( type ); + } + /** * @inheritDoc */ diff --git a/src/model/operation/detachoperation.js b/src/model/operation/detachoperation.js index f4a311421..6ceb74033 100644 --- a/src/model/operation/detachoperation.js +++ b/src/model/operation/detachoperation.js @@ -52,6 +52,13 @@ export default class DetachOperation extends Operation { return 'detach'; } + /** + * @inheritDoc + */ + is( type ) { + return type == 'detachOperation' || type == 'model:operation:detach' || super.is( type ); + } + /** * @inheritDoc */ diff --git a/src/model/operation/insertoperation.js b/src/model/operation/insertoperation.js index 1ea873cfa..081cc2f0d 100644 --- a/src/model/operation/insertoperation.js +++ b/src/model/operation/insertoperation.js @@ -76,6 +76,13 @@ export default class InsertOperation extends Operation { return this.nodes.maxOffset; } + /** + * @inheritDoc + */ + is( type ) { + return type == 'insertOperation' || type == 'model:operation:insert' || super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/markeroperation.js b/src/model/operation/markeroperation.js index ace0bebe3..946848977 100644 --- a/src/model/operation/markeroperation.js +++ b/src/model/operation/markeroperation.js @@ -76,6 +76,13 @@ export default class MarkerOperation extends Operation { return 'marker'; } + /** + * @inheritDoc + */ + is( type ) { + return type == 'markerOperation' || type == 'model:operation:marker' || super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/mergeoperation.js b/src/model/operation/mergeoperation.js index d6a101861..577d5317a 100644 --- a/src/model/operation/mergeoperation.js +++ b/src/model/operation/mergeoperation.js @@ -104,6 +104,13 @@ export default class MergeOperation extends Operation { return new Range( this.sourcePosition, end ); } + /** + * @inheritDoc + */ + is( type ) { + return type == 'mergeOperation' || type == 'model:operation:merge' || super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/moveoperation.js b/src/model/operation/moveoperation.js index 799a3d1da..c7c0789d0 100644 --- a/src/model/operation/moveoperation.js +++ b/src/model/operation/moveoperation.js @@ -73,6 +73,13 @@ export default class MoveOperation extends Operation { return 'move'; } + /** + * @inheritDoc + */ + is( type ) { + return type == 'moveOperation' || type == 'model:operation:move' || super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/nooperation.js b/src/model/operation/nooperation.js index f2e0a2ede..4f71ce2a5 100644 --- a/src/model/operation/nooperation.js +++ b/src/model/operation/nooperation.js @@ -24,6 +24,13 @@ export default class NoOperation extends Operation { return 'noop'; } + /** + * @inheritDoc + */ + is( type ) { + return type == 'noOperation' || type == 'model:operation:no' || super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/operation.js b/src/model/operation/operation.js index 06ca9753c..d89738588 100644 --- a/src/model/operation/operation.js +++ b/src/model/operation/operation.js @@ -81,6 +81,34 @@ export default class Operation { */ } + /** + * Checks whether given object is of `operation` type. This method is useful when processing operations are of unknown type. + * This method can be used to check what type of operation is returned. General rules how to check operation are presented for + * attribute operation. You need to use analogical syntax for other {@link module:engine/model/operation/operation~Operation}. + * + * // Examples for AttributeOperation. + * operation.is( 'operation' ) // true for any operation + * operation.is( 'model:operation' ) // true for any operation + * operation.is( 'attributeOperation' ) // true for AttributeOperation + * operation.is( 'model:operation:attribute' ) // true for AttributeOperation + * + * // Random examples for other Operations + * operation.is( 'model:operation:no ) // true for no-operation + * operation.is( 'noOperation' ) // true for no-operation + * operation.is( 'model:operation:marker ) // true for marker operation + * operation.is( 'insertOperation' ) // true for insert operation + * operation.is( 'model:operation:rootAttribute' ) // true for rootAttributeOperation + * + * // Disallowed syntax + * operation.is( 'operation:attribute' ) // will always return false + * + * @param {String} type + * @returns {Boolean} + */ + is( type ) { + return type == 'operation' || type == 'model:operation'; + } + /** * Checks whether the operation's parameters are correct and the operation can be correctly executed. Throws * an error if operation is not valid. diff --git a/src/model/operation/renameoperation.js b/src/model/operation/renameoperation.js index 12d874f84..a7a48f612 100644 --- a/src/model/operation/renameoperation.js +++ b/src/model/operation/renameoperation.js @@ -63,6 +63,13 @@ export default class RenameOperation extends Operation { return 'rename'; } + /** + * @inheritDoc + */ + is( type ) { + return type == 'renameOperation' || type == 'model:operation:rename' || super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/rootattributeoperation.js b/src/model/operation/rootattributeoperation.js index 2af7597ba..19ca7a50e 100644 --- a/src/model/operation/rootattributeoperation.js +++ b/src/model/operation/rootattributeoperation.js @@ -83,6 +83,15 @@ export default class RootAttributeOperation extends Operation { } } + /** + * @inheritDoc + */ + is( type ) { + return type == 'rootAttributeOperation' || + type == 'model:operation:rootAttribute' || + super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/splitoperation.js b/src/model/operation/splitoperation.js index fd215a91d..cb9c3b581 100644 --- a/src/model/operation/splitoperation.js +++ b/src/model/operation/splitoperation.js @@ -111,6 +111,13 @@ export default class SplitOperation extends Operation { return new Range( this.splitPosition, end ); } + /** + * @inheritDoc + */ + is( type ) { + return type == 'splitOperation' || type == 'model:operation:split' || super.is( type ); + } + /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/tests/model/operation/attributeoperation.js b/tests/model/operation/attributeoperation.js index 78e6823aa..22a153f7f 100644 --- a/tests/model/operation/attributeoperation.js +++ b/tests/model/operation/attributeoperation.js @@ -60,6 +60,37 @@ describe( 'AttributeOperation', () => { } ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ); + operation = new AttributeOperation( + range, + 'key', + null, + 'newValue', + doc.version + ); + } ); + + it( 'should return true for all valid names of "attribute" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'attributeOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:attribute' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:attribute' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + it( 'should insert attribute to the set of nodes', () => { root._insertChild( 0, new Text( 'bar' ) ); diff --git a/tests/model/operation/detachoperation.js b/tests/model/operation/detachoperation.js index dadd7f3dc..3bc13d197 100644 --- a/tests/model/operation/detachoperation.js +++ b/tests/model/operation/detachoperation.js @@ -35,6 +35,31 @@ describe( 'DetachOperation', () => { expect( docFrag.childCount ).to.equal( 0 ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + const position = Position._createBefore( element ); + operation = new DetachOperation( position, 1 ); + } ); + + it( 'should return true for all valid names of "detach" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'detachOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:detach' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:detach' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'attributeOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( '_validate()', () => { it( 'should throw when is executed on element from document', () => { const root = doc.createRoot(); diff --git a/tests/model/operation/insertoperation.js b/tests/model/operation/insertoperation.js index c088389c3..bbedd3dd6 100644 --- a/tests/model/operation/insertoperation.js +++ b/tests/model/operation/insertoperation.js @@ -220,6 +220,31 @@ describe( 'InsertOperation', () => { expect( op2.nodes.getNode( 0 ) ).not.to.equal( text ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + const position = new Position( root, [ 0 ] ); + operation = new InsertOperation( position, new Text( 'x' ), doc.version ); + } ); + + it( 'should return true for all valid names of "insert" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'insertOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:insert' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:insert' ) ).to.be.false; + expect( operation.is( 'model:operation:attribute' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( '_validate()', () => { it( 'should throw an error if target position does not exist', () => { const element = new Element( 'p' ); diff --git a/tests/model/operation/markeroperation.js b/tests/model/operation/markeroperation.js index 3a5075305..a0045c4ec 100644 --- a/tests/model/operation/markeroperation.js +++ b/tests/model/operation/markeroperation.js @@ -130,6 +130,30 @@ describe( 'MarkerOperation', () => { expect( clone ).to.deep.equal( op ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + operation = new MarkerOperation( 'name', null, range, model.markers, true, doc.version ); + } ); + + it( 'should return true for all valid names of "marker" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'markerOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:marker' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:marker' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper serialized object', () => { const op = new MarkerOperation( 'name', null, range, model.markers, true, doc.version ); diff --git a/tests/model/operation/mergeoperation.js b/tests/model/operation/mergeoperation.js index d2ed0f202..03f64b274 100644 --- a/tests/model/operation/mergeoperation.js +++ b/tests/model/operation/mergeoperation.js @@ -195,6 +195,32 @@ describe( 'MergeOperation', () => { expect( clone.baseVersion ).to.equal( baseVersion ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + const sourcePosition = new Position( root, [ 1, 0 ] ); + const targetPosition = new Position( root, [ 0, 3 ] ); + operation = new MergeOperation( sourcePosition, 1, targetPosition, gyPos, doc.version ); + } ); + + it( 'should return true for all valid names of "merge" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'mergeOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:merge' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:merge' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper json object', () => { const sourcePosition = new Position( root, [ 1, 0 ] ); diff --git a/tests/model/operation/moveoperation.js b/tests/model/operation/moveoperation.js index 987b3cb35..20b3b48b8 100644 --- a/tests/model/operation/moveoperation.js +++ b/tests/model/operation/moveoperation.js @@ -264,6 +264,32 @@ describe( 'MoveOperation', () => { } ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + const sourcePosition = new Position( root, [ 0, 0 ] ); + const targetPosition = new Position( root, [ 1, 0 ] ); + operation = new MoveOperation( sourcePosition, 1, targetPosition, doc.version ); + } ); + + it( 'should return true for all valid names of "move" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'moveOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:move' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:move' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper json object', () => { const sourcePosition = new Position( root, [ 0, 0 ] ); diff --git a/tests/model/operation/nooperation.js b/tests/model/operation/nooperation.js index 809fdd20f..a66e743fd 100644 --- a/tests/model/operation/nooperation.js +++ b/tests/model/operation/nooperation.js @@ -33,6 +33,24 @@ describe( 'NoOperation', () => { expect( clone.baseVersion ).to.equal( 0 ); } ); + describe( 'is()', () => { + it( 'should return true for all valid names of "no" operation', () => { + expect( noop.is( 'operation' ) ).to.be.true; + expect( noop.is( 'model:operation' ) ).to.be.true; + expect( noop.is( 'noOperation' ) ).to.be.true; + expect( noop.is( 'model:operation:no' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( noop.is( 'operation:no' ) ).to.be.false; + expect( noop.is( 'model:operation:insert' ) ).to.be.false; + expect( noop.is( 'attributeOperation' ) ).to.be.false; + expect( noop.is( 'detachOperation' ) ).to.be.false; + expect( noop.is( 'rootAttributeOperation' ) ).to.be.false; + expect( noop.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper json object', () => { const serialized = noop.toJSON(); diff --git a/tests/model/operation/operation.js b/tests/model/operation/operation.js index 7783630f0..34d0b4b2d 100644 --- a/tests/model/operation/operation.js +++ b/tests/model/operation/operation.js @@ -27,6 +27,28 @@ describe( 'Operation', () => { } ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + operation = new Operation( null ); + } ); + + it( 'should return true for all valid names of operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:attribute' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper json object #1', () => { const op = new Operation( 4 ); diff --git a/tests/model/operation/renameoperation.js b/tests/model/operation/renameoperation.js index 2a9ab4feb..adfb0d5bc 100644 --- a/tests/model/operation/renameoperation.js +++ b/tests/model/operation/renameoperation.js @@ -103,6 +103,30 @@ describe( 'RenameOperation', () => { expect( clone.newName ).to.equal( newName ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + operation = new RenameOperation( Position._createAt( root, 'end' ), oldName, newName, doc.version ); + } ); + + it( 'should return true for all valid names of "rename" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'renameOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:rename' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:rename' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper serialized object', () => { const op = new RenameOperation( Position._createAt( root, 'end' ), oldName, newName, doc.version ); diff --git a/tests/model/operation/rootattributeoperation.js b/tests/model/operation/rootattributeoperation.js index cab0e3ec1..335ffe152 100644 --- a/tests/model/operation/rootattributeoperation.js +++ b/tests/model/operation/rootattributeoperation.js @@ -257,6 +257,36 @@ describe( 'RootAttributeOperation', () => { expect( clone.baseVersion ).to.equal( baseVersion ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + operation = new RootAttributeOperation( + root, + 'key', + null, + 'newValue', + doc.version + ); + } ); + + it( 'should return true for all valid names of "rootAttribute" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:rootAttribute' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'attributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:attribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper serialized object', () => { const op = new RootAttributeOperation( diff --git a/tests/model/operation/splitoperation.js b/tests/model/operation/splitoperation.js index 1da49447b..4e9a4a0c9 100644 --- a/tests/model/operation/splitoperation.js +++ b/tests/model/operation/splitoperation.js @@ -201,6 +201,31 @@ describe( 'SplitOperation', () => { expect( clone.baseVersion ).to.equal( baseVersion ); } ); + describe( 'is()', () => { + let operation; + + before( () => { + const position = new Position( root, [ 0, 3 ] ); + operation = new SplitOperation( position, 2, null, doc.version ); + } ); + + it( 'should return true for all valid names of "split" operation', () => { + expect( operation.is( 'operation' ) ).to.be.true; + expect( operation.is( 'model:operation' ) ).to.be.true; + expect( operation.is( 'splitOperation' ) ).to.be.true; + expect( operation.is( 'model:operation:split' ) ).to.be.true; + } ); + + it( 'should return false for invalid parameters', () => { + expect( operation.is( 'operation:split' ) ).to.be.false; + expect( operation.is( 'model:operation:insert' ) ).to.be.false; + expect( operation.is( 'noOperation' ) ).to.be.false; + expect( operation.is( 'detachOperation' ) ).to.be.false; + expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; + expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; + } ); + } ); + describe( 'toJSON', () => { it( 'should create proper json object #1', () => { const position = new Position( root, [ 0, 3 ] ); From 67a5a74efebae23e4caa0bcee627ea8e47565a96 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 10 May 2019 09:55:48 +0200 Subject: [PATCH 10/30] Update docs description for engine/model classes. --- src/model/batch.js | 2 ++ src/model/differ.js | 2 ++ src/model/document.js | 2 ++ src/model/documentfragment.js | 2 +- src/model/documentselection.js | 2 ++ src/model/element.js | 2 +- src/model/history.js | 2 ++ src/model/markercollection.js | 4 ++++ src/model/model.js | 16 ++++++++++++++++ src/model/node.js | 2 ++ src/model/nodelist.js | 2 ++ src/model/operation/operation.js | 15 +++++++++------ src/model/position.js | 2 ++ src/model/range.js | 2 ++ src/model/schema.js | 2 ++ src/model/selection.js | 2 ++ src/model/textproxy.js | 2 +- src/model/treewalker.js | 2 ++ src/model/writer.js | 2 ++ 19 files changed, 58 insertions(+), 9 deletions(-) diff --git a/src/model/batch.js b/src/model/batch.js index a47ae055f..8d5ca4a9e 100644 --- a/src/model/batch.js +++ b/src/model/batch.js @@ -83,6 +83,8 @@ export default class Batch { /** * Checks whether given object is of `batch` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/differ.js b/src/model/differ.js index 9bb0a1e38..ad4b489bc 100644 --- a/src/model/differ.js +++ b/src/model/differ.js @@ -522,6 +522,8 @@ export default class Differ { /** * Checks whether given object is of `differ` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/document.js b/src/model/document.js index ade127b14..6eec95cd7 100644 --- a/src/model/document.js +++ b/src/model/document.js @@ -244,6 +244,8 @@ export default class Document { /** * Checks whether given object is of `document` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/documentfragment.js b/src/model/documentfragment.js index ca6bd008c..e32c30329 100644 --- a/src/model/documentfragment.js +++ b/src/model/documentfragment.js @@ -118,7 +118,7 @@ export default class DocumentFragment { /** * Checks whether given model tree object is of given type. * - * Read more in {@link module:engine/model/node~Node#is}. + * Read more in {@link module:engine/model/node~Node#is `Node#is()`} and {@link module:engine/model/model~Model#is `Model#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/documentselection.js b/src/model/documentselection.js index d339a3485..0947a34d5 100644 --- a/src/model/documentselection.js +++ b/src/model/documentselection.js @@ -382,6 +382,8 @@ export default class DocumentSelection { * selection.is( 'model:node' ); // false * selection.is( 'element' ); // false * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/element.js b/src/model/element.js index cff992681..ba5fad4ba 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -105,7 +105,7 @@ export default class Element extends Node { * obj.is( 'view:element' ); // false * obj.is( 'element', 'image' ); // false * - * Read more in {@link module:engine/model/node~Node#is `Node#is()`}. + * Read more in {@link module:engine/model/node~Node#is `Node#is()`} and {@link module:engine/model/model~Model#is `Model#is()`}. * * @param {String} type Type to check when `name` parameter is present. * Otherwise, it acts like the `name` parameter. diff --git a/src/model/history.js b/src/model/history.js index 3feb7021f..bf1e79ed7 100644 --- a/src/model/history.js +++ b/src/model/history.js @@ -100,6 +100,8 @@ export default class History { /** * Checks whether given object is of `history` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/markercollection.js b/src/model/markercollection.js index c15980c54..6e3148f7f 100644 --- a/src/model/markercollection.js +++ b/src/model/markercollection.js @@ -75,6 +75,8 @@ export default class MarkerCollection { /** * Checks whether given object is of `markerCollection` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ @@ -461,6 +463,8 @@ class Marker { /** * Checks whether given object is of `marker` type. * + * Read more at {@link module:engine/model/model~Model#is}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/model.js b/src/model/model.js index bddede4a4..0404789f5 100644 --- a/src/model/model.js +++ b/src/model/model.js @@ -720,6 +720,22 @@ export default class Model { /** * Checks whether given object is of `model` type. * + * All classes related to {@link module:engine/model/model~Model} might contain `is` method, + * which checks if given object belong to specific type. It might simplify your code for cases, + * when you want to test unknown object for specific type. + * Instead of using `instanceof` and importing entire class for testing, + * there might be used `is` method which test for given name. + * There is also available `model:` prefix in each case, which gives more specific results. + * It helps to distinguish model's classes from view's. Few examples how to use this method you can find below: + * + * model.is( 'model' ) // return true + * range.is( 'range' ) // return true + * range.is( 'model:range' ) // return true + * range.is( 'view:range' ) // return false + * document.is( 'model:document' ) // return true + * + * See also {@link module:engine/model/node~Node#is `Node#is()`} for some more details related to elements. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/node.js b/src/model/node.js index d426b8b43..4f579f2d5 100644 --- a/src/model/node.js +++ b/src/model/node.js @@ -478,6 +478,8 @@ export default class Node { * obj.is( 'text' ); // true for text node, false for element and document fragment * obj.is( 'textProxy' ); // true for text proxy object * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @method #is * @param {String} type * @returns {Boolean} diff --git a/src/model/nodelist.js b/src/model/nodelist.js index b57383007..42f81868e 100644 --- a/src/model/nodelist.js +++ b/src/model/nodelist.js @@ -173,6 +173,8 @@ export default class NodeList { /** * Checks whether given object is of `nodeList` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/operation/operation.js b/src/model/operation/operation.js index d89738588..d84ff13f7 100644 --- a/src/model/operation/operation.js +++ b/src/model/operation/operation.js @@ -87,21 +87,24 @@ export default class Operation { * attribute operation. You need to use analogical syntax for other {@link module:engine/model/operation/operation~Operation}. * * // Examples for AttributeOperation. - * operation.is( 'operation' ) // true for any operation - * operation.is( 'model:operation' ) // true for any operation + * + * operation instanceof AttributeOperation + * + * operation.is( 'operation' ) // true for AttributeOperation and other operations + * operation.is( 'model:operation' ) // true for AttributeOperation and other operations * operation.is( 'attributeOperation' ) // true for AttributeOperation * operation.is( 'model:operation:attribute' ) // true for AttributeOperation * - * // Random examples for other Operations + * // Disallowed syntax + * operation.is( 'operation:attribute' ) // will return false + * + * // Few examples for other Operations * operation.is( 'model:operation:no ) // true for no-operation * operation.is( 'noOperation' ) // true for no-operation * operation.is( 'model:operation:marker ) // true for marker operation * operation.is( 'insertOperation' ) // true for insert operation * operation.is( 'model:operation:rootAttribute' ) // true for rootAttributeOperation * - * // Disallowed syntax - * operation.is( 'operation:attribute' ) // will always return false - * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/position.js b/src/model/position.js index 2ed8d8110..f10695e0c 100644 --- a/src/model/position.js +++ b/src/model/position.js @@ -505,6 +505,8 @@ export default class Position { /** * Checks whether given object is of `position` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/range.js b/src/model/range.js index 1974c787e..188c1b21b 100644 --- a/src/model/range.js +++ b/src/model/range.js @@ -146,6 +146,8 @@ export default class Range { /** * Checks whether given object is of `range` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/schema.js b/src/model/schema.js index 4017046a8..5ec0b3957 100644 --- a/src/model/schema.js +++ b/src/model/schema.js @@ -190,6 +190,8 @@ export default class Schema { /** * Checks whether given object is of `schema` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/selection.js b/src/model/selection.js index f5415fcae..e3fa111f8 100644 --- a/src/model/selection.js +++ b/src/model/selection.js @@ -634,6 +634,8 @@ export default class Selection { * selection.is( 'element' ); // false * selection.is( 'view:selection' ); // false * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/textproxy.js b/src/model/textproxy.js index 004f8c4d4..bbba241fe 100644 --- a/src/model/textproxy.js +++ b/src/model/textproxy.js @@ -175,7 +175,7 @@ export default class TextProxy { /** * Checks whether given object is of `textProxy` type. * - * Read more in {@link module:engine/model/node~Node#is}. + * Read more in {@link module:engine/model/node~Node#is `Node#is()`} and {@link module:engine/model/model~Model#is `Model#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/treewalker.js b/src/model/treewalker.js index 7dad34271..bf01dd3e2 100644 --- a/src/model/treewalker.js +++ b/src/model/treewalker.js @@ -155,6 +155,8 @@ export default class TreeWalker { /** * Checks whether given object is of `treeWalker` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/writer.js b/src/model/writer.js index df2580f6c..0ceadbf42 100644 --- a/src/model/writer.js +++ b/src/model/writer.js @@ -82,6 +82,8 @@ export default class Writer { /** * Checks whether given object is of `writer` type. * + * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * * @param {String} type * @returns {Boolean} */ From e239f4fe54a075560be52a94a18660e760a64450 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 10 May 2019 13:11:20 +0200 Subject: [PATCH 11/30] Add documentation for view's classes. --- src/model/model.js | 7 +++++++ src/view/document.js | 2 ++ src/view/documentfragment.js | 2 +- src/view/documentselection.js | 2 ++ src/view/domconverter.js | 2 ++ src/view/downcastwriter.js | 2 ++ src/view/element.js | 2 +- src/view/matcher.js | 2 ++ src/view/node.js | 11 +++++++++-- src/view/position.js | 2 ++ src/view/range.js | 2 ++ src/view/renderer.js | 2 ++ src/view/selection.js | 3 +++ src/view/textproxy.js | 2 +- src/view/treewalker.js | 2 ++ src/view/upcastwriter.js | 2 ++ src/view/view.js | 21 +++++++++++++++++++++ 17 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/model/model.js b/src/model/model.js index 0404789f5..41316aa33 100644 --- a/src/model/model.js +++ b/src/model/model.js @@ -728,10 +728,17 @@ export default class Model { * There is also available `model:` prefix in each case, which gives more specific results. * It helps to distinguish model's classes from view's. Few examples how to use this method you can find below: * + * const model = new Model(); * model.is( 'model' ) // return true + * + * const range = new LiveRange( startPosition ) * range.is( 'range' ) // return true + * range.is( 'liveRange' ) // return true * range.is( 'model:range' ) // return true + * range.is( 'model:liveRange' ) // return true * range.is( 'view:range' ) // return false + * + * const document = new Document(); * document.is( 'model:document' ) // return true * * See also {@link module:engine/model/node~Node#is `Node#is()`} for some more details related to elements. diff --git a/src/view/document.js b/src/view/document.js index 28e576f59..c8a76b812 100644 --- a/src/view/document.js +++ b/src/view/document.js @@ -135,6 +135,8 @@ export default class Document { /** * Checks whether given object is of `document` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/documentfragment.js b/src/view/documentfragment.js index 238db705e..a66e21861 100644 --- a/src/view/documentfragment.js +++ b/src/view/documentfragment.js @@ -96,7 +96,7 @@ export default class DocumentFragment { /** * Checks whether given view tree object is of given type. * - * Read more in {@link module:engine/view/node~Node#is}. + * Read more in {@link module:engine/view/node~Node#is `Node#is()`} and {@link module:engine/view/view~View#is `View#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/documentselection.js b/src/view/documentselection.js index ab4225688..b8c0c419b 100644 --- a/src/view/documentselection.js +++ b/src/view/documentselection.js @@ -289,6 +289,8 @@ export default class DocumentSelection { * selection.is( 'model:selection' ); // false * selection.is( 'element' ); // false * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/domconverter.js b/src/view/domconverter.js index f2342f191..dfa89a73b 100644 --- a/src/view/domconverter.js +++ b/src/view/domconverter.js @@ -108,6 +108,8 @@ export default class DomConverter { /** * Checks whether given object is of `domConverter` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/downcastwriter.js b/src/view/downcastwriter.js index 27fcab852..3ccb8c410 100644 --- a/src/view/downcastwriter.js +++ b/src/view/downcastwriter.js @@ -57,6 +57,8 @@ export default class DowncastWriter { /** * Checks whether given object is of `downcastWriter` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/element.js b/src/view/element.js index 0efc962b3..707d67ee0 100644 --- a/src/view/element.js +++ b/src/view/element.js @@ -155,7 +155,7 @@ export default class Element extends Node { * obj.is( 'text' ); // false * obj.is( 'element', 'img' ); // false * - * Read more in {@link module:engine/view/node~Node#is `Node#is()`}. + * Read more in {@link module:engine/view/node~Node#is `Node#is()`} and {@link module:engine/view/view~View#is `View#is()`}. * * @param {String} type * @param {String} [name] Element name. diff --git a/src/view/matcher.js b/src/view/matcher.js index 52645e2c0..d77d77992 100644 --- a/src/view/matcher.js +++ b/src/view/matcher.js @@ -178,6 +178,8 @@ export default class Matcher { /** * Checks whether given object is of `matcher` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/node.js b/src/view/node.js index 055023427..a301d6ff0 100644 --- a/src/view/node.js +++ b/src/view/node.js @@ -297,14 +297,21 @@ export default class 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( 'view: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( 'view: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( 'ciew:element' ); // true for any element, false for text node or document fragment * obj.is( 'element', 'p' ); // true only for element which name is 'p' + * obj.is( 'view:element', 'p' ); // true only for element which name is 'p' * obj.is( 'p' ); // shortcut for obj.is( 'element', 'p' ) + * obj.is( 'view:p' ); // shortcut for obj.is( 'view:element', 'p' ) * obj.is( 'text' ); // true for text node, false for element and document fragment + * obj.is( 'view:text' ); // true for text node, false for element and document fragment * - * @param {'element'|'containerElement'|'attributeElement'|'emptyElement'|'uiElement'| - * 'rootElement'|'documentFragment'|'text'|'textProxy'} type + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * + * @param {String} type * @returns {Boolean} */ is( type ) { diff --git a/src/view/position.js b/src/view/position.js index b37b7ef3b..95611c562 100644 --- a/src/view/position.js +++ b/src/view/position.js @@ -209,6 +209,8 @@ export default class Position { /** * Checks whether given object is of `position` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/range.js b/src/view/range.js index cc194387f..43478df21 100644 --- a/src/view/range.js +++ b/src/view/range.js @@ -397,6 +397,8 @@ export default class Range { /** * Checks whether given object is of `range` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/renderer.js b/src/view/renderer.js index 3bdf6029a..5b5b17a0c 100644 --- a/src/view/renderer.js +++ b/src/view/renderer.js @@ -121,6 +121,8 @@ export default class Renderer { /** * Checks whether given object is of `renderer` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/selection.js b/src/view/selection.js index 4ec70e947..b31dbb9f9 100644 --- a/src/view/selection.js +++ b/src/view/selection.js @@ -604,9 +604,12 @@ export default class Selection { * const selection = new Selection( ... ); * * selection.is( 'selection' ); // true + * selection.is( 'view:selection' ); // true * selection.is( 'node' ); // false * selection.is( 'element' ); // false * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/textproxy.js b/src/view/textproxy.js index 4b4cdadb8..3d459e37c 100644 --- a/src/view/textproxy.js +++ b/src/view/textproxy.js @@ -143,7 +143,7 @@ export default class TextProxy { /** * Checks whether given view tree object is of given type. * - * Read more in {@link module:engine/view/node~Node#is}. + * Read more in {@link module:engine/view/node~Node#is `Node#is()`} and Read {@link module:engine/view/view~View#is `View#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/treewalker.js b/src/view/treewalker.js index 543d02c92..b1ae7ba67 100644 --- a/src/view/treewalker.js +++ b/src/view/treewalker.js @@ -147,6 +147,8 @@ export default class TreeWalker { /** * Checks whether given object is of `treeWalker` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/upcastwriter.js b/src/view/upcastwriter.js index 01f9af824..ec0a83736 100644 --- a/src/view/upcastwriter.js +++ b/src/view/upcastwriter.js @@ -38,6 +38,8 @@ export default class UpcastWriter { /** * Checks whether given object is of `upcastWriter` type. * + * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/view.js b/src/view/view.js index 0f813cb06..e98a10f96 100644 --- a/src/view/view.js +++ b/src/view/view.js @@ -655,6 +655,27 @@ export default class View { /** * Checks whether given object is of `view` type. * + * All classes related to {@link module:engine/view/view~View} might contain `is` method, + * which checks if given object belong to specific type. It might simplify your code for cases, + * when you want to test unknown object for specific type. + * Instead of using `instanceof` and importing entire class for testing, + * there might be used `is` method which test for given name. + * There is also available `view:` prefix in each case, which gives more specific results. + * It helps to distinguish view's classes from model's. Few examples how to use this method you can find below: + * + * const view = new View() + * view.is( 'view' ) // return true + * + * const range = new Range( startPosition ) + * range.is( 'range' ) // return true + * range.is( 'view:range' ) // return true + * range.is( 'model:range' ) // return false + * + * const document = new Document(); + * document.is( 'model:document' ) // return true + * + * See also {@link module:engine/view/node~Node#is `Node#is()`} for some more details related to elements. + * * @param {String} type * @returns {Boolean} */ From c2863de9c6cf9421a0ed9e40979e5339d26330a6 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 10 May 2019 14:09:36 +0200 Subject: [PATCH 12/30] Some additioanl tweaks to do cumentation description. --- src/model/model.js | 11 +++++++---- src/view/view.js | 11 +++++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/model/model.js b/src/model/model.js index 41316aa33..436552de7 100644 --- a/src/model/model.js +++ b/src/model/model.js @@ -721,10 +721,13 @@ export default class Model { * Checks whether given object is of `model` type. * * All classes related to {@link module:engine/model/model~Model} might contain `is` method, - * which checks if given object belong to specific type. It might simplify your code for cases, - * when you want to test unknown object for specific type. - * Instead of using `instanceof` and importing entire class for testing, - * there might be used `is` method which test for given name. + * which checks if given object belong to specific type. Types are defined as name of the class + * written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. E.g. class `LiveRange` + * will get type `liveRange`. There might appear some additional possibilities to test types which will be described in + * related documentation. + * + * Method might simplify your code for cases, when you want to test unknown object for specific type. + * Instead of using `instanceof` and importing entire class for testing, there might be used `is` method which test for given name. * There is also available `model:` prefix in each case, which gives more specific results. * It helps to distinguish model's classes from view's. Few examples how to use this method you can find below: * diff --git a/src/view/view.js b/src/view/view.js index e98a10f96..5f284aa53 100644 --- a/src/view/view.js +++ b/src/view/view.js @@ -656,10 +656,13 @@ export default class View { * Checks whether given object is of `view` type. * * All classes related to {@link module:engine/view/view~View} might contain `is` method, - * which checks if given object belong to specific type. It might simplify your code for cases, - * when you want to test unknown object for specific type. - * Instead of using `instanceof` and importing entire class for testing, - * there might be used `is` method which test for given name. + * which checks if given object belong to specific type. Types are defined as name of the class + * written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. E.g. class `DocumentFragment` + * will get type `documentFragment`. There might appear some additional possibilities to test types which will be described in + * related documentation. + * + * It might simplify your code for cases, when you want to test unknown object for specific type. + * Instead of using `instanceof` and importing entire class for testing, there might be used `is` method which test for given name. * There is also available `view:` prefix in each case, which gives more specific results. * It helps to distinguish view's classes from model's. Few examples how to use this method you can find below: * From 264457df5c7032fceca4e918b8e1a892477df3ce Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 10 May 2019 14:20:31 +0200 Subject: [PATCH 13/30] Improve code spacing. --- tests/model/liverange.js | 1 + tests/model/markercollection.js | 1 + tests/model/position.js | 1 + tests/view/node.js | 2 ++ 4 files changed, 5 insertions(+) diff --git a/tests/model/liverange.js b/tests/model/liverange.js index 00c6edaa2..e5ade9da0 100644 --- a/tests/model/liverange.js +++ b/tests/model/liverange.js @@ -185,6 +185,7 @@ describe( 'LiveRange', () => { describe( 'is()', () => { let live; + beforeEach( () => { live = new LiveRange( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ); live.detach(); diff --git a/tests/model/markercollection.js b/tests/model/markercollection.js index fff7897b3..7247c37a2 100644 --- a/tests/model/markercollection.js +++ b/tests/model/markercollection.js @@ -427,6 +427,7 @@ describe( 'Marker', () => { describe( 'is()', () => { let marker; + beforeEach( () => { const range = new Range( Position._createAt( root, 1 ), Position._createAt( root, 2 ) ); marker = model.markers._set( 'name', range ); diff --git a/tests/model/position.js b/tests/model/position.js index d64ba4d0b..454984156 100644 --- a/tests/model/position.js +++ b/tests/model/position.js @@ -130,6 +130,7 @@ describe( 'Position', () => { describe( 'is()', () => { let position; + beforeEach( () => { position = new Position( root, [ 0 ] ); } ); diff --git a/tests/view/node.js b/tests/view/node.js index 6f159d2c8..d56a841a4 100644 --- a/tests/view/node.js +++ b/tests/view/node.js @@ -32,9 +32,11 @@ describe( 'Node', () => { describe( 'is()', () => { let node; + beforeEach( () => { node = new Node(); } ); + it( 'should return true for node', () => { expect( node.is( 'node' ) ).to.be.true; expect( node.is( 'view:node' ) ).to.be.true; From 608ce64618bd61ec13b61dab2674e56db1caaacb Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 10 May 2019 15:27:51 +0200 Subject: [PATCH 14/30] Fix and add test to have coverage on 100%. --- src/model/history.js | 2 +- tests/view/domconverter/domconverter.js | 15 +++++++++++++++ tests/view/downcastwriter/is.js | 23 +++++++++++++++++++++++ tests/view/treewalker.js | 2 +- tests/view/view/view.js | 14 ++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/view/downcastwriter/is.js diff --git a/src/model/history.js b/src/model/history.js index bf1e79ed7..bdf11eb61 100644 --- a/src/model/history.js +++ b/src/model/history.js @@ -106,7 +106,7 @@ export default class History { * @returns {Boolean} */ is( type ) { - return type == 'history' || type == 'mdoel:history'; + return type == 'history' || type == 'model:history'; } /** diff --git a/tests/view/domconverter/domconverter.js b/tests/view/domconverter/domconverter.js index 661c181a0..fff3bd783 100644 --- a/tests/view/domconverter/domconverter.js +++ b/tests/view/domconverter/domconverter.js @@ -34,6 +34,21 @@ describe( 'DomConverter', () => { } ); } ); + describe( 'is()', () => { + it( 'should return true for "domConverter"', () => { + expect( converter.is( 'domConverter' ) ).to.be.true; + expect( converter.is( 'view:domConverter' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( converter.is( 'view' ) ).to.be.false; + expect( converter.is( 'model:domConverter' ) ).to.be.false; + expect( converter.is( 'node' ) ).to.be.false; + expect( converter.is( 'view:node' ) ).to.be.false; + expect( converter.is( 'element', 'text' ) ).to.be.false; + } ); + } ); + describe( 'focus()', () => { let viewEditable, domEditable, domEditableParent, viewDocument; diff --git a/tests/view/downcastwriter/is.js b/tests/view/downcastwriter/is.js new file mode 100644 index 000000000..7cad0ddfb --- /dev/null +++ b/tests/view/downcastwriter/is.js @@ -0,0 +1,23 @@ +import DowncastWriter from '../../../src/view/downcastwriter'; +import Document from '../../../src/view/document'; + +describe( 'is()', () => { + let writer; + + before( () => { + writer = new DowncastWriter( new Document() ); + } ); + + it( 'should return true for "downcastWriter"', () => { + expect( writer.is( 'downcastWriter' ) ).to.be.true; + expect( writer.is( 'view:downcastWriter' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( writer.is( 'view' ) ).to.be.false; + expect( writer.is( 'model:downcastWriter' ) ).to.be.false; + expect( writer.is( 'node' ) ).to.be.false; + expect( writer.is( 'view:node' ) ).to.be.false; + expect( writer.is( 'element', 'text' ) ).to.be.false; + } ); +} ); diff --git a/tests/view/treewalker.js b/tests/view/treewalker.js index 76679c9dc..e01c75eae 100644 --- a/tests/view/treewalker.js +++ b/tests/view/treewalker.js @@ -74,7 +74,7 @@ describe( 'TreeWalker', () => { let treeWalker; beforeEach( () => { - treeWalker = new TreeWalker(); + treeWalker = new TreeWalker( { startPosition: rootBeginning } ); } ); it( 'should return true for "treeWalker"', () => { diff --git a/tests/view/view/view.js b/tests/view/view/view.js index 9b410ad50..33ed05dd4 100644 --- a/tests/view/view/view.js +++ b/tests/view/view/view.js @@ -488,6 +488,20 @@ describe( 'view', () => { } ); } ); + describe( 'is()', () => { + it( 'should return true for "view"', () => { + expect( view.is( 'view' ) ).to.be.true; + } ); + + it( 'should return false for incorrect values', () => { + expect( view.is( 'model' ) ).to.be.false; + expect( view.is( 'model:document' ) ).to.be.false; + expect( view.is( 'node' ) ).to.be.false; + expect( view.is( 'view:node' ) ).to.be.false; + expect( view.is( 'element', 'text' ) ).to.be.false; + } ); + } ); + describe( 'isFocused', () => { it( 'should change renderer.isFocused too', () => { expect( viewDocument.isFocused ).to.equal( false ); From cf2bce286d65c2611af4e850151a448514d6d6fe Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Mon, 17 Jun 2019 16:25:39 +0200 Subject: [PATCH 15/30] Revert "Add 'is()' method to operations." This reverts commit a7bdbeaa45654617ec3355ecb45309a97013b01e. --- src/model/operation/attributeoperation.js | 7 ----- src/model/operation/detachoperation.js | 7 ----- src/model/operation/insertoperation.js | 7 ----- src/model/operation/markeroperation.js | 7 ----- src/model/operation/mergeoperation.js | 7 ----- src/model/operation/moveoperation.js | 7 ----- src/model/operation/nooperation.js | 7 ----- src/model/operation/operation.js | 31 ------------------- src/model/operation/renameoperation.js | 7 ----- src/model/operation/rootattributeoperation.js | 9 ------ src/model/operation/splitoperation.js | 7 ----- tests/model/operation/attributeoperation.js | 31 ------------------- tests/model/operation/detachoperation.js | 25 --------------- tests/model/operation/insertoperation.js | 25 --------------- tests/model/operation/markeroperation.js | 24 -------------- tests/model/operation/mergeoperation.js | 26 ---------------- tests/model/operation/moveoperation.js | 26 ---------------- tests/model/operation/nooperation.js | 18 ----------- tests/model/operation/operation.js | 22 ------------- tests/model/operation/renameoperation.js | 24 -------------- .../model/operation/rootattributeoperation.js | 30 ------------------ tests/model/operation/splitoperation.js | 25 --------------- 22 files changed, 379 deletions(-) diff --git a/src/model/operation/attributeoperation.js b/src/model/operation/attributeoperation.js index 81aecb7ab..2cc283c8f 100644 --- a/src/model/operation/attributeoperation.js +++ b/src/model/operation/attributeoperation.js @@ -107,13 +107,6 @@ export default class AttributeOperation extends Operation { return new AttributeOperation( this.range, this.key, this.newValue, this.oldValue, this.baseVersion + 1 ); } - /** - * @inheritDoc - */ - is( type ) { - return type == 'attributeOperation' || type == 'model:operation:attribute' || super.is( type ); - } - /** * @inheritDoc */ diff --git a/src/model/operation/detachoperation.js b/src/model/operation/detachoperation.js index 6ceb74033..f4a311421 100644 --- a/src/model/operation/detachoperation.js +++ b/src/model/operation/detachoperation.js @@ -52,13 +52,6 @@ export default class DetachOperation extends Operation { return 'detach'; } - /** - * @inheritDoc - */ - is( type ) { - return type == 'detachOperation' || type == 'model:operation:detach' || super.is( type ); - } - /** * @inheritDoc */ diff --git a/src/model/operation/insertoperation.js b/src/model/operation/insertoperation.js index 081cc2f0d..1ea873cfa 100644 --- a/src/model/operation/insertoperation.js +++ b/src/model/operation/insertoperation.js @@ -76,13 +76,6 @@ export default class InsertOperation extends Operation { return this.nodes.maxOffset; } - /** - * @inheritDoc - */ - is( type ) { - return type == 'insertOperation' || type == 'model:operation:insert' || super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/markeroperation.js b/src/model/operation/markeroperation.js index 946848977..ace0bebe3 100644 --- a/src/model/operation/markeroperation.js +++ b/src/model/operation/markeroperation.js @@ -76,13 +76,6 @@ export default class MarkerOperation extends Operation { return 'marker'; } - /** - * @inheritDoc - */ - is( type ) { - return type == 'markerOperation' || type == 'model:operation:marker' || super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/mergeoperation.js b/src/model/operation/mergeoperation.js index 577d5317a..d6a101861 100644 --- a/src/model/operation/mergeoperation.js +++ b/src/model/operation/mergeoperation.js @@ -104,13 +104,6 @@ export default class MergeOperation extends Operation { return new Range( this.sourcePosition, end ); } - /** - * @inheritDoc - */ - is( type ) { - return type == 'mergeOperation' || type == 'model:operation:merge' || super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/moveoperation.js b/src/model/operation/moveoperation.js index c7c0789d0..799a3d1da 100644 --- a/src/model/operation/moveoperation.js +++ b/src/model/operation/moveoperation.js @@ -73,13 +73,6 @@ export default class MoveOperation extends Operation { return 'move'; } - /** - * @inheritDoc - */ - is( type ) { - return type == 'moveOperation' || type == 'model:operation:move' || super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/nooperation.js b/src/model/operation/nooperation.js index 4f71ce2a5..f2e0a2ede 100644 --- a/src/model/operation/nooperation.js +++ b/src/model/operation/nooperation.js @@ -24,13 +24,6 @@ export default class NoOperation extends Operation { return 'noop'; } - /** - * @inheritDoc - */ - is( type ) { - return type == 'noOperation' || type == 'model:operation:no' || super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/operation.js b/src/model/operation/operation.js index d84ff13f7..06ca9753c 100644 --- a/src/model/operation/operation.js +++ b/src/model/operation/operation.js @@ -81,37 +81,6 @@ export default class Operation { */ } - /** - * Checks whether given object is of `operation` type. This method is useful when processing operations are of unknown type. - * This method can be used to check what type of operation is returned. General rules how to check operation are presented for - * attribute operation. You need to use analogical syntax for other {@link module:engine/model/operation/operation~Operation}. - * - * // Examples for AttributeOperation. - * - * operation instanceof AttributeOperation - * - * operation.is( 'operation' ) // true for AttributeOperation and other operations - * operation.is( 'model:operation' ) // true for AttributeOperation and other operations - * operation.is( 'attributeOperation' ) // true for AttributeOperation - * operation.is( 'model:operation:attribute' ) // true for AttributeOperation - * - * // Disallowed syntax - * operation.is( 'operation:attribute' ) // will return false - * - * // Few examples for other Operations - * operation.is( 'model:operation:no ) // true for no-operation - * operation.is( 'noOperation' ) // true for no-operation - * operation.is( 'model:operation:marker ) // true for marker operation - * operation.is( 'insertOperation' ) // true for insert operation - * operation.is( 'model:operation:rootAttribute' ) // true for rootAttributeOperation - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'operation' || type == 'model:operation'; - } - /** * Checks whether the operation's parameters are correct and the operation can be correctly executed. Throws * an error if operation is not valid. diff --git a/src/model/operation/renameoperation.js b/src/model/operation/renameoperation.js index a7a48f612..12d874f84 100644 --- a/src/model/operation/renameoperation.js +++ b/src/model/operation/renameoperation.js @@ -63,13 +63,6 @@ export default class RenameOperation extends Operation { return 'rename'; } - /** - * @inheritDoc - */ - is( type ) { - return type == 'renameOperation' || type == 'model:operation:rename' || super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/rootattributeoperation.js b/src/model/operation/rootattributeoperation.js index 19ca7a50e..2af7597ba 100644 --- a/src/model/operation/rootattributeoperation.js +++ b/src/model/operation/rootattributeoperation.js @@ -83,15 +83,6 @@ export default class RootAttributeOperation extends Operation { } } - /** - * @inheritDoc - */ - is( type ) { - return type == 'rootAttributeOperation' || - type == 'model:operation:rootAttribute' || - super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/src/model/operation/splitoperation.js b/src/model/operation/splitoperation.js index cb9c3b581..fd215a91d 100644 --- a/src/model/operation/splitoperation.js +++ b/src/model/operation/splitoperation.js @@ -111,13 +111,6 @@ export default class SplitOperation extends Operation { return new Range( this.splitPosition, end ); } - /** - * @inheritDoc - */ - is( type ) { - return type == 'splitOperation' || type == 'model:operation:split' || super.is( type ); - } - /** * Creates and returns an operation that has the same parameters as this operation. * diff --git a/tests/model/operation/attributeoperation.js b/tests/model/operation/attributeoperation.js index 22a153f7f..78e6823aa 100644 --- a/tests/model/operation/attributeoperation.js +++ b/tests/model/operation/attributeoperation.js @@ -60,37 +60,6 @@ describe( 'AttributeOperation', () => { } ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - const range = new Range( new Position( root, [ 0 ] ), new Position( root, [ 2 ] ) ); - operation = new AttributeOperation( - range, - 'key', - null, - 'newValue', - doc.version - ); - } ); - - it( 'should return true for all valid names of "attribute" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'attributeOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:attribute' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:attribute' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - it( 'should insert attribute to the set of nodes', () => { root._insertChild( 0, new Text( 'bar' ) ); diff --git a/tests/model/operation/detachoperation.js b/tests/model/operation/detachoperation.js index 3bc13d197..dadd7f3dc 100644 --- a/tests/model/operation/detachoperation.js +++ b/tests/model/operation/detachoperation.js @@ -35,31 +35,6 @@ describe( 'DetachOperation', () => { expect( docFrag.childCount ).to.equal( 0 ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - const position = Position._createBefore( element ); - operation = new DetachOperation( position, 1 ); - } ); - - it( 'should return true for all valid names of "detach" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'detachOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:detach' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:detach' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'attributeOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( '_validate()', () => { it( 'should throw when is executed on element from document', () => { const root = doc.createRoot(); diff --git a/tests/model/operation/insertoperation.js b/tests/model/operation/insertoperation.js index bbedd3dd6..c088389c3 100644 --- a/tests/model/operation/insertoperation.js +++ b/tests/model/operation/insertoperation.js @@ -220,31 +220,6 @@ describe( 'InsertOperation', () => { expect( op2.nodes.getNode( 0 ) ).not.to.equal( text ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - const position = new Position( root, [ 0 ] ); - operation = new InsertOperation( position, new Text( 'x' ), doc.version ); - } ); - - it( 'should return true for all valid names of "insert" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'insertOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:insert' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:insert' ) ).to.be.false; - expect( operation.is( 'model:operation:attribute' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( '_validate()', () => { it( 'should throw an error if target position does not exist', () => { const element = new Element( 'p' ); diff --git a/tests/model/operation/markeroperation.js b/tests/model/operation/markeroperation.js index a0045c4ec..3a5075305 100644 --- a/tests/model/operation/markeroperation.js +++ b/tests/model/operation/markeroperation.js @@ -130,30 +130,6 @@ describe( 'MarkerOperation', () => { expect( clone ).to.deep.equal( op ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - operation = new MarkerOperation( 'name', null, range, model.markers, true, doc.version ); - } ); - - it( 'should return true for all valid names of "marker" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'markerOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:marker' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:marker' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper serialized object', () => { const op = new MarkerOperation( 'name', null, range, model.markers, true, doc.version ); diff --git a/tests/model/operation/mergeoperation.js b/tests/model/operation/mergeoperation.js index 03f64b274..d2ed0f202 100644 --- a/tests/model/operation/mergeoperation.js +++ b/tests/model/operation/mergeoperation.js @@ -195,32 +195,6 @@ describe( 'MergeOperation', () => { expect( clone.baseVersion ).to.equal( baseVersion ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - const sourcePosition = new Position( root, [ 1, 0 ] ); - const targetPosition = new Position( root, [ 0, 3 ] ); - operation = new MergeOperation( sourcePosition, 1, targetPosition, gyPos, doc.version ); - } ); - - it( 'should return true for all valid names of "merge" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'mergeOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:merge' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:merge' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper json object', () => { const sourcePosition = new Position( root, [ 1, 0 ] ); diff --git a/tests/model/operation/moveoperation.js b/tests/model/operation/moveoperation.js index 20b3b48b8..987b3cb35 100644 --- a/tests/model/operation/moveoperation.js +++ b/tests/model/operation/moveoperation.js @@ -264,32 +264,6 @@ describe( 'MoveOperation', () => { } ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - const sourcePosition = new Position( root, [ 0, 0 ] ); - const targetPosition = new Position( root, [ 1, 0 ] ); - operation = new MoveOperation( sourcePosition, 1, targetPosition, doc.version ); - } ); - - it( 'should return true for all valid names of "move" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'moveOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:move' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:move' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper json object', () => { const sourcePosition = new Position( root, [ 0, 0 ] ); diff --git a/tests/model/operation/nooperation.js b/tests/model/operation/nooperation.js index a66e743fd..809fdd20f 100644 --- a/tests/model/operation/nooperation.js +++ b/tests/model/operation/nooperation.js @@ -33,24 +33,6 @@ describe( 'NoOperation', () => { expect( clone.baseVersion ).to.equal( 0 ); } ); - describe( 'is()', () => { - it( 'should return true for all valid names of "no" operation', () => { - expect( noop.is( 'operation' ) ).to.be.true; - expect( noop.is( 'model:operation' ) ).to.be.true; - expect( noop.is( 'noOperation' ) ).to.be.true; - expect( noop.is( 'model:operation:no' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( noop.is( 'operation:no' ) ).to.be.false; - expect( noop.is( 'model:operation:insert' ) ).to.be.false; - expect( noop.is( 'attributeOperation' ) ).to.be.false; - expect( noop.is( 'detachOperation' ) ).to.be.false; - expect( noop.is( 'rootAttributeOperation' ) ).to.be.false; - expect( noop.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper json object', () => { const serialized = noop.toJSON(); diff --git a/tests/model/operation/operation.js b/tests/model/operation/operation.js index 34d0b4b2d..7783630f0 100644 --- a/tests/model/operation/operation.js +++ b/tests/model/operation/operation.js @@ -27,28 +27,6 @@ describe( 'Operation', () => { } ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - operation = new Operation( null ); - } ); - - it( 'should return true for all valid names of operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:attribute' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper json object #1', () => { const op = new Operation( 4 ); diff --git a/tests/model/operation/renameoperation.js b/tests/model/operation/renameoperation.js index adfb0d5bc..2a9ab4feb 100644 --- a/tests/model/operation/renameoperation.js +++ b/tests/model/operation/renameoperation.js @@ -103,30 +103,6 @@ describe( 'RenameOperation', () => { expect( clone.newName ).to.equal( newName ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - operation = new RenameOperation( Position._createAt( root, 'end' ), oldName, newName, doc.version ); - } ); - - it( 'should return true for all valid names of "rename" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'renameOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:rename' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:rename' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper serialized object', () => { const op = new RenameOperation( Position._createAt( root, 'end' ), oldName, newName, doc.version ); diff --git a/tests/model/operation/rootattributeoperation.js b/tests/model/operation/rootattributeoperation.js index 335ffe152..cab0e3ec1 100644 --- a/tests/model/operation/rootattributeoperation.js +++ b/tests/model/operation/rootattributeoperation.js @@ -257,36 +257,6 @@ describe( 'RootAttributeOperation', () => { expect( clone.baseVersion ).to.equal( baseVersion ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - operation = new RootAttributeOperation( - root, - 'key', - null, - 'newValue', - doc.version - ); - } ); - - it( 'should return true for all valid names of "rootAttribute" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:rootAttribute' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'attributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:attribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper serialized object', () => { const op = new RootAttributeOperation( diff --git a/tests/model/operation/splitoperation.js b/tests/model/operation/splitoperation.js index 4e9a4a0c9..1da49447b 100644 --- a/tests/model/operation/splitoperation.js +++ b/tests/model/operation/splitoperation.js @@ -201,31 +201,6 @@ describe( 'SplitOperation', () => { expect( clone.baseVersion ).to.equal( baseVersion ); } ); - describe( 'is()', () => { - let operation; - - before( () => { - const position = new Position( root, [ 0, 3 ] ); - operation = new SplitOperation( position, 2, null, doc.version ); - } ); - - it( 'should return true for all valid names of "split" operation', () => { - expect( operation.is( 'operation' ) ).to.be.true; - expect( operation.is( 'model:operation' ) ).to.be.true; - expect( operation.is( 'splitOperation' ) ).to.be.true; - expect( operation.is( 'model:operation:split' ) ).to.be.true; - } ); - - it( 'should return false for invalid parameters', () => { - expect( operation.is( 'operation:split' ) ).to.be.false; - expect( operation.is( 'model:operation:insert' ) ).to.be.false; - expect( operation.is( 'noOperation' ) ).to.be.false; - expect( operation.is( 'detachOperation' ) ).to.be.false; - expect( operation.is( 'rootAttributeOperation' ) ).to.be.false; - expect( operation.is( 'model:operation:rootAttribute' ) ).to.be.false; - } ); - } ); - describe( 'toJSON', () => { it( 'should create proper json object #1', () => { const position = new Position( root, [ 0, 3 ] ); From 21db0afa3affd23ed319ce7ffc7c1f08d5cc373b Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Mon, 17 Jun 2019 16:54:27 +0200 Subject: [PATCH 16/30] Remove is() method from unnecessary model's classes. --- src/model/batch.js | 12 ------------ src/model/differ.js | 12 ------------ src/model/document.js | 12 ------------ src/model/documentfragment.js | 2 +- src/model/documentselection.js | 2 -- src/model/element.js | 2 +- src/model/history.js | 12 ------------ src/model/markercollection.js | 14 +------------ src/model/model.js | 36 ---------------------------------- src/model/node.js | 6 ++++-- src/model/nodelist.js | 12 ------------ src/model/position.js | 2 +- src/model/range.js | 2 +- src/model/schema.js | 12 ------------ src/model/selection.js | 2 -- src/model/textproxy.js | 2 +- src/model/treewalker.js | 12 ------------ src/model/writer.js | 12 ------------ 18 files changed, 10 insertions(+), 156 deletions(-) diff --git a/src/model/batch.js b/src/model/batch.js index 8d5ca4a9e..ed5a5c517 100644 --- a/src/model/batch.js +++ b/src/model/batch.js @@ -79,16 +79,4 @@ export default class Batch { return operation; } - - /** - * Checks whether given object is of `batch` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'batch' || type == 'model:batch'; - } } diff --git a/src/model/differ.js b/src/model/differ.js index ad4b489bc..b1dfce3e4 100644 --- a/src/model/differ.js +++ b/src/model/differ.js @@ -519,18 +519,6 @@ export default class Differ { } } - /** - * Checks whether given object is of `differ` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'differ' || type == 'model:differ'; - } - /** * Resets `Differ`. Removes all buffered changes. */ diff --git a/src/model/document.js b/src/model/document.js index 6eec95cd7..9f0d646c9 100644 --- a/src/model/document.js +++ b/src/model/document.js @@ -241,18 +241,6 @@ export default class Document { return Array.from( this.roots, root => root.rootName ).filter( name => name != graveyardName ); } - /** - * Checks whether given object is of `document` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @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. diff --git a/src/model/documentfragment.js b/src/model/documentfragment.js index e32c30329..6390071fe 100644 --- a/src/model/documentfragment.js +++ b/src/model/documentfragment.js @@ -118,7 +118,7 @@ export default class DocumentFragment { /** * Checks whether given model tree object is of given type. * - * Read more in {@link module:engine/model/node~Node#is `Node#is()`} and {@link module:engine/model/model~Model#is `Model#is()`}. + * Read more in {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/documentselection.js b/src/model/documentselection.js index 0947a34d5..d339a3485 100644 --- a/src/model/documentselection.js +++ b/src/model/documentselection.js @@ -382,8 +382,6 @@ export default class DocumentSelection { * selection.is( 'model:node' ); // false * selection.is( 'element' ); // false * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/element.js b/src/model/element.js index ba5fad4ba..cff992681 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -105,7 +105,7 @@ export default class Element extends Node { * obj.is( 'view:element' ); // false * obj.is( 'element', 'image' ); // false * - * Read more in {@link module:engine/model/node~Node#is `Node#is()`} and {@link module:engine/model/model~Model#is `Model#is()`}. + * Read more in {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type Type to check when `name` parameter is present. * Otherwise, it acts like the `name` parameter. diff --git a/src/model/history.js b/src/model/history.js index bdf11eb61..d51d54e29 100644 --- a/src/model/history.js +++ b/src/model/history.js @@ -97,18 +97,6 @@ export default class History { this._undoneOperations.add( undoneOperation ); } - /** - * Checks whether given object is of `history` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'history' || type == 'model:history'; - } - /** * Checks whether given `operation` is undoing any other operation. * diff --git a/src/model/markercollection.js b/src/model/markercollection.js index 6e3148f7f..50e1e441e 100644 --- a/src/model/markercollection.js +++ b/src/model/markercollection.js @@ -72,18 +72,6 @@ export default class MarkerCollection { return this._markers.get( markerName ) || null; } - /** - * Checks whether given object is of `markerCollection` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'markerCollection' || type == 'model:markerCollection'; - } - /** * Creates and adds a {@link ~Marker marker} to the `MarkerCollection` with given name on given * {@link module:engine/model/range~Range range}. @@ -463,7 +451,7 @@ class Marker { /** * Checks whether given object is of `marker` type. * - * Read more at {@link module:engine/model/model~Model#is}. + * Read more at {@link module:engine/model/model~Node#is}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/model.js b/src/model/model.js index 436552de7..61a6b73f0 100644 --- a/src/model/model.js +++ b/src/model/model.js @@ -717,42 +717,6 @@ export default class Model { this.stopListening(); } - /** - * Checks whether given object is of `model` type. - * - * All classes related to {@link module:engine/model/model~Model} might contain `is` method, - * which checks if given object belong to specific type. Types are defined as name of the class - * written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. E.g. class `LiveRange` - * will get type `liveRange`. There might appear some additional possibilities to test types which will be described in - * related documentation. - * - * Method might simplify your code for cases, when you want to test unknown object for specific type. - * Instead of using `instanceof` and importing entire class for testing, there might be used `is` method which test for given name. - * There is also available `model:` prefix in each case, which gives more specific results. - * It helps to distinguish model's classes from view's. Few examples how to use this method you can find below: - * - * const model = new Model(); - * model.is( 'model' ) // return true - * - * const range = new LiveRange( startPosition ) - * range.is( 'range' ) // return true - * range.is( 'liveRange' ) // return true - * range.is( 'model:range' ) // return true - * range.is( 'model:liveRange' ) // return true - * range.is( 'view:range' ) // return false - * - * const document = new Document(); - * document.is( 'model:document' ) // return true - * - * See also {@link module:engine/model/node~Node#is `Node#is()`} for some more details related to elements. - * - * @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. diff --git a/src/model/node.js b/src/model/node.js index 4f579f2d5..f9b09598f 100644 --- a/src/model/node.js +++ b/src/model/node.js @@ -469,6 +469,10 @@ export default class 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`. + * Types are defined as name of the class written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. + * E.g. class `LiveRange` will get type `liveRange`. + * + * There is more classes in model which follows similar naming convention. Check corresponding elements documentation for more details. * * 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 @@ -478,8 +482,6 @@ export default class Node { * obj.is( 'text' ); // true for text node, false for element and document fragment * obj.is( 'textProxy' ); // true for text proxy object * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * * @method #is * @param {String} type * @returns {Boolean} diff --git a/src/model/nodelist.js b/src/model/nodelist.js index 42f81868e..d6c631c6f 100644 --- a/src/model/nodelist.js +++ b/src/model/nodelist.js @@ -170,18 +170,6 @@ export default class NodeList { return this.length; } - /** - * Checks whether given object is of `nodeList` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'nodeList' || type == 'model:nodeList'; - } - /** * Inserts given nodes at given index. * diff --git a/src/model/position.js b/src/model/position.js index f10695e0c..f70fe2a96 100644 --- a/src/model/position.js +++ b/src/model/position.js @@ -505,7 +505,7 @@ export default class Position { /** * Checks whether given object is of `position` type. * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * Read more at {@link module:engine/model/model~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/range.js b/src/model/range.js index 188c1b21b..070709d28 100644 --- a/src/model/range.js +++ b/src/model/range.js @@ -146,7 +146,7 @@ export default class Range { /** * Checks whether given object is of `range` type. * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. + * Read more at {@link module:engine/model/model~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/schema.js b/src/model/schema.js index 5ec0b3957..3c407fe94 100644 --- a/src/model/schema.js +++ b/src/model/schema.js @@ -187,18 +187,6 @@ export default class Schema { return this.getDefinitions()[ itemName ]; } - /** - * Checks whether given object is of `schema` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'schema' || type == 'model:schema'; - } - /** * Returns `true` if the given item is registered in the schema. * diff --git a/src/model/selection.js b/src/model/selection.js index e3fa111f8..f5415fcae 100644 --- a/src/model/selection.js +++ b/src/model/selection.js @@ -634,8 +634,6 @@ export default class Selection { * selection.is( 'element' ); // false * selection.is( 'view:selection' ); // false * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/textproxy.js b/src/model/textproxy.js index bbba241fe..511f84dd3 100644 --- a/src/model/textproxy.js +++ b/src/model/textproxy.js @@ -175,7 +175,7 @@ export default class TextProxy { /** * Checks whether given object is of `textProxy` type. * - * Read more in {@link module:engine/model/node~Node#is `Node#is()`} and {@link module:engine/model/model~Model#is `Model#is()`}. + * Read more in {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/treewalker.js b/src/model/treewalker.js index bf01dd3e2..d56004513 100644 --- a/src/model/treewalker.js +++ b/src/model/treewalker.js @@ -152,18 +152,6 @@ export default class TreeWalker { this._visitedParent = this.position.parent; } - /** - * Checks whether given object is of `treeWalker` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'treeWalker' || type == 'model:treeWalker'; - } - /** * Iterable interface. * diff --git a/src/model/writer.js b/src/model/writer.js index 0ceadbf42..9e28394ac 100644 --- a/src/model/writer.js +++ b/src/model/writer.js @@ -79,18 +79,6 @@ export default class Writer { this.batch = batch; } - /** - * Checks whether given object is of `writer` type. - * - * Read more at {@link module:engine/model/model~Model#is `Model#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'writer' || type == 'model:writer'; - } - /** * Creates a new {@link module:engine/model/text~Text text node}. * From ecea81a3ef6824bec525f72de1a6d63e09571939 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 19 Jun 2019 11:55:10 +0200 Subject: [PATCH 17/30] Remove unit test which are no longer valid. --- tests/model/batch.js | 20 -------------------- tests/model/differ.js | 14 -------------- tests/model/document.js | 15 --------------- tests/model/history.js | 14 -------------- tests/model/markercollection.js | 14 -------------- tests/model/model.js | 13 ------------- tests/model/nodelist.js | 14 -------------- tests/model/schema.js | 14 -------------- tests/model/treewalker.js | 20 -------------------- tests/model/writer.js | 20 -------------------- 10 files changed, 158 deletions(-) diff --git a/tests/model/batch.js b/tests/model/batch.js index a7be0ae69..b6f64baa2 100644 --- a/tests/model/batch.js +++ b/tests/model/batch.js @@ -60,24 +60,4 @@ describe( 'Batch', () => { expect( batch.baseVersion ).to.equal( null ); } ); } ); - - describe( 'is()', () => { - let batch; - - beforeEach( () => { - batch = new Batch(); - } ); - - it( 'should return true for "batch"', () => { - expect( batch.is( 'batch' ) ).to.be.true; - expect( batch.is( 'model:batch' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( batch.is( 'model' ) ).to.be.false; - expect( batch.is( 'node' ) ).to.be.false; - expect( batch.is( 'model:element' ) ).to.be.false; - expect( batch.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); } ); diff --git a/tests/model/differ.js b/tests/model/differ.js index 9eadaa7e9..2ac12d0dc 100644 --- a/tests/model/differ.js +++ b/tests/model/differ.js @@ -36,20 +36,6 @@ describe( 'Differ', () => { ] ); } ); - describe( 'is()', () => { - it( 'should return true for "differ"', () => { - expect( differ.is( 'differ' ) ).to.be.true; - expect( differ.is( 'model:differ' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( differ.is( 'model' ) ).to.be.false; - expect( differ.is( 'model:node' ) ).to.be.false; - expect( differ.is( 'text' ) ).to.be.false; - expect( differ.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); - describe( 'insert', () => { // Simple. it( 'an element', () => { diff --git a/tests/model/document.js b/tests/model/document.js index 1e8d4b083..e7c79d71b 100644 --- a/tests/model/document.js +++ b/tests/model/document.js @@ -499,19 +499,4 @@ describe( 'Document', () => { expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' ); expect( serialized.model ).to.equal( '[engine.model.Model]' ); } ); - - describe( 'is()', () => { - it( 'should return true for "document"', () => { - expect( doc.is( 'document' ) ).to.be.true; - expect( doc.is( 'model:document' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( doc.is( 'model' ) ).to.be.false; - expect( doc.is( 'node' ) ).to.be.false; - expect( doc.is( 'model:node' ) ).to.be.false; - expect( doc.is( 'view:document' ) ).to.be.false; - expect( doc.is( 'element', 'text' ) ).to.be.false; - } ); - } ); } ); diff --git a/tests/model/history.js b/tests/model/history.js index 69a11bc67..da028a6d6 100644 --- a/tests/model/history.js +++ b/tests/model/history.js @@ -178,20 +178,6 @@ describe( 'History', () => { expect( history.getUndoneOperation( op ) ).to.be.undefined; } ); } ); - - describe( 'is()', () => { - it( 'should return true for "history"', () => { - expect( history.is( 'history' ) ).to.be.true; - expect( history.is( 'model:history' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( history.is( 'model' ) ).to.be.false; - expect( history.is( 'model:node' ) ).to.be.false; - expect( history.is( 'text' ) ).to.be.false; - expect( history.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); } ); function getOperations() { diff --git a/tests/model/markercollection.js b/tests/model/markercollection.js index 7247c37a2..bec4dd670 100644 --- a/tests/model/markercollection.js +++ b/tests/model/markercollection.js @@ -146,20 +146,6 @@ describe( 'MarkerCollection', () => { } ); } ); - describe( 'is()', () => { - it( 'should return true for "markerCollection"', () => { - expect( markers.is( 'markerCollection' ) ).to.be.true; - expect( markers.is( 'model:markerCollection' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( markers.is( 'model' ) ).to.be.false; - expect( markers.is( 'model:node' ) ).to.be.false; - expect( markers.is( 'text' ) ).to.be.false; - expect( markers.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); - describe( '_remove', () => { it( 'should remove marker, return true and fire update: event', () => { const marker = markers._set( 'name', range ); diff --git a/tests/model/model.js b/tests/model/model.js index 1a10269fb..2762e8163 100644 --- a/tests/model/model.js +++ b/tests/model/model.js @@ -808,17 +808,4 @@ describe( 'Model', () => { sinon.assert.notCalled( spy ); } ); } ); - - describe( 'is()', () => { - it( 'should return true for "model"', () => { - expect( model.is( 'model' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( model.is( 'view' ) ).to.be.false; - expect( model.is( 'model:node' ) ).to.be.false; - expect( model.is( 'text' ) ).to.be.false; - expect( model.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); } ); diff --git a/tests/model/nodelist.js b/tests/model/nodelist.js index e2c53cab3..fe9ca7f13 100644 --- a/tests/model/nodelist.js +++ b/tests/model/nodelist.js @@ -32,20 +32,6 @@ describe( 'NodeList', () => { } ); } ); - describe( 'is()', () => { - it( 'should return true for "nodeList"', () => { - expect( nodes.is( 'nodeList' ) ).to.be.true; - expect( nodes.is( 'model:nodeList' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( nodes.is( 'model' ) ).to.be.false; - expect( nodes.is( 'model:node' ) ).to.be.false; - expect( nodes.is( 'text' ) ).to.be.false; - expect( nodes.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); - describe( 'iterator', () => { it( 'should iterate over all nodes from node list', () => { expect( Array.from( nodes ) ).to.deep.equal( [ p, foo, img ] ); diff --git a/tests/model/schema.js b/tests/model/schema.js index ca3eee46f..ce52e35ef 100644 --- a/tests/model/schema.js +++ b/tests/model/schema.js @@ -311,20 +311,6 @@ describe( 'Schema', () => { } ); } ); - describe( 'is()', () => { - it( 'should return true for "schema"', () => { - expect( schema.is( 'schema' ) ).to.be.true; - expect( schema.is( 'model:schema' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( schema.is( 'model' ) ).to.be.false; - expect( schema.is( 'model:node' ) ).to.be.false; - expect( schema.is( 'text' ) ).to.be.false; - expect( schema.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); - describe( 'isRegistered()', () => { it( 'returns true if an item was registered', () => { schema.register( 'foo' ); diff --git a/tests/model/treewalker.js b/tests/model/treewalker.js index 347e77247..f865ff237 100644 --- a/tests/model/treewalker.js +++ b/tests/model/treewalker.js @@ -69,26 +69,6 @@ describe( 'TreeWalker', () => { } ); } ); - describe( 'is()', () => { - let treeWalker; - - beforeEach( () => { - treeWalker = new TreeWalker( { startPosition: rootBeginning } ); - } ); - - it( 'should return true for "treeWalker"', () => { - expect( treeWalker.is( 'treeWalker' ) ).to.be.true; - expect( treeWalker.is( 'model:treeWalker' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( treeWalker.is( 'model' ) ).to.be.false; - expect( treeWalker.is( 'model:node' ) ).to.be.false; - expect( treeWalker.is( 'text' ) ).to.be.false; - expect( treeWalker.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); - describe( 'iterate from start position `startPosition`', () => { let expected; diff --git a/tests/model/writer.js b/tests/model/writer.js index e796af3a6..e557d7ba3 100644 --- a/tests/model/writer.js +++ b/tests/model/writer.js @@ -86,26 +86,6 @@ describe( 'Writer', () => { } ); } ); - describe( 'is()', () => { - let writer; - - beforeEach( () => { - writer = new Writer( model, batch ); - } ); - - it( 'should return true for "writer"', () => { - expect( writer.is( 'writer' ) ).to.be.true; - expect( writer.is( 'model:writer' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( writer.is( 'model' ) ).to.be.false; - expect( writer.is( 'model:node' ) ).to.be.false; - expect( writer.is( 'text' ) ).to.be.false; - expect( writer.is( 'element', 'paragraph' ) ).to.be.false; - } ); - } ); - describe( 'insert()', () => { it( 'should insert node at given position', () => { const parent = createDocumentFragment(); From 290a40c1159a9cf4ed82f11ad6dedb7418086181 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 19 Jun 2019 13:09:03 +0200 Subject: [PATCH 18/30] Remove is() method from unnecessary view classes. --- src/view/document.js | 12 ------------ src/view/documentfragment.js | 5 ++--- src/view/documentselection.js | 2 -- src/view/domconverter.js | 12 ------------ src/view/downcastwriter.js | 12 ------------ src/view/element.js | 2 +- src/view/matcher.js | 12 ------------ src/view/node.js | 6 +++++- src/view/position.js | 5 ++--- src/view/range.js | 5 ++--- src/view/renderer.js | 12 ------------ src/view/selection.js | 2 -- src/view/textproxy.js | 5 ++--- src/view/treewalker.js | 12 ------------ src/view/upcastwriter.js | 12 ------------ src/view/view.js | 34 ---------------------------------- 16 files changed, 14 insertions(+), 136 deletions(-) diff --git a/src/view/document.js b/src/view/document.js index c8a76b812..376abdde3 100644 --- a/src/view/document.js +++ b/src/view/document.js @@ -132,18 +132,6 @@ export default class Document { this.stopListening(); } - /** - * Checks whether given object is of `document` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'document' || type == 'view:document'; - } - /** * Performs post-fixer loops. Executes post-fixer callbacks as long as none of them has done any changes to the model. * diff --git a/src/view/documentfragment.js b/src/view/documentfragment.js index a66e21861..16e03d7fe 100644 --- a/src/view/documentfragment.js +++ b/src/view/documentfragment.js @@ -94,9 +94,8 @@ export default class DocumentFragment { } /** - * Checks whether given view tree object is of given type. - * - * Read more in {@link module:engine/view/node~Node#is `Node#is()`} and {@link module:engine/view/view~View#is `View#is()`}. + * Checks whether given view tree object is of given type following the convention set by + * {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/documentselection.js b/src/view/documentselection.js index b8c0c419b..ab4225688 100644 --- a/src/view/documentselection.js +++ b/src/view/documentselection.js @@ -289,8 +289,6 @@ export default class DocumentSelection { * selection.is( 'model:selection' ); // false * selection.is( 'element' ); // false * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/domconverter.js b/src/view/domconverter.js index dfa89a73b..66505be86 100644 --- a/src/view/domconverter.js +++ b/src/view/domconverter.js @@ -105,18 +105,6 @@ export default class DomConverter { this._fakeSelectionMapping = new WeakMap(); } - /** - * Checks whether given object is of `domConverter` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'domConverter' || type == 'view:domConverter'; - } - /** * Binds given DOM element that represents fake selection to {@link module:engine/view/documentselection~DocumentSelection * document selection}. Document selection copy is stored and can be retrieved by diff --git a/src/view/downcastwriter.js b/src/view/downcastwriter.js index 3ccb8c410..e12e381b5 100644 --- a/src/view/downcastwriter.js +++ b/src/view/downcastwriter.js @@ -54,18 +54,6 @@ export default class DowncastWriter { this._cloneGroups = new Map(); } - /** - * Checks whether given object is of `downcastWriter` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'downcastWriter' || type == 'view:downcastWriter'; - } - /** * Sets {@link module:engine/view/documentselection~DocumentSelection selection's} ranges and direction to the * specified location based on the given {@link module:engine/view/selection~Selectable selectable}. diff --git a/src/view/element.js b/src/view/element.js index 707d67ee0..0efc962b3 100644 --- a/src/view/element.js +++ b/src/view/element.js @@ -155,7 +155,7 @@ export default class Element extends Node { * obj.is( 'text' ); // false * obj.is( 'element', 'img' ); // false * - * Read more in {@link module:engine/view/node~Node#is `Node#is()`} and {@link module:engine/view/view~View#is `View#is()`}. + * Read more in {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @param {String} [name] Element name. diff --git a/src/view/matcher.js b/src/view/matcher.js index d77d77992..e8e32e8b6 100644 --- a/src/view/matcher.js +++ b/src/view/matcher.js @@ -174,18 +174,6 @@ export default class Matcher { return ( typeof pattern != 'function' && name && !( name instanceof RegExp ) ) ? name : null; } - - /** - * Checks whether given object is of `matcher` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'matcher' || type == 'view:matcher'; - } } // Returns match information if {@link module:engine/view/element~Element element} is matching provided pattern. diff --git a/src/view/node.js b/src/view/node.js index a301d6ff0..cc3e2a420 100644 --- a/src/view/node.js +++ b/src/view/node.js @@ -295,13 +295,17 @@ export default class Node { * This method is useful when processing view tree objects that are of unknown type. For example, a function * may return {@link module:engine/view/documentfragment~DocumentFragment} or {@link module:engine/view/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 `view:` to narrow search exclusively to view's objects. + * That should prevent of situation where `model:node` accidentally might be considered as `view:node`. + * Types are defined as name of the class written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. + * E.g. class `RootEditableElement` will get type `rootEditableElement`. * * obj.is( 'node' ); // true for any node, false for document fragment and text fragment * obj.is( 'view: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( 'view: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( 'ciew:element' ); // true for any element, false for text node or document fragment + * obj.is( 'view:element' ); // true for any element, false for text node or document fragment * obj.is( 'element', 'p' ); // true only for element which name is 'p' * obj.is( 'view:element', 'p' ); // true only for element which name is 'p' * obj.is( 'p' ); // shortcut for obj.is( 'element', 'p' ) diff --git a/src/view/position.js b/src/view/position.js index 95611c562..0c8798173 100644 --- a/src/view/position.js +++ b/src/view/position.js @@ -207,9 +207,8 @@ export default class Position { } /** - * Checks whether given object is of `position` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * Checks whether given object is of `position` type following the convention set by + * {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/range.js b/src/view/range.js index 43478df21..174952fda 100644 --- a/src/view/range.js +++ b/src/view/range.js @@ -395,9 +395,8 @@ export default class Range { } /** - * Checks whether given object is of `range` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. + * Checks whether given object is of `range` type following the convention set by + * {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/renderer.js b/src/view/renderer.js index 5b5b17a0c..c8bc5cc2e 100644 --- a/src/view/renderer.js +++ b/src/view/renderer.js @@ -118,18 +118,6 @@ export default class Renderer { this._fakeSelectionContainer = null; } - /** - * Checks whether given object is of `renderer` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'renderer' || type == 'view:renderer'; - } - /** * Marks a view node to be updated in the DOM by {@link #render `render()`}. * diff --git a/src/view/selection.js b/src/view/selection.js index b31dbb9f9..10fa8ceeb 100644 --- a/src/view/selection.js +++ b/src/view/selection.js @@ -608,8 +608,6 @@ export default class Selection { * selection.is( 'node' ); // false * selection.is( 'element' ); // false * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/textproxy.js b/src/view/textproxy.js index 3d459e37c..6b723385f 100644 --- a/src/view/textproxy.js +++ b/src/view/textproxy.js @@ -141,9 +141,8 @@ export default class TextProxy { } /** - * Checks whether given view tree object is of given type. - * - * Read more in {@link module:engine/view/node~Node#is `Node#is()`} and Read {@link module:engine/view/view~View#is `View#is()`}. + * Checks whether given view tree object is of given type following the convention set by + * {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/treewalker.js b/src/view/treewalker.js index b1ae7ba67..40f9fe689 100644 --- a/src/view/treewalker.js +++ b/src/view/treewalker.js @@ -144,18 +144,6 @@ export default class TreeWalker { return this; } - /** - * Checks whether given object is of `treeWalker` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'treeWalker' || type == 'view:treeWalker'; - } - /** * Moves {@link #position} in the {@link #direction} skipping values as long as the callback function returns `true`. * diff --git a/src/view/upcastwriter.js b/src/view/upcastwriter.js index ec0a83736..990e6e096 100644 --- a/src/view/upcastwriter.js +++ b/src/view/upcastwriter.js @@ -35,18 +35,6 @@ import Selection from './selection'; * writer.appendChild( text, someViewElement ); */ export default class UpcastWriter { - /** - * Checks whether given object is of `upcastWriter` type. - * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'upcastWriter' || type == 'view:upcastWriter'; - } - /** * Creates a new {@link module:engine/view/documentfragment~DocumentFragment} instance. * diff --git a/src/view/view.js b/src/view/view.js index 5f284aa53..c2abf2a47 100644 --- a/src/view/view.js +++ b/src/view/view.js @@ -652,40 +652,6 @@ export default class View { return new Selection( selectable, placeOrOffset, options ); } - /** - * Checks whether given object is of `view` type. - * - * All classes related to {@link module:engine/view/view~View} might contain `is` method, - * which checks if given object belong to specific type. Types are defined as name of the class - * written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. E.g. class `DocumentFragment` - * will get type `documentFragment`. There might appear some additional possibilities to test types which will be described in - * related documentation. - * - * It might simplify your code for cases, when you want to test unknown object for specific type. - * Instead of using `instanceof` and importing entire class for testing, there might be used `is` method which test for given name. - * There is also available `view:` prefix in each case, which gives more specific results. - * It helps to distinguish view's classes from model's. Few examples how to use this method you can find below: - * - * const view = new View() - * view.is( 'view' ) // return true - * - * const range = new Range( startPosition ) - * range.is( 'range' ) // return true - * range.is( 'view:range' ) // return true - * range.is( 'model:range' ) // return false - * - * const document = new Document(); - * document.is( 'model:document' ) // return true - * - * See also {@link module:engine/view/node~Node#is `Node#is()`} for some more details related to elements. - * - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'view'; - } - /** * Disables or enables rendering. If the flag is set to `true` then the rendering will be disabled. * If the flag is set to `false` and if there was some change in the meantime, then the rendering action will be performed. From c9e771998184862df7480493d99490cf48c8ad72 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 26 Jun 2019 14:33:15 +0200 Subject: [PATCH 19/30] Remove unit test for classes without 'is()' method. --- tests/view/document.js | 15 -------------- tests/view/domconverter/domconverter.js | 15 -------------- tests/view/downcastwriter/is.js | 23 --------------------- tests/view/matcher.js | 26 ------------------------ tests/view/renderer.js | 21 ------------------- tests/view/treewalker.js | 27 ------------------------- tests/view/upcastwriter.js | 21 ------------------- tests/view/view/view.js | 14 ------------- 8 files changed, 162 deletions(-) delete mode 100644 tests/view/downcastwriter/is.js diff --git a/tests/view/document.js b/tests/view/document.js index c664ff434..e0dc15ae2 100644 --- a/tests/view/document.js +++ b/tests/view/document.js @@ -37,21 +37,6 @@ describe( 'Document', () => { } ); } ); - describe( 'is()', () => { - it( 'should return true for "document"', () => { - expect( viewDocument.is( 'document' ) ).to.be.true; - expect( viewDocument.is( 'view:document' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( viewDocument.is( 'model' ) ).to.be.false; - expect( viewDocument.is( 'model:document' ) ).to.be.false; - expect( viewDocument.is( 'node' ) ).to.be.false; - expect( viewDocument.is( 'view:node' ) ).to.be.false; - expect( viewDocument.is( 'element', 'text' ) ).to.be.false; - } ); - } ); - describe( 'getRoot()', () => { it( 'should return "main" root', () => { createViewRoot( viewDocument, 'div', 'main' ); diff --git a/tests/view/domconverter/domconverter.js b/tests/view/domconverter/domconverter.js index fff3bd783..661c181a0 100644 --- a/tests/view/domconverter/domconverter.js +++ b/tests/view/domconverter/domconverter.js @@ -34,21 +34,6 @@ describe( 'DomConverter', () => { } ); } ); - describe( 'is()', () => { - it( 'should return true for "domConverter"', () => { - expect( converter.is( 'domConverter' ) ).to.be.true; - expect( converter.is( 'view:domConverter' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( converter.is( 'view' ) ).to.be.false; - expect( converter.is( 'model:domConverter' ) ).to.be.false; - expect( converter.is( 'node' ) ).to.be.false; - expect( converter.is( 'view:node' ) ).to.be.false; - expect( converter.is( 'element', 'text' ) ).to.be.false; - } ); - } ); - describe( 'focus()', () => { let viewEditable, domEditable, domEditableParent, viewDocument; diff --git a/tests/view/downcastwriter/is.js b/tests/view/downcastwriter/is.js deleted file mode 100644 index 7cad0ddfb..000000000 --- a/tests/view/downcastwriter/is.js +++ /dev/null @@ -1,23 +0,0 @@ -import DowncastWriter from '../../../src/view/downcastwriter'; -import Document from '../../../src/view/document'; - -describe( 'is()', () => { - let writer; - - before( () => { - writer = new DowncastWriter( new Document() ); - } ); - - it( 'should return true for "downcastWriter"', () => { - expect( writer.is( 'downcastWriter' ) ).to.be.true; - expect( writer.is( 'view:downcastWriter' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( writer.is( 'view' ) ).to.be.false; - expect( writer.is( 'model:downcastWriter' ) ).to.be.false; - expect( writer.is( 'node' ) ).to.be.false; - expect( writer.is( 'view:node' ) ).to.be.false; - expect( writer.is( 'element', 'text' ) ).to.be.false; - } ); -} ); diff --git a/tests/view/matcher.js b/tests/view/matcher.js index 80e2cbe89..5a9edff47 100644 --- a/tests/view/matcher.js +++ b/tests/view/matcher.js @@ -442,30 +442,4 @@ describe( 'Matcher', () => { expect( matcher.getElementName() ).to.be.null; } ); } ); - - describe( 'is', () => { - let matcher; - - before( () => { - matcher = new Matcher( 'div' ); - } ); - - it( 'should return true for "matcher"', () => { - expect( matcher.is( 'matcher' ) ).to.be.true; - expect( matcher.is( 'view:matcher' ) ).to.be.true; - } ); - - it( 'should return false for other accept values', () => { - expect( matcher.is( 'rootElement' ) ).to.be.false; - expect( matcher.is( 'containerElement' ) ).to.be.false; - expect( matcher.is( 'element' ) ).to.be.false; - expect( matcher.is( 'p' ) ).to.be.false; - expect( matcher.is( 'text' ) ).to.be.false; - expect( matcher.is( 'textProxy' ) ).to.be.false; - expect( matcher.is( 'attributeElement' ) ).to.be.false; - expect( matcher.is( 'uiElement' ) ).to.be.false; - expect( matcher.is( 'emptyElement' ) ).to.be.false; - expect( matcher.is( 'documentFragment' ) ).to.be.false; - } ); - } ); } ); diff --git a/tests/view/renderer.js b/tests/view/renderer.js index 3dc8c5cfb..617a7fda6 100644 --- a/tests/view/renderer.js +++ b/tests/view/renderer.js @@ -41,27 +41,6 @@ describe( 'Renderer', () => { renderer.domDocuments.add( document ); } ); - describe( 'is()', () => { - it( 'should return true for "renderer"', () => { - expect( renderer.is( 'renderer' ) ).to.be.true; - expect( renderer.is( 'view:renderer' ) ).to.be.true; - } ); - - it( 'should return false for other accept values', () => { - expect( renderer.is( 'rootElement' ) ).to.be.false; - expect( renderer.is( 'containerElement' ) ).to.be.false; - expect( renderer.is( 'element' ) ).to.be.false; - expect( renderer.is( 'p' ) ).to.be.false; - expect( renderer.is( 'text' ) ).to.be.false; - expect( renderer.is( 'textProxy' ) ).to.be.false; - expect( renderer.is( 'attributeElement' ) ).to.be.false; - expect( renderer.is( 'uiElement' ) ).to.be.false; - expect( renderer.is( 'emptyElement' ) ).to.be.false; - expect( renderer.is( 'documentFragment' ) ).to.be.false; - expect( renderer.is( 'model:renderer' ) ).to.be.false; - } ); - } ); - describe( 'markToSync', () => { let viewRoot; diff --git a/tests/view/treewalker.js b/tests/view/treewalker.js index e01c75eae..dda9f095e 100644 --- a/tests/view/treewalker.js +++ b/tests/view/treewalker.js @@ -70,33 +70,6 @@ describe( 'TreeWalker', () => { } ); } ); - describe( 'is()', () => { - let treeWalker; - - beforeEach( () => { - treeWalker = new TreeWalker( { startPosition: rootBeginning } ); - } ); - - it( 'should return true for "treeWalker"', () => { - expect( treeWalker.is( 'treeWalker' ) ).to.be.true; - expect( treeWalker.is( 'view:treeWalker' ) ).to.be.true; - } ); - - it( 'should return false for other accept values', () => { - expect( treeWalker.is( 'rootElement' ) ).to.be.false; - expect( treeWalker.is( 'containerElement' ) ).to.be.false; - expect( treeWalker.is( 'element' ) ).to.be.false; - expect( treeWalker.is( 'p' ) ).to.be.false; - expect( treeWalker.is( 'text' ) ).to.be.false; - expect( treeWalker.is( 'textProxy' ) ).to.be.false; - expect( treeWalker.is( 'attributeElement' ) ).to.be.false; - expect( treeWalker.is( 'uiElement' ) ).to.be.false; - expect( treeWalker.is( 'emptyElement' ) ).to.be.false; - expect( treeWalker.is( 'documentFragment' ) ).to.be.false; - expect( treeWalker.is( 'model:treeWalker' ) ).to.be.false; - } ); - } ); - describe( 'iterate from start position `startPosition`', () => { let expected; diff --git a/tests/view/upcastwriter.js b/tests/view/upcastwriter.js index b361aa8d7..26ce26089 100644 --- a/tests/view/upcastwriter.js +++ b/tests/view/upcastwriter.js @@ -34,27 +34,6 @@ describe( 'UpcastWriter', () => { view = dataprocessor.toView( html ); } ); - describe( 'is()', () => { - it( 'should return true for "upcastWriter"', () => { - expect( writer.is( 'upcastWriter' ) ).to.be.true; - expect( writer.is( 'view:upcastWriter' ) ).to.be.true; - } ); - - it( 'should return false for other accept values', () => { - expect( writer.is( 'rootElement' ) ).to.be.false; - expect( writer.is( 'containerElement' ) ).to.be.false; - expect( writer.is( 'element' ) ).to.be.false; - expect( writer.is( 'p' ) ).to.be.false; - expect( writer.is( 'text' ) ).to.be.false; - expect( writer.is( 'textProxy' ) ).to.be.false; - expect( writer.is( 'attributeElement' ) ).to.be.false; - expect( writer.is( 'uiElement' ) ).to.be.false; - expect( writer.is( 'emptyElement' ) ).to.be.false; - expect( writer.is( 'documentFragment' ) ).to.be.false; - expect( writer.is( 'model:renderer' ) ).to.be.false; - } ); - } ); - describe( 'createDocumentFragment', () => { it( 'should create empty document fragment', () => { const df = writer.createDocumentFragment(); diff --git a/tests/view/view/view.js b/tests/view/view/view.js index 33ed05dd4..9b410ad50 100644 --- a/tests/view/view/view.js +++ b/tests/view/view/view.js @@ -488,20 +488,6 @@ describe( 'view', () => { } ); } ); - describe( 'is()', () => { - it( 'should return true for "view"', () => { - expect( view.is( 'view' ) ).to.be.true; - } ); - - it( 'should return false for incorrect values', () => { - expect( view.is( 'model' ) ).to.be.false; - expect( view.is( 'model:document' ) ).to.be.false; - expect( view.is( 'node' ) ).to.be.false; - expect( view.is( 'view:node' ) ).to.be.false; - expect( view.is( 'element', 'text' ) ).to.be.false; - } ); - } ); - describe( 'isFocused', () => { it( 'should change renderer.isFocused too', () => { expect( viewDocument.isFocused ).to.equal( false ); From 4e07dec1e9678deb4df839a94dbbd6e1ef423324 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Wed, 26 Jun 2019 14:50:50 +0200 Subject: [PATCH 20/30] Fix docs entries to non existing places. --- src/model/markercollection.js | 2 +- src/model/position.js | 2 +- src/model/range.js | 2 +- src/view/node.js | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/model/markercollection.js b/src/model/markercollection.js index 50e1e441e..90b304d12 100644 --- a/src/model/markercollection.js +++ b/src/model/markercollection.js @@ -451,7 +451,7 @@ class Marker { /** * Checks whether given object is of `marker` type. * - * Read more at {@link module:engine/model/model~Node#is}. + * Read more at {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/position.js b/src/model/position.js index f70fe2a96..35376868c 100644 --- a/src/model/position.js +++ b/src/model/position.js @@ -505,7 +505,7 @@ export default class Position { /** * Checks whether given object is of `position` type. * - * Read more at {@link module:engine/model/model~Node#is `Node#is()`}. + * Read more at {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/range.js b/src/model/range.js index 070709d28..4635c9edb 100644 --- a/src/model/range.js +++ b/src/model/range.js @@ -146,7 +146,7 @@ export default class Range { /** * Checks whether given object is of `range` type. * - * Read more at {@link module:engine/model/model~Node#is `Node#is()`}. + * Read more at {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/node.js b/src/view/node.js index cc3e2a420..693259312 100644 --- a/src/view/node.js +++ b/src/view/node.js @@ -313,7 +313,6 @@ export default class Node { * obj.is( 'text' ); // true for text node, false for element and document fragment * obj.is( 'view:text' ); // true for text node, false for element and document fragment * - * Read more at {@link module:engine/view/view~View#is `View#is()`}. * * @param {String} type * @returns {Boolean} From b961330eabd30283edf12973674b92c40f7e866e Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 28 Jun 2019 16:36:17 +0200 Subject: [PATCH 21/30] Unify names of unit tests. --- tests/model/documentfragment.js | 2 +- tests/model/documentselection.js | 2 +- tests/model/element.js | 2 +- tests/model/rootelement.js | 2 +- tests/model/selection.js | 2 +- tests/model/text.js | 2 +- tests/model/textproxy.js | 2 +- tests/view/attributeelement.js | 2 +- tests/view/containerelement.js | 2 +- tests/view/documentselection.js | 2 +- tests/view/element.js | 2 +- tests/view/emptyelement.js | 2 +- tests/view/position.js | 2 +- tests/view/rooteditableelement.js | 2 +- tests/view/selection.js | 2 +- tests/view/text.js | 2 +- tests/view/textproxy.js | 2 +- 17 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/model/documentfragment.js b/tests/model/documentfragment.js index b110e5860..a2eb870e8 100644 --- a/tests/model/documentfragment.js +++ b/tests/model/documentfragment.js @@ -61,7 +61,7 @@ describe( 'DocumentFragment', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let frag; before( () => { diff --git a/tests/model/documentselection.js b/tests/model/documentselection.js index 5ac47af66..12d7b149f 100644 --- a/tests/model/documentselection.js +++ b/tests/model/documentselection.js @@ -490,7 +490,7 @@ describe( 'DocumentSelection', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { it( 'should return true for selection', () => { expect( selection.is( 'selection' ) ).to.be.true; expect( selection.is( 'model:selection' ) ).to.be.true; diff --git a/tests/model/element.js b/tests/model/element.js index 8c3132fdd..8e448303d 100644 --- a/tests/model/element.js +++ b/tests/model/element.js @@ -38,7 +38,7 @@ describe( 'Element', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let element; before( () => { diff --git a/tests/model/rootelement.js b/tests/model/rootelement.js index 34bd4b389..6d90626aa 100644 --- a/tests/model/rootelement.js +++ b/tests/model/rootelement.js @@ -22,7 +22,7 @@ describe( 'RootElement', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let root; before( () => { diff --git a/tests/model/selection.js b/tests/model/selection.js index fd440e61f..5897da938 100644 --- a/tests/model/selection.js +++ b/tests/model/selection.js @@ -826,7 +826,7 @@ describe( 'Selection', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { it( 'should return true for selection', () => { expect( selection.is( 'selection' ) ).to.be.true; } ); diff --git a/tests/model/text.js b/tests/model/text.js index ab812c4af..86c93b748 100644 --- a/tests/model/text.js +++ b/tests/model/text.js @@ -32,7 +32,7 @@ describe( 'Text', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let text; before( () => { diff --git a/tests/model/textproxy.js b/tests/model/textproxy.js index be314c961..a6cff835f 100644 --- a/tests/model/textproxy.js +++ b/tests/model/textproxy.js @@ -102,7 +102,7 @@ describe( 'TextProxy', () => { }, /model-textproxy-wrong-length/, model ); } ); - describe( 'is', () => { + describe( 'is()', () => { it( 'should return true for textProxy', () => { expect( textProxy.is( 'textProxy' ) ).to.be.true; expect( textProxy.is( 'model:textProxy' ) ).to.be.true; diff --git a/tests/view/attributeelement.js b/tests/view/attributeelement.js index 726112d58..224325ff6 100644 --- a/tests/view/attributeelement.js +++ b/tests/view/attributeelement.js @@ -21,7 +21,7 @@ describe( 'AttributeElement', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let el; before( () => { diff --git a/tests/view/containerelement.js b/tests/view/containerelement.js index b38394819..7a8cd2302 100644 --- a/tests/view/containerelement.js +++ b/tests/view/containerelement.js @@ -18,7 +18,7 @@ describe( 'ContainerElement', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let el; before( () => { diff --git a/tests/view/documentselection.js b/tests/view/documentselection.js index 4b6b7a371..e9e2d5a04 100644 --- a/tests/view/documentselection.js +++ b/tests/view/documentselection.js @@ -725,7 +725,7 @@ describe( 'DocumentSelection', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { it( 'should return true for selection', () => { expect( documentSelection.is( 'selection' ) ).to.be.true; expect( documentSelection.is( 'view:selection' ) ).to.be.true; diff --git a/tests/view/element.js b/tests/view/element.js index 5285658ca..1f161e163 100644 --- a/tests/view/element.js +++ b/tests/view/element.js @@ -82,7 +82,7 @@ describe( 'Element', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let el; before( () => { diff --git a/tests/view/emptyelement.js b/tests/view/emptyelement.js index 166b905a3..695ddd881 100644 --- a/tests/view/emptyelement.js +++ b/tests/view/emptyelement.js @@ -20,7 +20,7 @@ describe( 'EmptyElement', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let el; before( () => { diff --git a/tests/view/position.js b/tests/view/position.js index e20ae286e..7e5ea011e 100644 --- a/tests/view/position.js +++ b/tests/view/position.js @@ -31,7 +31,7 @@ describe( 'Position', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let position; beforeEach( () => { diff --git a/tests/view/rooteditableelement.js b/tests/view/rooteditableelement.js index 31d941614..17b8b31e5 100644 --- a/tests/view/rooteditableelement.js +++ b/tests/view/rooteditableelement.js @@ -38,7 +38,7 @@ describe( 'RootEditableElement', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let el; before( () => { diff --git a/tests/view/selection.js b/tests/view/selection.js index c008cd02d..891f9dee6 100644 --- a/tests/view/selection.js +++ b/tests/view/selection.js @@ -600,7 +600,7 @@ describe( 'Selection', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { it( 'should return true for selection', () => { expect( selection.is( 'selection' ) ).to.be.true; expect( selection.is( 'view:selection' ) ).to.be.true; diff --git a/tests/view/text.js b/tests/view/text.js index b47e96f84..b3eb9e315 100644 --- a/tests/view/text.js +++ b/tests/view/text.js @@ -17,7 +17,7 @@ describe( 'Text', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { let text; before( () => { diff --git a/tests/view/textproxy.js b/tests/view/textproxy.js index 0147fa84f..08a4b318f 100644 --- a/tests/view/textproxy.js +++ b/tests/view/textproxy.js @@ -61,7 +61,7 @@ describe( 'TextProxy', () => { } ); } ); - describe( 'is', () => { + describe( 'is()', () => { it( 'should return true for textProxy', () => { expect( textProxy.is( 'textProxy' ) ).to.be.true; expect( textProxy.is( 'view:textProxy' ) ).to.be.true; From 76f8a51b9073aa0444ece15220918d7f457fb81c Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 25 Jul 2019 11:47:59 +0200 Subject: [PATCH 22/30] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Piotrek Koszuliński --- src/model/element.js | 3 ++- src/view/attributeelement.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/model/element.js b/src/model/element.js index cff992681..a3ef2974d 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -113,7 +113,8 @@ export default class Element extends Node { * @returns {Boolean} */ is( type, name = null ) { - const cutType = type.replace( 'model:', '' ); + const cutType = type.replace( /^model:/, '' ); + if ( !name ) { return cutType == 'element' || cutType == this.name || super.is( type ); } else { diff --git a/src/view/attributeelement.js b/src/view/attributeelement.js index ef74cedba..9fa77c815 100644 --- a/src/view/attributeelement.js +++ b/src/view/attributeelement.js @@ -128,7 +128,8 @@ export default class AttributeElement extends Element { * @inheritDoc */ is( type, name = null ) { - const cutType = type && type.replace( 'view:', '' ); + const cutType = type && type.replace( /^view:/, '' ); + if ( !name ) { return cutType == 'attributeElement' || super.is( type ); } else { From 0fb88210e8b5ac380f6e9cb3791a9dd0c5e2f202 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Thu, 25 Jul 2019 12:15:23 +0200 Subject: [PATCH 23/30] Revert change replacing spaces to tabs. --- src/view/downcastwriter.js | 40 +++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/view/downcastwriter.js b/src/view/downcastwriter.js index e12e381b5..6abdf3316 100644 --- a/src/view/downcastwriter.js +++ b/src/view/downcastwriter.js @@ -788,26 +788,26 @@ export default class DowncastWriter { } /** - * Wraps elements within range with provided {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. - * If a collapsed range is provided, it will be wrapped only if it is equal to view selection. - * - * If a collapsed range was passed and is same as selection, the selection - * will be moved to the inside of the wrapped attribute element. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-invalid-range-container` - * when {@link module:engine/view/range~Range#start} - * and {@link module:engine/view/range~Range#end} positions are not placed inside same parent container. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-invalid-attribute` when passed attribute element is not - * an instance of {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. - * - * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-nonselection-collapsed-range` when passed range - * is collapsed and different than view selection. - * - * @param {module:engine/view/range~Range} range Range to wrap. - * @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}. + * If a collapsed range is provided, it will be wrapped only if it is equal to view selection. + * + * If a collapsed range was passed and is same as selection, the selection + * will be moved to the inside of the wrapped attribute element. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-invalid-range-container` + * when {@link module:engine/view/range~Range#start} + * and {@link module:engine/view/range~Range#end} positions are not placed inside same parent container. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-invalid-attribute` when passed attribute element is not + * an instance of {@link module:engine/view/attributeelement~AttributeElement AttributeElement}. + * + * Throws {@link module:utils/ckeditorerror~CKEditorError} `view-writer-wrap-nonselection-collapsed-range` when passed range + * is collapsed and different than view selection. + * + * @param {module:engine/view/range~Range} range Range to wrap. + * @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. + */ wrap( range, attribute ) { if ( !( attribute instanceof AttributeElement ) ) { throw new CKEditorError( 'view-writer-wrap-invalid-attribute', this.document ); From 3aa2407c94768342ce614d3210b9109b653c8f02 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 26 Jul 2019 16:11:19 +0200 Subject: [PATCH 24/30] Update docs for View, and replace trig with regexp in is() method. --- src/view/containerelement.js | 2 +- src/view/documentfragment.js | 10 ++++++++-- src/view/documentselection.js | 21 ++++++++------------- src/view/editableelement.js | 2 +- src/view/element.js | 16 +++++++++++++--- src/view/emptyelement.js | 2 +- src/view/node.js | 2 ++ src/view/position.js | 10 ++++++++-- src/view/range.js | 10 ++++++++-- src/view/rooteditableelement.js | 2 +- src/view/selection.js | 11 +++++++++-- src/view/textproxy.js | 10 ++++++++-- src/view/uielement.js | 2 +- 13 files changed, 69 insertions(+), 31 deletions(-) diff --git a/src/view/containerelement.js b/src/view/containerelement.js index bb8d3c2a4..1d1d8ff31 100644 --- a/src/view/containerelement.js +++ b/src/view/containerelement.js @@ -54,7 +54,7 @@ export default class ContainerElement extends Element { * @inheritDoc */ is( type, name = null ) { - const cutType = type && type.replace( 'view:', '' ); + const cutType = type && type.replace( /^view:/, '' ); if ( !name ) { return cutType == 'containerElement' || super.is( type ); } else { diff --git a/src/view/documentfragment.js b/src/view/documentfragment.js index 16e03d7fe..7b469c248 100644 --- a/src/view/documentfragment.js +++ b/src/view/documentfragment.js @@ -94,8 +94,14 @@ export default class DocumentFragment { } /** - * Checks whether given view tree object is of given type following the convention set by - * {@link module:engine/view/node~Node#is `Node#is()`}. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, + * for example `view:documentFragment`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. + * + * Acceptable type for this class is `documentFragment` and its prefixed version. + * + * See also: {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/documentselection.js b/src/view/documentselection.js index ab4225688..290a9c618 100644 --- a/src/view/documentselection.js +++ b/src/view/documentselection.js @@ -275,19 +275,14 @@ export default class DocumentSelection { } /** - * Checks whether object is of given type following the convention set by - * {@link module:engine/view/node~Node#is `Node#is()`}. - * - * const selection = new DocumentSelection( ... ); - * - * selection.is( 'selection' ); // true - * selection.is( 'view:selection' ); // true - * selection.is( 'documentSelection' ); // true - * selection.is( 'view:documentSelection' ); // true - * selection.is( 'node' ); // false - * selection.is( 'view:node' ); // false - * selection.is( 'model:selection' ); // false - * selection.is( 'element' ); // false + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, + * for example `view:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. + * + * Acceptable types for this class are `selection` and `documentSelection` and its prefixed version. + * + * See also: {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/editableelement.js b/src/view/editableelement.js index f702073c3..5c7323486 100644 --- a/src/view/editableelement.js +++ b/src/view/editableelement.js @@ -70,7 +70,7 @@ export default class EditableElement extends ContainerElement { * @inheritDoc */ is( type, name = null ) { - const cutType = type && type.replace( 'view:', '' ); + const cutType = type && type.replace( /^view:/, '' ); if ( !name ) { return cutType == 'editableElement' || super.is( type ); } else { diff --git a/src/view/element.js b/src/view/element.js index 0efc962b3..113842048 100644 --- a/src/view/element.js +++ b/src/view/element.js @@ -147,22 +147,32 @@ export default class Element extends Node { } /** - * Checks whether this view object is of the given type. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, + * for example `view:element`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * + * There is also possibility to check element's {@link #name} rather than just type. Name might be provided as second attribute + * or might replace the type. + * + * // obj is a `li` element * obj.is( 'element' ); // true + * obj.is( 'view:element' ); // true * obj.is( 'li' ); // true * obj.is( 'element', 'li' ); // true * obj.is( 'text' ); // false * obj.is( 'element', 'img' ); // false * - * Read more in {@link module:engine/view/node~Node#is `Node#is()`}. + * Acceptable type for this class is `element` and its prefixed version, element's name or combination of both arguments. + * + * See also: {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @param {String} [name] Element name. * @returns {Boolean} */ is( type, name = null ) { - const cutType = type.replace( 'view:', '' ); + const cutType = type.replace( /^view:/, '' ); if ( !name ) { return cutType == 'element' || cutType == this.name || super.is( type ); } else { diff --git a/src/view/emptyelement.js b/src/view/emptyelement.js index 6d4cae9ee..2cb2603db 100644 --- a/src/view/emptyelement.js +++ b/src/view/emptyelement.js @@ -47,7 +47,7 @@ export default class EmptyElement extends Element { * @inheritDoc */ is( type, name = null ) { - const cutType = type.replace( 'view:', '' ); + const cutType = type.replace( /^view:/, '' ); if ( !name ) { return cutType == 'emptyElement' || super.is( type ); } else { diff --git a/src/view/node.js b/src/view/node.js index 693259312..71e8ebe85 100644 --- a/src/view/node.js +++ b/src/view/node.js @@ -314,6 +314,8 @@ export default class Node { * obj.is( 'view:text' ); // true for text node, false for element and document fragment * * + * Acceptable types for this class is `node` and its prefixed version. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/view/position.js b/src/view/position.js index 0c8798173..ca7ee7fce 100644 --- a/src/view/position.js +++ b/src/view/position.js @@ -207,8 +207,14 @@ export default class Position { } /** - * Checks whether given object is of `position` type following the convention set by - * {@link module:engine/view/node~Node#is `Node#is()`}. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, + * for example `view:position`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. + * + * Acceptable type for this class is `position` and its prefixed version. + * + * See also: {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/range.js b/src/view/range.js index 174952fda..37308709c 100644 --- a/src/view/range.js +++ b/src/view/range.js @@ -395,8 +395,14 @@ export default class Range { } /** - * Checks whether given object is of `range` type following the convention set by - * {@link module:engine/view/node~Node#is `Node#is()`}. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, + * for example `view:range`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. + * + * Acceptable type for this class is `range` and its prefixed version. + * + * See also: {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/rooteditableelement.js b/src/view/rooteditableelement.js index 85b183bc7..c4de2e411 100644 --- a/src/view/rooteditableelement.js +++ b/src/view/rooteditableelement.js @@ -41,7 +41,7 @@ export default class RootEditableElement extends EditableElement { * @inheritDoc */ is( type, name = null ) { - const cutType = type.replace( 'view:', '' ); + const cutType = type.replace( /^view:/, '' ); if ( !name ) { return cutType == 'rootElement' || super.is( type ); } else { diff --git a/src/view/selection.js b/src/view/selection.js index 10fa8ceeb..2c267ffc7 100644 --- a/src/view/selection.js +++ b/src/view/selection.js @@ -598,8 +598,10 @@ export default class Selection { } /** - * Checks whether object is of given type following the convention set by - * {@link module:engine/view/node~Node#is `Node#is()`}. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, + * for example `view:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * * const selection = new Selection( ... ); * @@ -607,6 +609,11 @@ export default class Selection { * selection.is( 'view:selection' ); // true * selection.is( 'node' ); // false * selection.is( 'element' ); // false + * + * Acceptable type for this class is `selection` and its prefixed version. + * + * See also: {@link module:engine/view/node~Node#is `Node#is()`}. + * * @param {String} type * @returns {Boolean} diff --git a/src/view/textproxy.js b/src/view/textproxy.js index 6b723385f..a30ac01e4 100644 --- a/src/view/textproxy.js +++ b/src/view/textproxy.js @@ -141,8 +141,14 @@ export default class TextProxy { } /** - * Checks whether given view tree object is of given type following the convention set by - * {@link module:engine/view/node~Node#is `Node#is()`}. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, + * for example `view:textProxy`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. + * + * Acceptable type for this class is `textProxy` and its prefixed version. + * + * See also: {@link module:engine/view/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/view/uielement.js b/src/view/uielement.js index 68c2a39a5..bdae99965 100644 --- a/src/view/uielement.js +++ b/src/view/uielement.js @@ -60,7 +60,7 @@ export default class UIElement extends Element { * @inheritDoc */ is( type, name = null ) { - const cutType = type.replace( 'view:', '' ); + const cutType = type.replace( /^view:/, '' ); if ( !name ) { return cutType == 'uiElement' || super.is( type ); } else { From 887ba8802cc5df6bcd1058c6572d3b782028017a Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Fri, 26 Jul 2019 16:51:14 +0200 Subject: [PATCH 25/30] Improve description to model's is method. --- src/model/documentfragment.js | 9 +++++++-- src/model/documentselection.js | 10 ++++++++-- src/model/element.js | 12 ++++++++++-- src/model/markercollection.js | 9 +++++++-- src/model/node.js | 2 ++ src/model/position.js | 10 ++++++++-- src/model/range.js | 10 ++++++++-- src/model/selection.js | 9 +++++++-- src/model/textproxy.js | 9 +++++++-- 9 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/model/documentfragment.js b/src/model/documentfragment.js index 6390071fe..301a64f63 100644 --- a/src/model/documentfragment.js +++ b/src/model/documentfragment.js @@ -116,9 +116,14 @@ export default class DocumentFragment { } /** - * Checks whether given model tree object is of given type. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:documentFragment`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * - * Read more in {@link module:engine/model/node~Node#is `Node#is()`}. + * Acceptable type for this class is `documentFragment` and its prefixed version. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/documentselection.js b/src/model/documentselection.js index d339a3485..1dde223b5 100644 --- a/src/model/documentselection.js +++ b/src/model/documentselection.js @@ -368,8 +368,10 @@ 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()`}. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * * const selection = new DocumentSelection( ... ); * @@ -382,6 +384,10 @@ export default class DocumentSelection { * selection.is( 'model:node' ); // false * selection.is( 'element' ); // false * + * Acceptable type for this class is `selection` and its prefixed version. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * * @param {String} type * @returns {Boolean} */ diff --git a/src/model/element.js b/src/model/element.js index a3ef2974d..1d9cfce31 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -89,7 +89,13 @@ export default class Element extends Node { } /** - * Checks whether this model object is of the given type. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:element`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. + * + * There is also possibility to check element's {@link #name} rather than just type. Name might be provided as second attribute + * or might replace the type. * * obj.name; // 'listItem' * obj instanceof Element; // true @@ -105,7 +111,9 @@ export default class Element extends Node { * obj.is( 'view:element' ); // false * obj.is( 'element', 'image' ); // false * - * Read more in {@link module:engine/model/node~Node#is `Node#is()`}. + * Acceptable type for this class is `element` and its prefixed version, element's name or combination of both arguments. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type Type to check when `name` parameter is present. * Otherwise, it acts like the `name` parameter. diff --git a/src/model/markercollection.js b/src/model/markercollection.js index 90b304d12..7e01d1ce4 100644 --- a/src/model/markercollection.js +++ b/src/model/markercollection.js @@ -449,9 +449,14 @@ class Marker { } /** - * Checks whether given object is of `marker` type. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:marker`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * - * Read more at {@link module:engine/model/node~Node#is `Node#is()`}. + * Acceptable type for this class is `marker` and its prefixed version. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/node.js b/src/model/node.js index f9b09598f..5d03f9725 100644 --- a/src/model/node.js +++ b/src/model/node.js @@ -482,6 +482,8 @@ export default class Node { * obj.is( 'text' ); // true for text node, false for element and document fragment * obj.is( 'textProxy' ); // true for text proxy object * + * Acceptable types for this class is `node` and its prefixed version. + * * @method #is * @param {String} type * @returns {Boolean} diff --git a/src/model/position.js b/src/model/position.js index 35376868c..c5be603d1 100644 --- a/src/model/position.js +++ b/src/model/position.js @@ -503,9 +503,15 @@ export default class Position { } /** - * Checks whether given object is of `position` type. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:position`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * - * Read more at {@link module:engine/model/node~Node#is `Node#is()`}. + * Acceptable type for this class is `position` and its prefixed version. + * For {@link module:engine/model/liveposition~LivePosition} class there also acceptable `livePosition` type and its prefixed version. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/range.js b/src/model/range.js index 4635c9edb..91ce5e193 100644 --- a/src/model/range.js +++ b/src/model/range.js @@ -144,9 +144,15 @@ export default class Range { } /** - * Checks whether given object is of `range` type. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:range`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * - * Read more at {@link module:engine/model/node~Node#is `Node#is()`}. + * Acceptable type for this class is `range` and its prefixed version. + * For {@link module:engine/model/liverange~LiveRange} class there also acceptable `liveRange` type and its prefixed version. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} diff --git a/src/model/selection.js b/src/model/selection.js index f5415fcae..4985b62cf 100644 --- a/src/model/selection.js +++ b/src/model/selection.js @@ -623,8 +623,10 @@ 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()`}. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * * const selection = new Selection( ... ); * @@ -634,6 +636,9 @@ export default class Selection { * selection.is( 'element' ); // false * selection.is( 'view:selection' ); // false * + * Acceptable type for this class is `selection` and its prefixed version. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * @param {String} type * @returns {Boolean} */ diff --git a/src/model/textproxy.js b/src/model/textproxy.js index 511f84dd3..fc6e1048f 100644 --- a/src/model/textproxy.js +++ b/src/model/textproxy.js @@ -173,9 +173,14 @@ export default class TextProxy { } /** - * Checks whether given object is of `textProxy` type. + * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, + * for example `model:textProxy`. Type is a string which usually equal to a name of the class written in camelCase convention. + * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` + * for any type match for an entire child-parent chain. * - * Read more in {@link module:engine/model/node~Node#is `Node#is()`}. + * Acceptable type for this class is `textProxy` and its prefixed version. + * + * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * * @param {String} type * @returns {Boolean} From f33f4453c020ffb1507efa7bcd2c54330aae0274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20Koszuli=C5=84ski?= Date: Thu, 8 Aug 2019 13:54:43 +0200 Subject: [PATCH 26/30] Refined the documentation. --- src/model/documentfragment.js | 14 +++--- src/model/documentselection.js | 25 ++++------- src/model/element.js | 33 ++++++-------- src/model/node.js | 78 ++++++++++++++++++++-------------- 4 files changed, 77 insertions(+), 73 deletions(-) diff --git a/src/model/documentfragment.js b/src/model/documentfragment.js index 301a64f63..bf9c5ce2a 100644 --- a/src/model/documentfragment.js +++ b/src/model/documentfragment.js @@ -116,14 +116,16 @@ export default class DocumentFragment { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:documentFragment`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given type. * - * Acceptable type for this class is `documentFragment` and its prefixed version. + * docFrag.is( 'documentFragment' ); // -> true + * docFrag.is( 'model:documentFragment' ); // -> true * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * 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} diff --git a/src/model/documentselection.js b/src/model/documentselection.js index 605ce09c4..c40412b16 100644 --- a/src/model/documentselection.js +++ b/src/model/documentselection.js @@ -367,25 +367,18 @@ export default class DocumentSelection { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * 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( '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 + * selection.is( 'view:selection' ); // -> false + * selection.is( 'element' ); // -> false + * selection.is( 'node' ); // -> false * - * Acceptable type for this class is `selection` and its prefixed version. - * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method. * * @param {String} type * @returns {Boolean} diff --git a/src/model/element.js b/src/model/element.js index 1d9cfce31..16b10a276 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -89,31 +89,24 @@ export default class Element extends Node { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:element`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given. * - * There is also possibility to check element's {@link #name} rather than just type. Name might be provided as second attribute - * or might replace the type. + * element.is( 'element' ); // -> true + * element.is( 'node' ); // -> true + * element.is( 'model:element' ); // -> true + * element.is( 'model:documentSelection' ); // -> true * - * obj.name; // 'listItem' - * obj instanceof Element; // true + * element.is( 'view:element' ); // -> false + * element.is( 'documentSelection' ); // -> false * - * 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 + * Assuming that the object being checked is an element, you can also check its + * {@link module:engine/model/element~Element#name name}: * - * Acceptable type for this class is `element` and its prefixed version, element's name or combination of both arguments. + * element.is( 'image' ); // -> true if this is an element + * element.is( 'element', 'image' ); // -> same as above + * text.is( 'image' ); -> false * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * {@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. diff --git a/src/model/node.js b/src/model/node.js index 5d03f9725..df30fa8eb 100644 --- a/src/model/node.js +++ b/src/model/node.js @@ -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. * @@ -460,37 +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. - * 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`. - * Types are defined as name of the class written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. - * E.g. class `LiveRange` will get type `liveRange`. - * - * There is more classes in model which follows similar naming convention. Check corresponding elements documentation for more details. - * - * 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 - * - * Acceptable types for this class is `node` and its prefixed version. - * - * @method #is - * @param {String} type - * @returns {Boolean} - */ - is( type ) { - return type == 'node' || type == 'model:node'; - } } /** From 96f6213577d1590225e003d16fef513c1fb78a37 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Mon, 12 Aug 2019 12:01:05 +0200 Subject: [PATCH 27/30] Fix wrong example in docs. --- src/model/element.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/model/element.js b/src/model/element.js index 16b10a276..4d6d1f928 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -94,7 +94,6 @@ export default class Element extends Node { * element.is( 'element' ); // -> true * element.is( 'node' ); // -> true * element.is( 'model:element' ); // -> true - * element.is( 'model:documentSelection' ); // -> true * * element.is( 'view:element' ); // -> false * element.is( 'documentSelection' ); // -> false From a1885ed9c21dafc4f1010de7a8d6678773a1448e Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Mon, 12 Aug 2019 13:08:33 +0200 Subject: [PATCH 28/30] Correct description for models' classes. --- src/model/liveposition.js | 15 ++++++++++++++- src/model/liverange.js | 15 ++++++++++++++- src/model/markercollection.js | 13 +++++++------ src/model/position.js | 14 +++++++------- src/model/range.js | 14 +++++++------- src/model/rootelement.js | 25 ++++++++++++++++++++++++- src/model/selection.js | 18 ++++++------------ src/model/text.js | 16 +++++++++++++++- src/model/textproxy.js | 13 +++++++------ 9 files changed, 101 insertions(+), 42 deletions(-) diff --git a/src/model/liveposition.js b/src/model/liveposition.js index 4bfbbffd2..7a03cfe9d 100644 --- a/src/model/liveposition.js +++ b/src/model/liveposition.js @@ -64,7 +64,20 @@ export default class LivePosition extends Position { } /** - * @inheritDoc + * 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 ); diff --git a/src/model/liverange.js b/src/model/liverange.js index 98b57b781..034a716e5 100644 --- a/src/model/liverange.js +++ b/src/model/liverange.js @@ -41,7 +41,20 @@ export default class LiveRange extends Range { } /** - * @inheritDoc + * 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 ); diff --git a/src/model/markercollection.js b/src/model/markercollection.js index 7e01d1ce4..488021640 100644 --- a/src/model/markercollection.js +++ b/src/model/markercollection.js @@ -449,14 +449,15 @@ class Marker { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:marker`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given. * - * Acceptable type for this class is `marker` and its prefixed version. + * marker.is( 'marker' ); // -> true + * marker.is( 'model:marker' ); // -> true * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * 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} diff --git a/src/model/position.js b/src/model/position.js index c5be603d1..712eacf82 100644 --- a/src/model/position.js +++ b/src/model/position.js @@ -503,15 +503,15 @@ export default class Position { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:position`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given. * - * Acceptable type for this class is `position` and its prefixed version. - * For {@link module:engine/model/liveposition~LivePosition} class there also acceptable `livePosition` type and its prefixed version. + * position.is( 'position' ); // -> true + * position.is( 'model:position' ); // -> true * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * 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} diff --git a/src/model/range.js b/src/model/range.js index 91ce5e193..bd4390743 100644 --- a/src/model/range.js +++ b/src/model/range.js @@ -144,15 +144,15 @@ export default class Range { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:range`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given. * - * Acceptable type for this class is `range` and its prefixed version. - * For {@link module:engine/model/liverange~LiveRange} class there also acceptable `liveRange` type and its prefixed version. + * range.is( 'range' ); // -> true + * range.is( 'model:range' ); // -> true * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * 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} diff --git a/src/model/rootelement.js b/src/model/rootelement.js index 62bd3b45c..06aae94fd 100644 --- a/src/model/rootelement.js +++ b/src/model/rootelement.js @@ -55,7 +55,30 @@ export default class RootElement extends Element { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * rootElement.is( 'rootElement' ); // -> true + * rootElement.is( 'model:rootElement' ); // -> true + * rootElement.is( 'element' ); // -> true + * rootElement.is( 'node' ); // -> true + * rootElement.is( 'model:element' ); // -> 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( 'element', '$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:', '' ); diff --git a/src/model/selection.js b/src/model/selection.js index 4985b62cf..2b14b49a5 100644 --- a/src/model/selection.js +++ b/src/model/selection.js @@ -623,22 +623,16 @@ export default class Selection { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * 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( 'model:selection' ); // true - * selection.is( 'node' ); // false - * selection.is( 'element' ); // false - * selection.is( 'view:selection' ); // false + * selection.is( 'view:selection' ); // -> false + * selection.is( 'range' ); // -> false * - * Acceptable type for this class is `selection` and its prefixed version. + * {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method. * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. * @param {String} type * @returns {Boolean} */ diff --git a/src/model/text.js b/src/model/text.js index ea487552f..5d07be4f7 100644 --- a/src/model/text.js +++ b/src/model/text.js @@ -63,7 +63,21 @@ export default class Text extends Node { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * text.is( 'text' ); // -> true + * text.is( 'node' ); // -> true + * text.is( 'model:text' ); // -> true + * + * text.is( 'view:text' ); // -> false + * text.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 Type to check when `name` parameter is present. + * Otherwise, it acts like the `name` parameter. + * @param {String} [name] Element name. + * @returns {Boolean} */ is( type ) { return type == 'text' || type == 'model:text' || super.is( type ); diff --git a/src/model/textproxy.js b/src/model/textproxy.js index fc6e1048f..f52051f35 100644 --- a/src/model/textproxy.js +++ b/src/model/textproxy.js @@ -173,14 +173,15 @@ export default class TextProxy { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `model:` string, - * for example `model:textProxy`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given. * - * Acceptable type for this class is `textProxy` and its prefixed version. + * textProxy.is( 'textProxy' ); // -> true + * textProxy.is( 'model:textProxy' ); // -> true * - * See also: {@link module:engine/model/node~Node#is `Node#is()`}. + * textProxy.is( 'view:textProxy' ); // -> false + * textProxy.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} From 45c4c87b91bfc6adebb935c392e6d8a06ab7f7f6 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 13 Aug 2019 10:49:17 +0200 Subject: [PATCH 29/30] Improve docs for views classes. --- src/view/attributeelement.js | 26 ++++++++++++++- src/view/containerelement.js | 26 ++++++++++++++- src/view/documentfragment.js | 14 ++++---- src/view/documentselection.js | 16 +++++---- src/view/editableelement.js | 26 ++++++++++++++- src/view/element.js | 32 +++++++++--------- src/view/emptyelement.js | 26 ++++++++++++++- src/view/node.js | 57 +++++++++++++++++++++------------ src/view/position.js | 14 ++++---- src/view/range.js | 14 ++++---- src/view/rooteditableelement.js | 28 +++++++++++++++- src/view/selection.js | 20 ++++-------- src/view/text.js | 16 ++++++++- src/view/textproxy.js | 14 ++++---- src/view/uielement.js | 26 ++++++++++++++- 15 files changed, 268 insertions(+), 87 deletions(-) diff --git a/src/view/attributeelement.js b/src/view/attributeelement.js index 9fa77c815..b6f7f95b4 100644 --- a/src/view/attributeelement.js +++ b/src/view/attributeelement.js @@ -125,7 +125,31 @@ export default class AttributeElement extends Element { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * attributeElement.is( 'attributeElement' ); // -> true + * attributeElement.is( 'element' ); // -> true + * attributeElement.is( 'node' ); // -> true + * attributeElement.is( 'view:attributeElement' ); // -> true + * attributeElement.is( 'view:element' ); // -> true + * attributeElement.is( 'view:node' ); // -> true + * + * attributeElement.is( 'model:element' ); // -> false + * attributeElement.is( 'documentFragment' ); // -> false + * + * Assuming that the object being checked is an attribute element, you can also check its + * {@link module:engine/view/attributeelement~AttributeElement#name name}: + * + * attributeElement.is( 'b' ); // -> true if this is a bold element + * attributeElement.is( 'attributeElement', 'b' ); // -> same as above + * text.is( 'b' ); -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view 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 && type.replace( /^view:/, '' ); diff --git a/src/view/containerelement.js b/src/view/containerelement.js index 1d1d8ff31..cf929fa2c 100644 --- a/src/view/containerelement.js +++ b/src/view/containerelement.js @@ -51,7 +51,31 @@ export default class ContainerElement extends Element { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * containerElement.is( 'containerElement' ); // -> true + * containerElement.is( 'element' ); // -> true + * containerElement.is( 'node' ); // -> true + * containerElement.is( 'view:containerElement' ); // -> true + * containerElement.is( 'view:element' ); // -> true + * containerElement.is( 'view:node' ); // -> true + * + * containerElement.is( 'model:element' ); // -> false + * containerElement.is( 'documentFragment' ); // -> false + * + * Assuming that the object being checked is a container element, you can also check its + * {@link module:engine/view/containerelement~ContainerElement#name name}: + * + * containerElement.is( 'div' ); // -> true if this is a div container element + * containerElement.is( 'contaienrElement', 'div' ); // -> same as above + * text.is( 'div' ); -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view 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 && type.replace( /^view:/, '' ); diff --git a/src/view/documentfragment.js b/src/view/documentfragment.js index 7b469c248..bbb7044a3 100644 --- a/src/view/documentfragment.js +++ b/src/view/documentfragment.js @@ -94,14 +94,16 @@ export default class DocumentFragment { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, - * for example `view:documentFragment`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given type. * - * Acceptable type for this class is `documentFragment` and its prefixed version. + * docFrag.is( 'documentFragment' ); // -> true + * docFrag.is( 'view:documentFragment' ); // -> true * - * See also: {@link module:engine/view/node~Node#is `Node#is()`}. + * docFrag.is( 'model:documentFragment' ); // -> false + * docFrag.is( 'element' ); // -> false + * docFrag.is( 'node' ); // -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method. * * @param {String} type * @returns {Boolean} diff --git a/src/view/documentselection.js b/src/view/documentselection.js index 290a9c618..c0d1d29fd 100644 --- a/src/view/documentselection.js +++ b/src/view/documentselection.js @@ -275,14 +275,18 @@ export default class DocumentSelection { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, - * for example `view:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given type. * - * Acceptable types for this class are `selection` and `documentSelection` and its prefixed version. + * docSelection.is( 'selection' ); // -> true + * docSelection.is( 'documentSelection' ); // -> true + * docSelection.is( 'view:selection' ); // -> true + * docSelection.is( 'view:documentSelection' ); // -> true * - * See also: {@link module:engine/view/node~Node#is `Node#is()`}. + * docSelection.is( 'model:documentSelection' ); // -> false + * docSelection.is( 'element' ); // -> false + * docSelection.is( 'node' ); // -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method. * * @param {String} type * @returns {Boolean} diff --git a/src/view/editableelement.js b/src/view/editableelement.js index 5c7323486..5d31754d3 100644 --- a/src/view/editableelement.js +++ b/src/view/editableelement.js @@ -67,7 +67,31 @@ export default class EditableElement extends ContainerElement { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * editableElement.is( 'editableElement' ); // -> true + * editableElement.is( 'element' ); // -> true + * editableElement.is( 'node' ); // -> true + * editableElement.is( 'view:editableElement' ); // -> true + * editableElement.is( 'view:element' ); // -> true + * editableElement.is( 'view:node' ); // -> true + * + * editableElement.is( 'model:element' ); // -> false + * editableElement.is( 'documentFragment' ); // -> false + * + * Assuming that the object being checked is an editbale element, you can also check its + * {@link module:engine/view/editableelement~EditableElement#name name}: + * + * editableElement.is( 'div' ); // -> true if this is a div element + * editableElement.is( 'editableElement', 'div' ); // -> same as above + * text.is( 'div' ); -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view 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 && type.replace( /^view:/, '' ); diff --git a/src/view/element.js b/src/view/element.js index 113842048..d31d70ff7 100644 --- a/src/view/element.js +++ b/src/view/element.js @@ -147,27 +147,27 @@ export default class Element extends Node { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, - * for example `view:element`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given. * - * There is also possibility to check element's {@link #name} rather than just type. Name might be provided as second attribute - * or might replace the type. + * element.is( 'element' ); // -> true + * element.is( 'node' ); // -> true + * element.is( 'view:element' ); // -> true + * element.is( 'view:node' ); // -> true * - * // obj is a `li` element - * obj.is( 'element' ); // true - * obj.is( 'view:element' ); // true - * obj.is( 'li' ); // true - * obj.is( 'element', 'li' ); // true - * obj.is( 'text' ); // false - * obj.is( 'element', 'img' ); // false + * element.is( 'model:element' ); // -> false + * element.is( 'documentSelection' ); // -> false * - * Acceptable type for this class is `element` and its prefixed version, element's name or combination of both arguments. + * Assuming that the object being checked is an element, you can also check its + * {@link module:engine/view/element~Element#name name}: * - * See also: {@link module:engine/view/node~Node#is `Node#is()`}. + * element.is( 'img' ); // -> true if this is an element + * element.is( 'element', 'img' ); // -> same as above + * text.is( 'img' ); -> false * - * @param {String} type + * {@link module:engine/view/node~Node#is Check the entire list of view 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} */ diff --git a/src/view/emptyelement.js b/src/view/emptyelement.js index 2cb2603db..812e028e4 100644 --- a/src/view/emptyelement.js +++ b/src/view/emptyelement.js @@ -44,7 +44,31 @@ export default class EmptyElement extends Element { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * emptyElement.is( 'emptyElement' ); // -> true + * emptyElement.is( 'element' ); // -> true + * emptyElement.is( 'node' ); // -> true + * emptyElement.is( 'view:emptyElement' ); // -> true + * emptyElement.is( 'view:element' ); // -> true + * emptyElement.is( 'view:node' ); // -> true + * + * emptyElement.is( 'model:element' ); // -> false + * emptyElement.is( 'documentFragment' ); // -> false + * + * Assuming that the object being checked is an empty element, you can also check its + * {@link module:engine/view/emptyelement~EmptyElement#name name}: + * + * emptyElement.is( 'img' ); // -> true if this is a img element + * emptyElement.is( 'emptyElement', 'img' ); // -> same as above + * text.is( 'img' ); -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view 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( /^view:/, '' ); diff --git a/src/view/node.js b/src/view/node.js index 71e8ebe85..9458d4e07 100644 --- a/src/view/node.js +++ b/src/view/node.js @@ -290,32 +290,47 @@ export default class Node { } /** - * Checks whether this view object is of the given type. + * Checks whether this object is of the given type. * - * This method is useful when processing view tree objects that are of unknown type. For example, a function - * may return {@link module:engine/view/documentfragment~DocumentFragment} or {@link module:engine/view/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 `view:` to narrow search exclusively to view's objects. - * That should prevent of situation where `model:node` accidentally might be considered as `view:node`. - * Types are defined as name of the class written in [camelCase](https://en.wikipedia.org/wiki/Camel_case) notation. - * E.g. class `RootEditableElement` will get type `rootEditableElement`. + * This method is useful when processing view objects that are of unknown type. For example, a function + * may return a {@link module:engine/view/documentfragment~DocumentFragment} or a {@link module:engine/view/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. * - * obj.is( 'node' ); // true for any node, false for document fragment and text fragment - * obj.is( 'view: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( 'view: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( 'view:element' ); // true for any element, false for text node or document fragment - * obj.is( 'element', 'p' ); // true only for element which name is 'p' - * obj.is( 'view:element', 'p' ); // true only for element which name is 'p' - * obj.is( 'p' ); // shortcut for obj.is( 'element', 'p' ) - * obj.is( 'view:p' ); // shortcut for obj.is( 'view:element', 'p' ) - * obj.is( 'text' ); // true for text node, false for element and document fragment - * obj.is( 'view:text' ); // true for text node, false for element and document fragment + * 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 model 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: * - * Acceptable types for this class is `node` and its prefixed version. + * viewElement.is( 'view:element' ); // -> true + * viewElement.is( 'model:element' ); // -> false * + * By using this method it is also possible to check a name of an element: + * + * imgElement.is( 'img' ); // -> true + * imgElement.is( 'element', 'img' ); // -> same as above + * imgElement.is( 'view:element', 'img' ); // -> same as above, but more precise + * + * The list of view objects which implement the `is()` method: + * + * * {@link module:engine/view/attributeelement~AttributeElement#is `AttributeElement#is()`} + * * {@link module:engine/view/containerelement~ContainerElement#is `ContainerElement#is()`} + * * {@link module:engine/view/documentfragment~DocumentFragment#is `DocumentFragment#is()`} + * * {@link module:engine/view/documentselection~DocumentSelection#is `DocumentSelection#is()`} + * * {@link module:engine/view/editableelement~EditableElement#is `EditableElement#is()`} + * * {@link module:engine/view/element~Element#is `Element#is()`} + * * {@link module:engine/view/emptyelement~EmptyElement#is `EmptyElement#is()`} + * * {@link module:engine/view/node~Node#is `Node#is()`} + * * {@link module:engine/view/position~Position#is `Position#is()`} + * * {@link module:engine/view/range~Range#is `Range#is()`} + * * {@link module:engine/view/rooteditableelement~RootEditableElement#is `RootEditableElement#is()`} + * * {@link module:engine/view/selection~Selection#is `Selection#is()`} + * * {@link module:engine/view/text~Text#is `Text#is()`} + * * {@link module:engine/view/textproxy~TextProxy#is `TextProxy#is()`} + * * {@link module:engine/view/uielement~UIElement#is `UIElement#is()`} + * + * @method #is * @param {String} type * @returns {Boolean} */ diff --git a/src/view/position.js b/src/view/position.js index ca7ee7fce..f9f5ebba4 100644 --- a/src/view/position.js +++ b/src/view/position.js @@ -207,14 +207,16 @@ export default class Position { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, - * for example `view:position`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given type. * - * Acceptable type for this class is `position` and its prefixed version. + * position.is( 'position' ); // -> true + * position.is( 'view:position' ); // -> true * - * See also: {@link module:engine/view/node~Node#is `Node#is()`}. + * position.is( 'model:position' ); // -> false + * position.is( 'element' ); // -> false + * position.is( 'range' ); // -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method. * * @param {String} type * @returns {Boolean} diff --git a/src/view/range.js b/src/view/range.js index 37308709c..f84919f6a 100644 --- a/src/view/range.js +++ b/src/view/range.js @@ -395,14 +395,16 @@ export default class Range { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, - * for example `view:range`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given type. * - * Acceptable type for this class is `range` and its prefixed version. + * range.is( 'range' ); // -> true + * range.is( 'view:range' ); // -> true * - * See also: {@link module:engine/view/node~Node#is `Node#is()`}. + * range.is( 'model:range' ); // -> false + * range.is( 'element' ); // -> false + * range.is( 'selection' ); // -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method. * * @param {String} type * @returns {Boolean} diff --git a/src/view/rooteditableelement.js b/src/view/rooteditableelement.js index c4de2e411..ad410e0cc 100644 --- a/src/view/rooteditableelement.js +++ b/src/view/rooteditableelement.js @@ -38,7 +38,33 @@ export default class RootEditableElement extends EditableElement { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * rootEditableElement.is( 'rootEditableElement' ); // -> true + * rootEditableElement.is( 'editableElement' ); // -> true + * rootEditableElement.is( 'element' ); // -> true + * rootEditableElement.is( 'node' ); // -> true + * rootEditableElement.is( 'view:rootEditableElement' ); // -> true + * rootEditableElement.is( 'view:editableElement' ); // -> true + * rootEditableElement.is( 'view:element' ); // -> true + * rootEditableElement.is( 'view:node' ); // -> true + * + * rootEditableElement.is( 'model:element' ); // -> false + * rootEditableElement.is( 'documentFragment' ); // -> false + * + * Assuming that the object being checked is a root editbale element, you can also check its + * {@link module:engine/view/rooteditableelement~RootEditableElement#name name}: + * + * rootEditableElement.is( 'div' ); // -> true if this is a div root editable element + * rootEditableElement.is( 'rootEditableElement', 'div' ); // -> same as above + * text.is( 'div' ); -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view 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( /^view:/, '' ); diff --git a/src/view/selection.js b/src/view/selection.js index 2c267ffc7..4d79a3ed1 100644 --- a/src/view/selection.js +++ b/src/view/selection.js @@ -598,22 +598,16 @@ export default class Selection { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, - * for example `view:selection`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given type. * - * const selection = new Selection( ... ); + * selection.is( 'selection' ); // -> true + * selection.is( 'view:selection' ); // -> true * - * selection.is( 'selection' ); // true - * selection.is( 'view:selection' ); // true - * selection.is( 'node' ); // false - * selection.is( 'element' ); // false + * selection.is( 'model:selection' ); // -> false + * selection.is( 'element' ); // -> false + * selection.is( 'range' ); // -> false * - * Acceptable type for this class is `selection` and its prefixed version. - * - * See also: {@link module:engine/view/node~Node#is `Node#is()`}. - + * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method. * * @param {String} type * @returns {Boolean} diff --git a/src/view/text.js b/src/view/text.js index 906fe9c22..615641d45 100644 --- a/src/view/text.js +++ b/src/view/text.js @@ -42,7 +42,21 @@ export default class Text extends Node { } /** - * @inheritDoc + * Checks whether this object is of the given type. + * + * text.is( 'text' ); // -> true + * text.is( 'node' ); // -> true + * text.is( 'view:text' ); // -> true + * text.is( 'view:node' ); // -> true + * + * text.is( 'model:text' ); // -> false + * text.is( 'element' ); // -> false + * text.is( 'range' ); // -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method. + * + * @param {String} type + * @returns {Boolean} */ is( type ) { return type == 'text' || type == 'view:text' || super.is( type ); diff --git a/src/view/textproxy.js b/src/view/textproxy.js index a30ac01e4..b8aef619d 100644 --- a/src/view/textproxy.js +++ b/src/view/textproxy.js @@ -141,14 +141,16 @@ export default class TextProxy { } /** - * Method verifies if the checked object belongs to a given a type. Type's name might be prefixed with a `view:` string, - * for example `view:textProxy`. Type is a string which usually equal to a name of the class written in camelCase convention. - * If the object is a class instance, which has a parent class with `is()` method, then type verification returns `true` - * for any type match for an entire child-parent chain. + * Checks whether this object is of the given type. * - * Acceptable type for this class is `textProxy` and its prefixed version. + * textProxy.is( 'textProxy' ); // -> true + * textProxy.is( 'view:textProxy' ); // -> true * - * See also: {@link module:engine/view/node~Node#is `Node#is()`}. + * textProxy.is( 'model:textProxy' ); // -> false + * textProxy.is( 'element' ); // -> false + * textProxy.is( 'range' ); // -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method. * * @param {String} type * @returns {Boolean} diff --git a/src/view/uielement.js b/src/view/uielement.js index bdae99965..b85d441c5 100644 --- a/src/view/uielement.js +++ b/src/view/uielement.js @@ -57,7 +57,31 @@ export default class UIElement extends Element { } /** - * @inheritDoc + * Checks whether this object is of the given. + * + * uiElement.is( 'uiElement' ); // -> true + * uiElement.is( 'element' ); // -> true + * uiElement.is( 'node' ); // -> true + * uiElement.is( 'view:uiElement' ); // -> true + * uiElement.is( 'view:element' ); // -> true + * uiElement.is( 'view:node' ); // -> true + * + * uiElement.is( 'model:element' ); // -> false + * uiElement.is( 'documentFragment' ); // -> false + * + * Assuming that the object being checked is an ui element, you can also check its + * {@link module:engine/view/uielement~UIElement#name name}: + * + * uiElement.is( 'span' ); // -> true if this is a span ui element + * uiElement.is( 'uiElement', 'span' ); // -> same as above + * text.is( 'span' ); -> false + * + * {@link module:engine/view/node~Node#is Check the entire list of view 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( /^view:/, '' ); From 78e28c26b74562e2861b90d982412e90fa4ba5d7 Mon Sep 17 00:00:00 2001 From: Mateusz Samsel Date: Tue, 13 Aug 2019 11:29:09 +0200 Subject: [PATCH 30/30] Minor changes in samples for is() method. --- src/model/element.js | 1 + src/model/rootelement.js | 5 +++-- src/model/text.js | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/model/element.js b/src/model/element.js index 4d6d1f928..13fbb1795 100644 --- a/src/model/element.js +++ b/src/model/element.js @@ -94,6 +94,7 @@ export default class Element extends Node { * element.is( 'element' ); // -> true * element.is( 'node' ); // -> true * element.is( 'model:element' ); // -> true + * element.is( 'model:node' ); // -> true * * element.is( 'view:element' ); // -> false * element.is( 'documentSelection' ); // -> false diff --git a/src/model/rootelement.js b/src/model/rootelement.js index 06aae94fd..5d97fe006 100644 --- a/src/model/rootelement.js +++ b/src/model/rootelement.js @@ -58,10 +58,11 @@ export default class RootElement extends Element { * Checks whether this object is of the given. * * rootElement.is( 'rootElement' ); // -> true - * rootElement.is( 'model: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 @@ -70,7 +71,7 @@ export default class RootElement extends Element { * {@link module:engine/model/element~Element#name name}: * * rootElement.is( '$root' ); // -> true if this is a $root element - * rootElement.is( 'element', '$root' ); // -> same as above + * 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. diff --git a/src/model/text.js b/src/model/text.js index 5d07be4f7..2093c5ec9 100644 --- a/src/model/text.js +++ b/src/model/text.js @@ -68,6 +68,7 @@ export default class Text extends Node { * text.is( 'text' ); // -> true * text.is( 'node' ); // -> true * text.is( 'model:text' ); // -> true + * text.is( 'model:node' ); // -> true * * text.is( 'view:text' ); // -> false * text.is( 'documentSelection' ); // -> false @@ -76,7 +77,6 @@ export default class Text extends Node { * * @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 ) {