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

Tests: Added tests for link highlight removal during the downcast conversion.

Aleksander Nowodzinski 7 лет назад
Родитель
Сommit
e6cb6cbe4e
2 измененных файлов с 109 добавлено и 13 удалено
  1. 12 11
      packages/ckeditor5-link/src/linkediting.js
  2. 97 2
      packages/ckeditor5-link/tests/linkediting.js

+ 12 - 11
packages/ckeditor5-link/src/linkediting.js

@@ -101,21 +101,22 @@ export default class LinkEditing extends Plugin {
 			}
 		} );
 
+		// Removing the class.
 		editor.conversion.for( 'editingDowncast' ).add( dispatcher => {
+			// Make sure the highlight is remove on every possible event.
 			dispatcher.on( 'insert', removeHighlight, { priority: 'highest' } );
-			dispatcher.on( 'attribute', removeHighlight, { priority: 'highest' } );
 			dispatcher.on( 'remove', removeHighlight, { priority: 'highest' } );
+			dispatcher.on( 'attribute', removeHighlight, { priority: 'highest' } );
 			dispatcher.on( 'selection', removeHighlight, { priority: 'highest' } );
-		} );
 
-		// Removing the class.
-		function removeHighlight() {
-			view.change( writer => {
-				for ( const item of highlightedLinks.values() ) {
-					writer.removeClass( HIGHLIGHT_CLASSES, item );
-					highlightedLinks.delete( item );
-				}
-			} );
-		}
+			function removeHighlight() {
+				view.change( writer => {
+					for ( const item of highlightedLinks.values() ) {
+						writer.removeClass( HIGHLIGHT_CLASSES, item );
+						highlightedLinks.delete( item );
+					}
+				} );
+			}
+		} );
 	}
 }

+ 97 - 2
packages/ckeditor5-link/tests/linkediting.js

@@ -10,12 +10,15 @@ import UnlinkCommand from '../src/unlinkcommand';
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
 import Enter from '@ckeditor/ckeditor5-enter/src/enter';
+import ModelRange from '@ckeditor/ckeditor5-engine/src/model/range';
 import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
 import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
 import { isLinkElement } from '../src/utils';
 import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
-
-import { downcastAttributeToElement } from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
+import {
+	downcastMarkerToHighlight,
+	downcastAttributeToElement
+} from '@ckeditor/ckeditor5-engine/src/conversion/downcast-converters';
 
 /* global document */
 
@@ -232,5 +235,97 @@ describe( 'LinkEditing', () => {
 				'<p>foo <a class="ck ck-link_selected" href="url">l{}ink</a> baz</p>'
 			);
 		} );
+
+		describe( 'downcast conversion integration', () => {
+			it( 'works for the #insert event', () => {
+				setModelData( model,
+					'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+				);
+
+				model.change( writer => {
+					writer.insertText( 'FOO', { linkHref: 'url' }, model.document.selection.getFirstPosition() );
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p>foo <a class="ck ck-link_selected" href="url">liFOO{}nk</a> baz</p>'
+				);
+			} );
+
+			it( 'works for the #remove event', () => {
+				setModelData( model,
+					'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+				);
+
+				model.change( writer => {
+					writer.remove( ModelRange.createFromParentsAndOffsets(
+						model.document.getRoot().getChild( 0 ), 0,
+						model.document.getRoot().getChild( 0 ), 5 )
+					);
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p><a class="ck ck-link_selected" href="url">i{}nk</a> baz</p>'
+				);
+			} );
+
+			it( 'works for the #attribute event', () => {
+				setModelData( model,
+					'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+				);
+
+				model.change( writer => {
+					writer.setAttribute( 'linkHref', 'new-url', new ModelRange(
+						model.document.selection.getFirstPosition().getShiftedBy( -1 ),
+						model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
+					);
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p>foo <a href="url">l</a><a class="ck ck-link_selected" href="new-url">i{}n</a><a href="url">k</a> baz</p>'
+				);
+			} );
+
+			it( 'works for the #selection event', () => {
+				setModelData( model,
+					'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+				);
+
+				model.change( writer => {
+					writer.setSelection( new ModelRange(
+						model.document.selection.getFirstPosition().getShiftedBy( -1 ),
+						model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
+					);
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p>foo <a class="ck ck-link_selected" href="url">l{in}k</a> baz</p>'
+				);
+			} );
+
+			it( 'works for the #addMarker and #removeMarker events', () => {
+				downcastMarkerToHighlight( { model: 'fooMarker', view: {} } )( editor.editing.downcastDispatcher );
+
+				setModelData( model,
+					'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+				);
+
+				model.change( writer => {
+					writer.setMarker( 'fooMarker', ModelRange.createFromParentsAndOffsets(
+						model.document.getRoot().getChild( 0 ), 0,
+						model.document.getRoot().getChild( 0 ), 5 )
+					);
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p><span>foo </span><a class="ck ck-link_selected" href="url"><span>l</span>i{}nk</a> baz</p>'
+				);
+
+				model.change( writer => writer.removeMarker( 'fooMarker' ) );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p>foo <a class="ck ck-link_selected" href="url">li{}nk</a> baz</p>'
+				);
+			} );
+		} );
 	} );
 } );