Sfoglia il codice sorgente

Tests: Added tests created while debugging ckeditor/ckeditor5-clipboard#2.

Piotrek Koszuliński 8 anni fa
parent
commit
695319a028

+ 30 - 2
packages/ckeditor5-engine/tests/dataprocessor/htmldataprocessor.js

@@ -13,7 +13,7 @@ import { stringify, parse } from '../../src/dev-utils/view';
 describe( 'HtmlDataProcessor', () => {
 	const dataProcessor = new HtmlDataProcessor();
 
-	describe( 'toView', () => {
+	describe( 'toView()', () => {
 		it( 'should return empty DocumentFragment when empty string is passed', () => {
 			const fragment = dataProcessor.toView( '' );
 			expect( fragment ).to.be.an.instanceOf( ViewDocumentFragment );
@@ -58,9 +58,37 @@ describe( 'HtmlDataProcessor', () => {
 				}, 10 );
 			} );
 		}
+
+		// Uncomment this test after fixing #404.
+		// describe( 'https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731 + #404', () => {
+		// 	it( 'does not lose whitespaces in Chrome\'s paste-like content', () => {
+		// 		const fragment = dataProcessor.toView(
+		// 			'<meta charset=\'utf-8\'>' +
+		// 			'<span>This is the<span>\u00a0</span></span>' +
+		// 			'<a href="url">third developer preview</a>' +
+		// 			'<span><span>\u00a0</span>of<span>\u00a0</span></span>' +
+		// 			'<strong>CKEditor\u00a05</strong>' +
+		// 			'<span>.</span>'
+		// 		);
+
+		// 		expect( stringify( fragment ) ).to.equal(
+		// 			'<span>This is the<span>\u00a0</span></span>' +
+		// 			'<a href="url">third developer preview</a>' +
+		// 			'<span><span>\u00a0</span>of<span>\u00a0</span></span>' +
+		// 			'<strong>CKEditor\u00a05</strong>' +
+		// 			'<span>.</span>'
+		// 		);
+
+		// 		// Just to be sure... stringify() uses conversion and the browser extensively,
+		// 		// so it's not entirely safe.
+		// 		expect( fragment.getChild( 0 ).getChild( 1 ).getChild( 0 ).data ).to.equal( '\u00a0' );
+		// 		expect( fragment.getChild( 2 ).getChild( 0 ).getChild( 0 ).data ).to.equal( '\u00a0' );
+		// 		expect( fragment.getChild( 2 ).getChild( 2 ).getChild( 0 ).data ).to.equal( '\u00a0' );
+		// 	} );
+		// } );
 	} );
 
-	describe( 'toData', () => {
+	describe( 'toData()', () => {
 		it( 'should return empty string when empty DocumentFragment is passed', () => {
 			const fragment = new ViewDocumentFragment();
 

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

@@ -460,6 +460,68 @@ describe( 'DomConverter', () => {
 				expect( viewDiv.getChild( 0 ).getChild( 0 ).data ).to.equal( '   foo\n   foo  ' );
 			} );
 
+			// https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731
+			it( 'not in span between two words (space)', () => {
+				const domDiv = createElement( document, 'div', {}, [
+					document.createTextNode( 'word' ),
+					createElement( document, 'span', {}, [
+						document.createTextNode( ' ' )
+					] ),
+					document.createTextNode( 'word' )
+				] );
+
+				const viewDiv = converter.domToView( domDiv );
+
+				expect( viewDiv.getChild( 1 ).name ).to.equal( 'span' );
+				expect( viewDiv.getChild( 1 ).childCount ).to.equal( 1 );
+				expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( ' ' );
+			} );
+
+			// https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731
+			it( 'not in span between two words (nbsp)', () => {
+				const domDiv = createElement( document, 'div', {}, [
+					document.createTextNode( 'word' ),
+					createElement( document, 'span', {}, [
+						document.createTextNode( '\u00a0' )
+					] ),
+					document.createTextNode( 'word' )
+				] );
+
+				const viewDiv = converter.domToView( domDiv );
+
+				expect( viewDiv.getChild( 1 ).name ).to.equal( 'span' );
+				expect( viewDiv.getChild( 1 ).childCount ).to.equal( 1 );
+				expect( viewDiv.getChild( 1 ).getChild( 0 ).data ).to.equal( '\u00a0' );
+			} );
+
+			// https://github.com/ckeditor/ckeditor5-clipboard/issues/2#issuecomment-310417731
+			it( 'not in a Chrome\'s paste-like content', () => {
+				// Like:
+				// <span style="color: rgb(0, 0, 0); font-family: Times;">This is the<span>\u00a0</span></span>
+				// <a href="url" style="font-family: Times; font-size: medium;">third developer preview</a>
+				const domDiv = createElement( document, 'div', {}, [
+					createElement( document, 'span', {}, [
+						document.createTextNode( 'word' ),
+						createElement( document, 'span', {}, [
+							document.createTextNode( '\u00a0' )
+						] )
+					] ),
+					createElement( document, 'a', {}, [
+						document.createTextNode( 'word' )
+					] )
+				] );
+
+				const viewDiv = converter.domToView( domDiv );
+
+				expect( viewDiv.getChild( 0 ).name ).to.equal( 'span' );
+				expect( viewDiv.getChild( 0 ).childCount ).to.equal( 2 );
+
+				expect( viewDiv.getChild( 0 ).getChild( 1 ).name ).to.equal( 'span' );
+				expect( viewDiv.getChild( 0 ).getChild( 1 ).childCount ).to.equal( 1 );
+
+				expect( viewDiv.getChild( 0 ).getChild( 1 ).getChild( 0 ).data ).to.equal( '\u00a0' );
+			} );
+
 			//
 			// See also whitespace-handling-integration.js.
 			//