Explorar o código

Improved schema validation of todoListChecked attribute.

Oskar Wróbel %!s(int64=6) %!d(string=hai) anos
pai
achega
4c71212067

+ 9 - 0
packages/ckeditor5-list/src/todolistediting.js

@@ -48,6 +48,15 @@ export default class TodoListEditing extends Plugin {
 			allowAttributes: [ 'todoListChecked' ]
 			allowAttributes: [ 'todoListChecked' ]
 		} );
 		} );
 
 
+		// Disallow todoListChecked attribute on other nodes than listItem with todo listType.
+		model.schema.addAttributeCheck( ( context, attributeName ) => {
+			const item = context.last;
+
+			if ( attributeName == 'todoListChecked' && item.name == 'listItem' && item.getAttribute( 'listType' ) != 'todo' ) {
+				return false;
+			}
+		} );
+
 		// Register commands.
 		// Register commands.
 		editor.commands.add( 'todoList', new ListCommand( editor, 'todo' ) );
 		editor.commands.add( 'todoList', new ListCommand( editor, 'todo' ) );
 		editor.commands.add( 'todoListCheck', new TodoListCheckCommand( editor ) );
 		editor.commands.add( 'todoListCheck', new TodoListCheckCommand( editor ) );

+ 4 - 4
packages/ckeditor5-list/tests/todolistcheckcommand.js

@@ -151,11 +151,11 @@ describe( 'TodoListCheckCommand', () => {
 			);
 			);
 		} );
 		} );
 
 
-		it( 'should toggle state on multiple items mixed with non list item', () => {
+		it( 'should toggle state on multiple items mixed with none todo list items', () => {
 			setModelData( model,
 			setModelData( model,
 				'<paragraph>a[bc</paragraph>' +
 				'<paragraph>a[bc</paragraph>' +
 				'<listItem listIndent="0" listType="todo">def</listItem>' +
 				'<listItem listIndent="0" listType="todo">def</listItem>' +
-				'<paragraph>ghi</paragraph>' +
+				'<listItem listIndent="0" listType="numbered">ghi</listItem>' +
 				'<listItem listIndent="0" listType="todo">jkl</listItem>' +
 				'<listItem listIndent="0" listType="todo">jkl</listItem>' +
 				'<paragraph>mn]o</paragraph>'
 				'<paragraph>mn]o</paragraph>'
 			);
 			);
@@ -165,7 +165,7 @@ describe( 'TodoListCheckCommand', () => {
 			expect( getModelData( model ) ).to.equal(
 			expect( getModelData( model ) ).to.equal(
 				'<paragraph>a[bc</paragraph>' +
 				'<paragraph>a[bc</paragraph>' +
 				'<listItem listIndent="0" listType="todo" todoListChecked="true">def</listItem>' +
 				'<listItem listIndent="0" listType="todo" todoListChecked="true">def</listItem>' +
-				'<paragraph>ghi</paragraph>' +
+				'<listItem listIndent="0" listType="numbered">ghi</listItem>' +
 				'<listItem listIndent="0" listType="todo" todoListChecked="true">jkl</listItem>' +
 				'<listItem listIndent="0" listType="todo" todoListChecked="true">jkl</listItem>' +
 				'<paragraph>mn]o</paragraph>'
 				'<paragraph>mn]o</paragraph>'
 			);
 			);
@@ -175,7 +175,7 @@ describe( 'TodoListCheckCommand', () => {
 			expect( getModelData( model ) ).to.equal(
 			expect( getModelData( model ) ).to.equal(
 				'<paragraph>a[bc</paragraph>' +
 				'<paragraph>a[bc</paragraph>' +
 				'<listItem listIndent="0" listType="todo">def</listItem>' +
 				'<listItem listIndent="0" listType="todo">def</listItem>' +
-				'<paragraph>ghi</paragraph>' +
+				'<listItem listIndent="0" listType="numbered">ghi</listItem>' +
 				'<listItem listIndent="0" listType="todo">jkl</listItem>' +
 				'<listItem listIndent="0" listType="todo">jkl</listItem>' +
 				'<paragraph>mn]o</paragraph>'
 				'<paragraph>mn]o</paragraph>'
 			);
 			);

+ 10 - 1
packages/ckeditor5-list/tests/todolistediting.js

@@ -9,6 +9,7 @@ import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
 import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
 import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
 import Typing from '@ckeditor/ckeditor5-typing/src/typing';
 import Typing from '@ckeditor/ckeditor5-typing/src/typing';
 import ListCommand from '../src/listcommand';
 import ListCommand from '../src/listcommand';
+import ModelElement from '@ckeditor/ckeditor5-engine/src/model/element';
 
 
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
@@ -48,7 +49,15 @@ describe( 'TodoListEditing', () => {
 	} );
 	} );
 
 
 	it( 'should set proper schema rules', () => {
 	it( 'should set proper schema rules', () => {
-		expect( model.schema.checkAttribute( [ '$root', 'listItem' ], 'todoListChecked' ) ).to.be.true;
+		const todoListItem = new ModelElement( 'listItem', { listType: 'todo' } );
+		const bulletedListItem = new ModelElement( 'listItem', { listType: 'bulleted' } );
+		const numberedListItem = new ModelElement( 'listItem', { listType: 'numbered' } );
+		const paragraph = new ModelElement( 'paragraph' );
+
+		expect( model.schema.checkAttribute( [ '$root', todoListItem ], 'todoListChecked' ) ).to.be.true;
+		expect( model.schema.checkAttribute( [ '$root', bulletedListItem ], 'todoListChecked' ) ).to.be.false;
+		expect( model.schema.checkAttribute( [ '$root', numberedListItem ], 'todoListChecked' ) ).to.be.false;
+		expect( model.schema.checkAttribute( [ '$root', paragraph ], 'todoListChecked' ) ).to.be.false;
 	} );
 	} );
 
 
 	describe( 'command', () => {
 	describe( 'command', () => {