浏览代码

Changed: use `is()` method in engine.

Szymon Cofalik 8 年之前
父节点
当前提交
b3f9143763

+ 4 - 5
packages/ckeditor5-engine/src/conversion/mapper.js

@@ -13,7 +13,6 @@ import ModelRange from '../model/range';
 import ViewPosition from '../view/position';
 import ViewRange from '../view/range';
 import ViewText from '../view/text';
-import ViewUIElement from '../view/uielement';
 
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
@@ -285,7 +284,7 @@ export default class Mapper {
 		// viewBlock == viewParent, so we need to calculate the offset in the parent element.
 
 		// If the position is a text it is simple ("ba|r" -> 2).
-		if ( viewParent instanceof ViewText ) {
+		if ( viewParent.is( 'text' ) ) {
 			return viewOffset;
 		}
 
@@ -340,9 +339,9 @@ export default class Mapper {
 			return callback( viewNode );
 		} else if ( this._viewToModelMapping.has( viewNode ) ) {
 			return 1;
-		} else if ( viewNode instanceof ViewText ) {
+		} else if ( viewNode.is( 'text' ) ) {
 			return viewNode.data.length;
-		} else if ( viewNode instanceof ViewUIElement ) {
+		} else if ( viewNode.is( 'uiElement' ) ) {
 			return 0;
 		} else {
 			let len = 0;
@@ -390,7 +389,7 @@ export default class Mapper {
 		let viewOffset = 0;
 
 		// In the text node it is simple: offset in the model equals offset in the text.
-		if ( viewParent instanceof ViewText ) {
+		if ( viewParent.is( 'text' ) ) {
 			return new ViewPosition( viewParent, expectedOffset );
 		}
 

+ 9 - 12
packages/ckeditor5-engine/src/conversion/viewconsumable.js

@@ -9,9 +9,6 @@
 
 import isArray from '@ckeditor/ckeditor5-utils/src/lib/lodash/isArray';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
-import ViewElement from '../view/element';
-import ViewText from '../view/text';
-import ViewDocumentFragment from '../view/documentfragment';
 
 /**
  * Class used for handling consumption of view {@link module:engine/view/element~Element elements},
@@ -83,7 +80,7 @@ export default class ViewConsumable {
 		let elementConsumables;
 
 		// For text nodes and document fragments just mark them as consumable.
-		if ( element instanceof ViewText || element instanceof ViewDocumentFragment ) {
+		if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
 			this._consumables.set( element, true );
 
 			return;
@@ -137,7 +134,7 @@ export default class ViewConsumable {
 		}
 
 		// For text nodes and document fragments return stored boolean value.
-		if ( element instanceof ViewText || element instanceof ViewDocumentFragment ) {
+		if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
 			return elementConsumables;
 		}
 
@@ -175,7 +172,7 @@ export default class ViewConsumable {
 	 */
 	consume( element, consumables ) {
 		if ( this.test( element, consumables ) ) {
-			if ( element instanceof ViewText || element instanceof ViewDocumentFragment ) {
+			if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
 				// For text nodes and document fragments set value to false.
 				this._consumables.set( element, false );
 			} else {
@@ -221,7 +218,7 @@ export default class ViewConsumable {
 		const elementConsumables = this._consumables.get( element );
 
 		if ( elementConsumables !== undefined ) {
-			if ( element instanceof ViewText || element instanceof ViewDocumentFragment ) {
+			if ( element.is( 'text' ) || element.is( 'documentFragment' ) ) {
 				// For text nodes and document fragments - set consumable to true.
 				this._consumables.set( element, true );
 			} else {
@@ -275,11 +272,11 @@ export default class ViewConsumable {
 
 	/**
 	 * Creates {@link module:engine/conversion/viewconsumable~ViewConsumable ViewConsumable} instance from
-	 * {@link module:engine/view/element~Element element} or {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
+	 * {@link module:engine/view/node~Node node} or {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
 	 * Instance will contain all elements, child nodes, attributes, styles and classes added for consumption.
 	 *
 	 * @static
-	 * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} from View element or document fragment
+	 * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} from View node or document fragment
 	 * from which `ViewConsumable` will be created.
 	 * @param {module:engine/conversion/viewconsumable~ViewConsumable} [instance] If provided, given `ViewConsumable` instance will be used
 	 * to add all consumables. It will be returned instead of a new instance.
@@ -289,18 +286,18 @@ export default class ViewConsumable {
 			instance = new ViewConsumable();
 		}
 
-		if ( from instanceof ViewText ) {
+		if ( from.is( 'text' ) ) {
 			instance.add( from );
 
 			return instance;
 		}
 
 		// Add `from` itself, if it is an element.
-		if ( from instanceof ViewElement ) {
+		if ( from.is( 'element' ) ) {
 			instance.add( from, ViewConsumable.consumablesFromElement( from ) );
 		}
 
-		if ( from instanceof ViewDocumentFragment ) {
+		if ( from.is( 'documentFragment' ) ) {
 			instance.add( from );
 		}
 

+ 2 - 4
packages/ckeditor5-engine/src/conversion/viewconversiondispatcher.js

@@ -8,8 +8,6 @@
  */
 
 import ViewConsumable from './viewconsumable';
-import ViewElement from '../view/element';
-import ViewText from '../view/text';
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
 import mix from '@ckeditor/ckeditor5-utils/src/mix';
 import extend from '@ckeditor/ckeditor5-utils/src/lib/lodash/extend';
@@ -150,9 +148,9 @@ export default class ViewConversionDispatcher {
 			output: null
 		} );
 
-		if ( input instanceof ViewElement ) {
+		if ( input.is( 'element' ) ) {
 			this.fire( 'element:' + input.name, data, consumable, this.conversionApi );
-		} else if ( input instanceof ViewText ) {
+		} else if ( input.is( 'text' ) ) {
 			this.fire( 'text', data, consumable, this.conversionApi );
 		} else {
 			this.fire( 'documentFragment', data, consumable, this.conversionApi );

+ 2 - 3
packages/ckeditor5-engine/src/dev-utils/enableenginedebug.js

@@ -38,7 +38,6 @@ import ModelRootElement from '../model/rootelement';
 
 import ViewDocument from '../view/document';
 import ViewElement from '../view/element';
-import ViewText from '../view/text';
 import ViewDocumentFragment from '../view/documentfragment';
 
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
@@ -369,7 +368,7 @@ function enableLoggingTools() {
 		string += '\t'.repeat( level ) + `<${ this.name }${ mapToTags( this.getAttributes() ) }>`;
 
 		for ( let child of this.getChildren() ) {
-			if ( child instanceof ViewText ) {
+			if ( child.is( 'text' ) ) {
 				string += '\n' + '\t'.repeat( level + 1 ) + child.data;
 			} else {
 				string += '\n' + child.printTree( level + 1 );
@@ -393,7 +392,7 @@ function enableLoggingTools() {
 		let string = 'ViewDocumentFragment: [';
 
 		for ( let child of this.getChildren() ) {
-			if ( child instanceof ViewText ) {
+			if ( child.is( 'text' ) ) {
 				string += '\n' + '\t'.repeat( 1 ) + child.data;
 			} else {
 				string += '\n' + child.printTree( 1 );

+ 13 - 17
packages/ckeditor5-engine/src/dev-utils/view.js

@@ -22,7 +22,6 @@ import AttributeElement from '../view/attributeelement';
 import ContainerElement from '../view/containerelement';
 import EmptyElement from '../view/emptyelement';
 import UIElement from '../view/uielement';
-import ViewText from '../view/text';
 
 const ELEMENT_RANGE_START_TOKEN = '[';
 const ELEMENT_RANGE_END_TOKEN = ']';
@@ -327,7 +326,7 @@ export function parse( data, options = {} ) {
 	const ranges = rangeParser.parse( view, options.order );
 
 	// If only one element is returned inside DocumentFragment - return that element.
-	if ( view instanceof ViewDocumentFragment && view.childCount === 1 ) {
+	if ( view.is( 'documentFragment' ) && view.childCount === 1 ) {
 		view = view.getChild( 0 );
 	}
 
@@ -410,7 +409,7 @@ class RangeParser {
 	 * @param {module:engine/view/node~Node} node Staring node.
 	 */
 	_getPositions( node ) {
-		if ( node instanceof ViewDocumentFragment || node instanceof ViewElement ) {
+		if ( node.is( 'documentFragment' ) || node.is( 'element' ) ) {
 			// Copy elements into the array, when nodes will be removed from parent node this array will still have all the
 			// items needed for iteration.
 			const children = [ ...node.getChildren() ];
@@ -420,7 +419,7 @@ class RangeParser {
 			}
 		}
 
-		if ( node instanceof ViewText ) {
+		if ( node.is( 'text' ) ) {
 			const regexp = new RegExp(
 				`[${ TEXT_RANGE_START_TOKEN }${ TEXT_RANGE_END_TOKEN }\\${ ELEMENT_RANGE_END_TOKEN }\\${ ELEMENT_RANGE_START_TOKEN }]`,
 				'g'
@@ -619,11 +618,10 @@ class ViewStringify {
 	 * @param {Function} callback
 	 */
 	_walkView( root, callback ) {
-		const isElement = root instanceof ViewElement;
 		const ignore = this.ignoreRoot && this.root === root;
 
-		if ( isElement || root instanceof ViewDocumentFragment ) {
-			if ( isElement && !ignore ) {
+		if ( root.is( 'element' ) || root.is( 'documentFragment' ) ) {
+			if ( root.is( 'element' ) && !ignore ) {
 				callback( this._stringifyElementOpen( root ) );
 			}
 
@@ -636,12 +634,12 @@ class ViewStringify {
 				callback( this._stringifyElementRanges( root, offset ) );
 			}
 
-			if ( isElement && !ignore ) {
+			if ( root.is( 'element' ) && !ignore ) {
 				callback( this._stringifyElementClose( root ) );
 			}
 		}
 
-		if ( root instanceof ViewText ) {
+		if ( root.is( 'text' ) ) {
 			callback( this._stringifyTextRanges( root ) );
 		}
 	}
@@ -804,7 +802,7 @@ class ViewStringify {
 	 * @returns {String}
 	 */
 	_stringifyElementPriority( element ) {
-		if ( this.showPriority && element instanceof AttributeElement ) {
+		if ( this.showPriority && element.is( 'attributeElement' ) ) {
 			return `view-priority="${ element.priority }"`;
 		}
 
@@ -844,19 +842,17 @@ class ViewStringify {
 // @returns {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment|
 // module:engine/view/text~Text} Root node of converted elements.
 function _convertViewElements( rootNode ) {
-	const isFragment = rootNode instanceof ViewDocumentFragment;
-
-	if ( rootNode instanceof ViewElement || isFragment ) {
+	if ( rootNode.is( 'element' ) || rootNode.is( 'documentFragment' ) ) {
 		// Convert element or leave document fragment.
-		const convertedElement = isFragment ? new ViewDocumentFragment() : _convertElement( rootNode );
+		const convertedElement = rootNode.is( 'documentFragment' ) ? new ViewDocumentFragment() : _convertElement( rootNode );
 
 		// Convert all child nodes.
 		for ( let child of rootNode.getChildren() ) {
-			if ( convertedElement instanceof EmptyElement ) {
+			if ( convertedElement.is( 'emptyElement' ) ) {
 				throw new Error( `Parse error - cannot parse inside EmptyElement.` );
 			}
 
-			if ( convertedElement instanceof UIElement ) {
+			if ( convertedElement.is( 'uiElement' ) ) {
 				throw new Error( `Parse error - cannot parse inside UIElement.` );
 			}
 
@@ -895,7 +891,7 @@ function _convertElement( viewElement ) {
 	const ElementConstructor = allowedTypes[ info.type ];
 	const newElement = ElementConstructor ? new ElementConstructor( info.name ) : new ViewElement( info.name );
 
-	if ( newElement instanceof AttributeElement ) {
+	if ( newElement.is( 'attributeElement' ) ) {
 		if ( info.priority !== null ) {
 			newElement.priority = info.priority;
 		}