Kaynağa Gözat

DomConverter.domToView() will return null for comments.

Piotrek Koszuliński 8 yıl önce
ebeveyn
işleme
c60d4c92e5

+ 12 - 0
packages/ckeditor5-engine/src/view/domconverter.js

@@ -360,6 +360,8 @@ export default class DomConverter {
 
 				return textData === '' ? null : new ViewText( textData );
 			}
+		} else if ( this.isComment( domNode ) ) {
+			return null;
 		} else {
 			if ( this.getCorrespondingView( domNode ) ) {
 				return this.getCorrespondingView( domNode );
@@ -767,6 +769,16 @@ export default class DomConverter {
 	}
 
 	/**
+	 * Returns `true` when `node.nodeType` equals `Node.COMMENT_NODE`.
+	 *
+	 * @param {Node} node Node to check.
+	 * @returns {Boolean}
+	 */
+	isComment( node ) {
+		return node && node.nodeType == Node.COMMENT_NODE;
+	}
+
+	/**
 	 * Returns `true` if given selection is a backward selection, that is, if it's `focus` is before `anchor`.
 	 *
 	 * @param {Selection} DOM Selection instance to check.

+ 6 - 0
packages/ckeditor5-engine/tests/view/domconverter/dom-to-view.js

@@ -169,6 +169,12 @@ describe( 'DomConverter', () => {
 			expect( converter.domToView( textNode ) ).to.be.null;
 		} );
 
+		it( 'should return null for a comment', () => {
+			const comment = document.createComment( 'abc' );
+
+			expect( converter.domToView( comment ) ).to.be.null;
+		} );
+
 		describe( 'it should clear whitespaces', () => {
 			it( 'at the beginning of block element', () => {
 				const domDiv = createElement( document, 'div', {}, [

+ 18 - 1
packages/ckeditor5-engine/tests/view/domconverter/domconverter.js

@@ -69,12 +69,13 @@ describe( 'DomConverter', () => {
 	} );
 
 	describe( 'DOM nodes type checking', () => {
-		let text, element, documentFragment;
+		let text, element, documentFragment, comment;
 
 		before( () => {
 			text = document.createTextNode( 'test' );
 			element = document.createElement( 'div' );
 			documentFragment = document.createDocumentFragment();
+			comment = document.createComment( 'a' );
 		} );
 
 		describe( 'isText()', () => {
@@ -85,6 +86,7 @@ describe( 'DomConverter', () => {
 			it( 'should return false for other arguments', () => {
 				expect( converter.isText( element ) ).to.be.false;
 				expect( converter.isText( documentFragment ) ).to.be.false;
+				expect( converter.isText( comment ) ).to.be.false;
 				expect( converter.isText( {} ) ).to.be.false;
 			} );
 		} );
@@ -97,6 +99,7 @@ describe( 'DomConverter', () => {
 			it( 'should return false for other arguments', () => {
 				expect( converter.isElement( text ) ).to.be.false;
 				expect( converter.isElement( documentFragment ) ).to.be.false;
+				expect( converter.isText( comment ) ).to.be.false;
 				expect( converter.isElement( {} ) ).to.be.false;
 			} );
 		} );
@@ -109,8 +112,22 @@ describe( 'DomConverter', () => {
 			it( 'should return false for other arguments', () => {
 				expect( converter.isDocumentFragment( text ) ).to.be.false;
 				expect( converter.isDocumentFragment( element ) ).to.be.false;
+				expect( converter.isText( comment ) ).to.be.false;
 				expect( converter.isDocumentFragment( {} ) ).to.be.false;
 			} );
 		} );
+
+		describe( 'isComment()', () => {
+			it( 'should return true for HTML comments', () => {
+				expect( converter.isComment( comment ) ).to.be.true;
+			} );
+
+			it( 'should return false for other arguments', () => {
+				expect( converter.isComment( text ) ).to.be.false;
+				expect( converter.isComment( element ) ).to.be.false;
+				expect( converter.isComment( documentFragment ) ).to.be.false;
+				expect( converter.isComment( {} ) ).to.be.false;
+			} );
+		} );
 	} );
 } );

+ 0 - 12
packages/ckeditor5-engine/tests/view/observer/domeventdata.js

@@ -11,18 +11,6 @@ import ViewDocument from '../../../src/view/document';
 describe( 'DomEventData', () => {
 	let viewDocument, viewBody, domRoot;
 
-	// Todo: the whole `before` hook can be removed.
-	// Depends on: https://github.com/ckeditor/ckeditor5-engine/issues/647
-	before( () => {
-		// Use Array.from because of MS Edge (#923).
-		for ( const node of Array.from( document.body.childNodes ) ) {
-			// Remove all <!-- Comments -->
-			if ( node.nodeType === 8 ) {
-				document.body.removeChild( node );
-			}
-		}
-	} );
-
 	beforeEach( () => {
 		viewDocument = new ViewDocument();