ソースを参照

Added: DocumentFragment is now supported in `engine.model.SchemaContext`.

Szymon Cofalik 8 年 前
コミット
e545950bbf

+ 12 - 3
packages/ckeditor5-engine/src/model/schema.js

@@ -871,12 +871,21 @@ export class SchemaContext {
 	 */
 	constructor( context ) {
 		if ( Array.isArray( context ) ) {
-			this._items = context.map( mapContextItem );
+			if ( context[ 0 ] && typeof context[ 0 ] != 'string' && context[ 0 ].is( 'documentFragment' ) ) {
+				context.shift();
+			}
 		}
-		// Item or position (PS. It's ok that Position#getAncestors() doesn't accept params).
 		else {
-			this._items = context.getAncestors( { includeSelf: true } ).map( mapContextItem );
+			// `context` is item or position.
+			// Position#getAncestors() doesn't accept any parameters but it works just fine here.
+			context = context.getAncestors( { includeSelf: true } );
+
+			if ( context[ 0 ].is( 'documentFragment' ) ) {
+				context.shift();
+			}
 		}
+
+		this._items = context.map( mapContextItem );
 	}
 
 	/**

+ 30 - 0
packages/ckeditor5-engine/tests/model/schema.js

@@ -9,6 +9,7 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
 
 import Model from '../../src/model/model';
 
+import DocumentFragment from '../../src/model/documentfragment';
 import Element from '../../src/model/element';
 import Text from '../../src/model/text';
 import TextProxy from '../../src/model/textproxy';
@@ -2081,6 +2082,35 @@ describe( 'SchemaContext', () => {
 
 			expect( Array.from( ctx.getItem( 2 ).getAttributeKeys() ).sort() ).to.deep.equal( [ 'align' ] );
 		} );
+
+		it( 'filters out DocumentFragment when it is a first item of context - array', () => {
+			const ctx = new SchemaContext( [ new DocumentFragment(), 'paragraph' ] );
+
+			expect( ctx.length ).to.equal( 1 );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
+		} );
+
+		it( 'filters out DocumentFragment when it is a first item of context - element', () => {
+			const p = new Element( 'paragraph' );
+			const docFrag = new DocumentFragment();
+			docFrag.appendChildren( p );
+
+			const ctx = new SchemaContext( p );
+
+			expect( ctx.length ).to.equal( 1 );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
+		} );
+
+		it( 'filters out DocumentFragment when it is a first item of context - position', () => {
+			const p = new Element( 'paragraph' );
+			const docFrag = new DocumentFragment();
+			docFrag.appendChildren( p );
+
+			const ctx = new SchemaContext( new Position( docFrag, [ 0, 0 ] ) );
+
+			expect( ctx.length ).to.equal( 1 );
+			expect( Array.from( ctx.getNames() ) ).to.deep.equal( [ 'paragraph' ] );
+		} );
 	} );
 
 	describe( 'length', () => {