Browse Source

Replaced writer by bath in getSelectedContent.

Oskar Wróbel 8 years ago
parent
commit
8e35c0ee64

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

@@ -295,10 +295,11 @@ export default class DataController {
 	 *
 	 * @fires module:engine/controller/datacontroller~DataController#getSelectedContent
 	 * @param {module:engine/model/selection~Selection} selection The selection of which content will be retrieved.
+	 * @param {module:engine/model/batch~Batch} batch Batch to which deltas will be added.
 	 * @returns {module:engine/model/documentfragment~DocumentFragment} Document fragment holding the clone of the selected content.
 	 */
-	getSelectedContent( selection ) {
-		return getSelectedContent( selection );
+	getSelectedContent( selection, batch ) {
+		return getSelectedContent( selection, batch );
 	}
 
 	/**

+ 10 - 12
packages/ckeditor5-engine/src/controller/getselectedcontent.js

@@ -7,11 +7,8 @@
  * @module engine/controller/getselectedcontent
  */
 
-import DocumentFragment from '../model/documentfragment';
 import Range from '../model/range';
 import Position from '../model/position';
-import Text from '../model/text';
-import { remove } from '../model/writer';
 
 /**
  * Gets a clone of the selected content.
@@ -25,10 +22,11 @@ import { remove } from '../model/writer';
  *		<quote><h>st</h></quote><p>se</p>
  *
  * @param {module:engine/model/selection~Selection} selection The selection of which content will be returned.
+ * @param {module:engine/model/batch~Batch} batch Batch to which deltas will be added.
  * @returns {module:engine/model/documentfragment~DocumentFragment}
  */
-export default function getSelectedContent( selection ) {
-	const frag = new DocumentFragment();
+export default function getSelectedContent( selection, batch ) {
+	const frag = batch.createDocumentFragment();
 	const range = selection.getFirstRange();
 
 	if ( !range || range.isCollapsed ) {
@@ -69,9 +67,9 @@ export default function getSelectedContent( selection ) {
 	// Clone the whole contents.
 	for ( const item of flatSubtreeRange.getItems( { shallow: true } ) ) {
 		if ( item.is( 'textProxy' ) ) {
-			frag.appendChildren( new Text( item.data, item.getAttributes() ) );
+			batch.appendText( item.data, item.getAttributes(), frag );
 		} else {
-			frag.appendChildren( item.clone( true ) );
+			batch.append( item.clone( true ), frag );
 		}
 	}
 
@@ -97,8 +95,8 @@ export default function getSelectedContent( selection ) {
 		const leftExcessRange = new Range( Position.createAt( frag ), newRange.start );
 		const rightExcessRange = new Range( newRange.end, Position.createAt( frag, 'end' ) );
 
-		removeRangeContent( rightExcessRange );
-		removeRangeContent( leftExcessRange );
+		removeRangeContent( rightExcessRange, batch );
+		removeRangeContent( leftExcessRange, batch );
 	}
 
 	return frag;
@@ -106,7 +104,7 @@ export default function getSelectedContent( selection ) {
 
 // After https://github.com/ckeditor/ckeditor5-engine/issues/690 is fixed,
 // this function will, most likely, be able to rewritten using getMinimalFlatRanges().
-function removeRangeContent( range ) {
+function removeRangeContent( range, batch ) {
 	const parentsToCheck = [];
 
 	Array.from( range.getItems( { direction: 'backward' } ) )
@@ -128,7 +126,7 @@ function removeRangeContent( range ) {
 		.forEach( itemRange => {
 			parentsToCheck.push( itemRange.start.parent );
 
-			remove( itemRange );
+			batch.remove( itemRange );
 		} );
 
 	// Remove ancestors of the removed items if they turned to be empty now
@@ -141,7 +139,7 @@ function removeRangeContent( range ) {
 
 			parent = parent.parent;
 
-			remove( removeRange );
+			batch.remove( removeRange );
 		}
 	} );
 }

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

@@ -433,7 +433,7 @@ describe( 'DataController', () => {
 
 			data.on( 'getSelectedContent', spy );
 
-			data.getSelectedContent( sel );
+			data.getSelectedContent( sel, modelDocument.batch() );
 
 			expect( spy.calledOnce ).to.be.true;
 		} );
@@ -443,7 +443,7 @@ describe( 'DataController', () => {
 
 			setData( modelDocument, '<paragraph>fo[ob]ar</paragraph>' );
 
-			const content = data.getSelectedContent( modelDocument.selection );
+			const content = data.getSelectedContent( modelDocument.selection, modelDocument.batch() );
 
 			expect( stringify( content ) ).to.equal( 'ob' );
 		} );

+ 34 - 34
packages/ckeditor5-engine/tests/controller/getselectedcontent.js

@@ -30,7 +30,7 @@ describe( 'Delete utils', () => {
 			it( 'returns empty fragment for no selection', () => {
 				setData( doc, 'abc' );
 
-				const frag = getSelectedContent( doc.selection );
+				const frag = getSelectedContent( doc.selection, doc.batch() );
 
 				expect( frag ).instanceOf( DocumentFragment );
 				expect( frag.isEmpty ).to.be.true;
@@ -39,7 +39,7 @@ describe( 'Delete utils', () => {
 			it( 'returns empty fragment for empty selection', () => {
 				setData( doc, 'a[]bc' );
 
-				const frag = getSelectedContent( doc.selection );
+				const frag = getSelectedContent( doc.selection, doc.batch() );
 
 				expect( frag ).instanceOf( DocumentFragment );
 				expect( frag.isEmpty ).to.be.true;
@@ -48,7 +48,7 @@ describe( 'Delete utils', () => {
 			it( 'gets one character', () => {
 				setData( doc, 'a[b]c' );
 
-				const frag = getSelectedContent( doc.selection );
+				const frag = getSelectedContent( doc.selection, doc.batch() );
 				const content = stringify( frag );
 
 				expect( frag ).instanceOf( DocumentFragment );
@@ -58,49 +58,49 @@ describe( 'Delete utils', () => {
 			it( 'gets full text', () => {
 				setData( doc, '[abc]' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( 'abc' );
 			} );
 
 			it( 'gets text with an attribute', () => {
 				setData( doc, 'xxx<$text bold="true">a[b]c</$text>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<$text bold="true">b</$text>' );
 			} );
 
 			it( 'gets text with attributes', () => {
 				setData( doc, 'x<$text bold="true">a[b</$text><$text italic="true">c]d</$text>x' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<$text bold="true">b</$text><$text italic="true">c</$text>' );
 			} );
 
 			it( 'gets text with and without attribute', () => {
 				setData( doc, '<$text bold="true">a[b</$text>c]d' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<$text bold="true">b</$text>c' );
 			} );
 
 			it( 'gets text and element', () => {
 				setData( doc, '[ab<image></image>c]' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( 'ab<image></image>c' );
 			} );
 
 			it( 'gets one element', () => {
 				setData( doc, 'a[<image></image>]b' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<image></image>' );
 			} );
 
 			it( 'gets multiple elements', () => {
 				setData( doc, '[<image></image><image></image>]' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<image></image><image></image>' );
 			} );
 		} );
@@ -128,63 +128,63 @@ describe( 'Delete utils', () => {
 			it( 'gets one character', () => {
 				setData( doc, '<paragraph>a[b]c</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( 'b' );
 			} );
 
 			it( 'gets entire paragraph content', () => {
 				setData( doc, '<paragraph>[a<image></image>b]</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( 'a<image></image>b' );
 			} );
 
 			it( 'gets two blocks - partial, partial', () => {
 				setData( doc, '<heading1>a[bc</heading1><paragraph>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>bc</heading1><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets two blocks - full, partial', () => {
 				setData( doc, '<heading1>[abc</heading1><paragraph>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>abc</heading1><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets two blocks - full, partial 2', () => {
 				setData( doc, '<heading1>[abc</heading1><paragraph>de<image></image>]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>abc</heading1><paragraph>de<image></image></paragraph>' );
 			} );
 
 			it( 'gets two blocks - full, partial 3', () => {
 				setData( doc, '<heading1>x</heading1><heading1>[abc</heading1><paragraph><image></image>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>abc</heading1><paragraph><image></image>de</paragraph>' );
 			} );
 
 			it( 'gets two blocks - full, partial 4', () => {
 				setData( doc, '<heading1>[abc</heading1><paragraph>de]f<image></image></paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>abc</heading1><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets two blocks - partial, full', () => {
 				setData( doc, '<heading1>a[bc</heading1><paragraph>def]</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>bc</heading1><paragraph>def</paragraph>' );
 			} );
 
 			it( 'gets two blocks - partial, full 2', () => {
 				setData( doc, '<heading1>a[<image></image>bc</heading1><paragraph>def]</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1><image></image>bc</heading1><paragraph>def</paragraph>' );
 			} );
 
@@ -192,7 +192,7 @@ describe( 'Delete utils', () => {
 			it( 'gets two blocks - empty, full', () => {
 				setData( doc, '<heading1>abc[</heading1><paragraph>def]</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<paragraph>def</paragraph>' );
 			} );
 
@@ -200,28 +200,28 @@ describe( 'Delete utils', () => {
 			it( 'gets two blocks - partial, empty', () => {
 				setData( doc, '<heading1>a[bc</heading1><paragraph>]def</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>bc</heading1>' );
 			} );
 
 			it( 'gets three blocks', () => {
 				setData( doc, '<heading1>a[bc</heading1><paragraph>x</paragraph><paragraph>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<heading1>bc</heading1><paragraph>x</paragraph><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets block image', () => {
 				setData( doc, '<paragraph>a</paragraph>[<blockImage><caption>Foo</caption></blockImage>]<paragraph>b</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<blockImage><caption>Foo</caption></blockImage>' );
 			} );
 
 			it( 'gets two blocks', () => {
 				setData( doc, '<paragraph>a</paragraph>[<blockImage></blockImage><blockImage></blockImage>]<paragraph>b</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<blockImage></blockImage><blockImage></blockImage>' );
 			} );
 
@@ -229,7 +229,7 @@ describe( 'Delete utils', () => {
 			it( 'gets content when multiple text items needs to be removed from the right excess', () => {
 				setData( doc, '<paragraph>a[b</paragraph><paragraph>c]d<$text bold="true">e</$text>f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content )
 					.to.equal( '<paragraph>b</paragraph><paragraph>c</paragraph>' );
 			} );
@@ -238,7 +238,7 @@ describe( 'Delete utils', () => {
 			it( 'gets content when multiple text items needs to be removed from the left excess', () => {
 				setData( doc, '<paragraph>a<$text bold="true">b</$text>c[d</paragraph><paragraph>e]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content )
 					.to.equal( '<paragraph>d</paragraph><paragraph>e</paragraph>' );
 			} );
@@ -262,28 +262,28 @@ describe( 'Delete utils', () => {
 			it( 'gets content when ends are equally deeply nested', () => {
 				setData( doc, '<heading1>x</heading1><quote><paragraph>a[bc</paragraph><paragraph>de]f</paragraph></quote>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<paragraph>bc</paragraph><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets content when left end nested deeper', () => {
 				setData( doc, '<quote><paragraph>a[bc</paragraph></quote><paragraph>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<quote><paragraph>bc</paragraph></quote><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets content when left end nested deeper 2', () => {
 				setData( doc, '<quote><paragraph>a[bc</paragraph><heading1>x</heading1></quote><paragraph>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<quote><paragraph>bc</paragraph><heading1>x</heading1></quote><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets content when left end nested deeper 3', () => {
 				setData( doc, '<quote><heading1>x</heading1><paragraph>a[bc</paragraph></quote><paragraph>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<quote><paragraph>bc</paragraph></quote><paragraph>de</paragraph>' );
 			} );
 
@@ -291,21 +291,21 @@ describe( 'Delete utils', () => {
 			it( 'gets content when left end nested deeper 4', () => {
 				setData( doc, '<quote><heading1>x[</heading1><paragraph>abc</paragraph></quote><paragraph>de]f</paragraph>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<quote><paragraph>abc</paragraph></quote><paragraph>de</paragraph>' );
 			} );
 
 			it( 'gets content when right end nested deeper', () => {
 				setData( doc, '<paragraph>a[bc</paragraph><quote><paragraph>de]f</paragraph></quote>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content ).to.equal( '<paragraph>bc</paragraph><quote><paragraph>de</paragraph></quote>' );
 			} );
 
 			it( 'gets content when both ends nested deeper than the middle element', () => {
 				setData( doc, '<quote><heading1>a[bc</heading1></quote><heading1>x</heading1><quote><heading1>de]f</heading1></quote>' );
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content )
 					.to.equal( '<quote><heading1>bc</heading1></quote><heading1>x</heading1><quote><heading1>de</heading1></quote>' );
 			} );
@@ -325,7 +325,7 @@ describe( 'Delete utils', () => {
 					'</quote>'
 				);
 
-				const content = stringify( getSelectedContent( doc.selection ) );
+				const content = stringify( getSelectedContent( doc.selection, doc.batch() ) );
 				expect( content )
 					.to.equal( '<paragraph>ar</paragraph>bo' );
 			} );