Selaa lähdekoodia

Removed transfering markers from model writer.

Oskar Wróbel 8 vuotta sitten
vanhempi
commit
9ed384badf

+ 4 - 27
packages/ckeditor5-engine/src/model/writer.js

@@ -11,11 +11,9 @@ import Node from './node';
 import Text from './text';
 import TextProxy from './textproxy';
 import Range from './range';
-import Position from './position';
 import DocumentFragment from './documentfragment';
 import NodeList from './nodelist';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
-import log from '@ckeditor/ckeditor5-utils/src/log';
 
 /**
  * Contains functions used for composing model tree, grouped together under "model writer" name. Those functions
@@ -47,20 +45,16 @@ export default writer;
 /**
  * Inserts given nodes at given position.
  *
- * When inserted element has {@link engine/model/markercollection~MarkersCollection} and is being inserted
- * to the element which root also has {@link engine/model/markercollection~MarkersCollection} then markers will
- * be transferred as well.
- *
  * @function module:engine/model/writer~writer.insert
  * @param {module:engine/model/position~Position} position Position at which nodes should be inserted.
  * @param {module:engine/model/node~NodeSet} nodes Nodes to insert.
  * @returns {module:engine/model/range~Range} Range spanning over inserted elements.
  */
 export function insert( position, nodes ) {
-	const normalizedNodes = normalizeNodes( nodes );
+	nodes = normalizeNodes( nodes );
 
 	// We have to count offset before inserting nodes because they can get merged and we would get wrong offsets.
-	const offset = normalizedNodes.reduce( ( sum, node ) => sum + node.offsetSize, 0 );
+	const offset = nodes.reduce( ( sum, node ) => sum + node.offsetSize, 0 );
 	const parent = position.parent;
 
 	// Insertion might be in a text node, we should split it if that's the case.
@@ -69,29 +63,12 @@ export function insert( position, nodes ) {
 
 	// Insert nodes at given index. After splitting we have a proper index and insertion is between nodes,
 	// using basic `Element` API.
-	parent.insertChildren( index, normalizedNodes );
+	parent.insertChildren( index, nodes );
 
 	// Merge text nodes, if possible. Merging is needed only at points where inserted nodes "touch" "old" nodes.
-	_mergeNodesAtIndex( parent, index + normalizedNodes.length );
+	_mergeNodesAtIndex( parent, index + nodes.length );
 	_mergeNodesAtIndex( parent, index );
 
-	// If given element is a DocumentFragment and has markers.
-	if ( nodes instanceof DocumentFragment && nodes.markers.size ) {
-		// If node is being inserted to the element attached to Document or element which root element is DocumentFragment.
-		const targetElement = position.root.document || position.root;
-
-		if ( targetElement.markers ) {
-			// We need to transfer its markers and update position markers positions.
-			for ( const marker of nodes.markers ) {
-				const range = new Range( new Position( parent, marker[ 1 ].start.path ),  new Position( parent, marker[ 1 ].end.path ) );
-				targetElement.markers.set( marker[ 0 ], range );
-			}
-		// Otherwise we need to show warning about losing markers.
-		} else {
-			log.warn( 'model-writer-insert-lose-markers: Element containing markers is set to element without MarkersCollection.' );
-		}
-	}
-
 	return new Range( position, position.getShiftedBy( offset ) );
 }
 

+ 1 - 56
packages/ckeditor5-engine/tests/model/writer.js

@@ -11,10 +11,9 @@ import TextProxy from '../../src/model/textproxy';
 import Position from '../../src/model/position';
 import Range from '../../src/model/range';
 import writer from '../../src/model/writer';
-import { getData, stringify } from '../../src/dev-utils/model';
+import { getData } from '../../src/dev-utils/model';
 
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
-import log from '@ckeditor/ckeditor5-utils/src/log';
 
 let doc, root;
 
@@ -55,60 +54,6 @@ describe( 'writer', () => {
 
 			expectData( 'foo<$text bold="true">xxxbar</$text><image src="img.jpg"></image>xyz' );
 		} );
-
-		it( 'should transfer markers from given node to the root element of target position', () => {
-			root.appendChildren( [ new Element( 'div' ) ] );
-
-			const docFrag = new DocumentFragment( [
-				new Element( 'paragraph', null, [
-					new Text( 'foo bar' ),
-				] )
-			] );
-
-			docFrag.markers.set( 'foo', new Range( new Position( docFrag, [ 0, 2 ] ), new Position( docFrag, [ 0, 6 ] ) ) );
-
-			writer.insert( new Position( root, [ 10, 0 ] ), docFrag );
-
-			expect( stringify( root, doc.markers.get( 'foo' ).getRange() ) ).to.equal(
-				'foo<$text bold="true">bar</$text><image src="img.jpg"></image>xyz<div><paragraph>fo[o ba]r</paragraph></div>'
-			);
-		} );
-
-		it( 'should transfer markers from given node to the root element of target position when root is a documentFragment', () => {
-			const targetDocFrag = new DocumentFragment( [ new Element( 'div' ) ] );
-
-			const docFrag = new DocumentFragment( [
-				new Element( 'paragraph', null, [
-					new Text( 'foo bar' ),
-				] )
-			] );
-
-			docFrag.markers.set( 'foo', new Range( new Position( docFrag, [ 0, 2 ] ), new Position( docFrag, [ 0, 6 ] ) ) );
-
-			writer.insert( new Position( targetDocFrag, [ 0, 0 ] ), docFrag );
-
-			expect( stringify( targetDocFrag, targetDocFrag.markers.get( 'foo' ) ) )
-				.to.equal( '<div><paragraph>fo[o ba]r</paragraph></div>' );
-		} );
-
-		it( 'should log warning when element with markers is set to the element without markers collection', () => {
-			const warnSpy = sinon.spy( log, 'warn' );
-			const target = new Element( 'div' );
-			const docFrag = new DocumentFragment( [
-				new Element( 'paragraph', null, [
-					new Text( 'foo bar' ),
-				] )
-			] );
-			const range = new Range( new Position( docFrag, [ 1, 2 ] ), new Position( docFrag, [ 1, 4 ] ) );
-
-			docFrag.markers.set( 'foo', range );
-
-			writer.insert( new Position( target, [ 0 ] ), docFrag );
-
-			expect( warnSpy.calledWithExactly(
-				'model-writer-insert-lose-markers: Element containing markers is set to element without MarkersCollection.'
-			) ).to.true;
-		} );
 	} );
 
 	describe( 'remove', () => {