Kaynağa Gözat

Add tests for setupHighlight function,

copy them from linkediting, and adapt to no-plugin case.
Tomek Wytrębowicz 5 yıl önce
ebeveyn
işleme
63c6485a13

+ 232 - 0
packages/ckeditor5-typing/tests/utils/inlinehighlight.js

@@ -0,0 +1,232 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+import setupHighlight from '../../src/utils/inlinehighlight';
+
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
+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 '@ckeditor/ckeditor5-core/tests/_utils/assertions/attribute';
+
+/* global document */
+
+describe( 'LinkEditing', () => {
+	let element, editor, model, view;
+
+	beforeEach( async () => {
+		element = document.createElement( 'div' );
+		document.body.appendChild( element );
+
+		editor = await ClassicTestEditor.create( element );
+
+		model = editor.model;
+		view = editor.editing.view;
+
+		model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
+		editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
+
+		model.schema.extend( '$text', { allowAttributes: 'linkHref' } );
+
+		editor.conversion.for( 'editingDowncast' )
+			.attributeToElement( { model: 'linkHref', view: ( href, writer ) => {
+				return writer.createAttributeElement( 'a', { href } );
+			} } );
+
+		// Setup highlight over selected link.
+		setupHighlight( editor, 'linkHref', 'a', 'ck-link_selected' );
+	} );
+
+	afterEach( async () => {
+		element.remove();
+
+		await editor.destroy();
+	} );
+
+	describe( 'link highlighting', () => {
+		it( 'should convert the highlight to a proper view classes', () => {
+			setModelData( model,
+				'<paragraph>foo <$text linkHref="url">b{}ar</$text> baz</paragraph>'
+			);
+
+			expect( model.document.selection ).to.have.attribute( 'linkHref' );
+			expect( getViewData( view ) ).to.equal(
+				'<p>foo <a class="ck-link_selected" href="url">b{}ar</a> baz</p>'
+			);
+		} );
+
+		it( 'should work whenever selection has linkHref attribute - link start', () => {
+			setModelData( model,
+				'<paragraph>foo {}<$text linkHref="url">bar</$text> baz</paragraph>'
+			);
+
+			expect( model.document.selection ).to.not.have.attribute( 'linkHref' );
+
+			model.change( writer => {
+				writer.setSelectionAttribute( 'linkHref', 'url' );
+			} );
+
+			expect( model.document.selection ).to.have.attribute( 'linkHref' );
+			expect( getViewData( view ) ).to.equal(
+				'<p>foo <a class="ck-link_selected" href="url">{}bar</a> baz</p>'
+			);
+		} );
+
+		it( 'should work whenever selection has linkHref attribute - link end', () => {
+			setModelData( model,
+				'<paragraph>foo <$text linkHref="url">bar</$text>{} baz</paragraph>'
+			);
+
+			expect( model.document.selection ).to.have.attribute( 'linkHref' );
+			expect( getViewData( view ) ).to.equal(
+				'<p>foo <a class="ck-link_selected" href="url">bar{}</a> baz</p>'
+			);
+		} );
+
+		it( 'should render highlight correctly after splitting the link', () => {
+			setModelData( model,
+				'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+			);
+
+			editor.execute( 'enter' );
+
+			expect( getModelData( model ) ).to.equal(
+				'<paragraph>foo <$text linkHref="url">li</$text></paragraph>' +
+				'<paragraph><$text linkHref="url">[]nk</$text> baz</paragraph>'
+			);
+
+			expect( model.document.selection ).to.have.attribute( 'linkHref' );
+			expect( getViewData( view ) ).to.equal(
+				'<p>foo <a href="url">li</a></p>' +
+				'<p><a class="ck-link_selected" href="url">{}nk</a> baz</p>'
+			);
+		} );
+
+		it( 'should remove classes when selection is moved out from the link', () => {
+			setModelData( model,
+				'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+			);
+
+			expect( getViewData( view ) ).to.equal(
+				'<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
+			);
+
+			model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 0 ) );
+
+			expect( getViewData( view ) ).to.equal(
+				'<p>{}foo <a href="url">link</a> baz</p>'
+			);
+		} );
+
+		it( 'should work correctly when selection is moved inside link', () => {
+			setModelData( model,
+				'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+			);
+
+			expect( getViewData( view ) ).to.equal(
+				'<p>foo <a class="ck-link_selected" href="url">li{}nk</a> baz</p>'
+			);
+
+			model.change( writer => writer.setSelection( model.document.getRoot().getChild( 0 ), 5 ) );
+
+			expect( getViewData( view ) ).to.equal(
+				'<p>foo <a class="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-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( writer.createRange(
+						writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
+						writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
+					) );
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p><a class="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', writer.createRange(
+						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-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( writer.createRange(
+						model.document.selection.getFirstPosition().getShiftedBy( -1 ),
+						model.document.selection.getFirstPosition().getShiftedBy( 1 ) )
+					);
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p>foo <a class="ck-link_selected" href="url">l{in}k</a> baz</p>'
+				);
+			} );
+
+			it( 'works for the addMarker and removeMarker events', () => {
+				editor.conversion.for( 'editingDowncast' ).markerToHighlight( { model: 'fooMarker', view: {} } );
+
+				setModelData( model,
+					'<paragraph>foo <$text linkHref="url">li{}nk</$text> baz</paragraph>'
+				);
+
+				model.change( writer => {
+					const range = writer.createRange(
+						writer.createPositionAt( model.document.getRoot().getChild( 0 ), 0 ),
+						writer.createPositionAt( model.document.getRoot().getChild( 0 ), 5 )
+					);
+
+					writer.addMarker( 'fooMarker', { range, usingOperation: true } );
+				} );
+
+				expect( getViewData( view ) ).to.equal(
+					'<p><span>foo </span><a class="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-link_selected" href="url">li{}nk</a> baz</p>'
+				);
+			} );
+		} );
+	} );
+} );