Переглянути джерело

Schema#schema.checkAttributeInSelection() checks attributes of a text node.

Kamil Piechaczek 7 роки тому
батько
коміт
c2a00d9f9c

+ 7 - 1
packages/ckeditor5-engine/src/model/schema.js

@@ -488,8 +488,14 @@ export default class Schema {
 	 */
 	 */
 	checkAttributeInSelection( selection, attribute ) {
 	checkAttributeInSelection( selection, attribute ) {
 		if ( selection.isCollapsed ) {
 		if ( selection.isCollapsed ) {
+			const firstPosition = selection.getFirstPosition();
+			const context = [
+				...firstPosition.getAncestors(),
+				firstPosition.root.getNodeByPath( firstPosition.path )
+			].filter( Boolean );
+
 			// Check whether schema allows for a text with the attribute in the selection.
 			// Check whether schema allows for a text with the attribute in the selection.
-			return this.checkAttribute( [ ...selection.getFirstPosition().getAncestors(), '$text' ], attribute );
+			return this.checkAttribute( context, attribute );
 		} else {
 		} else {
 			const ranges = selection.getRanges();
 			const ranges = selection.getRanges();
 
 

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

@@ -1047,6 +1047,26 @@ describe( 'Schema', () => {
 					return true;
 					return true;
 				}
 				}
 			} );
 			} );
+
+			schema.addAttributeCheck( ( ctx, attributeName ) => {
+				// Disallow 'italic' on $text that has attribute 'bold'.
+				if ( inTextWithBold( ctx ) && attributeName == 'italic' ) {
+					return false;
+				}
+				// Allow 'italic' on p>$text.
+				if ( ctx.endsWith( 'p $text' ) && attributeName == 'italic' ) {
+					return true;
+				}
+
+				// Allow 'italic' on $root>p.
+				if ( ctx.endsWith( '$root p' ) && attributeName == 'italic' ) {
+					return true;
+				}
+
+				function inTextWithBold( context ) {
+					return context.endsWith( '$text' ) && context.last.getAttribute( 'bold' );
+				}
+			} );
 		} );
 		} );
 
 
 		describe( 'when selection is collapsed', () => {
 		describe( 'when selection is collapsed', () => {
@@ -1062,6 +1082,11 @@ describe( 'Schema', () => {
 				setData( model, '[]' );
 				setData( model, '[]' );
 				expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
 				expect( schema.checkAttributeInSelection( doc.selection, attribute ) ).to.be.false;
 			} );
 			} );
+
+			it( 'should check attributes of text', () => {
+				setData( model, '<p><$text bold="true">f[]oo</$text></p>' );
+				expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
+			} );
 		} );
 		} );
 
 
 		describe( 'when selection is not collapsed', () => {
 		describe( 'when selection is not collapsed', () => {
@@ -1102,6 +1127,11 @@ describe( 'Schema', () => {
 				setData( model, '[<figure name="figure" title="title"></figure>]' );
 				setData( model, '[<figure name="figure" title="title"></figure>]' );
 				expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
 				expect( schema.checkAttributeInSelection( doc.selection, 'title' ) ).to.be.true;
 			} );
 			} );
+
+			it( 'should check attributes of text', () => {
+				setData( model, '<p><$text bold="true">f[o]o</$text></p>' );
+				expect( schema.checkAttributeInSelection( doc.selection, 'italic' ) ).to.be.false;
+			} );
 		} );
 		} );
 	} );
 	} );