Kaynağa Gözat

Changed: Enhancements for `is()` method in engine model.

Szymon Cofalik 9 yıl önce
ebeveyn
işleme
446a5ec4cb

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

@@ -111,7 +111,7 @@ export default class DocumentFragment {
 	 *		obj.is( 'text' ); // true for text node, false for element and document fragment
 	 *		obj.is( 'textProxy' ); // true for text proxy object
 	 *
-	 * @param {'node'|'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
+	 * @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
 	 * @returns {Boolean}
 	 */
 	is( type ) {

+ 3 - 7
packages/ckeditor5-engine/src/model/element.js

@@ -95,20 +95,16 @@ export default class Element extends Node {
 	 *		obj.is( 'text' ); // true for text node, false for element and document fragment
 	 *		obj.is( 'textProxy' ); // true for text proxy object
 	 *
-	 * @param {'node'|'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
+	 * @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
 	 * @param {String} [name] Element name.
 	 * @returns {Boolean}
 	 */
 	is( type, name = null ) {
-		let matches = false;
-
 		if ( !name ) {
-			matches = type == 'element' || type == this.name;
+			return type == 'element' || type == this.name;
 		} else {
-			matches = type == 'element' && name == this.name;
+			return type == 'element' && name == this.name;
 		}
-
-		return matches || super.is( type );
 	}
 
 	/**

+ 20 - 22
packages/ckeditor5-engine/src/model/node.js

@@ -211,28 +211,6 @@ export default class Node {
 		return this.root.document || null;
 	}
 
-	/**
-	 * Checks whether given model tree object is of given type.
-	 *
-	 * This method is useful when processing model tree objects that are of unknown type. For example, a function
-	 * may return {@link module:engine/model/documentfragment~DocumentFragment} or {@link module:engine/model/node~Node}
-	 * that can be either text node or element. This method can be used to check what kind of object is returned.
-	 *
-	 *		obj.is( 'node' ); // true for any node, false for document fragment
-	 *		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
-	 *
-	 * @param {'node'|'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
-	 * @returns {Boolean}
-	 */
-	is( type ) {
-		return type == 'node';
-	}
-
 	/**
 	 * Creates a copy of this node, that is a node with exactly same attributes, and returns it.
 	 *
@@ -389,4 +367,24 @@ export default class Node {
 
 		return json;
 	}
+
+	/**
+	 * Checks whether given model tree object is of given type.
+	 *
+	 * This method is useful when processing model tree objects that are of unknown type. For example, a function
+	 * may return {@link module:engine/model/documentfragment~DocumentFragment} or {@link module:engine/model/node~Node}
+	 * that can be either text node or element. This method can be used to check what kind of object is returned.
+	 *
+	 *		obj.is( 'node' ); // true for any node, false for document fragment
+	 *		obj.is( 'documentFragment' ); // true for document fragment, false for any node
+	 *		obj.is( 'element' ); // true for any element, false for text node or document fragment
+	 *		obj.is( 'element', 'paragraph' ); // true only for element which name is 'paragraph'
+	 *		obj.is( 'paragraph' ); // shortcut for obj.is( 'element', 'paragraph' )
+	 *		obj.is( 'text' ); // true for text node, false for element and document fragment
+	 *		obj.is( 'textProxy' ); // true for text proxy object
+	 *
+	 * @method #is
+	 * @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
+	 * @returns {Boolean}
+	 */
 }

+ 5 - 1
packages/ckeditor5-engine/src/model/rootelement.js

@@ -57,7 +57,11 @@ export default class RootElement extends Element {
 	 * @inheritDoc
 	 */
 	is( type, name ) {
-		return type == 'rootElement' || super.is( type, name );
+		if ( !name ) {
+			return type == 'rootElement' || super.is( type );
+		} else {
+			return ( type == 'rootElement' && name == this.name ) || super.is( type, name );
+		}
 	}
 
 	/**

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

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

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

@@ -186,7 +186,7 @@ export default class TextProxy {
 	 *		obj.is( 'text' ); // true for text node, false for element and document fragment
 	 *		obj.is( 'textProxy' ); // true for text proxy object
 	 *
-	 * @param {'node'|'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
+	 * @param {'element'|'rootElement'|'text'|'textProxy'|'documentFragment'} type
 	 * @returns {Boolean}
 	 */
 	is( type ) {

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

@@ -213,7 +213,7 @@ export function normalizeNodes( nodes ) {
 		const node = normalized[ i ];
 		const prev = normalized[ i - 1 ];
 
-		if ( node.is( 'text' ) && prev.is( 'text' ) && _haveSameAttributes( node, prev ) ) {
+		if ( node instanceof Text && prev instanceof Text && _haveSameAttributes( node, prev ) ) {
 			// Doing this instead changing prev.data because .data is readonly.
 			normalized.splice( i - 1, 2, new Text( prev.data + node.data, prev.getAttributes() ) );
 			i--;

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

@@ -67,7 +67,6 @@ 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;

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

@@ -45,14 +45,15 @@ describe( 'Element', () => {
 			element = new Element( 'paragraph' );
 		} );
 
-		it( 'should return true for element, element with same name, element name and node', () => {
+		it( 'should return true for element, element with same name and element name', () => {
 			expect( element.is( 'element' ) ).to.be.true;
 			expect( element.is( 'element', 'paragraph' ) ).to.be.true;
 			expect( element.is( 'paragraph' ) ).to.be.true;
-			expect( element.is( 'node' ) ).to.be.true;
 		} );
 
 		it( 'should return false for other accept values', () => {
+			expect( element.is( 'element', 'image' ) ).to.be.false;
+			expect( element.is( 'image' ) ).to.be.false;
 			expect( element.is( 'text' ) ).to.be.false;
 			expect( element.is( 'textProxy' ) ).to.be.false;
 			expect( element.is( 'documentFragment' ) ).to.be.false;

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

@@ -134,20 +134,6 @@ describe( 'Node', () => {
 		} );
 	} );
 
-	describe( 'is', () => {
-		it( 'should return true for node', () => {
-			expect( node.is( 'node' ) ).to.be.true;
-		} );
-
-		it( 'should return false for other accept values', () => {
-			expect( node.is( 'text' ) ).to.be.false;
-			expect( node.is( 'textProxy' ) ).to.be.false;
-			expect( node.is( 'element' ) ).to.be.false;
-			expect( node.is( 'rootElement' ) ).to.be.false;
-			expect( node.is( 'documentFragment' ) ).to.be.false;
-		} );
-	} );
-
 	describe( 'clone', () => {
 		it( 'should return a copy of cloned node', () => {
 			let node = new Node( { foo: 'bar' } );

+ 6 - 3
packages/ckeditor5-engine/tests/model/rootelement.js

@@ -29,15 +29,18 @@ describe( 'RootElement', () => {
 			root = new RootElement( doc, '$root' );
 		} );
 
-		it( 'should return true for rootElement, element, element with same name, element name and node', () => {
-			expect( root.is( 'element' ) ).to.be.true;
+		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( 'element' ) ).to.be.true;
 			expect( root.is( '$root' ) ).to.be.true;
+			expect( root.is( 'rootElement', '$root' ) ).to.be.true;
 			expect( root.is( 'rootElement' ) ).to.be.true;
-			expect( root.is( 'node' ) ).to.be.true;
 		} );
 
 		it( 'should return false for other accept values', () => {
+			expect( root.is( 'element', '$graveyard' ) ).to.be.false;
+			expect( root.is( '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;

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

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

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

@@ -106,7 +106,6 @@ 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;