Browse Source

Changed: Use `is()` method.

Szymon Cofalik 9 năm trước cách đây
mục cha
commit
30fcbb548e

+ 1 - 2
packages/ckeditor5-engine/src/controller/getselectedcontent.js

@@ -10,7 +10,6 @@
 import DocumentFragment from '../model/documentfragment';
 import Range from '../model/range';
 import Position from '../model/position';
-import TextProxy from '../model/textproxy';
 import Text from '../model/text';
 import { remove } from '../model/writer';
 
@@ -69,7 +68,7 @@ export default function getSelectedContent( selection ) {
 
 	// Clone the whole contents.
 	for ( const item of flatSubtreeRange.getItems( { shallow: true } ) ) {
-		if ( item instanceof TextProxy ) {
+		if ( item.is( 'textProxy' ) ) {
 			frag.appendChildren( new Text( item.data, item.getAttributes() ) );
 		} else {
 			frag.appendChildren( item.clone( true ) );

+ 2 - 3
packages/ckeditor5-engine/src/controller/insertcontent.js

@@ -9,7 +9,6 @@
 
 import Position from '../model/position';
 import LivePosition from '../model/liveposition';
-import Text from '../model/text';
 import Element from '../model/element';
 import Range from '../model/range';
 import log from '@ckeditor/ckeditor5-utils/src/log';
@@ -218,7 +217,7 @@ class Insertion {
 	 */
 	_handleDisallowedNode( node, context ) {
 		// Try inserting its children (strip the parent).
-		if ( node instanceof Element ) {
+		if ( node.is( 'element' ) ) {
 			this.handleNodes( node.getChildren(), context );
 		}
 		// Try autoparagraphing.
@@ -413,7 +412,7 @@ class Insertion {
 	 * @param {module:engine/model/node~Node} node The node.
 	 */
 	_getNodeSchemaName( node ) {
-		if ( node instanceof Text ) {
+		if ( node.is( 'text' ) ) {
 			return '$text';
 		}
 

+ 1 - 2
packages/ckeditor5-engine/src/conversion/modelconversiondispatcher.js

@@ -9,7 +9,6 @@
 
 import Consumable from './modelconsumable';
 import Range from '../model/range';
-import TextProxy from '../model/textproxy';
 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';
@@ -463,7 +462,7 @@ export default class ModelConversionDispatcher {
 		}
 
 		if ( type === 'insert' || type === 'remove' || type == 'move' ) {
-			if ( data.item instanceof TextProxy ) {
+			if ( data.item.is( 'textProxy' ) ) {
 				// Example: insert:$text.
 				this.fire( type + ':$text', data, consumable, this.conversionApi );
 			} else {

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

@@ -167,7 +167,7 @@ function enableLoggingTools() {
 		for ( let child of this.getChildren() ) {
 			string += '\n';
 
-			if ( child instanceof ModelText ) {
+			if ( child.is( 'text' ) ) {
 				const textAttrs = mapToTags( child._attrs );
 
 				string += '\t'.repeat( level + 1 );
@@ -217,7 +217,7 @@ function enableLoggingTools() {
 		for ( let child of this.getChildren() ) {
 			string += '\n';
 
-			if ( child instanceof ModelText ) {
+			if ( child.is( 'text' ) ) {
 				const textAttrs = mapToTags( child._attrs );
 
 				string += '\t'.repeat( 1 );

+ 4 - 5
packages/ckeditor5-engine/src/dev-utils/model.js

@@ -20,7 +20,6 @@ import ModelSelection from '../model/selection';
 import ModelDocumentFragment from '../model/documentfragment';
 import ModelElement from '../model/element';
 import ModelText from '../model/text';
-import ModelTextProxy from '../model/textproxy';
 import modelWriter from '../model/writer';
 
 import ViewConversionDispatcher from '../conversion/viewconversiondispatcher';
@@ -132,13 +131,13 @@ export function setData( document, data, options = {} ) {
 				// Each range returned from `parse()` method has its root placed in DocumentFragment.
 				// Here we convert each range to have its root re-calculated properly and be placed inside
 				// model document root.
-				if ( range.start.parent instanceof ModelDocumentFragment ) {
+				if ( range.start.parent.is( 'documentFragment' ) ) {
 					start = ModelPosition.createFromParentAndOffset( modelRoot, range.start.offset );
 				} else {
 					start = ModelPosition.createFromParentAndOffset( range.start.parent, range.start.offset );
 				}
 
-				if ( range.end.parent instanceof ModelDocumentFragment ) {
+				if ( range.end.parent.is( 'documentFragment' ) ) {
 					end = ModelPosition.createFromParentAndOffset( modelRoot, range.end.offset );
 				} else {
 					end = ModelPosition.createFromParentAndOffset( range.end.parent, range.end.offset );
@@ -216,7 +215,7 @@ export function stringify( node, selectionOrPositionOrRange = null ) {
 
 	modelToView.on( 'insert:$text', insertText() );
 	modelToView.on( 'addAttribute', wrapItem( ( value, data ) => {
-		if ( data.item instanceof ModelTextProxy ) {
+		if ( data.item.is( 'textProxy' ) ) {
 			return new ViewAttributeElement( 'model-text-with-attributes', { [ data.attributeKey ]: stringifyAttributeValue( value ) } );
 		}
 	} ) );
@@ -299,7 +298,7 @@ export function parse( data, schema, options = {} ) {
 	let model = viewToModel.convert( viewDocumentFragment.root, { context: options.context || [ '$root' ] } );
 
 	// If root DocumentFragment contains only one element - return that element.
-	if ( model instanceof ModelDocumentFragment && model.childCount == 1 ) {
+	if ( model.is( 'documentFragment' ) && model.childCount == 1 ) {
 		model = model.getChild( 0 );
 	}
 

+ 3 - 5
packages/ckeditor5-engine/src/model/delta/attributedelta.js

@@ -14,8 +14,6 @@ import AttributeOperation from '../operation/attributeoperation';
 import RootAttributeOperation from '../operation/rootattributeoperation';
 import Position from '../position';
 import Range from '../range';
-import RootElement from '../rootelement';
-import Element from '../element';
 
 /**
  * To provide specific OT behavior and better collisions solving, methods to change attributes
@@ -166,15 +164,15 @@ function changeItem( batch, doc, key, value, item ) {
 	const previousValue = item.getAttribute( key );
 	let range, operation;
 
-	const delta = item instanceof RootElement ? new RootAttributeDelta() : new AttributeDelta();
+	const delta = item.is( 'rootElement' ) ? new RootAttributeDelta() : new AttributeDelta();
 	batch.addDelta( delta );
 
 	if ( previousValue != value ) {
-		if ( item instanceof RootElement ) {
+		if ( item.is( 'rootElement' ) ) {
 			// If we change attributes of root element, we have to use `RootAttributeOperation`.
 			operation = new RootAttributeOperation( item, key, previousValue, value, doc.version );
 		} else {
-			if ( item instanceof Element ) {
+			if ( item.is( 'element' ) ) {
 				// If we change the attribute of the element, we do not want to change attributes of its children, so
 				// the end of the range cannot be after the closing tag, it should be inside that element, before any of
 				// it's children, so the range will contain only the opening tag.

+ 3 - 4
packages/ckeditor5-engine/src/model/liveposition.js

@@ -7,7 +7,6 @@
  * @module engine/model/liveposition
  */
 
-import RootElement from './rootelement';
 import Position from './position';
 import Range from './range';
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
@@ -40,7 +39,9 @@ export default class LivePosition extends Position {
 	 * See {@link module:engine/model/liveposition~LivePosition#stickiness}.
 	 */
 	constructor( root, path, stickiness ) {
-		if ( !( root instanceof RootElement ) ) {
+		super( root, path );
+
+		if ( !this.root.is( 'rootElement' ) ) {
 			/**
 			 * LivePosition root has to be an instance of RootElement.
 			 *
@@ -49,8 +50,6 @@ export default class LivePosition extends Position {
 			throw new CKEditorError( 'model-liveposition-root-not-rootelement: LivePosition root has to be an instance of RootElement.' );
 		}
 
-		super( root, path );
-
 		/**
 		 * Flag representing `LivePosition` stickiness. `LivePosition` might be sticking to previous node or next node.
 		 * Whenever some nodes are inserted at the same position as `LivePosition`, `stickiness` is checked to decide if

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

@@ -651,7 +651,7 @@ export default class LiveSelection extends Selection {
 // Helper function for {@link module:engine/model/liveselection~LiveSelection#_updateAttributes}. It takes model item, checks whether
 // it is a text node (or text proxy) and if so, returns it's attributes. If not, returns `null`.
 //
-// @param {module:engine/model/item~Item}  node
+// @param {module:engine/model/item~Item|null}  node
 // @returns {Boolean}
 function getAttrsIfCharacter( node ) {
 	if ( node instanceof TextProxy || node instanceof Text ) {

+ 3 - 5
packages/ckeditor5-engine/src/model/position.js

@@ -7,8 +7,6 @@
  * @module engine/model/position
  */
 
-import DocumentFragment from './documentfragment';
-import Element from './element';
 import TreeWalker from './treewalker';
 import last from '@ckeditor/ckeditor5-utils/src/lib/lodash/last';
 import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
@@ -45,7 +43,7 @@ export default class Position {
 	 * @param {Array.<Number>} path Position path. See {@link module:engine/model/position~Position#path}.
 	 */
 	constructor( root, path ) {
-		if ( !( root instanceof Element ) && !( root instanceof DocumentFragment ) ) {
+		if ( !root.is( 'element' ) && !root.is( 'documentFragment' ) ) {
 			/**
 			 * Position root invalid.
 			 *
@@ -293,7 +291,7 @@ export default class Position {
 	 * @returns {Array.<module:engine/model/item~Item>} Array with ancestors.
 	 */
 	getAncestors() {
-		if ( this.parent instanceof DocumentFragment ) {
+		if ( this.parent.is( 'documentFragment' ) ) {
 			return [ this.parent ];
 		} else {
 			return this.parent.getAncestors( { includeNode: true } );
@@ -707,7 +705,7 @@ export default class Position {
 	 * @returns {module:engine/model/position~Position}
 	 */
 	static createFromParentAndOffset( parent, offset ) {
-		if ( !( parent instanceof Element || parent instanceof DocumentFragment ) ) {
+		if ( !parent.is( 'element' ) && !parent.is( 'documentFragment' ) ) {
 			/**
 			 * Position parent have to be a model element or model document fragment.
 			 *

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

@@ -151,7 +151,7 @@ export function setAttribute( range, key, value ) {
 		// Iterator will return `TextProxy` instances but we know that those text proxies will
 		// always represent full text nodes (this is guaranteed thanks to splitting we did before).
 		// So, we can operate on those text proxies' text nodes.
-		let node = item instanceof TextProxy ? item.textNode : item;
+		let node = item.is( 'textProxy' ) ? item.textNode : item;
 
 		if ( value !== null ) {
 			node.setAttribute( key, value );
@@ -213,7 +213,7 @@ export function normalizeNodes( nodes ) {
 		const node = normalized[ i ];
 		const prev = normalized[ i - 1 ];
 
-		if ( node instanceof Text && prev instanceof Text && _haveSameAttributes( node, prev ) ) {
+		if ( node.is( 'text' ) && prev.is( '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--;
@@ -239,7 +239,7 @@ function _mergeNodesAtIndex( element, index ) {
 	const nodeAfter = element.getChild( index );
 
 	// Check if both of those nodes are text objects with same attributes.
-	if ( nodeBefore instanceof Text && nodeAfter instanceof Text && _haveSameAttributes( nodeBefore, nodeAfter ) ) {
+	if ( nodeBefore && nodeAfter && nodeBefore.is( 'text' ) && nodeAfter.is( 'text' ) && _haveSameAttributes( nodeBefore, nodeAfter ) ) {
 		// Append text of text node after index to the before one.
 		const mergedNode = new Text( nodeBefore.data + nodeAfter.data, nodeBefore.getAttributes() );
 

+ 2 - 2
packages/ckeditor5-engine/tests/controller/datacontroller.js

@@ -181,7 +181,7 @@ describe( 'DataController', () => {
 			buildViewConverter().for( data.viewToModel ).fromElement( 'p' ).toElement( 'paragraph' );
 		} );
 
-		it( 'should convert content of an element', () => {
+		it( 'should convert content of an element #1', () => {
 			const viewElement = parseView( '<p>foo</p>' );
 			const modelElement = data.toModel( viewElement );
 
@@ -189,7 +189,7 @@ describe( 'DataController', () => {
 			expect( stringify( modelElement ) ).to.equal( '<paragraph>foo</paragraph>' );
 		} );
 
-		it( 'should convert content of an element', () => {
+		it( 'should convert content of an element #2', () => {
 			const viewFragment = parseView( '<p>foo</p><p>bar</p>' );
 			const modelFragment = data.toModel( viewFragment );
 

+ 1 - 1
packages/ckeditor5-engine/tests/model/position.js

@@ -114,7 +114,7 @@ describe( 'position', () => {
 
 			expect( () => {
 				new Position();
-			} ).to.throw( CKEditorError, /model-position-root-invalid/ );
+			} ).to.throw();
 		} );
 	} );