8
0
Просмотр исходного кода

Added DOM node type checking methods to engine.view.DomConverter.

Szymon Kupś 9 лет назад
Родитель
Сommit
9ebb2485e0

+ 38 - 8
packages/ckeditor5-engine/src/view/domconverter.js

@@ -234,7 +234,7 @@ export default class DomConverter {
 
 
 			// If there is an inline filler at position return position inside the filler. We should never return
 			// If there is an inline filler at position return position inside the filler. We should never return
 			// the position before the inline filler.
 			// the position before the inline filler.
-			if ( domAfter instanceof Text && startsWithFiller( domAfter ) ) {
+			if ( this.isText( domAfter ) && startsWithFiller( domAfter ) ) {
 				return { parent: domAfter, offset: INLINE_FILLER_LENGTH };
 				return { parent: domAfter, offset: INLINE_FILLER_LENGTH };
 			}
 			}
 
 
@@ -261,7 +261,7 @@ export default class DomConverter {
 			return null;
 			return null;
 		}
 		}
 
 
-		if ( domNode instanceof Text ) {
+		if ( this.isText( domNode ) ) {
 			if ( isInlineFiller( domNode ) ) {
 			if ( isInlineFiller( domNode ) ) {
 				return null;
 				return null;
 			} else {
 			} else {
@@ -274,7 +274,7 @@ export default class DomConverter {
 
 
 			let viewElement;
 			let viewElement;
 
 
-			if ( domNode instanceof DocumentFragment ) {
+			if ( this.isDocumentFragment( domNode ) ) {
 				// Create view document fragment.
 				// Create view document fragment.
 				viewElement = new ViewDocumentFragment();
 				viewElement = new ViewDocumentFragment();
 
 
@@ -383,7 +383,7 @@ export default class DomConverter {
 			return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
 			return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
 		}
 		}
 
 
-		if ( domParent instanceof Text ) {
+		if ( this.isText( domParent ) ) {
 			if ( isInlineFiller( domParent ) ) {
 			if ( isInlineFiller( domParent ) ) {
 				return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
 				return this.domPositionToView( domParent.parentNode, indexOf( domParent ) );
 			}
 			}
@@ -435,11 +435,11 @@ export default class DomConverter {
 	 * @returns {engine.view.Node|engine.view.DocumentFragment|null} Corresponding view item.
 	 * @returns {engine.view.Node|engine.view.DocumentFragment|null} Corresponding view item.
 	 */
 	 */
 	getCorrespondingView( domNode ) {
 	getCorrespondingView( domNode ) {
-		if ( domNode instanceof HTMLElement ) {
+		if ( this.isElement( domNode ) ) {
 			return this.getCorrespondingViewElement( domNode );
 			return this.getCorrespondingViewElement( domNode );
-		} else if ( domNode instanceof DocumentFragment ) {
+		} else if ( this.isDocumentFragment( domNode ) ) {
 			return this.getCorrespondingViewDocumentFragment( domNode );
 			return this.getCorrespondingViewDocumentFragment( domNode );
-		} else if ( domNode instanceof Text ) {
+		} else if ( this.isText( domNode ) ) {
 			return this.getCorrespondingViewText( domNode );
 			return this.getCorrespondingViewText( domNode );
 		}
 		}
 
 
@@ -495,7 +495,7 @@ export default class DomConverter {
 
 
 		// Try to use previous sibling to find the corresponding text node.
 		// Try to use previous sibling to find the corresponding text node.
 		if ( previousSibling ) {
 		if ( previousSibling ) {
-			if ( !( previousSibling instanceof HTMLElement ) ) {
+			if ( !( this.isElement( previousSibling ) ) ) {
 				// The previous is text or comment.
 				// The previous is text or comment.
 				return null;
 				return null;
 			}
 			}
@@ -619,4 +619,34 @@ export default class DomConverter {
 			domEditable.focus();
 			domEditable.focus();
 		}
 		}
 	}
 	}
+
+	/**
+	 * Returns `true` when `node.nodeType` equals `Node.TEXT_NODE`.
+	 *
+	 * @param {Node} node Node to check.
+	 * @returns {Boolean}
+	 */
+	isText( node ) {
+		return node && node.nodeType == Node.TEXT_NODE;
+	}
+
+	/**
+	 * Returns `true` when `node.nodeType` equals `Node.ELEMENT_NODE`.
+	 *
+	 * @param {Node} node Node to check.
+	 * @returns {Boolean}
+	 */
+	isElement( node ) {
+		return node && node.nodeType == Node.ELEMENT_NODE;
+	}
+
+	/**
+	 * Returns `true` when `node.nodeType` equals `Node.DOCUMENT_FRAGMENT_NODE`.
+	 *
+	 * @param {Node} node Node to check.
+	 * @returns {Boolean}
+	 */
+	isDocumentFragment( node ) {
+		return node && node.nodeType == Node.DOCUMENT_FRAGMENT_NODE;
+	}
 }
 }

+ 46 - 0
packages/ckeditor5-engine/tests/view/domconverter/domconverter.js

@@ -65,4 +65,50 @@ describe( 'DomConverter', () => {
 			expect( focusSpy.calledOnce ).to.be.true;
 			expect( focusSpy.calledOnce ).to.be.true;
 		} );
 		} );
 	} );
 	} );
+
+	describe( 'DOM nodes type checking', () => {
+		let text, element, documentFragment;
+
+		before( () => {
+			text = document.createTextNode( 'test' );
+			element = document.createElement( 'div' );
+			documentFragment = document.createDocumentFragment();
+		} );
+
+		describe( 'isText', () => {
+			it( 'should return true for Text nodes', () => {
+				expect( converter.isText( text ) ).to.be.true;
+			} );
+
+			it( 'should return false for other arguments', () => {
+				expect( converter.isText( element ) ).to.be.false;
+				expect( converter.isText( documentFragment ) ).to.be.false;
+				expect( converter.isText( {} ) ).to.be.false;
+			} );
+		} );
+
+		describe( 'isElement', () => {
+			it( 'should return true for HTMLElement nodes', () => {
+				expect( converter.isElement( element ) ).to.be.true;
+			} );
+
+			it( 'should return false for other arguments', () => {
+				expect( converter.isElement( text ) ).to.be.false;
+				expect( converter.isElement( documentFragment ) ).to.be.false;
+				expect( converter.isElement( {} ) ).to.be.false;
+			} );
+		} );
+
+		describe( 'isDocumentFragment', () => {
+			it( 'should return true for HTMLElement nodes', () => {
+				expect( converter.isDocumentFragment( documentFragment ) ).to.be.true;
+			} );
+
+			it( 'should return false for other arguments', () => {
+				expect( converter.isDocumentFragment( text ) ).to.be.false;
+				expect( converter.isDocumentFragment( element ) ).to.be.false;
+				expect( converter.isDocumentFragment( {} ) ).to.be.false;
+			} );
+		} );
+	} );
 } );
 } );