8
0
Просмотр исходного кода

Tests: Update `HighlightCommand` tests.

Maciej Gołaszewski 7 лет назад
Родитель
Сommit
d7a275e450

+ 0 - 5
packages/ckeditor5-highlight/src/highlightcommand.js

@@ -50,11 +50,6 @@ export default class HighlightCommand extends Command {
 		const document = model.document;
 		const selection = document.selection;
 
-		// Do not apply highlight on wrong selection.
-		if ( !this.isEnabled ) {
-			return;
-		}
-
 		const highlighter = options.value;
 
 		model.change( writer => {

+ 48 - 28
packages/ckeditor5-highlight/tests/highlightcommand.js

@@ -25,8 +25,13 @@ describe( 'HighlightCommand', () => {
 				command = new HighlightCommand( newEditor );
 				editor.commands.add( 'highlight', command );
 
-				model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
-				model.schema.extend( '$text', { allowAttributes: 'highlight' } );
+				model.schema.register( 'p', { inheritAllFrom: '$block' } );
+				model.schema.addAttributeCheck( ( ctx, attributeName ) => {
+					// Allow 'highlight' on p>$text.
+					if ( ctx.endsWith( 'p $text' ) && attributeName == 'highlight' ) {
+						return true;
+					}
+				} );
 			} );
 	} );
 
@@ -41,55 +46,64 @@ describe( 'HighlightCommand', () => {
 
 	describe( 'value', () => {
 		it( 'is set to highlight attribute value when selection is in text with highlight attribute', () => {
-			setData( model, '<paragraph><$text highlight="marker">fo[o]</$text></paragraph>' );
+			setData( model, '<p><$text highlight="marker">fo[o]</$text></p>' );
 
 			expect( command ).to.have.property( 'value', 'marker' );
 		} );
 
 		it( 'is undefined when selection is not in text with highlight attribute', () => {
-			setData( model, '<paragraph>fo[]o</paragraph>' );
+			setData( model, '<p>fo[]o</p>' );
 
 			expect( command ).to.have.property( 'value', undefined );
 		} );
 	} );
 
 	describe( 'isEnabled', () => {
+		beforeEach( () => {
+			model.schema.register( 'x', { inheritAllFrom: '$block' } );
+		} );
+
 		it( 'is true when selection is on text which can have highlight added', () => {
-			setData( model, '<paragraph>fo[]o</paragraph>' );
+			setData( model, '<p>fo[]o</p>' );
 
 			expect( command ).to.have.property( 'isEnabled', true );
 		} );
+
+		it( 'is false when selection is on text which can not have highlight added', () => {
+			setData( model, '<x>fo[]o</x>' );
+			expect( command.isEnabled ).to.be.false;
+		} );
 	} );
 
 	describe( 'execute()', () => {
 		describe( 'with option.value set', () => {
 			describe( 'on collapsed range', () => {
 				it( 'should change entire highlight when inside highlighted text', () => {
-					setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+					setData( model, '<p>abc<$text highlight="marker">foo[]bar</$text>xyz</p>' );
 
 					expect( command.value ).to.equal( 'marker' );
 
 					command.execute( { value: 'greenMarker' } );
 
-					expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
+					expect( getData( model ) ).to.equal( '<p>abc[<$text highlight="greenMarker">foobar</$text>]xyz</p>' );
 
 					expect( command.value ).to.equal( 'greenMarker' );
 				} );
 
 				it( 'should remove entire highlight when inside highlighted text of the same value', () => {
-					setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+					setData( model, '<p>abc<$text highlight="marker">foo[]bar</$text>xyz</p>' );
 
 					expect( command.value ).to.equal( 'marker' );
 
 					command.execute( { value: 'marker' } );
 
-					expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
+					expect( getData( model ) ).to.equal( '<p>abcfoo[]barxyz</p>' );
 
 					expect( command.value ).to.be.undefined;
 				} );
 
 				it( 'should change selection attribute in non-empty parent', () => {
-					setData( model, '<paragraph>a[]bc<$text highlight="marker">foobar</$text>xyz</paragraph>' );
+					setData( model, '<p>a[]bc<$text highlight="marker">foobar</$text>xyz</p>' );
 					expect( command.value ).to.be.undefined;
 
 					command.execute( { value: 'foo' } );
@@ -101,10 +115,16 @@ describe( 'HighlightCommand', () => {
 
 					expect( command.value ).to.be.undefined;
 					expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.false;
+
+					// Execute remove highlight on selection without 'highlight' attribute should do nothing.
+					command.execute();
+
+					expect( command.value ).to.be.undefined;
+					expect( doc.selection.hasAttribute( 'highlight' ) ).to.be.false;
 				} );
 
 				it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => {
-					setData( model, '<paragraph>a[]bc<$text highlight="marker">foobar</$text>xyz</paragraph>' );
+					setData( model, '<p>a[]bc<$text highlight="marker">foobar</$text>xyz</p>' );
 
 					command.execute( { value: 'foo' } );
 
@@ -122,7 +142,7 @@ describe( 'HighlightCommand', () => {
 				} );
 
 				it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => {
-					setData( model, '<paragraph>abc<$text highlight="marker">foobar</$text>xyz</paragraph><paragraph>[]</paragraph>' );
+					setData( model, '<p>abc<$text highlight="marker">foobar</$text>xyz</p><p>[]</p>' );
 
 					expect( command.value ).to.be.undefined;
 
@@ -154,13 +174,13 @@ describe( 'HighlightCommand', () => {
 				} );
 
 				it( 'should change entire highlight when inside highlighted text', () => {
-					setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+					setData( model, '<p>abc<$text highlight="marker">foo[]bar</$text>xyz</p>' );
 
 					expect( command.value ).to.equal( 'marker' );
 
 					command.execute( { value: 'greenMarker' } );
 
-					expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
+					expect( getData( model ) ).to.equal( '<p>abc[<$text highlight="greenMarker">foobar</$text>]xyz</p>' );
 
 					expect( command.value ).to.equal( 'greenMarker' );
 				} );
@@ -168,7 +188,7 @@ describe( 'HighlightCommand', () => {
 
 			describe( 'on not collapsed range', () => {
 				it( 'should set highlight attribute on selected node when passed as parameter', () => {
-					setData( model, '<paragraph>a[bc<$text highlight="marker">fo]obar</$text>xyz</paragraph>' );
+					setData( model, '<p>a[bc<$text highlight="marker">fo]obar</$text>xyz</p>' );
 
 					expect( command.value ).to.be.undefined;
 
@@ -176,15 +196,15 @@ describe( 'HighlightCommand', () => {
 
 					expect( command.value ).to.equal( 'marker' );
 
-					expect( getData( model ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
+					expect( getData( model ) ).to.equal( '<p>a[<$text highlight="marker">bcfo]obar</$text>xyz</p>' );
 				} );
 
 				it( 'should set highlight attribute on selected node when passed as parameter (multiple nodes)', () => {
 					setData(
 						model,
-						'<paragraph>abcabc[abc</paragraph>' +
-						'<paragraph>foofoofoo</paragraph>' +
-						'<paragraph>barbar]bar</paragraph>'
+						'<p>abcabc[abc</p>' +
+						'<p>foofoofoo</p>' +
+						'<p>barbar]bar</p>'
 					);
 
 					command.execute( { value: 'marker' } );
@@ -192,21 +212,21 @@ describe( 'HighlightCommand', () => {
 					expect( command.value ).to.equal( 'marker' );
 
 					expect( getData( model ) ).to.equal(
-						'<paragraph>abcabc[<$text highlight="marker">abc</$text></paragraph>' +
-						'<paragraph><$text highlight="marker">foofoofoo</$text></paragraph>' +
-						'<paragraph><$text highlight="marker">barbar</$text>]bar</paragraph>'
+						'<p>abcabc[<$text highlight="marker">abc</$text></p>' +
+						'<p><$text highlight="marker">foofoofoo</$text></p>' +
+						'<p><$text highlight="marker">barbar</$text>]bar</p>'
 					);
 				} );
 
 				it( 'should set highlight attribute on selected nodes when passed as parameter only on selected characters', () => {
-					setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
+					setData( model, '<p>abc[<$text highlight="marker">foo]bar</$text>xyz</p>' );
 
 					expect( command.value ).to.equal( 'marker' );
 
 					command.execute( { value: 'foo' } );
 
 					expect( getData( model ) ).to.equal(
-						'<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
+						'<p>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</p>'
 					);
 
 					expect( command.value ).to.equal( 'foo' );
@@ -217,13 +237,13 @@ describe( 'HighlightCommand', () => {
 		describe( 'with undefined option.value', () => {
 			describe( 'on collapsed range', () => {
 				it( 'should remove entire highlight when inside highlighted text', () => {
-					setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+					setData( model, '<p>abc<$text highlight="marker">foo[]bar</$text>xyz</p>' );
 
 					expect( command.value ).to.equal( 'marker' );
 
 					command.execute();
 
-					expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
+					expect( getData( model ) ).to.equal( '<p>abcfoo[]barxyz</p>' );
 
 					expect( command.value ).to.be.undefined;
 				} );
@@ -231,13 +251,13 @@ describe( 'HighlightCommand', () => {
 
 			describe( 'on not collapsed range', () => {
 				it( 'should remove highlight attribute on selected node when undefined passed as parameter', () => {
-					setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
+					setData( model, '<p>abc[<$text highlight="marker">foo]bar</$text>xyz</p>' );
 
 					expect( command.value ).to.equal( 'marker' );
 
 					command.execute();
 
-					expect( getData( model ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
+					expect( getData( model ) ).to.equal( '<p>abc[foo]<$text highlight="marker">bar</$text>xyz</p>' );
 
 					expect( command.value ).to.be.undefined;
 				} );