浏览代码

LiveSelection will not read attributes from object and its children.

Kamil Piechaczek 8 年之前
父节点
当前提交
caabcaf865

+ 7 - 0
packages/ckeditor5-engine/src/model/liveselection.js

@@ -7,6 +7,7 @@
  * @module engine/model/liveselection
  */
 
+import Element from './element';
 import Position from './position';
 import Range from './range';
 import LiveRange from './liverange';
@@ -556,6 +557,7 @@ export default class LiveSelection extends Selection {
 	 */
 	_getSurroundingAttributes() {
 		const position = this.getFirstPosition();
+		const schema = this._document.schema;
 
 		let attrs = null;
 
@@ -565,6 +567,11 @@ export default class LiveSelection extends Selection {
 
 			// ...look for a first character node in that range and take attributes from it.
 			for ( const item of range ) {
+				// If the item is an object, we don't want to get attributes from its children.
+				if ( item.item instanceof Element && schema.objects.has( item.item.name ) ) {
+					break;
+				}
+
 				// This is not an optimal solution because of https://github.com/ckeditor/ckeditor5-engine/issues/454.
 				// It can be done better by using `break;` instead of checking `attrs === null`.
 				if ( item.type == 'text' && attrs === null ) {

+ 33 - 0
packages/ckeditor5-engine/tests/model/liveselection.js

@@ -872,4 +872,37 @@ describe( 'LiveSelection', () => {
 			expect( values ).to.deep.equal( [] );
 		} );
 	} );
+
+	describe( '_updateAttributes()', () => {
+		beforeEach( () => {
+			doc.schema.registerItem( 'image', '$inline' );
+			doc.schema.disallow( { name: 'image', attributes: 'bold', inside: '$root' } );
+			doc.schema.objects.add( 'image' );
+
+			doc.schema.registerItem( 'caption', '$block' );
+			doc.schema.allow( { name: '$inline', inside: 'caption' } );
+			doc.schema.allow( { name: 'caption', inside: 'image' } );
+			doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } );
+
+			root.removeChildren( 0, root.childCount );
+
+			root.insertChildren( 0, [
+				new Element( 'p', null, [
+					new Element( 'image', [], [
+						new Element( 'caption', [], [
+							new Text( 'Caption for the image.', { bold: true } )
+						] )
+					] )
+				] )
+			] );
+
+			selection.addRange( new Range( new Position( root, [ 0 ] ), new Position( root, [ 1 ] ) ) );
+		} );
+
+		it( 'ignores attributes from nested editable if selection contains an object', () => {
+			const liveSelection = LiveSelection.createFromSelection( selection );
+
+			expect( liveSelection.hasAttribute( 'bold' ) ).to.equal( false );
+		} );
+	} );
 } );