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

Added: Remove whole highlight when using eraser on collapsed range inside existing highlight.

Maciej Gołaszewski 8 лет назад
Родитель
Сommit
49ee1513f3

+ 2 - 2
packages/ckeditor5-highlight/src/highlightcommand.js

@@ -75,8 +75,8 @@ export default class HighlightCommand extends Command {
 					const highlightRange = new Range( highlightStart, highlightEnd );
 
 					// Then depending on current value...
-					if ( this.value === highlighter ) {
-						// ...remove attribute.
+					if ( !highlighter || this.value === highlighter ) {
+						// ...remove attribute when passing highlighter different then current or executing "eraser".
 						writer.removeAttribute( 'highlight', highlightRange );
 					} else {
 						// ...update `highlight` value.

+ 86 - 50
packages/ckeditor5-highlight/tests/highlightcommand.js

@@ -58,85 +58,121 @@ describe( 'HighlightCommand', () => {
 	} );
 
 	describe( 'execute()', () => {
-		it( 'should add highlight attribute on selected nodes nodes when passed as parameter', () => {
-			setData( model, '<paragraph>a[bc<$text highlight="marker">fo]obar</$text>xyz</paragraph>' );
+		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>' );
 
-			expect( command.value ).to.be.undefined;
+					expect( command.value ).to.equal( 'marker' );
 
-			command.execute( { value: 'marker' } );
+					command.execute( { value: 'greenMarker' } );
 
-			expect( command.value ).to.equal( 'marker' );
+					expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
 
-			expect( getData( model ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
-		} );
+					expect( command.value ).to.equal( 'greenMarker' );
+				} );
 
-		it( 'should add highlight attribute on selected nodes nodes when passed as parameter (multiple nodes)', () => {
-			setData(
-				model,
-				'<paragraph>abcabc[abc</paragraph>' +
-				'<paragraph>foofoofoo</paragraph>' +
-				'<paragraph>barbar]bar</paragraph>'
-			);
+				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>' );
 
-			command.execute( { value: 'marker' } );
+					expect( command.value ).to.equal( 'marker' );
 
-			expect( command.value ).to.equal( 'marker' );
+					command.execute( { value: '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>'
-			);
-		} );
+					expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
 
-		it( 'should set highlight attribute on selected nodes when passed as parameter', () => {
-			setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
+					expect( command.value ).to.be.undefined;
+				} );
 
-			expect( command.value ).to.equal( 'marker' );
+				it( 'should change entire highlight when inside highlighted text', () => {
+					setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
 
-			command.execute( { value: 'foo' } );
+					expect( command.value ).to.equal( 'marker' );
 
-			expect( getData( model ) ).to.equal(
-				'<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
-			);
+					command.execute( { value: 'greenMarker' } );
 
-			expect( command.value ).to.equal( 'foo' );
-		} );
+					expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
 
-		it( 'should remove highlight attribute on selected nodes nodes when undefined passed as parameter', () => {
-			setData( model, '<paragraph>abc[<$text highlight="marker">foo]bar</$text>xyz</paragraph>' );
+					expect( command.value ).to.equal( 'greenMarker' );
+				} );
+			} );
 
-			expect( command.value ).to.equal( 'marker' );
+			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>' );
 
-			command.execute();
+					expect( command.value ).to.be.undefined;
 
-			expect( getData( model ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
+					command.execute( { value: 'marker' } );
 
-			expect( command.value ).to.be.undefined;
-		} );
+					expect( command.value ).to.equal( 'marker' );
+
+					expect( getData( model ) ).to.equal( '<paragraph>a[<$text highlight="marker">bcfo]obar</$text>xyz</paragraph>' );
+				} );
+
+				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>'
+					);
+
+					command.execute( { value: 'marker' } );
+
+					expect( command.value ).to.equal( 'marker' );
 
-		it( 'should change entire highlight on collapsed range when inside highlighted text', () => {
-			setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+					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>'
+					);
+				} );
 
-			expect( command.value ).to.equal( 'marker' );
+				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>' );
 
-			command.execute( { value: 'greenMarker' } );
+					expect( command.value ).to.equal( 'marker' );
 
-			expect( getData( model ) ).to.equal( '<paragraph>abc[<$text highlight="greenMarker">foobar</$text>]xyz</paragraph>' );
+					command.execute( { value: 'foo' } );
 
-			expect( command.value ).to.equal( 'greenMarker' );
+					expect( getData( model ) ).to.equal(
+						'<paragraph>abc[<$text highlight="foo">foo</$text>]<$text highlight="marker">bar</$text>xyz</paragraph>'
+					);
+
+					expect( command.value ).to.equal( 'foo' );
+				} );
+			} );
 		} );
 
-		it( 'should remove entire highlight on collapsed range when inside highlighted text of the same value', () => {
-			setData( model, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+		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>' );
+
+					expect( command.value ).to.equal( 'marker' );
 
-			expect( command.value ).to.equal( 'marker' );
+					command.execute();
 
-			command.execute( { value: 'marker' } );
+					expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
+
+					expect( command.value ).to.be.undefined;
+				} );
+			} );
 
-			expect( getData( model ) ).to.equal( '<paragraph>abcfoo[]barxyz</paragraph>' );
+			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>' );
 
-			expect( command.value ).to.be.undefined;
+					expect( command.value ).to.equal( 'marker' );
+
+					command.execute();
+
+					expect( getData( model ) ).to.equal( '<paragraph>abc[foo]<$text highlight="marker">bar</$text>xyz</paragraph>' );
+
+					expect( command.value ).to.be.undefined;
+				} );
+			} );
 		} );
 	} );
 } );