浏览代码

Fixed transfering mrkers to work for documentFragment -> documentFragment.

Oskar Wróbel 8 年之前
父节点
当前提交
874233796e
共有 2 个文件被更改,包括 24 次插入5 次删除
  1. 5 4
      packages/ckeditor5-engine/src/model/writer.js
  2. 19 1
      packages/ckeditor5-engine/tests/model/writer.js

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

@@ -13,7 +13,6 @@ import TextProxy from './textproxy';
 import Range from './range';
 import Position from './position';
 import DocumentFragment from './documentfragment';
-import MarkerCollection from './markercollection';
 import NodeList from './nodelist';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import log from '@ckeditor/ckeditor5-utils/src/log';
@@ -78,12 +77,14 @@ export function insert( position, nodes ) {
 
 	// If given element is a DocumentFragment and has markers.
 	if ( nodes instanceof DocumentFragment && nodes.markers.size ) {
-		// If node is being inserted to the Document with markers collection.
-		if ( position.root.document && position.root.document.markers instanceof MarkerCollection ) {
+		// 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 ) );
-				position.root.document.markers.set( marker[ 0 ], range );
+				targetElement.markers.set( marker[ 0 ], range );
 			}
 		// Otherwise we need to show warning about losing markers.
 		} else {

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

@@ -11,7 +11,7 @@ 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 } from '../../src/dev-utils/model';
+import { getData, stringify } from '../../src/dev-utils/model';
 
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 import log from '@ckeditor/ckeditor5-utils/src/log';
@@ -77,6 +77,24 @@ describe( 'writer', () => {
 			expect( marker.getRange().isEqual( expectedRange ) ).to.true;
 		} );
 
+		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' ),
+				] )
+			] );
+			const range = new Range( new Position( docFrag, [ 0, 2 ] ), new Position( docFrag, [ 0, 6 ] ) );
+
+			docFrag.markers.set( 'foo', range );
+
+			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' );