Sfoglia il codice sorgente

Added: Introduced `.is( 'node' )` in view and model.

Szymon Cofalik 7 anni fa
parent
commit
4dc160cf26

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

@@ -109,7 +109,7 @@ export default class Element extends Node {
 	 */
 	is( type, name = null ) {
 		if ( !name ) {
-			return type == 'element' || type == this.name;
+			return type == 'element' || type == this.name || super.is( type );
 		} else {
 			return type == 'element' && name == this.name;
 		}

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

@@ -401,7 +401,7 @@ export default class Node {
 	 * may return {@link module:engine/model/documentfragment~DocumentFragment} or {@link module:engine/model/node~Node}
 	 * that can be either text node or element. This method can be used to check what kind of object is returned.
 	 *
-	 *		obj.is( 'node' ); // true for any node, false for document fragment
+	 *		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'
@@ -413,6 +413,9 @@ export default class Node {
 	 * @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
 	 * @returns {Boolean}
 	 */
+	is( type ) {
+		return type == 'node';
+	}
 }
 
 /**

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

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

+ 1 - 1
packages/ckeditor5-engine/src/view/element.js

@@ -152,7 +152,7 @@ export default class Element extends Node {
 	 */
 	is( type, name = null ) {
 		if ( !name ) {
-			return type == 'element' || type == this.name;
+			return type == 'element' || type == this.name || super.is( type );
 		} else {
 			return type == 'element' && name == this.name;
 		}

+ 18 - 15
packages/ckeditor5-engine/src/view/node.js

@@ -199,27 +199,13 @@ export default class Node {
 	}
 
 	/**
-	 * Clones this node.
-	 *
-	 * @method #clone
-	 * @returns {module:engine/view/node~Node} Clone of this node.
-	 */
-
-	/**
-	 * Checks if provided node is similar to this node.
-	 *
-	 * @method #isSimilar
-	 * @returns {Boolean} True if nodes are similar.
-	 */
-
-	/**
 	 * Checks whether given view tree object is of 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.
 	 *
-	 *		obj.is( 'node' ); // true for any node, false for document fragment
+	 *		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', 'p' ); // true only for element which name is 'p'
@@ -231,6 +217,23 @@ export default class Node {
 	 * 'rootElement'|'documentFragment'|'text'|'textProxy'} type
 	 * @returns {Boolean}
 	 */
+	is( type ) {
+		return type == 'node';
+	}
+
+	/**
+	 * Clones this node.
+	 *
+	 * @method #clone
+	 * @returns {module:engine/view/node~Node} Clone of this node.
+	 */
+
+	/**
+	 * Checks if provided node is similar to this node.
+	 *
+	 * @method #isSimilar
+	 * @returns {Boolean} True if nodes are similar.
+	 */
 }
 
 /**

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

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

+ 1 - 0
packages/ckeditor5-engine/tests/model/documentfragment.js

@@ -74,6 +74,7 @@ describe( 'DocumentFragment', () => {
 		} );
 
 		it( 'should return false for other accept 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;

+ 2 - 1
packages/ckeditor5-engine/tests/model/element.js

@@ -46,7 +46,8 @@ describe( 'Element', () => {
 			element = new Element( 'paragraph' );
 		} );
 
-		it( 'should return true for element, element with same name and element name', () => {
+		it( 'should return true for node, element, element with same name and element name', () => {
+			expect( element.is( 'node' ) ).to.be.true;
 			expect( element.is( 'element' ) ).to.be.true;
 			expect( element.is( 'element', 'paragraph' ) ).to.be.true;
 			expect( element.is( 'paragraph' ) ).to.be.true;

+ 6 - 0
packages/ckeditor5-engine/tests/model/node.js

@@ -164,6 +164,12 @@ describe( 'Node', () => {
 		} );
 	} );
 
+	describe( 'is()', () => {
+		it( 'should return true for node', () => {
+			expect( node.is( 'node' ) ).to.be.true;
+		} );
+	} );
+
 	describe( 'startOffset', () => {
 		it( 'should return null if the parent is null', () => {
 			expect( root.startOffset ).to.be.null;

+ 2 - 1
packages/ckeditor5-engine/tests/model/text.js

@@ -40,7 +40,8 @@ describe( 'Text', () => {
 			text = new Text( 'bar' );
 		} );
 
-		it( 'should return true for text', () => {
+		it( 'should return true for node, text', () => {
+			expect( text.is( 'node' ) ).to.be.true;
 			expect( text.is( 'text' ) ).to.be.true;
 		} );
 

+ 1 - 0
packages/ckeditor5-engine/tests/model/textproxy.js

@@ -107,6 +107,7 @@ describe( 'TextProxy', () => {
 		} );
 
 		it( 'should return false for other accept values', () => {
+			expect( textProxy.is( 'node' ) ).to.be.false;
 			expect( textProxy.is( 'text' ) ).to.be.false;
 			expect( textProxy.is( 'element' ) ).to.be.false;
 			expect( textProxy.is( 'documentFragment' ) ).to.be.false;

+ 1 - 0
packages/ckeditor5-engine/tests/view/documentfragment.js

@@ -83,6 +83,7 @@ describe( 'DocumentFragment', () => {
 		} );
 
 		it( 'should return false for other accept 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;

+ 2 - 1
packages/ckeditor5-engine/tests/view/element.js

@@ -89,7 +89,8 @@ describe( 'Element', () => {
 			el = new Element( 'p' );
 		} );
 
-		it( 'should return true for element, element with correct name and element name', () => {
+		it( 'should return true for node, element, element with correct name and element name', () => {
+			expect( el.is( 'node' ) ).to.be.true;
 			expect( el.is( 'element' ) ).to.be.true;
 			expect( el.is( 'element', 'p' ) ).to.be.true;
 			expect( el.is( 'p' ) ).to.be.true;

+ 9 - 0
packages/ckeditor5-engine/tests/view/node.js

@@ -5,6 +5,7 @@
 
 import Element from '../../src/view/element';
 import Text from '../../src/view/text';
+import Node from '../../src/view/node';
 import DocumentFragment from '../../src/view/documentfragment';
 import RootEditableElement from '../../src/view/rooteditableelement';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
@@ -29,6 +30,14 @@ describe( 'Node', () => {
 		root = new Element( null, null, [ one, two, three ] );
 	} );
 
+	describe( 'is()', () => {
+		it( 'should return true for node', () => {
+			const node = new Node();
+
+			expect( node.is( 'node' ) ).to.be.true;
+		} );
+	} );
+
 	describe( 'getNextSibling/getPreviousSibling()', () => {
 		it( 'should return next sibling', () => {
 			expect( root.nextSibling ).to.be.null;

+ 2 - 1
packages/ckeditor5-engine/tests/view/text.js

@@ -24,7 +24,8 @@ describe( 'Text', () => {
 			text = new Text( 'foo' );
 		} );
 
-		it( 'should return true for text', () => {
+		it( 'should return true for node, text', () => {
+			expect( text.is( 'node' ) ).to.be.true;
 			expect( text.is( 'text' ) ).to.be.true;
 		} );
 

+ 1 - 0
packages/ckeditor5-engine/tests/view/textproxy.js

@@ -67,6 +67,7 @@ describe( 'TextProxy', () => {
 		} );
 
 		it( 'should return false for other accept values', () => {
+			expect( textProxy.is( 'node' ) ).to.be.false;
 			expect( textProxy.is( 'text' ) ).to.be.false;
 			expect( textProxy.is( 'element' ) ).to.be.false;
 			expect( textProxy.is( 'containerElement' ) ).to.be.false;