Selaa lähdekoodia

Merge branch 'master' into t/23

Szymon Kupś 9 vuotta sitten
vanhempi
commit
e06fbd57ee

+ 3 - 3
packages/ckeditor5-undo/src/undocommand.js

@@ -263,9 +263,9 @@ function transformRangesByDeltas( ranges, deltas ) {
 
 				switch ( operation.type ) {
 					case 'insert':
-						result = ranges[ i ].getTransformedByInsertion(
+						result = ranges[ i ]._getTransformedByInsertion(
 							operation.position,
-							operation.nodeList.length,
+							operation.nodes.totalOffset,
 							true
 						);
 						break;
@@ -273,7 +273,7 @@ function transformRangesByDeltas( ranges, deltas ) {
 					case 'move':
 					case 'remove':
 					case 'reinsert':
-						result = ranges[ i ].getTransformedByMove(
+						result = ranges[ i ]._getTransformedByMove(
 							operation.sourcePosition,
 							operation.targetPosition,
 							operation.howMany,

+ 18 - 17
packages/ckeditor5-undo/tests/redocommand.js

@@ -8,6 +8,7 @@ import Range from '/ckeditor5/engine/model/range.js';
 import Position from '/ckeditor5/engine/model/position.js';
 import UndoCommand from '/ckeditor5/undo/undocommand.js';
 import RedoCommand from '/ckeditor5/undo/redocommand.js';
+import { itemAt, getText } from '/tests/engine/model/_utils/utils.js';
 
 let editor, doc, root, redo, undo;
 
@@ -103,9 +104,9 @@ describe( 'RedoCommand', () => {
 			 - {o
 			 - o} (key: value)
 			 */
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'fbaroo' );
-			expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
-			expect( root.getChild( 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( getText( root ) ).to.equal( 'fbaroo' );
+			expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 4, 6 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
@@ -127,8 +128,8 @@ describe( 'RedoCommand', () => {
 			 - a
 			 - r
 			 */
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
-			expect( root._children._nodes.find( node => node.hasAttribute( 'key' ) ) ).to.be.undefined;
+			expect( getText( root ) ).to.equal( 'foobar' );
+			expect( Array.from( root.getChildren() ).find( node => node.hasAttribute( 'key' ) ) ).to.be.undefined;
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 2, 4 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.true;
@@ -144,9 +145,9 @@ describe( 'RedoCommand', () => {
 			 - a
 			 - r
 			 */
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
-			expect( root.getChild( 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
-			expect( root.getChild( 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( getText( root ) ).to.equal( 'foobar' );
+			expect( itemAt( root, 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 3 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
@@ -162,9 +163,9 @@ describe( 'RedoCommand', () => {
 			 - {o
 			 - o} (key: value)
 			 */
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'fbaroo' );
-			expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
-			expect( root.getChild( 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( getText( root ) ).to.equal( 'fbaroo' );
+			expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 4, 6 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
@@ -184,9 +185,9 @@ describe( 'RedoCommand', () => {
 			 - {o
 			 - o} (key: value)
 			 */
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'fbaroo' );
-			expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
-			expect( root.getChild( 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( getText( root ) ).to.equal( 'fbaroo' );
+			expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 4, 6 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
@@ -204,9 +205,9 @@ describe( 'RedoCommand', () => {
 			// Redo moving "oo" to the end of string. It should be "fboo".
 			redo._execute();
 
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'fboo' );
-			expect( root.getChild( 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
-			expect( root.getChild( 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( getText( root ) ).to.equal( 'fboo' );
+			expect( itemAt( root, 1 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
 
 			// Selection after redo is not working properly if there was another batch in-between.
 			// Thankfully this will be very rare situation outside of OT, because normally an applied batch

+ 51 - 49
packages/ckeditor5-undo/tests/undocommand.js

@@ -6,8 +6,10 @@
 import ModelTestEditor from '/tests/ckeditor5/_utils/modeltesteditor.js';
 import Range from '/ckeditor5/engine/model/range.js';
 import Position from '/ckeditor5/engine/model/position.js';
+import Text from '/ckeditor5/engine/model/text.js';
 import UndoCommand from '/ckeditor5/undo/undocommand.js';
 import AttributeDelta from '/ckeditor5/engine/model/delta/attributedelta.js';
+import { itemAt, getText } from '/tests/engine/model/_utils/utils.js';
 
 let editor, doc, root, undo;
 
@@ -84,9 +86,9 @@ describe( 'UndoCommand', () => {
 			 [root]
 			 - f
 			 - [p]
-			 - {b (key: value)
-			 - a
-			 - r}
+			 	- {b (key: value)
+			 	- a
+			 	- r}
 			 - o
 			 - o (key: value)
 			 */
@@ -95,9 +97,9 @@ describe( 'UndoCommand', () => {
 			/*
 			 [root]
 			 - [p]
-			 - b (key: value)
-			 - a
-			 - r
+			 	- b (key: value)
+			 	- a
+			 	- r
 			 - o
 			 - f
 			 - o{} (key: value)
@@ -119,9 +121,9 @@ describe( 'UndoCommand', () => {
 			 - o (key: value)
 			 */
 
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'barofo' );
-			expect( root.getChild( 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
-			expect( root.getChild( 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( getText( root ) ).to.equal( 'barofo' );
+			expect( itemAt( root, 0 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( itemAt( root, 5 ).getAttribute( 'key' ) ).to.equal( 'value' );
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 3 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
@@ -139,9 +141,9 @@ describe( 'UndoCommand', () => {
 			 - r
 			 */
 
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
-			expect( root.getChild( 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
-			expect( root.getChild( 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( getText( root ) ).to.equal( 'foobar' );
+			expect( itemAt( root, 2 ).getAttribute( 'key' ) ).to.equal( 'value' );
+			expect( itemAt( root, 3 ).getAttribute( 'key' ) ).to.equal( 'value' );
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 3 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
@@ -159,9 +161,9 @@ describe( 'UndoCommand', () => {
 			 - r
 			 */
 
-			expect( Array.from( root._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'foobar' );
-			expect( root.getChild( 2 ).hasAttribute( 'key' ) ).to.be.false;
-			expect( root.getChild( 3 ).hasAttribute( 'key' ) ).to.be.false;
+			expect( getText( root ) ).to.equal( 'foobar' );
+			expect( itemAt( root, 2 ).hasAttribute( 'key' ) ).to.be.false;
+			expect( itemAt( root, 3 ).hasAttribute( 'key' ) ).to.be.false;
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 2, 4 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.true;
@@ -178,26 +180,26 @@ describe( 'UndoCommand', () => {
 		} );
 
 		it( 'should revert changes done by deltas from given batch, if parameter was passed (test: revert set attribute)', () => {
-			// editor.document.selection.setRanges( [ r( [ 0, 1 ], [ 0, 2 ] ) ] );
-
 			undo._execute( batch1 );
 			// Remove attribute:
 			/*
 			 [root]
 			 - [p]
-			 - b
-			 - a
-			 - r
+			 	- b
+			 	- a
+			 	- r
 			 - o
 			 - f
 			 - o
 			 */
 
-			expect( root.getChild( 0 ).name ).to.equal( 'p' );
-			expect( Array.from( root.getChild( 0 )._children._nodes.map( node => node.text ) ).join( '' ) ).to.equal( 'bar' );
-			expect( root.getChild( 0 ).getChild( 0 ).hasAttribute( 'key' ) ).to.be.false;
-			expect( root.getChild( 2 ).hasAttribute( 'key' ) ).to.be.false;
-			expect( root.getChild( 3 ).hasAttribute( 'key' ) ).to.be.false;
+			expect( itemAt( root, 0 ).name ).to.equal( 'p' );
+			expect( getText( root.getChild( 0 ) ) ).to.equal( 'bar' );
+			expect( root.getChild( 1 ).data ).to.equal( 'ofo' );
+
+			expect( itemAt( root.getChild( 0 ), 0 ).hasAttribute( 'key' ) ).to.be.false;
+			expect( itemAt( root, 2 ).hasAttribute( 'key' ) ).to.be.false;
+			expect( itemAt( root, 3 ).hasAttribute( 'key' ) ).to.be.false;
 
 			// Selection is only partially restored because the range got broken.
 			// The selection would have to best on letter "b" and letter "o", but it is set only on letter "b".
@@ -217,7 +219,7 @@ describe( 'UndoCommand', () => {
 			// It would be perfect if the `P` got removed aswell because wrapping was on removed nodes.
 			// But this would need a lot of logic / hardcoded ifs or a post-fixer.
 			expect( root.getChildCount() ).to.equal( 1 );
-			expect( root.getChild( 0 ).name ).to.equal( 'p' );
+			expect( itemAt( root, 0 ).name ).to.equal( 'p' );
 
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 0 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
@@ -228,13 +230,13 @@ describe( 'UndoCommand', () => {
 			// But those nodes should change in the graveyard and we can check them there.
 
 			expect( root.getChildCount() ).to.equal( 1 );
-			expect( root.getChild( 0 ).name ).to.equal( 'p' );
+			expect( itemAt( root, 0 ).name ).to.equal( 'p' );
 
 			// Operations for undoing that batch were working on graveyard so document selection should not change.
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 0 ) ) ).to.be.true;
 			expect( editor.document.selection.isBackward ).to.be.false;
 
-			expect( doc.graveyard.getChild( 0 ).getChildCount() ).to.equal( 6 );
+			expect( doc.graveyard.getChild( 0 ).getMaxOffset() ).to.equal( 6 );
 
 			for ( let char of doc.graveyard._children ) {
 				expect( char.hasAttribute( 'key' ) ).to.be.false;
@@ -242,7 +244,7 @@ describe( 'UndoCommand', () => {
 
 			// Let's undo wrapping. This should leave us with empty root.
 			undo._execute( batch3 );
-			expect( root.getChildCount() ).to.equal( 0 );
+			expect( root.getMaxOffset() ).to.equal( 0 );
 
 			// Once again transformed range ends up in the graveyard.
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 0, 0 ) ) ).to.be.true;
@@ -252,12 +254,12 @@ describe( 'UndoCommand', () => {
 
 	// Some tests to ensure 100% CC and proper behavior in edge cases.
 	describe( 'edge cases', () => {
-		function getText( root ) {
+		function getCaseText( root ) {
 			let text = '';
 
 			for ( let i = 0; i < root.getChildCount(); i++ ) {
 				let node = root.getChild( i );
-				text += node.getAttribute( 'uppercase' ) ? node.character.toUpperCase() : node.character;
+				text += node.getAttribute( 'uppercase' ) ? node.data.toUpperCase() : node.data;
 			}
 
 			return text;
@@ -273,29 +275,29 @@ describe( 'UndoCommand', () => {
 			// Still there might be some weird scenarios connected with OT / Undo / Collaborative Editing / other deltas /
 			// fancy 3rd party plugin where it may come up, so it's better to be safe than sorry.
 
-			root.appendChildren( 'abcdef' );
-			expect( getText( root ) ).to.equal( 'abcdef' );
+			root.appendChildren( new Text( 'abcdef' ) );
+			expect( getCaseText( root ) ).to.equal( 'abcdef' );
 
 			editor.document.selection.setRanges( [ r( 1, 4 ) ] );
 			let batch0 = doc.batch();
 			undo.addBatch( batch0 );
 			batch0.move( r( 1, 4 ), p( 5 ) );
-			expect( getText( root ) ).to.equal( 'aebcdf' );
+			expect( getCaseText( root ) ).to.equal( 'aebcdf' );
 
 			editor.document.selection.setRanges( [ r( 1, 1 ) ]  );
 			let batch1 = doc.batch();
 			undo.addBatch( batch1 );
 			batch1.remove( r( 0, 1 ) );
-			expect( getText( root ) ).to.equal( 'ebcdf' );
+			expect( getCaseText( root ) ).to.equal( 'ebcdf' );
 
 			editor.document.selection.setRanges( [ r( 0, 3 ) ] );
 			let batch2 = doc.batch();
 			undo.addBatch( batch2 );
 			batch2.setAttribute( r( 0, 3 ), 'uppercase', true );
-			expect( getText( root ) ).to.equal( 'EBCdf' );
+			expect( getCaseText( root ) ).to.equal( 'EBCdf' );
 
 			undo._execute( batch0 );
-			expect( getText( root ) ).to.equal( 'BCdEf' );
+			expect( getCaseText( root ) ).to.equal( 'BCdEf' );
 
 			// Let's simulate splitting the delta by updating the history by hand.
 			let attrHistoryDelta = doc.history.getDelta( 2 )[ 0 ];
@@ -308,57 +310,57 @@ describe( 'UndoCommand', () => {
 			undo._execute( batch1 );
 			// After this execution, undo algorithm should update both `attrDelta1` and `attrDelta2` with new
 			// versions, that have incremented offsets.
-			expect( getText( root ) ).to.equal( 'aBCdEf' );
+			expect( getCaseText( root ) ).to.equal( 'aBCdEf' );
 
 			undo._execute( batch2 );
 			// This execution checks whether undo algorithm correctly updated deltas in previous execution
 			// and also whether it correctly "reads" both deltas from history.
-			expect( getText( root ) ).to.equal( 'abcdef' );
+			expect( getCaseText( root ) ).to.equal( 'abcdef' );
 		} );
 
 		it( 'merges touching ranges when restoring selection', () => {
-			root.appendChildren( 'abcdef' );
-			expect( getText( root ) ).to.equal( 'abcdef' );
+			root.appendChildren( new Text( 'abcdef' ) );
+			expect( getCaseText( root ) ).to.equal( 'abcdef' );
 
 			editor.document.selection.setRanges( [ r( 1, 4 ) ] );
 			let batch0 = doc.batch();
 			undo.addBatch( batch0 );
 			batch0.setAttribute( r( 1, 4 ), 'uppercase', true );
-			expect( getText( root ) ).to.equal( 'aBCDef' );
+			expect( getCaseText( root ) ).to.equal( 'aBCDef' );
 
 			editor.document.selection.setRanges( [ r( 3, 4 ) ] );
 			let batch1 = doc.batch();
 			undo.addBatch( batch1 );
 			batch1.move( r( 3, 4 ), p( 1 ) );
-			expect( getText( root ) ).to.equal( 'aDBCef' );
+			expect( getCaseText( root ) ).to.equal( 'aDBCef' );
 
 			undo._execute( batch0 );
 
 			// After undo-attr: acdbef <--- "cdb" should be selected, it would look weird if only "cd" or "b" is selected
 			// but the whole unbroken part "cdb" changed attribute.
-			expect( getText( root ) ).to.equal( 'adbcef' );
+			expect( getCaseText( root ) ).to.equal( 'adbcef' );
 			expect( editor.document.selection.getRanges().next().value.isEqual( r( 1, 4 ) ) ).to.be.true;
 		} );
 
 		it( 'does nothing (and not crashes) if delta to undo is no longer in history', () => {
 			// Also an edgy situation but it may come up if other plugins use `CompressedHistory` API.
-			root.appendChildren( 'abcdef' );
-			expect( getText( root ) ).to.equal( 'abcdef' );
+			root.appendChildren( new Text( 'abcdef' ) );
+			expect( getCaseText( root ) ).to.equal( 'abcdef' );
 
 			editor.document.selection.setRanges( [ r( 0, 1 ) ] );
 			let batch0 = doc.batch();
 			undo.addBatch( batch0 );
 			batch0.setAttribute( r( 0, 1 ), 'uppercase', true );
-			expect( getText( root ) ).to.equal( 'Abcdef' );
+			expect( getCaseText( root ) ).to.equal( 'Abcdef' );
 
 			doc.history.removeDelta( 0 );
 			root.getChild( 0 ).removeAttribute( 'uppercase' );
-			expect( getText( root ) ).to.equal( 'abcdef' );
+			expect( getCaseText( root ) ).to.equal( 'abcdef' );
 
 			undo._execute();
 
 			// Nothing happened. We are still alive.
-			expect( getText( root ) ).to.equal( 'abcdef' );
+			expect( getCaseText( root ) ).to.equal( 'abcdef' );
 		} );
 	} );
 } );